monit.js
2.94 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
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
'use strict';
const Rp = require('request-promise');
const _ = require('lodash');
const ssh = require('ssh2');
const models = require('../models');
const Server = models.Server;
const ws = require('../../lib/ws');
const monit = {
node: function (project, hosts) {
hosts.forEach((host) => {
var obj = {
'total': 0,
'status': {},
};
Rp({
uri: `http://${host}:9615`,
json: true
}).then(function (data) {
var processes = data.processes || [];
processes.forEach(function (p) {
if (p.name === 'pm2-server-monit') {
obj.cpuUsg = p.pm2_env.axm_monitor['CPU usage'].value;
obj.freeMen = p.pm2_env.axm_monitor['Free memory'].value;
}
if (p.name === project.name) {
obj.total++;
if (!obj.status[p.pm2_env.status]) {
obj.status[p.pm2_env.status] = 1;
} else {
obj.status[p.pm2_env.status]++;
}
}
});
}).catch(function (err) {
obj.errmsg = '获取监控状态失败'
}).finally(function () {
ws.broadcast(`/monit/${project._id}`, {
host: host,
monitdata: obj
});
});
});
},
php: function(project, hosts) {
if (!_.isArray(hosts)) {
return;
}
_.forEach(hosts, host => {
var obj = {
'total': 0,
'status': {},
};
Server.findByHost(host).then(server => {
let conn = new ssh.Client();
conn.on('ready', async() => {
conn.exec('ps aux|grep httpd|wc -l ', (err, stream) => {
if (err) {
console.log(err);
} else {
stream.on('close', () => {
ws.broadcast(`/monit/${project._id}`, {
host: host,
monitdata: obj
});
conn.end();
}).on('data', (data) => {
let total = _.trim(data.toString('utf-8'));
obj.total = total;
obj.status.online = total;
});
}
});
}).on('error', (err) => {
console.log(err);
conn.end();
}).connect(server);
}).catch(e => {});
});
}
};
module.exports = monit;