2010-09-03 18:26:59 +00:00
#!/usr/bin/env php
2010-06-13 18:27:38 +00:00
<? php
include ( "includes/defaults.inc.php" );
include ( "config.php" );
include ( "includes/functions.php" );
include ( "includes/discovery/functions.inc.php" );
include_once ( 'Net/SmartIRC.php' );
2010-07-17 12:58:09 +00:00
class observiumbot
2010-06-13 18:27:38 +00:00
{
2011-03-23 09:14:33 +00:00
function device_info ( & $irc , & $data )
{
$hostname = $data -> messageex [ 1 ];
2010-06-13 18:27:38 +00:00
2011-03-23 09:14:33 +00:00
$device = mysql_fetch_array ( mysql_query ( "SELECT * FROM `devices` WHERE `hostname` = '" . mres ( $hostname ) . "'" ));
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-03-23 09:14:33 +00:00
$irc -> message ( SMARTIRC_TYPE_CHANNEL , $data -> channel , 'id' . $device [ 'device_id' ] . " " . $device [ 'os' ] . " " . $device [ 'version' ] . " " .
$device [ 'features' ] . " " . $status );
}
2010-06-13 18:27:38 +00:00
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-03-23 09:14:33 +00:00
$device = mysql_fetch_array ( mysql_query ( "SELECT * FROM `devices` WHERE `hostname` = '" . mres ( $hostname ) . "'" ));
$port = mysql_fetch_Array ( mysql_query ( "SELECT * FROM `ports` WHERE `ifName` = '" . $ifname . "' OR `ifDescr` = '" . $ifname . "' AND device_id = '" . $device [ 'device_id' ] . "'" ));
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-03-23 09:14:33 +00:00
$irc -> message ( SMARTIRC_TYPE_CHANNEL , $data -> channel , 'id' . $port [ 'interface_id' ] . " " . $port [ 'ifAdminStatus' ] . "/" . $port [ 'ifOperStatus' ] . " " .
$bps_in . " > bps > " . $bps_out . " | " . $pps_in . "pps > PPS > " . $pps_out . "pps" );
}
2010-06-13 18:27:38 +00:00
}
$host = "chat.eu.freenode.net" ;
$port = 6667 ;
2010-08-09 18:42:27 +00:00
$nick = "Observium" ;
2010-07-17 12:58:09 +00:00
$chan = "#observium" ;
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-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' );
2010-06-13 18:27:38 +00:00
2011-03-23 09:13:16 +00:00
$irc -> connect ( $host , $port );
2011-03-17 11:12:32 +00:00
$irc -> login ( $nick , 'Observium Bot' , 0 , $nick );
2011-03-23 09:13:16 +00:00
$irc -> join ( array ( $chan ));
2011-03-15 15:27:14 +00:00
$irc -> listen ();
$irc -> disconnect ();
2010-06-13 18:27:38 +00:00
2011-03-23 09:14:33 +00:00
?>