-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverController.js
149 lines (133 loc) · 5.2 KB
/
serverController.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
const pidusage = require("pidusage"),
os =require('os'),
request = require('request'),
express = require('express'),
exec = require('child_process').exec;
TelegramBot = require('node-telegram-bot-api');
const bot = new TelegramBot('261219001:AAEtz7spMMNwQQ_AcbCBtKXHAN01gCFVQSI', {
polling:false
});
// CHECK SERVER HEALTH STATUS CONTINUOUSLY TILL USER SESSION CONTINUOUSLY
exports.checkServerStatus = function (fromId,url){
request(url, function(socket,response,err){
var date = '';
var connection = false;
if(err){
if(err.code == 408 || err.code == 404 || err.code == 400 || err.code == 403)
{
let result = "Server Error. Please try again."
bot.sendMessage(fromId,result);
}
}
if(socket){
setTimeout(function(){
bot.sendMessage(fromId,'Cannot connect to the server');
},5000);
var start = process.hrtime();
var usageInStart = process.cpuUsage();
}
if(response){
intervalId = setInterval(function(){
let response_code = response.statusCode;
let response_time = process.hrtime(start);
let response_msg = response.statusMessage;
date = Date(Date.now()).toString();
connection = true;
bot.sendMessage(fromId,
"Response time: " + ~~((response_time[0]/1000000)-1) + " s, " + ~~(response_time[1]/1000000) +"ms" + "\nStatus: " + response_code +
"\nResponse: " + response_msg + "\nDate: " + date +
"\nAlive: " + connection);
},5000);
}
});
}
// TO STOP THE SERVER
exports.stopServer = function(fromId){
clearInterval(intervalId);
bot.sendMessage(fromId,'Monitoring stopped');
}
// CHECK SERVER STATUS ONLY ONCE
exports.checkServerStatusOnce = function (fromId,url){
request(url, function(socket,response,err){
var date = '';
var connection = false;
if(err){
if(err.code == 408 || err.code == 404 || err.code == 400 || err.code == 403)
{
let result = "Server Error. Please try again."
bot.sendMessage(fromId,result);
}
}
if(socket){
setTimeout(function(){
bot.sendMessage(fromId,'Cannot connect to the server');
},5000);
var start = process.hrtime();
var usageInStart = process.cpuUsage();
}
if(response){
let response_code = response.statusCode;
let response_time = process.hrtime(start);
let response_msg = response.statusMessage;
date = Date(Date.now()).toString();
connection = true;
bot.sendMessage(fromId,
"Response time: " + ~~((response_time[0]/1000000)-1) + " s, " + response_time[1]/1000000+"ms" + "\nStatus: " + response_code +
"\nResponse: " + response_msg + "\nDate: " + date +
"\nAlive: " + connection);
}
});
}
// CHECK RUNNING PROCESSES
exports.checkRunningProcesses = function(fromId) {
exec( 'ps -e', function(err, stdout, stderr) {
if (err) {
bot.sendMessage(fromId,"\n"+stderr);
} else {
console.log(stdout);
bot.sendMessage(fromId,"Running Processes - \n" + stdout);
}
});
}
// EXECUTE COMMAND DIRECTLY TO THE SERVER
exports.userCmd = function(fromId,cmdC){
exec(cmdC,function (err,stdout,stderr) {
if (err) {
bot.sendMessage(fromId,"\n"+stderr);
} else {
bot.sendMessage(fromId,stdout + "\n\n Job done !");
}
});
}
// CHECK SERVER AND SEND NOTIFICATIONS TO USER
exports.checkServerStat = function (fromId){
let date = Date(Date.now()).toString();
var cpus = os.cpus();
bot.sendMessage(fromId,"The health status of this server is :\n");
for(var i = 0, len = cpus.length; i < len; i++) {
var cpu = cpus[i], total = 0, processTotal = 0, strPercent = '';
console.log("User time in ms: " + cpu.times.user + "\nSystem time in ms:" + cpu.times.sys + "\n Idle time in ms: " + cpu.times.idle );
for (type in cpu.times){
var total = total + cpu.times[type];
}
for (type in cpu.times){
var percent = ~~(100 * cpu.times[type]/total);
strPercent += type + ' ' + percent + '%';
if(type != 'idle'){
processTotal += percent;
}
}
console.log("\t",strPercent)
console.log("\t",'Total Processor: ',total);
console.log("\t",'TOTAL: ',processTotal + "%");
console.log("\t","free memory in MB:", os.freemem()/(1024*1024));
console.log("\t","total memory in MB:", os.totalmem()/(1024*1024));
console.log("\t","uptime", os.uptime()/(60*60) + "hours.");
console.log("\t","loadavg", os.loadavg());
bot.sendMessage(fromId,"Cpu :" + (++i) +
"\nServer Performance : " + strPercent +
"\nProcesses comsuming: " + processTotal + "%" +
"\nFree memory available in MB:" + os.freemem()/(1024*1024) + "\nTotal Memory Available : " + os.totalmem()/(1024*1024) +
"\nUptime (in hrs): " + os.uptime()/(60*60) + "\nLoad Average : " + os.loadavg());
}
}