-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
191 lines (174 loc) · 6.05 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use strict';
require('dotenv').config();
require('date-utils');
const config = require('config');
const cron = require('node-cron');
const portscanner = require('portscanner');
const request = require('request');
const text2png = require('text2png');
const twitter = require('twitter');
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
const MAX_RETRY = config.maxRetry;
const bot = new twitter ({
consumer_key : process.env.CONSUMER_KEY,
consumer_secret : process.env.CONSUMER_SECRET,
access_token_key : process.env.ACCESS_TOKEN,
access_token_secret : process.env.ACCESS_TOKEN_SECRET
});
const previousStatus = {};
for(const pool of config.pools) {
previousStatus[pool.id] = { api : true, stratum : true, hashRate : 0 };
}
/*** check MPOS API reachability ***/
const checkAPI = async(uri) => {
const data = await new Promise((resolve) => {
request.get(uri, { timeout : config.timeout.api || 1000 }, (error, response, body) => {
if(error) { resolve({ error }); }
else { resolve({ body }); }
});
});
if(!data.error) {
try {
data.json = JSON.parse(data.body);
} catch(err) {
if(err.name === 'SyntaxError') { data.error = err.name; }
else { console.error(err); }
}
}
return data;
};
/*** check Stratum Port reachability ***/
const checkStratum = async(host, port) => {
const portStatus = await portscanner.checkPortStatus(port, {
host,
timeout : config.timeout.stratum || 1000
}).catch(err => {
console.error(err);
return false;
});
return portStatus === 'open' ? true : false;
};
const postTweet = async(status, media) => {
if(process.env.DEBUG) { return; }
if(media) {
bot.post('media/upload', { media }, (err, res) => {
if (err) { console.error(err); }
const mediaIds = res.media_id_string;
bot.post('statuses/update', { status, media_ids : mediaIds }, (err) => {
if (err) { console.error(err); }
});
});
}
else {
bot.post('statuses/update', { status }, (err/*, tweet, response*/) => {
if (err) { console.error(err); }
});
}
};
const checkCurrentStatus = async() => {
console.info(`[${new Date()}] Checking pools...`);
for(const pool of config.pools) {
const status = { api : false, stratum : false, hashRate : 0 };
for(let retry = 0; retry < MAX_RETRY; ++retry) {
const api = await checkAPI(pool.api_url || (pool.url + (config.apiPath[pool.type])));
if(api.error) { continue; }
status.api = true;
switch(pool.type) {
case 'mpos':
status.hashRate = api.json.hashrate; // [kH/s]
break;
case 'nomp': {
const algos = api.json.algos;
status.hashRate = (algos.yescryptR8 ? algos.yescryptR8 : algos.yescrypt).hashrate / 1e3; // [kH/s]
break;
}
case 'yiimp':
if(!api.json.yescryptR8 && !api.json.yescryptr8) {
break;
}
status.hashRate = (api.json.yescryptR8 || api.json.yescryptr8).hashrate / 1e3; // [kH/s]
break;
default:
status.hashRate = 0;
}
break;
}
for(let retry = 0; retry < MAX_RETRY; ++retry) {
status.stratum = await checkStratum(pool.stratum.host, pool.stratum.port);
if(status.stratum) { break; }
}
if(previousStatus[pool.id].api !== status.api || previousStatus[pool.id].stratum !== status.stratum) {
let text = '';
if(!status.api || !status.stratum) { text += `【鯖落ち】「${pool.name}」に接続障害の可能性\n`; }
else { text += `【復旧】「${pool.name}」が復帰しました\n`; }
text += `${pool.message ? pool.message : ''}\n`;
text += `${pool.url}\n`
+ `Webダッシュボード: ${status.api ? '\u2705 正常' : '\u26a0 停止'}\n`
+ `Stratumポート: ${status.stratum ? '\u2705 正常' : '\u26a0 停止'}\n`
+ `(${(new Date()).toFormat('YYYY/MM/DD HH24:MI:SS')} JST)\n`;
console.info(text);
if(!pool.alert_disabled) { postTweet(text); }
}
previousStatus[pool.id] = status;
}
};
const tweetAllStatus = () => {
let text = '【定期】接続ができないプール\n';
const okPools = [];
const allHashRate = config.pools.reduce((sum, pool) => {
if(!(previousStatus[pool.id]).hashRate) { return sum; }
return sum + (previousStatus[pool.id]).hashRate;
}, 0);
for(const pool of config.pools) {
const status = previousStatus[pool.id];
if(status.api && status.stratum) { okPools.push(pool.name); }
else {
text += `\u26a0 ${pool.shortname || pool.name}`;
if(status.stratum) {
text += '(Web';
}
else if(status.api) {
text += '(Stratum';
}
else {
text += '(Web/Stratum';
}
text += `${pool.message_summary ? ' ' + pool.message_summary : ''})\n`;
}
const prop = 1e2 * status.hashRate / allHashRate;
if(prop >= config.hashPowerWarn) {
text += `\u2757 ${pool.shortname || pool.name}にハッシュパワーが集中しています`
+ `(${prop.toFixed(1)}%)。分散しましょう!\n`;
}
}
if(okPools.length === config.pools.length) { text += '全プールが正常です!\ud83c\udf8a\n'; }
else { text += `その他${okPools.length}プールが正常\n`; }
text += `(${(new Date()).toFormat('YYYY/MM/DD HH24:MI:SS')} JST)\n#BitZeny`;
console.info(text);
let imageText = `【${okPools.length}プールが正常稼働中】\n`;
for(const name of okPools) { imageText += ` ・${name}\n`; }
imageText += `(${(new Date()).toFormat('YYYY/MM/DD HH24:MI:SS')} JST @bitzenypoolbot)\n`;
const image = text2png(imageText, {
localFontPath : 'font/ipagp.ttf',
localFontName : 'IPA P Gothic',
lineSpacing : 10,
bgColor : 'white'
});
var fs = require('fs');
fs.writeFileSync('out.png', image);
postTweet(text, image);
};
if(process.env.DEBUG) {
(async() => {
await checkCurrentStatus();
tweetAllStatus();
})();
}
else {
cron.schedule(`*/${config.cron.check} * * * *`, () => {
checkCurrentStatus();
});
cron.schedule(`0 0-23/${config.cron.tweet} * * *`, async() => {
tweetAllStatus();
});
}