collect_data.js
4.18 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* 采集数据
*
* @class Collect
* @author shenzm<zhimin.shen@yoho.cn>
* @date 2016/10/12
*/
import ssh from 'ssh2';
import Rp from 'request-promise';
import Trace from '../logger/trace.js';
import config from '../../config/config.js';
import {
Server
} from '../models';
var tracer = new Trace({
host: config.influxdb.host,
port: config.influxdb.port
});
class Collect {
constructor(host, projectname, cloud) {
this.host = host;
this.projectname = projectname;
this.cloud = cloud;
this.scriptRunning = false;
this.retry = {};
}
async collect() {
let self = this;
let server = await Server.findByHost(self.host);
self.server = {
host: server.host,
username: server.username,
password: server.password,
port: server.port
}
let obj = {
'total': 0,
'status': {}
};
Rp({
uri: `http://${self.host}:9615`,
json: true
}).then(function(data) {
var processes = data.processes || [];
processes.forEach(function(p) {
if (p.name === 'pm2-server-monit') {
var cpuUsg = p.pm2_env.axm_monitor['CPU usage'].value;
var freeMem = p.pm2_env.axm_monitor['Free memory'].value;
obj.cpuUsg = cpuUsg ? parseFloat(cpuUsg.replace('%', '')) : 0;
obj.freeMem = freeMem ? parseFloat(freeMem.replace('%', '')) : 0;
}
if (p.name === self.projectname) {
obj.total++;
if (!obj.status[p.pm2_env.status]) {
obj.status[p.pm2_env.status] = 1;
} else {
obj.status[p.pm2_env.status]++;
}
}
});
if (obj.cpuUsg === undefined) { // install server monit
const script = 'pm2 install pm2-server-monit';
self.execScript(script);
}
// add into influxDB todo
tracer.report('server_data', {
host: self.host,
cloud: self.cloud
}, obj);
}).catch(function(err) {
const script = 'pm2 web';
self.execScript(script);
});
}
execScript(script) {
let self = this;
if (self.scriptRunning || self.retry[`${self.host}_${script}`] > 5) {
// 脚本执行中,或者重试次数大于5次以上时,不执行脚本
return;
}
let retryCount = self.retry[`${self.host}_${script}`] || 0;
self.retry[`${self.host}_${script}`] = ++retryCount;
self.scriptRunning = true;
let conn = new ssh.Client();
conn.on('ready', () => {
self._log(`>>>>host:[${self.host}] script[${script}]`);
conn.exec(`${script}`, (err, stream) => {
if (err) {
conn.end();
self._log(`host:[${self.host}] script:[${script}] exec fail error: ${err}`);
self.scriptRunning = false;
} else {
stream.stdout.on('data', (data) => {
//self._log(data.toString());
});
stream.stderr.on('data', (data) => {
//self._log(data.toString());
});
stream.on('exit', (code) => {
conn.end();
if (code === 0) {
self._log(`host:[${self.host}] script[${script}] exec success`);
} else {
self._log(`host:[${self.host}] script[${script}] exec fail`);
}
self.scriptRunning = false;
});
}
});
}).on('error', (err) => {
self._log(`connect error ${self.host} ${err}`);
self.scriptRunning = false;
}).connect(Object.assign(self.server, {
readyTimeout: 5000
}));
}
_log(msg) {
console.log(msg);
}
}
export default Collect;