forked from pgmillon/observium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.php
executable file
·316 lines (243 loc) · 10.5 KB
/
irc.php
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env php
<?php
/**
* Observium
*
* This file is part of Observium.
*
* @package observium
* @subpackage ircbot
* @author Adam Armstrong <[email protected]>
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2016 Observium Limited
*
*/
chdir(dirname($argv[0]));
# Disable annoying messages... well... all messages actually :)
error_reporting(0);
$debug=0;
include("includes/sql-config.inc.php");
include_once("includes/polling/functions.inc.php");
include_once("includes/discovery/functions.inc.php");
include_once('Net/SmartIRC.php');
mysql_close();
# Redirect to /dev/null or logfile if you aren't using screen to keep tabs
echo "Observium Bot Starting ...\n";
echo "\n";
echo "Timestamp Command\n";
echo "----------------- ------- \n";
class observiumbot
{
///
# HELP Function
///
function help_info(&$irc, &$data)
{
global $config;
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, "Commands: .help, .log, .status, .version, .down, .port, .device, .listdevices");
echo date("m-d-y H:i:s ");
echo "HELP\n";
mysql_close();
}
///
# VERSION Function
///
function version_info(&$irc, &$data)
{
global $config;
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, OBSERVIUM_PRODUCT . " " . OBSERVIUM_VERSION);
echo date("m-d-y H:i:s ");
echo "VERSION\t\t". OBSERVIUM_VERSION . "\n";
mysql_close();
}
///
# LOG Function
///
function log_info(&$irc, &$data)
{
global $config;
mysql_connect($config['db_host'],$config['db_user'],$config['db_pass']);
mysql_select_db($config['db_name']);
$entry = dbFetchRow("SELECT `event_id`,`device_id`,`timestamp`,`message`,`type` FROM `eventlog` ORDER BY `event_id` DESC LIMIT 1");
$device_id = $entry['device_id'];
$device = dbFetchRow("SELECT `hostname` FROM `devices` WHERE `device_id` = $device_id");
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, $entry['event_id'] ." ". $device['hostname'] ." ". $entry['timestamp'] ." ". $entry['message'] ." ". $entry['type']);
echo date("m-d-y H:i:s ");
echo "LOG\n";
mysql_close();
}
///
# DOWN Function
///
function down_info(&$irc, &$data)
{
global $config;
mysql_connect($config['db_host'],$config['db_user'],$config['db_pass']);
mysql_select_db($config['db_name']);
foreach (dbFetchRows("SELECT * FROM `devices` where status=0") as $device)
{
$message .= $sep . $device['hostname'];
$sep = ", ";
}
if (!($message)) { $message = "0 host down"; }
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, $message);
mysql_close();
echo date("m-d-y H:i:s ");
echo "DOWN\n";
}
///
# DEVICE Function
///
function device_info(&$irc, &$data)
{
global $config;
$hostname = $data->messageex[1];
mysql_connect($config['db_host'],$config['db_user'],$config['db_pass']);
mysql_select_db($config['db_name']);
$device = dbFetchRow("SELECT * FROM `devices` WHERE `hostname` = ?",array($hostname));
mysql_close();
if (!$device)
{
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, "Error: Bad or Missing hostname, use .listdevices to show all devices.");
} else {
if ($device['status'] == 1) { $status = "Up " . formatUptime($device['uptime'] . " "); } else { $status = "Down "; }
if ($device['ignore']) { $status = "*Ignored*"; }
if ($device['disabled']) { $status = "*Disabled*"; }
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, $device['os'] . " " . $device['version'] . " " .
$device['features'] . " " . $status);
echo date("m-d-y H:i:s ");
echo "DEVICE\t\t". $device['hostname']."\n";
}
}
///
# PORT Function
///
function port_info(&$irc, &$data)
{
global $config;
$hostname = $data->messageex[1];
$ifname = $data->messageex[2];
mysql_connect($config['db_host'],$config['db_user'],$config['db_pass']);
mysql_select_db($config['db_name']);
$device = dbFetchRow("SELECT * FROM `devices` WHERE `hostname` = ?",array($hostname));
$sql = "SELECT *, `ports`.`port_id` as `port_id`";
$sql .= " FROM `ports`";
$sql .= " LEFT JOIN `ports-state` ON `ports`.port_id = `ports-state`.port_id";
$sql .= " WHERE ports.`ifName` = ? OR ports.`ifDescr` = ? AND ports.device_id = ?";
$port = dbFetchRow($sql, array($ifname, $ifname, $device['device_id']));
mysql_close();
$bps_in = formatRates($port['ifInOctets_rate']);
$bps_out = formatRates($port['ifOutOctets_rate']);
$pps_in = format_bi($port['ifInUcastPkts_rate']);
$pps_out = format_bi($port['ifOutUcastPkts_rate']);
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, $port['ifAdminStatus'] . "/" . $port['ifOperStatus'] . " " .
$bps_in. " > bps > " . $bps_out . " | " . $pps_in. "pps > PPS > " . $pps_out ."pps");
echo date("m-d-y H:i:s ");
echo "PORT\t\t\t" . $hostname . "\t". $ifname . "\n";
}
///
# LISTPORTS Function
///
function list_ports(&$irc, &$data)
{
global $config;
unset ($message);
$hostname = $data->messageex[1];
mysql_connect($config['db_host'],$config['db_user'],$config['db_pass']);
mysql_select_db($config['db_name']);
$device = dbFetchRow("SELECT * FROM `devices` WHERE `hostname` = ?",array($hostname));
$ports = dbFetchRows("SELECT * FROM `ports` WHERE device_id = ?", array($device['device_id']));
mysql_close();
foreach ($ports as $port)
{
$message .= $sep . $port['ifDescr'];
$sep = ", ";
}
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, $message);
echo date("m-d-y H:i:s ");
echo "LISTPORTS\t\t\t" . $hostname . "\n";
}
///
# LISTDEVICES Function
///
function list_devices(&$irc, &$data)
{
global $config;
unset ($message);
mysql_connect($config['db_host'],$config['db_user'],$config['db_pass']);
mysql_select_db($config['db_name']);
foreach (dbFetchRows("SELECT `hostname` FROM `devices`") as $device)
{
$message .= $sep . $device['hostname'];
$sep = ", ";
}
mysql_close();
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, $message);
unset($sep);
echo date("m-d-y H:i:s ");
echo "LISTDEVICES\n";
}
///
# STATUS Function
///
function status_info(&$irc, &$data)
{
global $config;
$statustype = $data->messageex[1];
mysql_connect($config['db_host'],$config['db_user'],$config['db_pass']);
mysql_select_db($config['db_name']);
if ($statustype == "dev")
{
$devcount = array_pop(dbFetchRow("SELECT count(*) FROM devices"));
$devup = array_pop(dbFetchRow("SELECT count(*) FROM devices WHERE status = '1' AND `ignore` = '0'"));
$devdown = array_pop(dbFetchRow("SELECT count(*) FROM devices WHERE status = '0' AND `ignore` = '0'"));
$devign = array_pop(dbFetchRow("SELECT count(*) FROM devices WHERE `ignore` = '1'"));
$devdis = array_pop(dbFetchRow("SELECT count(*) FROM devices WHERE `disabled` = '1'"));
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, "Devices: " .$devcount . " (" .$devup . " up, " .$devdown . " down, " .$devign . " ignored, " .$devdis . " disabled" . ")");
} else if ($statustype == "prt") {
$prtcount = array_pop(dbFetchRow("SELECT count(*) FROM ports"));
$prtup = array_pop(dbFetchRow("SELECT count(*) FROM ports AS I, devices AS D WHERE I.ifOperStatus = 'up' AND I.ignore = '0' AND I.device_id = D.device_id AND D.ignore = '0'"));
$prtdown = array_pop(dbFetchRow("SELECT count(*) FROM ports AS I, devices AS D WHERE I.ifOperStatus = 'down' AND I.ifAdminStatus = 'up' AND I.ignore = '0' AND D.device_id = I.device_id AND D.ignore = '0'"));
$prtsht = array_pop(dbFetchRow("SELECT count(*) FROM ports AS I, devices AS D WHERE I.ifAdminStatus = 'down' AND I.ignore = '0' AND D.device_id = I.device_id AND D.ignore = '0'"));
$prtign = array_pop(dbFetchRow("SELECT count(*) FROM ports AS I, devices AS D WHERE D.device_id = I.device_id AND (I.ignore = '1' OR D.ignore = '1')"));
$prterr = array_pop(dbFetchRow("SELECT count(*) FROM ports AS I, devices AS D WHERE D.device_id = I.device_id AND (I.ignore = '0' OR D.ignore = '0') AND (I.ifInErrors_delta > '0' OR I.ifOutErrors_delta > '0')"));
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, "Ports: " .$prtcount . " (" .$prtup . " up, " .$prtdown . " down, " .$prtign . " ignored, " .$prtsht . " shutdown" . ")");
} else if ($statustype == "srv") {
$srvcount = array_pop(dbFetchRow("SELECT count(service_id) FROM services"));
$srvup = array_pop(dbFetchRow("SELECT count(service_id) FROM services WHERE service_status = '1' AND service_ignore ='0'"));
$srvdown = array_pop(dbFetchRow("SELECT count(service_id) FROM services WHERE service_status = '0' AND service_ignore = '0'"));
$srvign = array_pop(dbFetchRow("SELECT count(service_id) FROM services WHERE service_ignore = '1'"));
$srvdis = array_pop(dbFetchRow("SELECT count(service_id) FROM services WHERE service_disabled = '1'"));
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, "Services: " .$srvcount . " (" .$srvup . " up, " .$srvdown . " down, " .$srvign . " ignored, " .$srvdis . " disabled" . ")");
} else {
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, "Error: STATUS requires one of the following <dev prt srv>"); }
mysql_close();
echo date("m-d-y H:i:s ");
echo "STATUS\t\t$statustype\n";
}
}
$bot = &new observiumbot();
$irc = &new Net_SmartIRC();
if (OBS_DEBUG) $irc->setDebug(SMARTIRC_DEBUG_ALL);
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!listdevices', $bot, 'list_devices');
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!listports', $bot, 'list_ports');
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!device', $bot, 'device_info');
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!port', $bot, 'port_info');
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!down', $bot, 'down_info');
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!version', $bot, 'version_info');
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!status', $bot, 'status_info');
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!log', $bot, 'log_info');
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!help', $bot, 'help_info');
if($config['irc_ssl'])
{
$irc->connect( 'ssl://' . $config['irc_host'] , $config['irc_port'] );
}
else
{
$irc->connect($config['irc_host'], $config['irc_port']);
}
$irc->login($config['irc_nick'], 'Observium Bot', 0, $config['irc_nick']);
if($config['irc_chankey'] != '' ) $irc->join($config['irc_chan'], $config['irc_chankey']);
else $irc->join($config['irc_chan']);
$irc->listen();
$irc->disconnect();
// EOF