-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
277 lines (248 loc) · 6.78 KB
/
app.js
File metadata and controls
277 lines (248 loc) · 6.78 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
var snmp = require ("net-snmp");
var TelegramBot = require('node-telegram-bot-api');
// Define BothFather Token
var token = 'YOU_BOT_TOKEN';
// Create a TelegramBot instance
var bot = new TelegramBot(token, { polling: true });
/**
* Command /start
* Show the list of commands available to the bot
**/
bot.onText(/\/start/, function (msg, match) {
var chatId = msg.chat.id;
var resp = "Avaliable commands:\n";
resp += "/start - Show this message\n";
resp += "/session - Start a new SNMP session\n";
resp += '/commands - Show the command list\n';
// send back the matched "whatever" to the chat
bot.sendMessage(chatId, resp);
});
// Listen for any kind of message. There are different kinds of
// messages.
bot.on('message', function (msg) {
switch (getCommand()) {
case 'session':
commandSession(chatId, msg.text);
setCommand();
break;
default:
break;
}
// send a message to the chat acknowledging receipt of their message
//bot.sendMessage(chatId, "Received your message");
});
/**
* Snmp Session Setter
**/
var setSession = function(session)
{
this.session = session;
};
/**
* Snmp Session Getter
**/
var getSession = function() {
return this.session;
};
/**
* Set current command
**/
var setCommand = function(command) {
this.command = command;
};
/**
* Get current command
**/
var getCommand = function() {
return this.command;
}
/**
* Set current ChatId
**/
function setChatId(chatId) {
this.chatId = chatId;
}
/**
* Get current ChatId
**/
function getChatId()
{
return this.chatId;
}
/**
* Load Buttons
**/
function loadButtons() {
var options = {
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: 'Load average', callback_data: 'loadaverage' }],
[{ text: 'CPU', callback_data: 'cpu' }],
[{ text: 'Memory', callback_data: 'memory' }],
[{ text: 'Uptime', callback_data: 'uptime' }]
]
})
};
bot.sendMessage(getChatId(), 'Seleect an option to show:', options).then(function (sended) {
// `sended` is the sent message.
});
}
/**
* Show a default buttons options
**/
bot.onText(/\/commands/, function (msg, match) {
loadButtons();
});
/**
* Receive a button callback
**/
bot.on('callback_query', function (msg) {
setChatId(msg.message.chat.id);
setCommand(msg.data);
bot.sendMessage(getChatId(), 'Processing, wait...').then(function (sended) {
switch (msg.data) {
case 'loadaverage':
loadaverage();
setCommand();
break;
case 'cpu':
cpu();
setCommand();
break;
case 'memory':
memory();
setCommand();
break;
case 'uptime':
uptime();
setCommand();
break;
default:
break;
}
});
});
/**
* Get the load average
**/
function loadaverage() {
console.log('processing load average');
var msg = "Load average:\n\n";
var min = [1,5,15];
var i = 0;
getOid(['1.3.6.1.4.1.2021.10.1.3.1', '1.3.6.1.4.1.2021.10.1.3.2', '1.3.6.1.4.1.2021.10.1.3.3'], function(data) {
msg += min[i]+" minute(s): "+data+"\n";
if( i == 2)
{
bot.sendMessage(getChatId(),msg);
}
i = i+1;
});
}
/**
* Get CPU info
**/
function cpu() {
console.log('processing cpu');
var msg = "CPU:\n\n";
var i = 0;
var desc = ['percentage of user: ',
'raw user cpu time: ',
'percentages of system CPU time: ',
'raw system cpu time: ',
'percentages of idle CPU time: ',
'raw idle cpu time: ',
'raw nice cpu time: '];
getOid(['1.3.6.1.4.1.2021.11.9.0', '1.3.6.1.4.1.2021.11.50.0', '1.3.6.1.4.1.2021.11.10.0', '1.3.6.1.4.1.2021.11.52.0',
'1.3.6.1.4.1.2021.11.11.0', '1.3.6.1.4.1.2021.11.53.0', '1.3.6.1.4.1.2021.11.51.0'], function(data) {
msg += desc[i]+data+"\n";
if( i == desc.length -1 )
{
bot.sendMessage(getChatId(), msg);
}
i = i+1;
});
}
/**
* Get memory info
**/
function memory() {
console.log('processing memory');
var msg = "Memory:\n\n";
var i = 0;
var desc = ['Total Swap Size: ',
'Available Swap Space: ',
'Total RAM in machine: ',
'Total RAM used: ',
'Total RAM Free: ',
'Total RAM Shared: ',
'Total RAM Buffered: ',
'Total Cached Memory: '];
getOid(['1.3.6.1.4.1.2021.4.3.0', '1.3.6.1.4.1.2021.4.4.0', '1.3.6.1.4.1.2021.4.5.0',
'1.3.6.1.4.1.2021.4.6.0', '1.3.6.1.4.1.2021.4.11.0', '1.3.6.1.4.1.2021.4.13.0',
'1.3.6.1.4.1.2021.4.14.0', '1.3.6.1.4.1.2021.4.15.0'], function(data) {
msg += desc[i]+(data/1000)+" MB\n";
if( i == desc.length -1 )
{
bot.sendMessage(getChatId(), msg);
}
i = i+1;
});
}
/**
* Get uptime info
**/
function uptime() {
console.log('function uptime');
var msg = "UPTIME:\n\n";
getOid(['1.3.6.1.2.1.1.3.0'], function(data) {
var seconds = Math.round(data/100);
var minutes = Math.round(seconds/60);
var hours = Math.round(minutes/60);
var time = hours+':'+(minutes/10)+':'+(seconds/1000);
msg += 'Uptime: '+time+"\n";
bot.sendMessage(getChatId(), msg);
});
}
/**
* Execute an SNMP get
**/
function getOid(oid, callback) {
if( !getSession() )
{
// send back the matched "whatever" to the chat
bot.sendMessage(chatId, "You have not established a connection. Use /session <address> before.");
}
else
{
getSession().get (oid, function (error, varbinds) {
if (error) {
console.error (error);
} else {
for (var i = 0; i < varbinds.length; i++)
if (snmp.isVarbindError (varbinds[i])) {
console.error (snmp.varbindError (varbinds[i]));
bot.sendMessage(chatId, "An error occurred:\n "+snmp.varbindError (varbinds[i]));
} else {
callback(varbinds[i].value);
}
}
});
}
}
/**
* Open a new SNMP session
**/
bot.onText(/\/session/, function (msg, match) {
setCommand('session');
setChatId(msg.chat.id);
bot.sendMessage(msg.from.id, 'Enter the SNMP agent address');
});
/**
* User enter Session
**/
var commandSession = function(chatId, ip) {
setSession(snmp.createSession(ip, "public"));
bot.sendMessage(chatId, "Sure! A new SNMP connection open in "+ip);
console.log('Sure! A new SNMP connection open in %s', ip);
}