2009-09-07 11:07:59 +00:00
< ? php
2007-04-03 14:10:23 +00:00
2008-04-13 16:27:05 +00:00
## Include from PEAR
include_once ( " Net/IPv4.php " );
include_once ( " Net/IPv6.php " );
## Observer Includes
2009-05-07 13:47:51 +00:00
include_once ( $config [ 'install_dir' ] . " /includes/common.php " );
2008-04-13 16:27:05 +00:00
include_once ( $config [ 'install_dir' ] . " /includes/generic.php " );
include_once ( $config [ 'install_dir' ] . " /includes/procurve.php " );
include_once ( $config [ 'install_dir' ] . " /includes/graphing.php " );
include_once ( $config [ 'install_dir' ] . " /includes/print-functions.php " );
2008-11-26 18:22:11 +00:00
include_once ( $config [ 'install_dir' ] . " /includes/billing.php " );
2008-04-13 16:27:05 +00:00
include_once ( $config [ 'install_dir' ] . " /includes/cisco-entities.php " );
include_once ( $config [ 'install_dir' ] . " /includes/syslog.php " );
2008-11-27 12:36:37 +00:00
include_once ( $config [ 'install_dir' ] . " /includes/rewrites.php " );
2008-03-23 15:40:58 +00:00
2009-04-29 15:07:28 +00:00
## CollectD
require ( 'collectd/config.php' );
require ( 'collectd/functions.php' );
require ( 'collectd/definitions.php' );
2010-02-07 22:39:02 +00:00
function device_by_id_cache ( $device_id )
{
2010-01-29 21:10:05 +00:00
global $device_cache ;
2010-02-07 22:39:02 +00:00
if ( is_array ( $device_cache [ $device_id ]))
{
2010-01-29 21:10:05 +00:00
$device = $device_cache [ $device_id ];
} else {
$device = mysql_fetch_array ( mysql_query ( " SELECT * FROM `devices` WHERE `device_id` = ' " . $device_id . " ' " ));
$device_cache [ $device_id ] = $device ;
}
return $device ;
}
2010-02-07 22:39:02 +00:00
function mac_clean_to_readable ( $mac )
{
$r = substr ( $mac , 0 , 2 );
$r .= " : " . substr ( $mac , 2 , 2 );
$r .= " : " . substr ( $mac , 4 , 2 );
$r .= " : " . substr ( $mac , 6 , 2 );
$r .= " : " . substr ( $mac , 8 , 2 );
$r .= " : " . substr ( $mac , 10 , 2 );
2009-05-03 22:32:01 +00:00
2010-02-07 22:39:02 +00:00
return ( $r );
2009-05-03 22:32:01 +00:00
}
function zeropad ( $num )
{
2010-02-07 22:39:02 +00:00
return ( strlen ( $num ) == 1 ) ? '0' . $num : $num ;
2009-05-03 22:32:01 +00:00
}
function zeropad_lineno ( $num , $length )
{
2010-02-07 22:39:02 +00:00
while ( strlen ( $num ) < $length )
{
$num = '0' . $num ;
}
return $num ;
2009-05-03 22:32:01 +00:00
}
2009-04-23 21:13:56 +00:00
function only_alphanumeric ( $string )
{
2010-02-07 22:39:02 +00:00
return preg_replace ( '/[^a-zA-Z0-9]/' , '' , $string );
2009-04-23 21:13:56 +00:00
}
2008-11-27 12:36:37 +00:00
2010-02-07 22:39:02 +00:00
function validate_hostip ( $host )
{
// FIXME
2008-07-18 12:07:51 +00:00
}
2010-02-07 22:39:02 +00:00
function write_dev_attrib ( $device_id , $attrib_type , $attrib_value )
{
2008-03-31 13:08:17 +00:00
$count_sql = " SELECT COUNT(*) FROM devices_attribs WHERE `device_id` = ' " . $device_id . " ' AND `attrib_type` = ' $attrib_type ' " ;
2010-02-07 22:39:02 +00:00
if ( mysql_result ( mysql_query ( $count_sql ), 0 ))
{
2008-03-31 13:08:17 +00:00
$update_sql = " UPDATE devices_attribs SET attrib_value = ' $attrib_value ' WHERE `device_id` = ' $device_id ' AND `attrib_type` = ' $attrib_type ' " ;
mysql_query ( $update_sql );
2010-02-07 22:39:02 +00:00
}
else
{
2008-03-31 13:08:17 +00:00
$insert_sql = " INSERT INTO devices_attribs (`device_id`, `attrib_type`, `attrib_value`) VALUES (' $device_id ', ' $attrib_type ', ' $attrib_value ') " ;
mysql_query ( $insert_sql );
}
2010-02-07 22:39:02 +00:00
2008-03-31 13:08:17 +00:00
return mysql_affected_rows ();
}
2010-02-07 22:39:02 +00:00
function shorthost ( $hostname , $len = 16 )
{
2010-01-16 23:16:58 +00:00
$parts = explode ( " . " , $hostname );
$shorthost = $parts [ 0 ];
$i = 1 ;
while ( $i < count ( $parts ) && strlen ( $shorthost . '.' . $parts [ $i ]) < $len )
{
$shorthost = $shorthost . '.' . $parts [ $i ];
$i ++ ;
2008-03-23 15:40:58 +00:00
}
return ( $shorthost );
}
2010-02-07 22:39:02 +00:00
function rrdtool_update ( $rrdfile , $rrdupdate )
{
global $config , $debug ;
if ( $debug ) { echo ( $config [ 'rrdtool' ] . " update $rrdfile $rrdupdate \n " ); }
2008-04-10 14:52:51 +00:00
return shell_exec ( $config [ 'rrdtool' ] . " update $rrdfile $rrdupdate " );
2008-03-09 21:13:27 +00:00
}
2007-06-06 09:23:41 +00:00
2010-02-07 22:39:02 +00:00
function rrdtool ( $command , $file , $options )
{
2009-11-07 02:30:38 +00:00
global $config ;
2010-02-07 22:39:02 +00:00
if ( $config [ 'debug' ]) { echo ( $config [ 'rrdtool' ] . " $command $file $options \n " ); }
2009-11-07 02:30:38 +00:00
return shell_exec ( $config [ 'rrdtool' ] . " $command $file $options " );
}
2010-02-07 22:39:02 +00:00
function device_array ( $device_id )
{
2009-11-21 15:07:09 +00:00
$sql = " SELECT * FROM `devices` WHERE `device_id` = ' " . $device_id . " ' " ;
$query = mysql_query ( $sql );
$device = mysql_fetch_array ( $query );
return $device ;
}
2008-03-12 11:15:58 +00:00
2010-02-07 22:39:02 +00:00
function getHostOS ( $device )
{
global $config ;
$sysDescr_cmd = $config [ 'snmpget' ] . " -m SNMPv2-MIB -O qv - " . $device [ 'snmpver' ] . " -c " . $device [ 'community' ] . " " . $device [ 'hostname' ] . " : " . $device [ 'port' ] . " sysDescr.0 " ;
$sysDescr = str_replace ( " \" " , " " , trim ( shell_exec ( $sysDescr_cmd )));
$dir_handle = @ opendir ( $config [ 'install_dir' ] . " /includes/osdiscovery " ) or die ( " Unable to open $path " );
while ( $file = readdir ( $dir_handle ))
{
if ( preg_match ( " /^discover-([a-z0-9 \ -]*).php/ " , $file ) )
{
include ( $config [ 'install_dir' ] . " /includes/osdiscovery/ " . $file );
2008-03-12 11:15:58 +00:00
}
2010-02-07 22:39:02 +00:00
}
closedir ( $dir_handle );
2010-02-17 18:54:34 +00:00
if ( $os ) { return $os ; } else { return " generic " ; }
2008-03-12 11:15:58 +00:00
}
2009-03-17 20:29:54 +00:00
function billpermitted ( $bill_id )
{
2008-03-09 17:39:14 +00:00
global $_SESSION ;
2010-02-07 22:39:02 +00:00
if ( $_SESSION [ 'userlevel' ] >= " 5 " )
{
2008-03-09 17:39:14 +00:00
$allowed = TRUE ;
2010-02-07 22:39:02 +00:00
}
elseif ( @ mysql_result ( mysql_query ( " SELECT count(*) FROM bill_perms WHERE `user_id` = ' " . $_SESSION [ 'user_id' ] . " ' AND `bill_id` = $bill_id " ), 0 ) > '0' )
{
2008-03-09 17:39:14 +00:00
$allowed = TRUE ;
2010-02-07 22:39:02 +00:00
}
else
{
2008-03-09 17:39:14 +00:00
$allowed = FALSE ;
}
2007-11-23 11:37:28 +00:00
2010-02-07 22:39:02 +00:00
return $allowed ;
2007-11-23 11:37:28 +00:00
}
2008-11-05 16:09:47 +00:00
function interfacepermitted ( $interface_id )
{
2008-03-09 17:39:14 +00:00
global $_SESSION ;
2010-02-07 22:39:02 +00:00
if ( $_SESSION [ 'userlevel' ] >= " 5 " ) {
2008-04-03 19:04:24 +00:00
$allowed = TRUE ;
2010-02-20 17:22:22 +00:00
} elseif ( devicepermitted ( mysql_result ( mysql_query ( " SELECT `device_id` FROM `ports` WHERE `interface_id` = ' $interface_id ' " ), 0 ))) {
2008-04-03 19:04:24 +00:00
$allowed = TRUE ;
2010-02-20 17:22:22 +00:00
} elseif ( @ mysql_result ( mysql_query ( " SELECT `interface_id` FROM `ports_perms` WHERE `user_id` = ' " . $_SESSION [ 'user_id' ] . " ' AND `interface_id` = $interface_id " ), 0 )) {
2008-04-03 19:04:24 +00:00
$allowed = TRUE ;
2009-03-17 20:29:54 +00:00
} else {
$allowed = FALSE ;
}
2008-03-09 17:39:14 +00:00
return $allowed ;
}
2008-11-05 16:09:47 +00:00
function devicepermitted ( $device_id )
{
2007-06-24 14:56:47 +00:00
global $_SESSION ;
2010-02-07 22:39:02 +00:00
if ( $_SESSION [ 'userlevel' ] >= " 5 " ) {
2009-03-17 20:29:54 +00:00
$allowed = true ;
2007-06-24 14:56:47 +00:00
} elseif ( @ mysql_result ( mysql_query ( " SELECT * FROM devices_perms WHERE `user_id` = ' " . $_SESSION [ 'user_id' ] . " ' AND `device_id` = $device_id " ), 0 ) > '0' ) {
$allowed = true ;
2009-03-17 20:29:54 +00:00
} else {
$allowed = false ;
}
2007-06-24 14:56:47 +00:00
return $allowed ;
}
2009-11-07 02:30:38 +00:00
function formatRates ( $rate ) {
2008-03-26 12:25:25 +00:00
$rate = format_si ( $rate ) . " bps " ;
return $rate ;
}
2009-04-23 21:13:56 +00:00
function formatstorage ( $rate , $round = '2' )
2008-11-05 16:09:47 +00:00
{
2009-04-23 21:13:56 +00:00
$rate = format_bi ( $rate , $round ) . " B " ;
2008-03-26 12:25:25 +00:00
return $rate ;
}
2008-11-05 16:09:47 +00:00
function format_si ( $rate )
{
2008-03-26 12:25:25 +00:00
$sizes = Array ( '' , 'K' , 'M' , 'G' , 'T' , 'P' , 'E' );
2007-06-24 14:56:47 +00:00
$round = Array ( '0' , '0' , '0' , '2' , '2' , '2' , '2' , '2' , '2' );
$ext = $sizes [ 0 ];
for ( $i = 1 ; (( $i < count ( $sizes )) && ( $rate >= 1000 )); $i ++ ) { $rate = $rate / 1000 ; $ext = $sizes [ $i ]; }
return round ( $rate , $round [ $i ]) . $ext ;
}
2007-04-03 14:10:23 +00:00
2009-04-23 21:13:56 +00:00
function format_bi ( $size , $round = '2' )
2008-11-27 12:36:37 +00:00
{
2009-04-23 21:13:56 +00:00
$sizes = Array ( '' , 'K' , 'M' , 'G' , 'T' , 'P' , 'E' );
2007-05-20 19:21:35 +00:00
$ext = $sizes [ 0 ];
2007-06-24 14:56:47 +00:00
for ( $i = 1 ; (( $i < count ( $sizes )) && ( $size >= 1024 )); $i ++ ) { $size = $size / 1024 ; $ext = $sizes [ $i ]; }
2009-04-23 21:13:56 +00:00
return round ( $size , $round ) . $ext ;
2007-05-20 19:21:35 +00:00
}
2008-11-27 12:36:37 +00:00
function arguments ( $argv )
{
2007-11-23 11:37:28 +00:00
$_ARG = array ();
foreach ( $argv as $arg ) {
if ( ereg ( '--([^=]+)=(.*)' , $arg , $reg )) {
$_ARG [ $reg [ 1 ]] = $reg [ 2 ];
2010-02-07 22:39:02 +00:00
} elseif ( ereg ( '-([a-zA-Z0-9])' , $arg , $reg )) {
2007-11-23 11:37:28 +00:00
$_ARG [ $reg [ 1 ]] = 'true' ;
}
}
return $_ARG ;
}
2007-04-03 14:10:23 +00:00
2007-06-24 14:56:47 +00:00
function percent_colour ( $perc )
{
2007-11-23 11:37:28 +00:00
$r = min ( 255 , 5 * ( $perc - 25 ));
$b = max ( 0 , 255 - ( 5 * ( $perc + 25 )));
return sprintf ( '#%02x%02x%02x' , $r , $b , $b );
2007-06-24 14:56:47 +00:00
}
2008-11-27 12:36:37 +00:00
function print_error ( $text )
{
2010-02-16 00:52:40 +00:00
echo ( '<table class="errorbox" cellpadding="3"><tr><td><img src="/images/15/exclamation.png" align="absmiddle">' . $text . '</td></tr></table>' );
2007-04-03 14:10:23 +00:00
}
2008-11-27 12:36:37 +00:00
function print_message ( $text )
{
2010-02-16 00:52:40 +00:00
echo ( '<table class="messagebox" cellpadding="3"><tr><td><img src="/images/16/tick.png" align="absmiddle">' . $text . '</td></tr></table>' );
2007-06-24 14:56:47 +00:00
}
2009-04-23 21:13:56 +00:00
function interface_rates ( $rrd_file ) // Returns the last in/out value in RRD
2008-11-27 12:36:37 +00:00
{
2009-03-16 15:19:44 +00:00
global $config ;
2009-04-23 21:13:56 +00:00
$cmd = $config [ 'rrdtool' ] . " fetch -s -600s -e now $rrd_file AVERAGE | grep : | cut -d \" \" -f 2,3 | grep e " ;
2007-11-23 11:37:28 +00:00
$data = trim ( `$cmd` );
2007-06-24 14:56:47 +00:00
foreach ( explode ( " \n " , $data ) as $entry ) {
list ( $in , $out ) = split ( " " , $entry );
$rate [ 'in' ] = $in * 8 ;
$rate [ 'out' ] = $out * 8 ;
}
return $rate ;
2007-04-03 14:10:23 +00:00
}
2009-10-13 20:07:31 +00:00
function interface_errors ( $rrd_file , $period = '-1d' ) // Returns the last in/out errors value in RRD
2008-11-27 12:36:37 +00:00
{
2009-03-16 15:19:44 +00:00
global $config ;
2009-10-13 20:07:31 +00:00
$cmd = $config [ 'rrdtool' ] . " fetch -s $period -e -300s $rrd_file AVERAGE | grep : | cut -d \" \" -f 4,5 " ;
2009-04-23 21:13:56 +00:00
$data = trim ( shell_exec ( $cmd ));
2007-06-24 14:56:47 +00:00
foreach ( explode ( " \n " , $data ) as $entry ) {
list ( $in , $out ) = explode ( " " , $entry );
$in_errors += ( $in * 300 );
$out_errors += ( $out * 300 );
2007-04-03 14:10:23 +00:00
}
2007-06-24 14:56:47 +00:00
$errors [ 'in' ] = round ( $in_errors );
$errors [ 'out' ] = round ( $out_errors );
return $errors ;
2007-04-03 14:10:23 +00:00
}
2009-04-28 10:28:58 +00:00
function interface_packets ( $rrd_file ) // Returns the last in/out pps value in RRD
{
global $config ;
$cmd = $config [ 'rrdtool' ] . " fetch -s -1d -e -300s $rrd_file AVERAGE | grep : | cut -d \" \" -f 6,7 " ;
$data = trim ( shell_exec ( $cmd ));
foreach ( explode ( " \n " , $data ) as $entry ) {
list ( $in , $out ) = explode ( " " , $entry );
}
$packets [ 'in' ] = round ( $in );
$packets [ 'out' ] = round ( $out );
return $packets ;
}
2007-06-24 14:56:47 +00:00
2008-11-27 12:36:37 +00:00
function geteventicon ( $message )
{
2010-02-07 22:39:02 +00:00
if ( $message == " Device status changed to Down " ) { $icon = " server_connect.png " ; }
if ( $message == " Device status changed to Up " ) { $icon = " server_go.png " ; }
if ( $message == " Interface went down " || $message == " Interface changed state to Down " ) { $icon = " if-disconnect.png " ; }
if ( $message == " Interface went up " || $message == " Interface changed state to Up " ) { $icon = " if-connect.png " ; }
if ( $message == " Interface disabled " ) { $icon = " if-disable.png " ; }
if ( $message == " Interface enabled " ) { $icon = " if-enable.png " ; }
if ( $icon ) { return $icon ; } else { return false ; }
2007-04-03 14:10:23 +00:00
}
2010-02-08 18:56:26 +00:00
function generateiflink ( $interface , $text = 0 , $type = NULL )
2008-11-27 12:36:37 +00:00
{
2009-03-26 11:43:31 +00:00
global $twoday ; global $now ; global $config ; global $day ; global $month ;
2009-09-18 09:11:52 +00:00
$interface = ifNameDescr ( $interface );
2010-02-07 22:39:02 +00:00
if ( ! $text ) { $text = fixIfName ( $interface [ 'label' ]); }
2010-02-08 18:56:26 +00:00
if ( isset ( $type )) { $interface [ 'graph_type' ] = $type ; } else { $interface [ 'graph_type' ] = 'port_bits' ; }
2010-02-09 14:11:10 +00:00
if ( ! isset ( $interface [ 'hostname' ])) { $interface = array_merge ( $interface , device_by_id_cache ( $interface [ 'device_id' ])); }
2007-04-07 21:15:23 +00:00
$class = ifclass ( $interface [ 'ifOperStatus' ], $interface [ 'ifAdminStatus' ]);
2010-01-10 18:19:15 +00:00
$graph_url = $config [ 'base_url' ] . " /graph.php?port= " . $interface [ 'interface_id' ] . " &from= $day &to= $now &width=400&height=100&type= " . $interface [ 'graph_type' ];
$graph_url_month = $config [ 'base_url' ] . " /graph.php?port= " . $interface [ 'interface_id' ] . " &from= $month &to= $now &width=400&height=100&type= " . $interface [ 'graph_type' ];
2009-03-02 18:12:09 +00:00
$device_id = getifhost ( $interface [ 'interface_id' ]);
2010-02-08 18:56:26 +00:00
$url = $config [ 'base_url' ] . " /device/ $device_id /interface/ " . $interface [ 'interface_id' ] . " / " ;
2008-12-09 13:55:41 +00:00
2010-02-08 18:56:26 +00:00
$contents = " <div class=list-large> " . $interface [ 'hostname' ] . " - " . fixifName ( $interface [ 'label' ]) . " </div> " ;
if ( $interface [ 'ifAlias' ]) { $contents .= htmlentities ( $interface [ 'ifAlias' ] . " <br /> " ); }
$contents .= " <img src= \ ' $graph_url\ '><br /><img src= \ ' $graph_url_month\ '> " ;
$link = overlib_link ( $url , $text , $contents , $class );
2007-04-03 14:10:23 +00:00
return $link ;
}
2010-02-08 18:56:26 +00:00
function overlib_link ( $url , $text , $contents , $class ) {
global $config ;
$output = " <a class=' " . $class . " ' href=' " . $url . " ' " ;
$output .= " onmouseover= \" return overlib(' " . $contents . " ' " . $config [ 'overlib_defaults' ] . " ); \" onmouseout= \" return nd(); \" > " ;
$output .= $text . " </a> " ;
return $output ;
}
2008-11-27 12:36:37 +00:00
function generatedevicelink ( $device , $text = 0 , $start = 0 , $end = 0 )
{
2007-11-23 11:37:28 +00:00
global $twoday ; global $day ; global $now ; global $config ;
2010-02-07 22:39:02 +00:00
if ( ! $start ) { $start = $day ; }
if ( ! $end ) { $end = $now ; }
2007-04-03 14:10:23 +00:00
$class = devclass ( $device );
2010-02-07 22:39:02 +00:00
if ( ! $text ) { $text = $device [ 'hostname' ]; }
2010-01-10 18:19:15 +00:00
$graph_url = $config [ 'base_url' ] . " /graph.php?device= " . $device [ 'device_id' ] . " &from= $start &to= $end &width=400&height=120&type=device_cpu " ;
$graph_url_b = $config [ 'base_url' ] . " /graph.php?device= " . $device [ 'device_id' ] . " &from= $start &to= $end &width=400&height=120&type=device_memory " ;
2010-02-08 18:56:26 +00:00
$url = $config [ 'base_url' ] . " /device/ " . $device [ 'device_id' ] . " / " ;
$contents = " <div class=list-large> " . $device [ 'hostname' ] . " - CPU & Memory Usage</div> " ;
if ( $device [ 'location' ]) { $contents .= " " . htmlentities ( $device [ 'location' ] . " <br /> " ); }
$contents .= " <img src= \ ' $graph_url\ '><br /><img src= \ ' $graph_url_b\ ' " ;
$text = htmlentities ( $text );
$link = overlib_link ( $url , $text , $contents , $class );
2007-04-03 14:10:23 +00:00
return $link ;
}
2007-04-04 14:25:17 +00:00
2008-11-27 12:36:37 +00:00
function device_traffic_image ( $device , $width , $height , $from , $to )
{
2010-01-10 18:19:15 +00:00
return " <img src='graph.php?device= " . $device . " &type=device_bits&from= " . $from . " &to= " . $to . " &width= " . $width . " &height= " . $height . " &legend=no' /> " ;
2007-04-04 14:25:17 +00:00
}
2009-03-23 17:32:25 +00:00
function devclass ( $device )
{
2010-01-16 23:16:58 +00:00
if ( isset ( $device [ 'status' ]) && $device [ 'status' ] == '0' ) { $class = " list-device-down " ; } else { $class = " list-device " ; }
2010-02-16 00:52:40 +00:00
if ( isset ( $device [ 'ignore' ]) && $device [ 'ignore' ] == '1' ) {
2007-04-03 14:10:23 +00:00
$class = " list-device-ignored " ;
2010-01-16 23:16:58 +00:00
if ( isset ( $device [ 'status' ]) && $device [ 'status' ] == '1' ) { $class = " list-device-ignored-up " ; }
2007-04-03 14:10:23 +00:00
}
return $class ;
}
2008-11-27 12:36:37 +00:00
function getImage ( $host )
{
2010-01-13 15:54:03 +00:00
global $config ;
$sql = " SELECT * FROM `devices` WHERE `device_id` = ' $host ' " ;
$data = mysql_fetch_array ( mysql_query ( $sql ));
$type = strtolower ( $data [ 'os' ]);
2010-02-07 22:39:02 +00:00
if ( file_exists ( $config [ 'html_dir' ] . " /images/os/ $type " . " .png " )){ $image = '<img src="' . $config [ 'base_url' ] . '/images/os/' . $type . '.png" />' ;
} elseif ( file_exists ( $config [ 'html_dir' ] . " /images/os/ $type " . " .gif " )){ $image = '<img src="' . $config [ 'base_url' ] . '/images/os/' . $type . '.gif" />' ; }
if ( $type == " linux " ) {
2010-01-08 23:47:54 +00:00
$features = strtolower ( trim ( $data [ 'features' ]));
2007-04-03 14:10:23 +00:00
list ( $distro ) = split ( " " , $features );
2010-02-07 22:39:02 +00:00
if ( file_exists ( $config [ 'html_dir' ] . " /images/os/ $distro " . " .png " )){ $image = '<img src="' . $config [ 'base_url' ] . '/images/os/' . $distro . '.png" />' ;
} elseif ( file_exists ( $config [ 'html_dir' ] . " /images/os/ $distro " . " .gif " )){ $image = '<img src="' . $config [ 'base_url' ] . '/images/os/' . $distro . '.gif" />' ; }
2007-04-03 14:10:23 +00:00
}
return $image ;
}
2009-11-07 02:30:38 +00:00
function renamehost ( $id , $new ) {
2008-11-19 12:12:54 +00:00
global $config ;
$host = mysql_result ( mysql_query ( " SELECT hostname FROM devices WHERE device_id = ' $id ' " ), 0 );
shell_exec ( " mv " . $config [ 'rrd_dir' ] . " / $host " . $config [ 'rrd_dir' ] . " / $new " );
mysql_query ( " UPDATE devices SET hostname = ' $new ' WHERE device_id = ' $id ' " );
2010-01-07 16:50:52 +00:00
eventlog ( " Hostname changed -> $new (console) " , $id );
2008-11-19 12:12:54 +00:00
}
2008-11-27 12:36:37 +00:00
function delHost ( $id )
{
2008-03-23 00:10:15 +00:00
global $config ;
2007-04-08 14:34:19 +00:00
$host = mysql_result ( mysql_query ( " SELECT hostname FROM devices WHERE device_id = ' $id ' " ), 0 );
mysql_query ( " DELETE FROM `devices` WHERE `device_id` = ' $id ' " );
2010-02-20 17:22:22 +00:00
$int_query = mysql_query ( " SELECT * FROM `ports` WHERE `device_id` = ' $id ' " );
2007-04-03 14:10:23 +00:00
while ( $int_data = mysql_fetch_array ( $int_query )) {
2007-04-09 10:54:20 +00:00
$int_if = $int_data [ 'ifDescr' ];
$int_id = $int_data [ 'interface_id' ];
2007-04-03 14:10:23 +00:00
mysql_query ( " DELETE from `adjacencies` WHERE `interface_id` = ' $int_id ' " );
2010-02-15 23:56:30 +00:00
mysql_query ( " DELETE from `links` WHERE `local_interface_id` = ' $int_id ' " );
mysql_query ( " DELETE from `links` WHERE `remote_interface_id` = ' $int_id ' " );
2007-04-03 14:10:23 +00:00
mysql_query ( " DELETE from `ipaddr` WHERE `interface_id` = ' $int_id ' " );
2009-03-25 15:44:16 +00:00
mysql_query ( " DELETE from `ip6adjacencies` WHERE `interface_id` = ' $int_id ' " );
mysql_query ( " DELETE from `ip6addr` WHERE `interface_id` = ' $int_id ' " );
mysql_query ( " DELETE from `mac_accounting` WHERE `interface_id` = ' $int_id ' " );
mysql_query ( " DELETE FROM `bill_ports` WHERE `port_id` = ' $int_id ' " );
mysql_query ( " DELETE from `pseudowires` WHERE `interface_id` = ' $int_id ' " );
2007-04-03 14:10:23 +00:00
echo ( " Removed interface $int_id ( $int_if )<br /> " );
}
2009-03-25 15:44:16 +00:00
mysql_query ( " DELETE FROM `entPhysical` WHERE `device_id` = ' $id ' " );
2007-04-09 10:54:20 +00:00
mysql_query ( " DELETE FROM `devices_attribs` WHERE `device_id` = ' $id ' " );
2009-03-25 15:44:16 +00:00
mysql_query ( " DELETE FROM `devices_perms` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `bgpPeers` WHERE `device_id` = ' $id ' " );
2010-02-13 07:40:43 +00:00
mysql_query ( " DELETE FROM `temperature` WHERE `device_id` = ' $id ' " );
2009-03-25 15:44:16 +00:00
mysql_query ( " DELETE FROM `vlans` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `vrfs` WHERE `device_id` = ' $id ' " );
2007-04-03 14:10:23 +00:00
mysql_query ( " DELETE FROM `storage` WHERE `host_id` = ' $id ' " );
mysql_query ( " DELETE FROM `alerts` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `eventlog` WHERE `host` = ' $id ' " );
2009-03-25 15:44:16 +00:00
mysql_query ( " DELETE FROM `syslog` WHERE `device_id` = ' $id ' " );
2010-02-20 17:22:22 +00:00
mysql_query ( " DELETE FROM `ports` WHERE `device_id` = ' $id ' " );
2010-02-21 19:32:08 +00:00
mysql_query ( " DELETE FROM `services` WHERE `device_id` = ' $id ' " );
2007-04-09 10:54:20 +00:00
mysql_query ( " DELETE FROM `alerts` WHERE `device_id` = ' $id ' " );
2009-03-16 15:19:44 +00:00
shell_exec ( " rm -rf " . $config [ 'rrd_dir' ] . " / $host " );
2007-04-03 14:10:23 +00:00
echo ( " Removed device $host <br /> " );
}
2009-08-07 16:10:52 +00:00
function retireHost ( $id )
{
global $config ;
$host = mysql_result ( mysql_query ( " SELECT hostname FROM devices WHERE device_id = ' $id ' " ), 0 );
mysql_query ( " DELETE FROM `devices` WHERE `device_id` = ' $id ' " );
2010-02-20 17:22:22 +00:00
$int_query = mysql_query ( " SELECT * FROM `ports` WHERE `device_id` = ' $id ' " );
2009-08-07 16:10:52 +00:00
while ( $int_data = mysql_fetch_array ( $int_query )) {
$int_if = $int_data [ 'ifDescr' ];
$int_id = $int_data [ 'interface_id' ];
mysql_query ( " DELETE from `adjacencies` WHERE `interface_id` = ' $int_id ' " );
2010-02-15 23:56:30 +00:00
mysql_query ( " DELETE from `links` WHERE `local_interface_id` = ' $int_id ' " );
mysql_query ( " DELETE from `links` WHERE `remote_interface_id` = ' $int_id ' " );
2009-08-07 16:10:52 +00:00
mysql_query ( " DELETE from `ipaddr` WHERE `interface_id` = ' $int_id ' " );
mysql_query ( " DELETE from `ip6adjacencies` WHERE `interface_id` = ' $int_id ' " );
mysql_query ( " DELETE from `ip6addr` WHERE `interface_id` = ' $int_id ' " );
mysql_query ( " DELETE from `pseudowires` WHERE `interface_id` = ' $int_id ' " );
echo ( " Removed interface $int_id ( $int_if )<br /> " );
}
mysql_query ( " DELETE FROM `entPhysical` WHERE `device_id` = ' $id ' " );
2010-02-13 07:40:43 +00:00
mysql_query ( " DELETE FROM `temperature` WHERE `device_id` = ' $id ' " );
2009-08-07 16:10:52 +00:00
mysql_query ( " DELETE FROM `storage` WHERE `host_id` = ' $id ' " );
mysql_query ( " DELETE FROM `alerts` WHERE `device_id` = ' $id ' " );
2010-02-21 19:32:08 +00:00
mysql_query ( " DELETE FROM `services` WHERE `device_id` = ' $id ' " );
2009-08-07 16:10:52 +00:00
shell_exec ( " rm -rf " . $config [ 'rrd_dir' ] . " / $host " );
echo ( " Removed device $host <br /> " );
}
2007-04-03 14:10:23 +00:00
2009-02-02 16:00:11 +00:00
function addHost ( $host , $community , $snmpver , $port = 161 )
2008-11-27 12:36:37 +00:00
{
2008-04-13 16:27:05 +00:00
global $config ;
2007-04-03 14:10:23 +00:00
list ( $hostshort ) = explode ( " . " , $host );
if ( isDomainResolves ( $host )){
if ( isPingable ( $host )) {
if ( mysql_result ( mysql_query ( " SELECT COUNT(*) FROM `devices` WHERE `hostname` = ' $host ' " ), 0 ) == '0' ) {
2009-04-24 15:04:45 +00:00
$snmphost = shell_exec ( $config [ 'snmpget' ] . " -m SNMPv2-MIB -Oqv - $snmpver -c $community $host : $port sysName.0 " );
2007-04-03 14:10:23 +00:00
if ( $snmphost == $host || $hostshort = $host ) {
2009-02-02 16:00:11 +00:00
createHost ( $host , $community , $snmpver , $port );
2007-04-03 14:10:23 +00:00
} else { echo ( " Given hostname does not match SNMP-read hostname! \n " ); }
} else { echo ( " Already got host $host\n " ); }
} else { echo ( " Could not ping $host\n " ); }
} else { echo ( " Could not resolve $host\n " ); }
}
function overlibprint ( $text ) {
return " onmouseover= \" return overlib(' " . $text . " '); \" onmouseout= \" return nd(); \" " ;
}
2008-11-27 12:36:37 +00:00
function scanUDP ( $host , $port , $timeout )
{
2007-04-03 14:10:23 +00:00
$handle = fsockopen ( $host , $port , & $errno , & $errstr , 2 );
if ( ! $handle ) {
}
socket_set_timeout ( $handle , $timeout );
$write = fwrite ( $handle , " \x00 " );
2009-09-25 12:18:25 +00:00
if ( ! $write ) { next ; }
2007-04-03 14:10:23 +00:00
$startTime = time ();
$header = fread ( $handle , 1 );
$endTime = time ();
$timeDiff = $endTime - $startTime ;
if ( $timeDiff >= $timeout ) {
2009-09-25 12:18:25 +00:00
fclose ( $handle ); return 1 ;
} else { fclose ( $handle ); return 0 ; }
2007-04-03 14:10:23 +00:00
}
2008-11-27 12:36:37 +00:00
function humanmedia ( $media )
{
2010-01-13 15:54:03 +00:00
array_preg_replace ( $rewrite_iftype , $media );
return $media ;
2007-04-03 14:10:23 +00:00
}
2009-03-23 17:32:25 +00:00
function humanspeed ( $speed )
{
2010-01-13 15:54:03 +00:00
$speed = formatRates ( $speed );
2010-02-07 22:39:02 +00:00
if ( $speed == " " ) { $speed = " - " ; }
2010-01-13 15:54:03 +00:00
return $speed ;
2007-04-03 14:10:23 +00:00
}
2008-11-27 12:36:37 +00:00
function netmask2cidr ( $netmask )
{
2010-01-13 15:54:03 +00:00
list ( $network , $cidr ) = explode ( " / " , trim ( `ipcalc $address/$mask | grep Network | cut -d" " -f 4` ));
return $cidr ;
2007-04-03 14:10:23 +00:00
}
2008-11-27 12:36:37 +00:00
function cidr2netmask ()
{
2010-01-13 15:54:03 +00:00
return ( long2ip ( ip2long ( " 255.255.255.255 " ) << ( 32 - $netmask )));
2007-04-03 14:10:23 +00:00
}
2008-11-27 12:36:37 +00:00
function formatUptime ( $diff , $format = " long " )
{
2008-03-23 15:40:58 +00:00
$yearsDiff = floor ( $diff / 31536000 );
$diff -= $yearsDiff * 31536000 ;
2007-04-03 14:10:23 +00:00
$daysDiff = floor ( $diff / 86400 );
$diff -= $daysDiff * 86400 ;
$hrsDiff = floor ( $diff / 60 / 60 );
$diff -= $hrsDiff * 60 * 60 ;
$minsDiff = floor ( $diff / 60 );
$diff -= $minsDiff * 60 ;
$secsDiff = $diff ;
2010-01-08 23:47:54 +00:00
$uptime = " " ;
2010-02-07 22:39:02 +00:00
if ( $format == " short " ) {
if ( $yearsDiff > '0' ){ $uptime .= $yearsDiff . " y " ; }
if ( $daysDiff > '0' ){ $uptime .= $daysDiff . " d " ; }
if ( $hrsDiff > '0' ){ $uptime .= $hrsDiff . " h " ; }
if ( $minsDiff > '0' ){ $uptime .= $minsDiff . " m " ; }
if ( $secsDiff > '0' ){ $uptime .= $secsDiff . " s " ; }
2008-03-23 15:40:58 +00:00
} else {
2010-02-07 22:39:02 +00:00
if ( $yearsDiff > '0' ){ $uptime .= $yearsDiff . " years, " ; }
if ( $daysDiff > '0' ){ $uptime .= $daysDiff . " day " . ( $daysDiff != 1 ? 's' : '' ) . " , " ; }
if ( $hrsDiff > '0' ){ $uptime .= $hrsDiff . " h " ; }
if ( $minsDiff > '0' ){ $uptime .= $minsDiff . " m " ; }
if ( $secsDiff > '0' ){ $uptime .= $secsDiff . " s " ; }
2008-03-23 15:40:58 +00:00
}
2010-01-07 11:43:55 +00:00
return trim ( $uptime );
2007-04-03 14:10:23 +00:00
}
2009-02-02 16:00:11 +00:00
function isSNMPable ( $hostname , $community , $snmpver , $port )
2008-11-27 12:36:37 +00:00
{
2008-04-13 16:27:05 +00:00
global $config ;
2009-04-24 15:04:45 +00:00
$pos = shell_exec ( $config [ 'snmpget' ] . " -m SNMPv2-MIB - $snmpver -c $community -t 1 $hostname : $port sysDescr.0 " );
2010-02-07 22:39:02 +00:00
if ( $pos == '' ) {
2010-01-13 15:54:03 +00:00
return false ;
2007-04-03 14:10:23 +00:00
} else {
2010-01-13 15:54:03 +00:00
return true ;
2007-04-03 14:10:23 +00:00
}
}
2009-11-07 02:30:38 +00:00
function isPingable ( $hostname ) {
2008-04-10 14:52:51 +00:00
global $config ;
$status = shell_exec ( $config [ 'fping' ] . " $hostname " );
2010-02-07 22:39:02 +00:00
if ( strstr ( $status , " alive " )) {
2007-04-03 14:10:23 +00:00
return TRUE ;
} else {
return FALSE ;
}
}
2009-11-07 02:30:38 +00:00
function is_odd ( $number ) {
return $number & 1 ; // 0 = even, 1 = odd
2007-04-03 14:10:23 +00:00
}
2009-11-07 02:30:38 +00:00
function isValidInterface ( $if ) {
2008-03-22 13:45:50 +00:00
global $config ;
2007-04-03 14:10:23 +00:00
$if = strtolower ( $if );
$nullintf = 0 ;
2008-03-22 13:45:50 +00:00
foreach ( $config [ 'bad_if' ] as $bi ) {
2007-04-03 14:10:23 +00:00
$pos = strpos ( $if , $bi );
if ( $pos !== FALSE ) {
$nullintf = 1 ;
echo ( " $if matched $bi \n " );
}
}
if ( preg_match ( '/serial[0-9]:/' , $if )) { $nullintf = '1' ; }
if ( $nullintf != '1' ) {
return 1 ;
} else { return 0 ; }
}
2008-11-27 12:36:37 +00:00
function ifclass ( $ifOperStatus , $ifAdminStatus )
{
2010-01-13 15:54:03 +00:00
$ifclass = " interface-upup " ;
if ( $ifAdminStatus == " down " ) { $ifclass = " interface-admindown " ; }
if ( $ifAdminStatus == " up " && $ifOperStatus == " down " ) { $ifclass = " interface-updown " ; }
if ( $ifAdminStatus == " up " && $ifOperStatus == " up " ) { $ifclass = " interface-upup " ; }
return $ifclass ;
2007-04-03 14:10:23 +00:00
}
2010-01-13 15:54:03 +00:00
function utime ()
{
$time = explode ( " " , microtime ());
$usec = ( double ) $time [ 0 ];
$sec = ( double ) $time [ 1 ];
return $sec + $usec ;
2007-04-03 14:10:23 +00:00
}
2008-11-27 12:36:37 +00:00
function fixIOSFeatures ( $features )
{
2008-09-25 13:54:58 +00:00
$features = preg_replace ( " /^PK9S $ / " , " IP w/SSH LAN Only " , $features );
2008-03-09 17:39:14 +00:00
$features = str_replace ( " LANBASEK9 " , " Lan Base Crypto " , $features );
$features = str_replace ( " LANBASE " , " Lan Base " , $features );
2008-09-25 13:54:58 +00:00
$features = str_replace ( " ADVENTERPRISEK9 " , " Advanced Enterprise Crypto " , $features );
2007-04-03 14:10:23 +00:00
$features = str_replace ( " ADVSECURITYK9 " , " Advanced Security Crypto " , $features );
$features = str_replace ( " K91P " , " Provider Crypto " , $features );
$features = str_replace ( " K4P " , " Provider Crypto " , $features );
$features = str_replace ( " ADVIPSERVICESK9 " , " Adv IP Services Crypto " , $features );
$features = str_replace ( " ADVIPSERVICES " , " Adv IP Services " , $features );
$features = str_replace ( " IK9P " , " IP Plus Crypto " , $features );
2008-11-04 15:01:10 +00:00
$features = str_replace ( " K9O3SY7 " , " IP ADSL FW IDS Plus IPSEC 3DES " , $features );
2007-04-03 14:10:23 +00:00
$features = str_replace ( " SPSERVICESK9 " , " SP Services Crypto " , $features );
2008-09-25 13:54:58 +00:00
$features = preg_replace ( " /^PK9SV $ / " , " IP MPLS/IPV6 W/SSH + BGP " , $features );
2007-04-03 14:10:23 +00:00
$features = str_replace ( " IS " , " IP Plus " , $features );
$features = str_replace ( " IPSERVICESK9 " , " IP Services Crypto " , $features );
$features = str_replace ( " BROADBAND " , " Broadband " , $features );
$features = str_replace ( " IPBASE " , " IP Base " , $features );
$features = str_replace ( " IPSERVICE " , " IP Services " , $features );
$features = preg_replace ( " /^P $ / " , " Service Provider " , $features );
2008-09-25 13:54:58 +00:00
$features = preg_replace ( " /^P11 $ / " , " Broadband Router " , $features );
$features = preg_replace ( " /^G4P5 $ / " , " NRP " , $features );
2007-11-23 11:37:28 +00:00
$features = str_replace ( " JK9S " , " Enterprise Plus Crypto " , $features );
2007-04-03 14:10:23 +00:00
$features = str_replace ( " IK9S " , " IP Plus Crypto " , $features );
2008-10-29 22:49:35 +00:00
$features = preg_replace ( " /^JK $ / " , " Enterprise Plus " , $features );
2007-04-03 14:10:23 +00:00
$features = str_replace ( " I6Q4L2 " , " Layer 2 " , $features );
$features = str_replace ( " I6K2L2Q4 " , " Layer 2 Crypto " , $features );
$features = str_replace ( " C3H2S " , " Layer 2 SI/EI " , $features );
2007-11-23 11:37:28 +00:00
$features = str_replace ( " _WAN " , " + WAN " , $features );
2007-04-03 14:10:23 +00:00
return $features ;
}
2010-02-07 22:39:02 +00:00
function fixIOSHardware ( $hardware )
{
2007-04-03 14:10:23 +00:00
$hardware = preg_replace ( " /C([0-9]+)/ " , " Cisco \\ 1 " , $hardware );
2007-11-23 11:37:28 +00:00
$hardware = preg_replace ( " /CISCO([0-9]+)/ " , " Cisco \\ 1 " , $hardware );
$hardware = str_replace ( " cat4000 " , " Cisco Catalyst 4000 " , $hardware );
2007-04-03 14:10:23 +00:00
$hardware = str_replace ( " s3223_rp " , " Cisco Catalyst 6500 SUP32 " , $hardware );
$hardware = str_replace ( " s222_rp " , " Cisco Catalyst 6500 SUP2 " , $hardware );
$hardware = str_replace ( " c6sup2_rp " , " Cisco Catalyst 6500 SUP2 " , $hardware );
$hardware = str_replace ( " s72033_rp " , " Cisco Catalyst 6500 SUP720 " , $hardware );
$hardware = str_replace ( " RSP " , " Cisco 7500 " , $hardware );
$hardware = str_replace ( " C3200XL " , " Cisco Catalyst 3200XL " , $hardware );
$hardware = str_replace ( " C3550 " , " Cisco Catalyst 3550 " , $hardware );
$hardware = str_replace ( " C2950 " , " Cisco Catalyst 2950 " , $hardware );
2007-11-23 11:37:28 +00:00
$hardware = str_replace ( " C7301 " , " Cisco 7301 " , $hardware );
2008-03-09 17:39:14 +00:00
$hardware = str_replace ( " CE500 " , " Catalyst Express 500 " , $hardware );
2007-04-03 14:10:23 +00:00
return $hardware ;
}
2010-02-07 22:39:02 +00:00
function createHost ( $host , $community , $snmpver , $port = 161 )
{
$host = trim ( strtolower ( $host ));
$device = array ( 'hostname' => $host , 'community' => $community , 'snmpver' => $snmpver , 'port' => $port );
$host_os = getHostOS ( $device );
if ( $host_os )
{
$sql = mysql_query ( " INSERT INTO `devices` (`hostname`, `sysName`, `community`, `port`, `os`, `status`,`snmpver`) VALUES (' $host ', ' $host ', ' $community ', ' $port ', ' $host_os ', '1',' $snmpver ') " );
if ( mysql_affected_rows ())
{
$device_id = mysql_result ( mysql_query ( " SELECT device_id FROM devices WHERE hostname = ' $host ' " ), 0 );
mysql_query ( " INSERT INTO devices_attribs (attrib_type, attrib_value, device_id) VALUES ('discover','1',' $device_id ') " );
return ( " Created host : $host (id: $device_id ) (os: $host_os ) " );
}
else
{
return FALSE ;
}
}
else
{
return FALSE ;
}
2007-04-03 14:10:23 +00:00
}
2010-01-13 15:54:03 +00:00
function isDomainResolves ( $domain )
{
return gethostbyname ( $domain ) != $domain ;
2007-04-03 14:10:23 +00:00
}
2010-01-13 15:54:03 +00:00
function hoststatus ( $id )
{
$sql = mysql_query ( " SELECT `status` FROM `devices` WHERE `device_id` = ' $id ' " );
$result = @ mysql_result ( $sql , 0 );
2010-02-07 22:39:02 +00:00
2010-01-13 15:54:03 +00:00
return $result ;
2007-04-03 14:10:23 +00:00
}
2010-02-07 22:39:02 +00:00
function match_network ( $nets , $ip , $first = false )
{
2007-04-03 14:10:23 +00:00
$return = false ;
if ( ! is_array ( $nets )) $nets = array ( $nets );
foreach ( $nets as $net ) {
$rev = ( preg_match ( " /^ \ !/ " , $net )) ? true : false ;
$net = preg_replace ( " /^ \ !/ " , " " , $net );
$ip_arr = explode ( '/' , $net );
$net_long = ip2long ( $ip_arr [ 0 ]);
$x = ip2long ( $ip_arr [ 1 ]);
$mask = long2ip ( $x ) == $ip_arr [ 1 ] ? $x : 0xffffffff << ( 32 - $ip_arr [ 1 ]);
$ip_long = ip2long ( $ip );
if ( $rev ) {
if (( $ip_long & $mask ) == ( $net_long & $mask )) return false ;
} else {
if (( $ip_long & $mask ) == ( $net_long & $mask )) $return = true ;
if ( $first && $return ) return true ;
}
}
2010-02-07 22:39:02 +00:00
2007-04-03 14:10:23 +00:00
return $return ;
}
2010-01-01 14:09:57 +00:00
function snmp2ipv6 ( $ipv6_snmp )
{
$ipv6 = explode ( '.' , $ipv6_snmp );
for ( $i = 0 ; $i <= 15 ; $i ++ ) { $ipv6 [ $i ] = zeropad ( dechex ( $ipv6 [ $i ])); }
for ( $i = 0 ; $i <= 15 ; $i += 2 ) { $ipv6_2 [] = $ipv6 [ $i ] . $ipv6 [ $i + 1 ]; }
2010-02-07 22:39:02 +00:00
2010-01-01 14:09:57 +00:00
return implode ( ':' , $ipv6_2 );
}
function ipv62snmp ( $ipv6 )
{
$ipv6_ex = explode ( ':' , Net_IPv6 :: uncompress ( $ipv6 ));
2010-01-03 09:45:19 +00:00
for ( $i = 0 ; $i < 8 ; $i ++ ) { $ipv6_ex [ $i ] = zeropad_lineno ( $ipv6_ex [ $i ], 4 ); }
2010-01-01 14:09:57 +00:00
$ipv6_ip = implode ( '' , $ipv6_ex );
2010-01-07 23:26:05 +00:00
for ( $i = 0 ; $i < 32 ; $i += 2 ) $ipv6_split [] = hexdec ( substr ( $ipv6_ip , $i , 2 ));
2010-02-07 22:39:02 +00:00
2010-01-02 17:12:04 +00:00
return implode ( '.' , $ipv6_split );
2010-01-01 14:09:57 +00:00
}
2010-01-01 14:14:09 +00:00
function discover_process_ipv6 ( $ifIndex , $ipv6_address , $ipv6_prefixlen , $ipv6_origin )
{
global $valid_v6 , $device , $config ;
$ipv6_network = trim ( shell_exec ( $config [ 'sipcalc' ] . " $ipv6_address / $ipv6_prefixlen | grep Subnet | cut -f 2 -d '-' " ));
$ipv6_compressed = trim ( shell_exec ( $config [ 'sipcalc' ] . " $ipv6_address / $ipv6_prefixlen | grep Compressed | cut -f 2 -d '-' " ));
2010-01-03 20:13:10 +00:00
$ipv6_type = trim ( shell_exec ( $config [ 'sipcalc' ] . " $ipv6_address / $ipv6_prefixlen | grep \" Address type \" | cut -f 2- -d '-' " ));
if ( $ipv6_type == " Link-Local Unicast Addresses " ) return ; # ignore link-locals (coming from IPV6-MIB)
2010-01-01 14:14:09 +00:00
2010-02-20 17:22:22 +00:00
if ( mysql_result ( mysql_query ( " SELECT count(*) FROM `ports`
2010-01-01 14:14:09 +00:00
WHERE device_id = '".$device[' device_id ']."' AND `ifIndex` = '$ifIndex' " ), 0) != '0' && $ipv6_prefixlen > '0' && $ipv6_prefixlen < '129' && $ipv6_compressed != '::1') {
2010-02-20 17:22:22 +00:00
$i_query = " SELECT interface_id FROM `ports` WHERE device_id = ' " . $device [ 'device_id' ] . " ' AND `ifIndex` = ' $ifIndex ' " ;
2010-01-01 14:14:09 +00:00
$interface_id = mysql_result ( mysql_query ( $i_query ), 0 );
if ( mysql_result ( mysql_query ( " SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = ' $ipv6_network ' " ), 0 ) < '1' ) {
mysql_query ( " INSERT INTO `ipv6_networks` (`ipv6_network`) VALUES (' $ipv6_network ') " );
echo ( " N " );
}
if ( mysql_result ( mysql_query ( " SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = ' $ipv6_network ' " ), 0 ) < '1' ) {
mysql_query ( " INSERT INTO `ipv6_networks` (`ipv6_network`) VALUES (' $ipv6_network ') " );
echo ( " N " );
}
$ipv6_network_id = @ mysql_result ( mysql_query ( " SELECT `ipv6_network_id` from `ipv6_networks` WHERE `ipv6_network` = ' $ipv6_network ' " ), 0 );
if ( mysql_result ( mysql_query ( " SELECT COUNT(*) FROM `ipv6_addresses` WHERE `ipv6_address` = ' $ipv6_address ' AND `ipv6_prefixlen` = ' $ipv6_prefixlen ' AND `interface_id` = ' $interface_id ' " ), 0 ) == '0' ) {
mysql_query ( " INSERT INTO `ipv6_addresses` (`ipv6_address`, `ipv6_compressed`, `ipv6_prefixlen`, `ipv6_origin`, `ipv6_network_id`, `interface_id`)
VALUES ( '$ipv6_address' , '$ipv6_compressed' , '$ipv6_prefixlen' , '$ipv6_origin' , '$ipv6_network_id' , '$interface_id' ) " );
echo ( " + " );
} else { echo ( " . " ); }
$full_address = " $ipv6_address / $ipv6_prefixlen " ;
$valid = $full_address . " - " . $interface_id ;
$valid_v6 [ $valid ] = 1 ;
}
}
2010-01-03 20:13:10 +00:00
function get_astext ( $asn )
{
2010-01-10 14:48:00 +00:00
global $config , $cache ;
2010-01-09 17:06:34 +00:00
if ( isset ( $config [ 'astext' ][ $asn ]))
{
return $config [ 'astext' ][ $asn ];
}
else
{
2010-01-10 14:48:00 +00:00
if ( isset ( $cache [ 'astext' ][ $asn ]))
{
return $cache [ 'astext' ][ $asn ];
}
else
{
$result = dns_get_record ( " AS $asn .asn.cymru.com " , DNS_TXT );
$txt = explode ( '|' , $result [ 0 ][ 'txt' ]);
2010-02-16 00:52:40 +00:00
$result = trim ( str_replace ( '"' , '' , $txt [ 4 ]));
$cache [ 'astext' ][ $asn ] = $result ;
return $result ;
2010-01-10 14:48:00 +00:00
}
2010-01-09 17:06:34 +00:00
}
2010-01-03 20:13:10 +00:00
}
2010-01-07 16:50:52 +00:00
function eventlog ( $eventtext , $device_id = " " , $interface_id = " " )
{
$event_query = " INSERT INTO eventlog (host, interface, datetime, message) VALUES ( " . ( $device_id ? $device_id : " NULL " );
$event_query .= " , " . ( $interface_id ? $interface_id : " NULL " ) . " , NOW(), ' " . mysql_escape_string ( $eventtext ) . " ') " ;
mysql_query ( $event_query );
}
2010-01-13 15:54:03 +00:00
2010-02-21 14:57:36 +00:00
function log_event ( $text , $device = NULL , $type = NULL , $reference = NULL )
{
global $debug ;
2010-02-17 11:02:18 +00:00
$event_query = " INSERT INTO eventlog (host, reference, type, datetime, message) VALUES ( " . ( $device [ 'device_id' ] ? $device [ 'device_id' ] : " NULL " );
2010-02-21 15:18:13 +00:00
$event_query .= " , ' " . ( $reference ? $reference : " NULL " ) . " ', ' " . ( $type ? $type : " NULL " ) . " ', NOW(), ' " . mres ( $text ) . " ') " ;
2010-02-21 14:56:20 +00:00
if ( $debug ) { echo ( $event_query . " \n " ); }
2010-02-17 11:02:18 +00:00
mysql_query ( $event_query );
}
2010-01-13 15:54:03 +00:00
function notify ( $device , $title , $message )
{
global $config ;
if ( $device [ 'sysContact' ]) { $email = $device [ 'sysContact' ]; } else { $email = $config [ 'email_default' ]; }
mail ( $email , $title , $message , $config [ 'email_headers' ]);
}
2010-02-21 17:02:20 +00:00
function formatCiscoHardware ( & $device , $short = false )
{
if ( $device [ 'os' ] == " ios " ) {
if ( $device [ 'hardware' ]) {
if ( preg_match ( " /^WS-C([A-Za-z0-9]+).*/ " , $device [ 'hardware' ], $matches )) {
if ( ! $short ) {
$device [ 'hardware' ] = " Cisco " . $matches [ 1 ] . " ( " . $device [ 'hardware' ] . " ) " ;
} else {
$device [ 'hardware' ] = " Cisco " . $matches [ 1 ];
}
2010-02-21 21:05:58 +00:00
} elseif ( preg_match ( " /^CISCO([0-9]+) $ / " , $device [ 'hardware' ], $matches )) {
$device [ 'hardware' ] = " Cisco " . $matches [ 1 ];
2010-02-21 17:02:20 +00:00
}
} else {
if ( preg_match ( " /Cisco IOS Software, C([A-Za-z0-9]+) Software.*/ " , $device [ 'sysDescr' ], $matches )) {
$device [ 'hardware' ] = " Cisco " . $matches [ 1 ];
}
}
}
}
2007-04-03 14:10:23 +00:00
?>