-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (58 loc) · 1.9 KB
/
index.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
const osu = require('node-os-utils')
const { send } = require('micro')
const { router, get } = require('microrouter')
const fetch = require('cross-fetch')
/************************************************
* Utilities
***********************************************/
const healthCheckHive = async () => {
try {
const res = await fetch(
'http://localhost:8085/.well-known/apollo/server-health',
)
if (res.status >= 400) return 'Hive failed to respond!'
} catch {
return 'Hive failed to respond!'
}
}
const healthCheckLCD = async () => {
try {
const res = await fetch('http://localhost:1317/cosmos/base/tendermint/v1beta1/syncing')
if (res.status >= 400) return 'LCD failed to respond!'
const json = await res.json()
if (json.syncing) return 'LCD is catching up!'
} catch {
return 'LCD failed to respond!'
}
}
/************************************************
* Route handlers
***********************************************/
const machine = async (req, res) => {
const [cpu, mem, storage, _network] = await Promise.all([
osu.cpu.usage(),
osu.mem.info(),
osu.drive.info(),
osu.netstat.inOut(),
])
const network = _network && _network.total
return send(res, 200, { cpu, mem, storage, network })
}
const hiveHealth = async (req, res) => {
const messages = []
const errors = await Promise.all([healthCheckHive(), healthCheckLCD()])
messages.push(...errors.filter(Boolean))
const healthy = messages.length === 0
return send(res, healthy ? 200 : 500, healthy ? 'Healthy' : messages)
}
const lcdHealth = async (req, res) => {
const lcdError = await healthCheckLCD()
return send(res, lcdError ? 500 : 200, lcdError || 'Healthy')
}
const notfound = (req, res) => send(res, 404, '404 Not Found')
module.exports = router(
get('/machine', machine),
get('/hive-health', hiveHealth),
get('/lcd-health', lcdHealth),
get('/*', notfound),
)