2010-09-03 18:26:59 +00:00
|
|
|
#!/usr/bin/env php
|
2011-05-14 21:51:58 +00:00
|
|
|
|
|
|
|
# status <dev prt srv>
|
|
|
|
# reboot
|
|
|
|
# log
|
|
|
|
# help
|
|
|
|
# down
|
|
|
|
|
2010-06-13 18:27:38 +00:00
|
|
|
<?php
|
|
|
|
|
2011-05-14 21:51:58 +00:00
|
|
|
# Disable annoying messages... well... all messages actually :)
|
|
|
|
error_reporting(0);
|
|
|
|
|
|
|
|
include_once("includes/defaults.inc.php");
|
|
|
|
include_once("config.php");
|
|
|
|
include_once("includes/functions.php");
|
|
|
|
include_once("includes/discovery/functions.inc.php");
|
2010-06-13 18:27:38 +00:00
|
|
|
|
|
|
|
include_once('Net/SmartIRC.php');
|
|
|
|
|
2011-05-14 21:51:58 +00:00
|
|
|
mysql_close();
|
|
|
|
|
|
|
|
# Redirect to /dev/null if you aren't using screen to keep tabs
|
|
|
|
echo "Observer Bot Starting ...\n";
|
|
|
|
echo "\n";
|
|
|
|
echo "COMMAND\t\tHOST\t\t\tDEVICE\n";
|
|
|
|
echo "-------\t\t----\t\t\t------\n";
|
|
|
|
|
2010-07-17 12:58:09 +00:00
|
|
|
class observiumbot
|
2011-05-14 21:51:58 +00:00
|
|
|
|
2010-06-13 18:27:38 +00:00
|
|
|
{
|
2011-05-14 21:51:58 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
# Get status on !version
|
|
|
|
###
|
|
|
|
function version_info(&$irc, &$data)
|
|
|
|
{
|
|
|
|
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, "Observium Version " . $config['version']);
|
|
|
|
|
|
|
|
echo "VERSION\t\t". $config['version'] . "\n";
|
|
|
|
|
|
|
|
mysql_close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
###
|
|
|
|
# Get status on !down devices
|
|
|
|
###
|
|
|
|
function down_info(&$irc, &$data)
|
|
|
|
{
|
|
|
|
|
|
|
|
global $config;
|
|
|
|
mysql_connect($config['db_host'],$config['db_user'],$config['db_pass']);
|
|
|
|
mysql_select_db($config['db_name']);
|
|
|
|
|
|
|
|
$query = mysql_query("SELECT * FROM `devices` where status=0");
|
|
|
|
unset($message);
|
|
|
|
while($device = mysql_fetch_assoc($query))
|
|
|
|
{
|
|
|
|
$message .= $sep . $device['hostname'];
|
|
|
|
$sep = ", ";
|
|
|
|
}
|
|
|
|
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, $message);
|
|
|
|
unset($sep);
|
|
|
|
|
|
|
|
mysql_close();
|
|
|
|
|
|
|
|
echo "DOWN\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
###
|
|
|
|
# Get status on !device <hostname>
|
|
|
|
###
|
2011-03-23 09:14:33 +00:00
|
|
|
function device_info(&$irc, &$data)
|
|
|
|
{
|
2011-05-14 21:51:58 +00:00
|
|
|
|
2011-03-23 09:14:33 +00:00
|
|
|
$hostname = $data->messageex[1];
|
2010-06-13 18:27:38 +00:00
|
|
|
|
2011-05-14 21:51:58 +00:00
|
|
|
global $config;
|
|
|
|
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();
|
2010-06-13 18:27:38 +00:00
|
|
|
|
2011-03-23 09:14:33 +00:00
|
|
|
if ($device['status'] == 1) { $status = "Up " . formatUptime($device['uptime'] . " "); } else { $status = "Down "; }
|
|
|
|
if ($device['ignore']) { $status = "*Ignored*"; }
|
|
|
|
if ($device['disabled']) { $status = "*Disabled*"; }
|
2010-06-13 18:27:38 +00:00
|
|
|
|
2011-04-21 15:27:41 +00:00
|
|
|
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, '#'.$device['device_id'] . " " . $device['os'] . " " . $device['version'] . " " .
|
2011-03-23 09:14:33 +00:00
|
|
|
$device['features'] . " " . $status);
|
2011-05-14 21:51:58 +00:00
|
|
|
|
|
|
|
echo "DEVICE\t\t". $device['hostname']."\n";
|
|
|
|
|
2011-03-23 09:14:33 +00:00
|
|
|
}
|
2010-06-13 18:27:38 +00:00
|
|
|
|
2011-05-14 21:51:58 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
# Get status on !port <hostname port>
|
|
|
|
###
|
2011-03-23 09:14:33 +00:00
|
|
|
function port_info(&$irc, &$data)
|
|
|
|
{
|
|
|
|
$hostname = $data->messageex[1];
|
|
|
|
$ifname = $data->messageex[2];
|
2010-06-13 18:27:38 +00:00
|
|
|
|
2011-05-14 21:51:58 +00:00
|
|
|
global $config;
|
|
|
|
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($device));
|
|
|
|
$port = dbFetchRow("SELECT * FROM `ports` WHERE `ifName` = ? OR `ifDescr` = ? AND device_id = ?", array($ifname, $ifname, $device['device_id']));
|
|
|
|
|
|
|
|
mysql_close();
|
2010-06-13 18:27:38 +00:00
|
|
|
|
2011-03-23 09:14:33 +00:00
|
|
|
$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']);
|
2010-06-13 18:27:38 +00:00
|
|
|
|
2011-04-21 15:27:41 +00:00
|
|
|
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, '#' . $port['interface_id'] . " " . $port['ifAdminStatus'] . "/" . $port['ifOperStatus'] . " " .
|
2011-03-23 09:14:33 +00:00
|
|
|
$bps_in. " > bps > " . $bps_out . " | " . $pps_in. "pps > PPS > " . $pps_out ."pps");
|
2011-05-14 21:51:58 +00:00
|
|
|
|
|
|
|
echo "PORT\t\t" . $hostname . "\t". $ifname . "\n";
|
|
|
|
|
2011-03-23 09:14:33 +00:00
|
|
|
}
|
2010-06-13 18:27:38 +00:00
|
|
|
|
2011-05-14 21:51:58 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
# !listdevices lists all devices
|
|
|
|
###
|
2011-04-21 15:27:41 +00:00
|
|
|
function list_devices(&$irc, &$data)
|
|
|
|
{
|
2011-05-14 21:51:58 +00:00
|
|
|
unset ($message);
|
|
|
|
|
|
|
|
global $config;
|
|
|
|
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)
|
2011-04-21 15:27:41 +00:00
|
|
|
{
|
|
|
|
$message .= $sep . $device['hostname'];
|
|
|
|
$sep = ", ";
|
|
|
|
}
|
2011-05-14 21:51:58 +00:00
|
|
|
|
|
|
|
mysql_close();
|
|
|
|
|
2011-04-21 15:27:41 +00:00
|
|
|
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, $message);
|
|
|
|
unset($sep);
|
2011-05-14 21:51:58 +00:00
|
|
|
|
|
|
|
echo "LISTDEVICES\n";
|
|
|
|
|
2011-04-21 15:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-13 18:27:38 +00:00
|
|
|
|
2011-03-15 15:27:14 +00:00
|
|
|
$bot = &new observiumbot();
|
|
|
|
$irc = &new Net_SmartIRC();
|
2011-03-17 11:12:32 +00:00
|
|
|
$irc->setUseSockets(TRUE);
|
2010-06-13 18:27:38 +00:00
|
|
|
|
2011-04-21 15:27:41 +00:00
|
|
|
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!listdevices', $bot, 'list_devices');
|
2011-03-17 11:12:32 +00:00
|
|
|
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!device', $bot, 'device_info');
|
|
|
|
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!port', $bot, 'port_info');
|
2011-05-14 21:51:58 +00:00
|
|
|
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!down', $bot, 'down_info');
|
|
|
|
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '!version', $bot, 'version_info');
|
2010-06-13 18:27:38 +00:00
|
|
|
|
2011-04-21 15:27:41 +00:00
|
|
|
$irc->connect($config['irc_host'], $config['irc_port']);
|
|
|
|
$irc->login($config['irc_nick'], 'Observium Bot', 0, $config['irc_nick']);
|
|
|
|
$irc->join($config['irc_chan']);
|
2011-03-15 15:27:14 +00:00
|
|
|
$irc->listen();
|
|
|
|
$irc->disconnect();
|
2010-06-13 18:27:38 +00:00
|
|
|
|
2011-04-21 15:27:41 +00:00
|
|
|
?>
|