-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (143 loc) · 3.98 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const CPM = require('comlog-process-manager');
function ComlogProcessWatcher(options) {
require('comlog-event-handler')(this);
var _self = this;
this.satus = null; // null = start, true = off, false = on
this.debug = false;
this.interval = 60000; // 1 Minute
this.maxMemory = -1;
this.minMemory = -1;
this.maxCount = -1;
this.minCount = -1;
this.logger = console;
// Private funktionen
var _running = false, _timer = null;
// Extracting local options
for(var i in options) {
if (typeof this[i] != 'undefined') {
this[i] = options[i];
delete options[i];
}
}
function _toFloat(num) {
num = num+'';
if (num.indexOf(',') > -1 && num.indexOf(',') > -1) {
if (num.indexOf(',') > num.indexOf('.')) num = num.split('.').join('').split(',').join('.');
if (num.indexOf('.') > num.indexOf(',')) num = num.split(',').join('');
}
else if (num.indexOf(',') > -1) num = num.split(',').join('.');
if (isNaN(num)) num = num.replace(/[^0-9.]/g, '');
if (isNaN(num)) num = 0;
return parseFloat(num);
}
function _memConvert(size) {
if (typeof size == 'string') {
var m = size.match(/([0-9.,]+)(.*)/);
switch (m[2]) {
case 'G':
case 'GB':
case 'g':
case 'gb':
size = _toFloat(m[1])*8589934592;
break;
case 'Gb':
size = _toFloat(m[1])*1000000000;
break;
case 'M':
case 'MB':
case 'm':
case 'mb':
size = _toFloat(m[1])*8388608;
break;
case 'Mb':
size = _toFloat(m[1])*1000000;
break;
case 'KB':
case 'kb':
size = _toFloat(m[1])*1024;
break;
case 'Kb':
size = _toFloat(m[1])*1000*8;
break;
case 'B':
case 'Byte':
size = _toFloat(m[1])*8;
break;
default:
size = _toFloat(m[1]);
break;
}
}
if (isNaN(size)) size = 0;
return size;
}
function _watch() {
if (_running) return;
_running = true;
CPM.lookup(options, function (err, resultList) {
if (err !== null) {
if (_self.debug) _self.logger.error(err.stack || err);
_self.trigger('error', [new Error("Process \""+options.name+"\" check error \n"+err.message)]);
if (_self.satus === true) _self.trigger('down');
_self.satus = false;
}
else {
if (resultList.length > 0) {
var new_status = true, msg = 'Process "'+options.name+'" check ok';
_self.maxMemory = _memConvert(_self.maxMemory);
_self.minMemory = _memConvert(_self.minMemory);
// to many processes
if (_self.maxCount > -1 && _self.maxCount < resultList.length) {
new_status = false;
msg = "To many \""+options.name+"\" processes";
}
// To few processes
else if (_self.minCount > -1 && _self.minCount > resultList.length) {
new_status = false;
msg = "To few \""+options.name+"\" processes";
}
// out of memory
else if (_self.maxMemory > -1 || _self.minMemory > -1) {
var memsum = 0;
for(var p=0; p < resultList.length; p++) {
memsum += _toFloat(resultList[p].memory);
}
if (_self.maxMemory > -1 && _self.maxMemory < memsum) {
new_status = false;
msg = "Big memory \""+options.name+"\" usage";
}
else if (_self.maxMemory > -1 && _self.minMemory > memsum) {
new_status = false;
msg = "Low memory \""+options.name+"\" usage";
}
}
// check result
if (_self.debug) _self.logger.info(msg);
if (_self.satus !== null && _self.satus !== new_status) _self.trigger(new_status ? 'up' : 'down');
_self.satus = new_status;
}
else {
if (_self.debug) _self.logger.info("Process \""+options.name+"\" not found");
if (_self.satus === true) _self.trigger('down');
_self.satus = false;
}
}
_running = false;
_timer = setTimeout(_watch, _self.interval);
});
}
/**
* Überwachung starten
*/
this.start = function() {
_watch();
};
/**
* Überwachung stoppen
*/
this.stop = function() {
if (_timer !== null) clearInterval(_timer);
};
for(var i in options) this[i] = options[i];
}
module.exports = ComlogProcessWatcher;