-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_api.js
More file actions
60 lines (55 loc) · 2.01 KB
/
Copy pathmonitor_api.js
File metadata and controls
60 lines (55 loc) · 2.01 KB
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
var config = require('./config');
const substrate = require('oo7-substrate');
const request = require('request');
var logger = config.logger();
substrate.setNodeUri(config.node.ws);
function sleep(time) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(' enough sleep~');
}, time);
})
}
(async function () {
try {
let api_best = 0
let warn_count = 0 //告警次数 超过5次自动退出
while (true) {
try {
request('https://api.chainx.org.cn/chain/status?r=' + Math.random(), function (error, response, body) {
let warn = ''
if (!error && response.statusCode == 200) {
let data = JSON.parse(body)
logger.debug('api高度', api_best, data.best)
if (api_best > 0 && api_best >= data.best) {
warn = 'api数据掉线'
} else {
api_best = data.best
if (warn_count > 0)
warn_count--
}
} else {
warn = 'api超时'
logger.error(error)
}
if (warn) {
logger.error("告警", warn, api_best, data.best)
request('http://x.x.x.x:xxx/sms?message=' + encodeURI(warn) + '&pass=ChainX.org', function (error, response, body) {
if (!error && response.statusCode == 200) {} else {
logger.error(error)
}
})
warn_count++
}
})
} catch (e) {
logger.error(e)
await substrate.runtimeUp
continue
}
await sleep(15000)
}
} catch (e) {
logger.error(e)
}
})()