-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
130 lines (114 loc) · 2.73 KB
/
server.js
File metadata and controls
130 lines (114 loc) · 2.73 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
var server = require('http');
var express = require('express');
var exec = require('child_process').exec;
var _getCurrentCPU = "top -bn 1 | awk '{print $9}' | tail -n +8 | awk '{s+=$1} END {print s}'";
var _getTotalRAM = "cat /proc/meminfo | grep MemTotal | awk '{print $2 \" \" $3}'";
var _getFreeRAM = "cat /proc/meminfo | grep MemFree | awk '{print $2 \" \" $3}'";
var _getUsedRAM = "expr $(cat /proc/meminfo | grep MemTotal | awk '{print $2}') - $(cat /proc/meminfo | grep MemFree | awk '{print $2}')";
var _getNetworkRX = "sh /home/razvan/work/NodeJs-Monitor/network_monitor_rx.sh wlan0";
var _getNetworkTX = "sh /home/razvan/work/NodeJs-Monitor/network_monitor_tx.sh wlan0";
var port = 5000;
var app = express();
server.createServer(app).listen(port);
app.get('/cpu', function(req, resp){
getCPU(function(_CPU){
resp.write(_CPU.toString(), function(){
resp.end();
});
});
})
app.get('/total_ram', function(req, resp){
getTotalRAM(function(_totalRAM){
resp.write(_totalRAM.toString(), function(){
resp.end();
});
});
})
app.get('/free_ram', function(req, resp){
getFreeRAM(function(_freeRAM){
resp.write(_freeRAM.toString(), function(){
resp.end();
});
});
})
app.get('/used_ram', function(req, resp){
getUsedRAM(function(_usedRAM){
resp.write(_usedRAM.toString(), function(){
resp.end();
});
});
})
app.get('/net_tx', function(req, resp){
getTXTraffic(function(tx_bytes){
resp.write(tx_bytes.toString(), function(){
resp.end();
});
});
})
app.get('/net_rx', function(req, resp){
getRXTraffic(function(rx_bytes){
resp.write(rx_bytes.toString(), function(){
resp.end();
});
});
})
function getCPU(next){
exec(_getCurrentCPU, function(err, stderr, stdout){
if (!err){
next(stderr);
next(stdout);
} else {
next(err);
}
});
}
function getTotalRAM(next){
exec(_getTotalRAM, function(err, stderr, stdout){
if (!err){
next(stderr);
next(stdout);
} else {
next(err);
}
});
}
function getFreeRAM(next){
exec(_getFreeRAM, function(err, stderr, stdout){
if (!err){
next(stderr);
next(stdout);
} else {
next(err);
}
});
}
function getUsedRAM(next){
exec(_getUsedRAM, function(err, stderr, stdout){
if (!err){
next(stderr);
next(stdout);
} else {
next(err);
}
});
}
function getRXTraffic(next){
exec(_getNetworkRX, function(err, stderr, stdout){
if (!err){
next(stderr);
next(stdout);
} else {
next(err);
}
});
}
function getTXTraffic(next){
exec(_getNetworkTX, function(err, stderr, stdout){
if (!err){
next(stderr);
next(stdout);
} else {
next(err);
}
});
}