forked from AlexeyKhristov/koa-ping
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
66 lines (56 loc) · 1.71 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
'use strict'
const os = require('os')
const path = require('path')
const util = require('util')
const appRootDir = require('app-root-path')
const pkg = require(appRootDir + path.sep + 'package.json')
const df = util.promisify(require('node-df'))
const healthCheck = (options = {}) => {
options.path = options.path || '/ping'
if (options.customCheck && typeof options.customCheck !== 'function') {
throw new TypeError('`options.customCheck` must be a function')
}
return async function healthCheck(ctx, next) {
if (ctx.request.path === options.path && ctx.request.method === 'GET') {
const diskInfo = await df()
const health = {
timestamp: Date.now(),
uptime: process.uptime(),
application: {
name: pkg.name,
version: pkg.version,
pid: process.pid,
title: process.title,
argv: process.argv,
versions: process.versions,
node_env: process.env.NODE_ENV,
},
resources: {
memory: process.memoryUsage(),
loadavg: os.loadavg(),
cpu: os.cpus(),
disk: diskInfo,
nics: os.networkInterfaces(),
},
system: {
arch: process.arch,
platform: process.platform,
type: os.type(),
release: os.release(),
hostname: os.hostname(),
uptime: os.uptime(),
cores: os.cpus().length,
memory: os.totalmem(),
},
}
const custom = options.customCheck ? await options.customCheck(ctx) : {}
ctx.response.body = options.customCheckOnly ? custom : {
custom,
...health,
}
return
}
return next()
}
}
module.exports = healthCheck