-
Notifications
You must be signed in to change notification settings - Fork 0
/
hydra-monitor-sentinel.js
118 lines (102 loc) · 3.01 KB
/
hydra-monitor-sentinel.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
// hydra-monitor-sentinel.js
// Copyright (C) Rob Colbert <rob.colbert@openplatform.us>
// License: MIT
'use strict';
const os = require('os');
const v8 = require('v8');
const diskusage = require('diskusage');
class HydraMonitorSentinel {
constructor (hydra) {
var self = this;
if (!hydra || !hydra.app || !hydra.config) {
throw new Error('invalid HYDRA module passed to hydra-monitor-sentinel');
}
self.hydra = hydra;
self.log = hydra.app.locals.log;
self.redis = hydra.app.locals.redis;
self.config = hydra.config;
self.app = hydra.express();
self.router = hydra.express.Router();
self.router.get('/', self.onMonitorRequest.bind(self));
self.app.use('/hydra-monitor', self.router);
self.status = 'initializing';
self.log.debug('hydra-monitor-sentinel', { status: self.status });
self.stats = { };
self.app.listen(self.config.monitor.port, self.config.monitor.bind, ( ) => {
self.status = 'attached';
self.log.info('hydra-monitor-sentinel online', {
bind: self.config.monitor.bind,
port: self.config.monitor.port
});
});
}
onMonitorRequest (req, res, next) {
var self = this;
self.log.debug('monitor request');
self.stats.cpuUsage = process.cpuUsage(self.stats.cpuUsage);
var viewModel = {
success: true,
os: {
arch: os.arch(),
cpus: os.cpus(),
freemem: os.freemem(),
hostname: os.hostname(),
loadavg: os.loadavg(),
networkInterfaces: os.networkInterfaces(),
platform: os.platform(),
release: os.release(),
totalmem: os.totalmem(),
type: os.type(),
uptime: os.uptime(),
userInfo: os.userInfo()
},
disks: { },
v8: {
HeapSpaceStatistics: v8.getHeapSpaceStatistics(),
HeapStatistics: v8.getHeapStatistics()
},
process: {
cpuUsage: self.stats.cpuUsage,
memoryUsage: process.memoryUsage(),
pid: process.pid,
platform: process.platform,
ppid: process.ppid,
release: process.release,
title: process.title,
uptime: process.uptime(),
version: process.version,
versions: process.versions
},
package: self.hydra.pkg
};
var jobs = [ ];
if (self.config.monitor.disks && self.config.monitor.disks.length) {
self.config.monitor.disks.forEach((pathname) => {
jobs.push(
diskusage
.check(pathname)
.then((result) => {
result.pathname = pathname;
return Promise.resolve(result);
})
);
});
}
Promise
.all(jobs)
.then((results) => {
viewModel.disks = results;
res.status(200).json(viewModel);
})
.catch((error) => {
res.status(500).json({ error: error });
});
}
}
module.exports = (hydra) => {
if (!hydra) {
throw new Error('Must pass main HYDRA module to hydra-monitor-sentinel');
}
var sentinel = new HydraMonitorSentinel(hydra);
return sentinel;
};