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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
var os = require('os');
var ps = require('portscanner');
var nl = require('./numberlogger.js');
var config = require('./config.js');
var express = require('express');
var compression = require('compression');
var PORT = 3369;
var serverstat = {
"uptime": os.uptime(),
"load": [0, 0, 0],
"services": {},
"stats" : {}
};
var serverinfo = {
"hostname": os.hostname(),
"release": os.release(),
"cpuname": os.cpus()[0].model,
"cpunum": os.cpus().length,
"totmem": Math.round(os.totalmem()/1024/1024),
"desc" : config.description,
"stats": {}
};
var statResponse = {};
function checkp(key) {
ps.checkPortStatus(config.services[key], 'localhost', function (error, status) {
serverstat.services[key] = (status == 'open');
})
}
var logger = function (name, length) {
this.name = name;
this.sec = new nl(name + ":sec", length);
this.min = new nl(name + ":min", length);
this.hrs = new nl(name + ":hrs", length);
this.day = new nl(name + ":day", length);
this.time = new Date();
};
logger.prototype.add = function (time, x) {
function average(l, n) {
var sum = 0;
for (var i = 0; i < n; ++i)
sum += l.ns[l.len - 1 - i];
return sum / n;
}
if (time.getDay() != this.time.getDay())
this.day.add(average(this.hrs, 24));
if (time.getHours() != this.time.getHours())
this.hrs.add(average(this.min, 60));
if (time.getMinutes() != this.time.getMinutes())
this.min.add(average(this.sec, 60));
this.sec.add(x);
this.time = time;
};
logger.prototype.setupResponse = function(res){
var host = this;
var stat = [];
function setupSingleStat(id){
stat.push(host[id].name);
res[host[id].name] = function (stat) {
stat.stats[host.name] = host[id].stringify();
};
}
console.log("Setting up response for "+this.name);
setupSingleStat("sec");
setupSingleStat("min");
setupSingleStat("hrs");
setupSingleStat("day");
serverinfo.stats[this.name] = stat;
};
logger.prototype.stringify = function () {
return [this.sec.stringify(), this.min.stringify(), this.hrs.stringify(), this.day.stringify()];
};
var cpustat = new logger("cpu", 100);
var memstat = new logger("mem", 100);
cpustat.setupResponse(statResponse);
memstat.setupResponse(statResponse);
function refreshStat() {
for (var key in config.services) {
if (config.services.hasOwnProperty(key)) {
checkp(key);
}
}
serverstat.uptime = os.uptime();
serverstat.load = os.loadavg();
}
refreshStat();
setInterval(refreshStat, 1000);
function getCPUInfo() {
var cpus = os.cpus();
var user = 0;
var nice = 0;
var sys = 0;
var idle = 0;
var irq = 0;
for (var cpu in cpus)
if (cpus.hasOwnProperty(cpu)) {
user += cpus[cpu].times.user;
nice += cpus[cpu].times.nice;
sys += cpus[cpu].times.sys;
irq += cpus[cpu].times.irq;
idle += cpus[cpu].times.idle;
}
var total = user + nice + sys + idle + irq;
return {
'idle': idle,
'total': total
};
}
function getUsage() {
var stats = getCPUInfo();
setInterval(function () {
var nstats = getCPUInfo();
var idle = nstats.idle - stats.idle;
var total = nstats.total - stats.total;
var perc = 1 - idle / total;
stats = nstats;
var time = new Date();
cpustat.add(time, perc);
memstat.add(time, memPercentage());
}, 1000);
}
function memPercentage() {
return 1 - os.freemem() / os.totalmem();
}
getUsage();
var app = express();
app.use(compression());
app.get('/', function (req, res) {
res.append('Content-Type', 'application/json');
res.append('Access-Control-Allow-Origin', '*');
res.send(JSON.stringify(serverinfo));
});
app.get('/stat', function (req, res) {
res.append('Content-Type', 'application/json');
res.append('Access-Control-Allow-Origin', '*');
var stat = JSON.parse(JSON.stringify(serverstat));
for (var query in req.query)
if (req.query.hasOwnProperty(query))
statResponse[req.query[query]](stat);
res.send(JSON.stringify(stat));
});
app.listen(PORT);
console.log("Server listening on: %s", PORT);
|