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
|
var os = require('os');
var http = require('http');
var ps = require('portscanner');
var dispatcher = require('httpdispatcher');
var spawn = require('child_process').spawn;
function getmem(){
var prc = spawn('free', []);
prc.stdout.setEncoding('utf8');
prc.stdout.on('data', function (data) {
var str = data.toString()
var lines = str.split(/\n/g);
for(var i = 0; i < lines.length; i++) {
lines[i] = lines[i].split(/\s+/);
}
serverstat.mem = lines[1][2]/lines[1][1];
});
prc.on('close', function (code) {
});
}
var PORT = 3369;
var serverstat={
"release":"undef",
"uptime":"",
"load":[0,0,0],
"mem":0,
"mc":false,
"pcs":{
"2222":false,
"2223":false,
"2224":false,
"2225":false
}
}
serverstat.release = os.release();
function secondsToString(seconds)
{
function adds(x,a){
return x + " " + a + " ";
}
var numdays = Math.floor(seconds / 86400); seconds%=86400;
var numhours = Math.floor(seconds / 3600); seconds%=3600;
var numminutes = Math.floor(seconds / 60); seconds%=60;
return adds(numdays,"day") + adds(numhours,"hrs")+ adds(numminutes,"min") + adds(seconds,"sec");
}
function refreshStat(){
ps.checkPortStatus(25565, 'cnjoe.info', function(error, status) {
// Status is 'open' if currently in use or 'closed' if available
if (status=='open')
serverstat.mc=true;
else
serverstat.mc=false;
})
for (var key in serverstat.pcs) {
if (serverstat.pcs.hasOwnProperty(key)) {
ps.checkPortStatus(key, 'cnjoe.info', function(error, status) {
// Status is 'open' if currently in use or 'closed' if available
if (status=='open')
serverstat.pcs[key]=true;
else
serverstat.pcs[key]=false;
})
}
}
getmem();
serverstat.uptime = os.uptime();
serverstat.load = os.loadavg();
}
function handleRequest(request, response){
try {
//log the request on console
// console.log(request.url);
//Disptach
dispatcher.dispatch(request, response);
} catch(err) {
console.log(err);
}
}
//For all your static (js/css/images/etc.) set the directory name (relative path).
dispatcher.setStatic('resources');
//A sample GET request
dispatcher.onGet("/", function(req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(serverstat));
});
//A sample POST request
//dispatcher.onPost("/post1", function(req, res) {
// res.writeHead(200, {'Content-Type': 'text/plain'});
// res.end('Got Post Data');
//});
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});
refreshStat();
setInterval(refreshStat,1000);
|