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 " );
2010-07-17 20:30:05 +00:00
## Observium Includes
2008-04-13 16:27:05 +00:00
2009-05-07 13:47:51 +00:00
include_once ( $config [ 'install_dir' ] . " /includes/common.php " );
2010-02-24 13:46:12 +00:00
include_once ( $config [ 'install_dir' ] . " /includes/rrdtool.inc.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 " );
2010-02-28 22:04:15 +00:00
include_once ( $config [ 'install_dir' ] . " /includes/snmp.inc.php " );
2010-06-28 12:49:26 +00:00
include_once ( $config [ 'install_dir' ] . " /includes/services.inc.php " );
2008-03-23 15:40:58 +00:00
2010-02-07 22:39:02 +00:00
function mac_clean_to_readable ( $mac )
{
2011-03-23 09:54:56 +00:00
$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
2011-03-23 09:54:56 +00:00
return ( $r );
2009-05-03 22:32:01 +00:00
}
2010-11-24 11:32:53 +00:00
function only_alphanumeric ( $string )
2009-04-23 21:13:56 +00:00
{
2011-03-23 09:54:56 +00:00
return preg_replace ( '/[^a-zA-Z0-9]/' , '' , $string );
2009-04-23 21:13:56 +00:00
}
2010-11-24 11:32:53 +00:00
function logfile ( $string )
{
2011-03-23 09:54:56 +00:00
global $config ;
2010-11-24 11:32:53 +00:00
2011-03-23 09:54:56 +00:00
$fd = fopen ( $config [ 'log_file' ], 'a' );
fputs ( $fd , $string . " \n " );
fclose ( $fd );
2010-11-24 11:32:53 +00:00
}
2010-02-07 22:39:02 +00:00
function shorthost ( $hostname , $len = 16 )
{
2011-03-23 09:54:56 +00:00
$parts = explode ( " . " , $hostname );
$shorthost = $parts [ 0 ];
2011-03-26 19:12:24 +00:00
$i = 1 ;
2011-03-23 09:54:56 +00:00
while ( $i < count ( $parts ) && strlen ( $shorthost . '.' . $parts [ $i ]) < $len )
{
$shorthost = $shorthost . '.' . $parts [ $i ];
$i ++ ;
}
return ( $shorthost );
2008-03-23 15:40:58 +00:00
}
2010-02-07 22:39:02 +00:00
function device_array ( $device_id )
{
2011-03-23 09:54:56 +00:00
$sql = " SELECT * FROM `devices` WHERE `device_id` = ' " . $device_id . " ' " ;
$query = mysql_query ( $sql );
2011-04-06 13:54:50 +00:00
$device = mysql_fetch_assoc ( $query );
2011-03-23 09:54:56 +00:00
return $device ;
2009-11-21 15:07:09 +00:00
}
2008-03-12 11:15:58 +00:00
2010-02-07 22:39:02 +00:00
function getHostOS ( $device )
{
2011-03-23 09:54:56 +00:00
global $config ;
2011-03-08 17:12:43 +00:00
2011-03-23 09:54:56 +00:00
$sysDescr = snmp_get ( $device , " SNMPv2-MIB::sysDescr.0 " , " -Ovq " );
$sysObjectId = snmp_get ( $device , " SNMPv2-MIB::sysObjectID.0 " , " -Ovqn " );
2010-08-11 00:40:07 +00:00
2011-03-23 09:54:56 +00:00
echo ( " | $sysDescr | $sysObjectId | " );
2010-07-11 20:12:24 +00:00
2011-03-23 09:54:56 +00:00
$dir_handle = @ opendir ( $config [ 'install_dir' ] . " /includes/discovery/os " ) or die ( " Unable to open $path " );
while ( $file = readdir ( $dir_handle ))
{
2011-03-27 10:21:19 +00:00
if ( preg_match ( " /.php $ / " , $file ))
2010-02-07 22:39:02 +00:00
{
2011-03-23 09:54:56 +00:00
include ( $config [ 'install_dir' ] . " /includes/discovery/os/ " . $file );
2008-03-12 11:15:58 +00:00
}
2011-03-23 09:54:56 +00:00
}
closedir ( $dir_handle );
2011-03-08 17:12:43 +00:00
2011-03-23 09:54:56 +00:00
if ( $os ) { return $os ; } else { return " generic " ; }
2008-03-12 11:15:58 +00:00
}
2007-06-24 14:56:47 +00:00
function percent_colour ( $perc )
{
2011-03-27 10:21:19 +00:00
$r = min ( 255 , 5 * ( $perc - 25 ));
$b = max ( 0 , 255 - ( 5 * ( $perc + 25 )));
return sprintf ( '#%02x%02x%02x' , $r , $b , $b );
2011-03-08 17:12:43 +00:00
}
2007-06-24 14:56:47 +00:00
2011-03-16 14:31:21 +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
{
2011-03-23 09:54:56 +00:00
global $config ;
2011-03-27 10:21:19 +00:00
2011-03-23 09:54:56 +00:00
$cmd = $config [ 'rrdtool' ] . " fetch -s $period -e -300s $rrd_file AVERAGE | grep : | cut -d \" \" -f 4,5 " ;
$data = trim ( shell_exec ( $cmd ));
foreach ( explode ( " \n " , $data ) as $entry )
{
list ( $in , $out ) = explode ( " " , $entry );
$in_errors += ( $in * 300 );
$out_errors += ( $out * 300 );
}
$errors [ 'in' ] = round ( $in_errors );
$errors [ 'out' ] = round ( $out_errors );
2011-03-27 10:21:19 +00:00
2011-03-23 09:54:56 +00:00
return $errors ;
2007-04-03 14:10:23 +00:00
}
2011-03-08 17:12:43 +00:00
function getImage ( $host )
2008-11-27 12:36:37 +00:00
{
2011-03-23 09:54:56 +00:00
global $config ;
$sql = " SELECT * FROM `devices` WHERE `device_id` = ' $host ' " ;
2011-04-06 13:54:50 +00:00
$data = mysql_fetch_assoc ( mysql_query ( $sql ));
2011-03-23 09:54:56 +00:00
$type = strtolower ( $data [ 'os' ]);
if ( $config [ 'os' ][ $type ][ 'icon' ] && file_exists ( $config [ 'html_dir' ] . " /images/os/ " . $config [ 'os' ][ $type ][ 'icon' ] . " .png " ))
{
$image = '<img src="' . $config [ 'base_url' ] . '/images/os/' . $config [ 'os' ][ $type ][ 'icon' ] . '.png" />' ;
} elseif ( $config [ 'os' ][ $type ][ 'icon' ] && file_exists ( $config [ 'html_dir' ] . " /images/os/ " . $config [ 'os' ][ $type ][ 'icon' ] . " .gif " ))
{
$image = '<img src="' . $config [ 'base_url' ] . '/images/os/' . $config [ 'os' ][ $type ][ 'icon' ] . '.gif" />' ;
} else {
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 " )
{
$features = strtolower ( trim ( $data [ 'features' ]));
list ( $distro ) = split ( " " , $features );
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" />' ; }
2010-07-17 14:40:02 +00:00
}
2011-03-23 09:54:56 +00:00
}
2011-03-27 10:21:19 +00:00
2011-03-23 09:54:56 +00:00
return $image ;
2007-04-03 14:10:23 +00:00
}
2011-03-27 10:21:19 +00:00
function renamehost ( $id , $new , $source = 'console' )
2011-03-11 18:03:49 +00:00
{
2011-03-23 09:54:56 +00:00
global $config ;
2011-03-27 10:21:19 +00:00
2011-03-31 17:19:54 +00:00
$host = mysql_result ( mysql_query ( " SELECT hostname FROM devices WHERE device_id = ' $id ' " ), 0 );
2011-03-23 09:54:56 +00:00
rename ( $config [ 'rrd_dir' ] . " / $host " , $config [ 'rrd_dir' ] . " / $new " );
mysql_query ( " UPDATE devices SET hostname = ' $new ' WHERE device_id = ' $id ' " );
2011-03-30 16:28:24 +00:00
log_event ( " Hostname changed -> $new ( $source ) " , $id , 'system' );
2008-11-19 12:12:54 +00:00
}
2011-03-08 17:12:43 +00:00
function delete_port ( $int_id )
2008-11-27 12:36:37 +00:00
{
2011-03-25 15:37:53 +00:00
global $config ;
$interface = mysql_fetch_assoc ( mysql_query ( " SELECT * FROM `ports` AS P, `devices` AS D WHERE P.interface_id = ' " . $int_id . " ' AND D.device_id = P.device_id " ));
mysql_query ( " DELETE from `adjacencies` WHERE `interface_id` = ' $int_id ' " );
mysql_query ( " DELETE from `links` WHERE `local_interface_id` = ' $int_id ' " );
mysql_query ( " DELETE from `links` WHERE `remote_interface_id` = ' $int_id ' " );
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 `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 ' " );
mysql_query ( " DELETE FROM `ports` WHERE `interface_id` = ' $int_id ' " );
unlink ( trim ( $config [ 'rrd_dir' ]) . " / " . trim ( $interface [ 'hostname' ]) . " / " . $interface [ 'ifIndex' ] . " .rrd " );
2010-04-24 23:16:12 +00:00
}
2011-03-08 17:12:43 +00:00
function delete_device ( $id )
2010-04-24 23:16:12 +00:00
{
2011-03-23 09:54:56 +00:00
global $config ;
2011-03-26 19:12:24 +00:00
2011-03-23 09:54:56 +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 ' " );
$int_query = mysql_query ( " SELECT * FROM `ports` WHERE `device_id` = ' $id ' " );
2011-04-06 13:54:50 +00:00
while ( $int_data = mysql_fetch_assoc ( $int_query ))
2011-03-23 09:54:56 +00:00
{
$int_if = $int_data [ 'ifDescr' ];
$int_id = $int_data [ 'interface_id' ];
delete_port ( $int_id );
$ret .= " Removed interface $int_id ( $int_if ) \n " ;
}
2011-03-26 19:12:24 +00:00
2011-03-23 09:54:56 +00:00
mysql_query ( " DELETE FROM `entPhysical` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `devices_attribs` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `devices_perms` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `bgpPeers` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `vlans` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `vrfs` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `storage` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `alerts` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `eventlog` WHERE `host` = ' $id ' " );
mysql_query ( " DELETE FROM `syslog` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `ports` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `services` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `alerts` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `toner` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `frequency` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `current` WHERE `device_id` = ' $id ' " );
mysql_query ( " DELETE FROM `sensors` WHERE `device_id` = ' $id ' " );
shell_exec ( " rm -rf " . trim ( $config [ 'rrd_dir' ]) . " / $host " );
2011-03-26 19:12:24 +00:00
2011-03-23 09:54:56 +00:00
$ret = " Removed Device $host\n " ;
return $ret ;
2007-04-03 14:10:23 +00:00
}
2011-03-16 14:31:21 +00:00
function addHost ( $host , $community , $snmpver , $port = 161 , $transport = 'udp' )
2008-11-27 12:36:37 +00:00
{
2011-03-23 09:54:56 +00:00
global $config ;
2011-03-26 19:12:24 +00:00
2011-03-25 15:37:53 +00:00
list ( $hostshort ) = explode ( " . " , $host );
2011-03-23 09:54:56 +00:00
if ( isDomainResolves ( $host ))
{
if ( isPingable ( $host ))
{
2011-03-27 10:21:19 +00:00
if ( mysql_result ( mysql_query ( " SELECT COUNT(*) FROM `devices` WHERE `hostname` = ' $host ' " ), 0 ) == '0' )
2011-03-23 09:54:56 +00:00
{
# FIXME internalize -- but we don't have $device yet!
# FIXME this needs to be addhost.php's content instead, kindof, also use this function there then.
$snmphost = shell_exec ( $config [ 'snmpget' ] . " -m SNMPv2-MIB -Oqv - $snmpver -c $community $host : $port sysName.0 " );
if ( $snmphost == $host || $hostshort = $host )
2011-03-08 17:12:43 +00:00
{
2011-03-23 09:54:56 +00:00
createHost ( $host , $community , $snmpver , $port , $transport );
} 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 " ); }
2007-04-03 14:10:23 +00:00
}
2011-03-16 14:31:21 +00:00
function scanUDP ( $host , $port , $timeout )
2011-03-08 17:12:43 +00:00
{
2011-03-23 09:54:56 +00:00
$handle = fsockopen ( $host , $port , $errno , $errstr , 2 );
socket_set_timeout ( $handle , $timeout );
$write = fwrite ( $handle , " \x00 " );
if ( ! $write ) { next ; }
$startTime = time ();
$header = fread ( $handle , 1 );
$endTime = time ();
$timeDiff = $endTime - $startTime ;
if ( $timeDiff >= $timeout )
{
fclose ( $handle ); return 1 ;
} else { fclose ( $handle ); return 0 ; }
2007-04-03 14:10:23 +00:00
}
2011-03-15 16:12:44 +00:00
function deviceArray ( $host , $community , $snmpver , $port = 161 , $transport = 'udp' )
{
2011-03-23 09:54:56 +00:00
$device = array ();
$device [ 'hostname' ] = $host ;
$device [ 'port' ] = $port ;
$device [ 'community' ] = $community ;
$device [ 'snmpver' ] = $snmpver ;
$device [ 'transport' ] = $transport ;
2011-03-15 16:12:44 +00:00
2011-03-23 09:54:56 +00:00
return $device ;
2011-03-15 16:12:44 +00:00
}
2011-03-08 17:12:43 +00:00
function netmask2cidr ( $netmask )
2008-11-27 12:36:37 +00:00
{
2011-03-23 09:54:56 +00:00
$addr = Net_IPv4 :: parseAddress ( " 1.2.3.4/ $netmask " );
return $addr -> bitmask ;
2007-04-03 14:10:23 +00:00
}
2011-03-08 17:12:43 +00:00
function cidr2netmask ()
2008-11-27 12:36:37 +00:00
{
2011-03-23 09:54:56 +00:00
return ( long2ip ( ip2long ( " 255.255.255.255 " ) << ( 32 - $netmask )));
2007-04-03 14:10:23 +00:00
}
2011-03-08 17:12:43 +00:00
function formatUptime ( $diff , $format = " long " )
2008-11-27 12:36:37 +00:00
{
2011-03-23 09:54:56 +00:00
$yearsDiff = floor ( $diff / 31536000 );
$diff -= $yearsDiff * 31536000 ;
$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 ;
$uptime = " " ;
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 " ; }
}
else
{
if ( $yearsDiff > '0' ) { $uptime .= $yearsDiff . " years, " ; }
2011-03-27 10:21:19 +00:00
if ( $daysDiff > '0' ) { $uptime .= $daysDiff . " day " . ( $daysDiff != 1 ? 's' : '' ) . " , " ; }
2011-03-23 09:54:56 +00:00
if ( $hrsDiff > '0' ) { $uptime .= $hrsDiff . " h " ; }
if ( $minsDiff > '0' ) { $uptime .= $minsDiff . " m " ; }
if ( $secsDiff > '0' ) { $uptime .= $secsDiff . " s " ; }
}
return trim ( $uptime );
2007-04-03 14:10:23 +00:00
}
2011-03-15 16:12:44 +00:00
function isSNMPable ( $device )
2008-11-27 12:36:37 +00:00
{
2011-03-23 09:54:56 +00:00
global $config ;
2011-03-15 16:12:44 +00:00
2011-03-23 09:54:56 +00:00
$pos = snmp_get ( $device , " sysObjectID.0 " , " -Oqv " , " SNMPv2-MIB " );
if ( $pos === '' || $pos === false )
{
return false ;
} else {
return true ;
}
2007-04-03 14:10:23 +00:00
}
2011-03-11 18:03:49 +00:00
function isPingable ( $hostname )
{
2011-03-23 09:54:56 +00:00
global $config ;
2011-03-27 10:21:19 +00:00
$status = shell_exec ( $config [ 'fping' ] . " $hostname 2>/dev/null " );
2011-03-23 09:54:56 +00:00
if ( strstr ( $status , " alive " ))
{
return TRUE ;
} else {
2011-03-27 10:21:19 +00:00
$status = shell_exec ( $config [ 'fping6' ] . " $hostname 2>/dev/null " );
2011-03-23 09:54:56 +00:00
if ( strstr ( $status , " alive " ))
{
return TRUE ;
} else {
return FALSE ;
}
}
2007-04-03 14:10:23 +00:00
}
2011-03-11 18:03:49 +00:00
function is_odd ( $number )
{
2011-03-23 09:54:56 +00:00
return $number & 1 ; // 0 = even, 1 = odd
2007-04-03 14:10:23 +00:00
}
2011-03-11 18:03:49 +00:00
function isValidInterface ( $if )
{
2011-03-23 09:54:56 +00:00
global $config ;
$if = strtolower ( $if );
$nullintf = 0 ;
foreach ( $config [ 'bad_if' ] as $bi )
{
2011-03-11 18:03:49 +00:00
$pos = strpos ( $if , $bi );
if ( $pos !== FALSE )
{
2011-03-23 09:54:56 +00:00
$nullintf = 1 ;
echo ( " $if matched $bi \n " );
2011-03-11 18:03:49 +00:00
}
2011-03-23 09:54:56 +00:00
}
if ( preg_match ( '/serial[0-9]:/' , $if )) { $nullintf = '1' ; }
if ( $nullintf != '1' )
{
return 1 ;
} else {
return 0 ;
}
2007-04-03 14:10:23 +00:00
}
2011-03-08 17:12:43 +00:00
function utime ()
2010-01-13 15:54:03 +00:00
{
2011-03-23 09:54:56 +00:00
$time = explode ( " " , microtime ());
$usec = ( double ) $time [ 0 ];
$sec = ( double ) $time [ 1 ];
return $sec + $usec ;
2007-04-03 14:10:23 +00:00
}
2011-03-16 14:31:21 +00:00
function createHost ( $host , $community , $snmpver , $port = 161 , $transport = 'udp' )
2010-02-07 22:39:02 +00:00
{
2011-03-23 09:54:56 +00:00
$host = trim ( strtolower ( $host ));
$device = deviceArray ( $host , $community , $snmpver , $port , $transport );
$host_os = getHostOS ( $device );
2011-03-15 16:12:44 +00:00
2011-03-23 09:54:56 +00:00
if ( $host_os )
{
$sql = mysql_query ( " INSERT INTO `devices` (`hostname`, `sysName`, `community`, `port`, `transport`, `os`, `status`,`snmpver`) VALUES (' $host ', ' $host ', ' $community ', ' $port ', ' $transport ', ' $host_os ', '1',' $snmpver ') " );
if ( mysql_affected_rows ())
2010-02-07 22:39:02 +00:00
{
2011-03-23 09:54:56 +00:00
return ( " Created host : $host (id: " . mysql_insert_id () . " ) (os: $host_os ) " );
2011-03-08 17:12:43 +00:00
}
else
{
2011-03-23 09:54:56 +00:00
return FALSE ;
2010-02-07 22:39:02 +00:00
}
2011-03-23 09:54:56 +00:00
}
else
{
return FALSE ;
}
2007-04-03 14:10:23 +00:00
}
2010-01-13 15:54:03 +00:00
function isDomainResolves ( $domain )
{
2011-03-23 09:54:56 +00:00
return ( gethostbyname ( $domain ) != $domain || count ( dns_get_record ( $domain )) != 0 );
2007-04-03 14:10:23 +00:00
}
2011-03-08 17:12:43 +00:00
function hoststatus ( $id )
2010-01-13 15:54:03 +00:00
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query ( " SELECT `status` FROM `devices` WHERE `device_id` = ' $id ' " );
$result = @ mysql_result ( $sql , 0 );
2011-03-08 17:12:43 +00:00
2011-03-23 09:54:56 +00:00
return $result ;
2007-04-03 14:10:23 +00:00
}
2011-03-11 18:03:49 +00:00
function match_network ( $nets , $ip , $first = false )
{
2011-03-23 09:54:56 +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 ;
2011-03-11 18:03:49 +00:00
}
2011-03-23 09:54:56 +00:00
}
2011-03-08 17:12:43 +00:00
2011-03-23 09:54:56 +00:00
return $return ;
2007-04-03 14:10:23 +00:00
}
2010-01-01 14:09:57 +00:00
function snmp2ipv6 ( $ipv6_snmp )
{
2011-03-23 09:54:56 +00:00
$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 ]; }
2011-03-08 17:12:43 +00:00
2011-03-23 09:54:56 +00:00
return implode ( ':' , $ipv6_2 );
2010-01-01 14:09:57 +00:00
}
function ipv62snmp ( $ipv6 )
{
2011-03-23 09:54:56 +00:00
$ipv6_ex = explode ( ':' , Net_IPv6 :: uncompress ( $ipv6 ));
for ( $i = 0 ; $i < 8 ; $i ++ ) { $ipv6_ex [ $i ] = zeropad ( $ipv6_ex [ $i ], 4 ); }
$ipv6_ip = implode ( '' , $ipv6_ex );
for ( $i = 0 ; $i < 32 ; $i += 2 ) $ipv6_split [] = hexdec ( substr ( $ipv6_ip , $i , 2 ));
2011-03-08 17:12:43 +00:00
2011-03-23 09:54:56 +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 )
{
2011-03-23 09:54:56 +00:00
global $valid_v6 , $device , $config ;
2011-03-08 17:12:43 +00:00
2011-03-23 09:54:56 +00:00
$ipv6_network = Net_IPv6 :: getNetmask ( " $ipv6_address / $ipv6_prefixlen " ) . '/' . $ipv6_prefixlen ;
$ipv6_compressed = Net_IPv6 :: compress ( $ipv6_address );
2010-01-01 14:14:09 +00:00
2011-03-23 09:54:56 +00:00
if ( Net_IPv6 :: getAddressType ( $ipv6_address ) == NET_IPV6_LOCAL_LINK )
{
# ignore link-locals (coming from IPV6-MIB)
return ;
}
2010-01-01 14:14:09 +00:00
2011-03-23 09:54:56 +00:00
if ( mysql_result ( mysql_query ( " SELECT count(*) FROM `ports`
2011-03-22 20:27:39 +00:00
WHERE device_id = '".$device[' device_id ']."' AND `ifIndex` = '$ifIndex' " ), 0) != '0' && $ipv6_prefixlen > '0' && $ipv6_prefixlen < '129' && $ipv6_compressed != '::1')
2011-03-23 09:54:56 +00:00
{
$i_query = " SELECT interface_id FROM `ports` WHERE device_id = ' " . $device [ 'device_id' ] . " ' AND `ifIndex` = ' $ifIndex ' " ;
$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' )
2011-03-11 18:03:49 +00:00
{
2011-03-23 09:54:56 +00:00
mysql_query ( " INSERT INTO `ipv6_networks` (`ipv6_network`) VALUES (' $ipv6_network ') " );
echo ( " N " );
}
2010-01-01 14:14:09 +00:00
2011-03-23 09:54:56 +00:00
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 " );
}
2010-01-01 14:14:09 +00:00
2011-03-23 09:54:56 +00:00
$ipv6_network_id = @ mysql_result ( mysql_query ( " SELECT `ipv6_network_id` from `ipv6_networks` WHERE `ipv6_network` = ' $ipv6_network ' " ), 0 );
2011-03-22 20:27:39 +00:00
2011-03-23 09:54:56 +00:00
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 ( " + " );
2011-03-08 17:12:43 +00:00
}
2011-03-23 09:54:56 +00:00
else
{
echo ( " . " );
}
$full_address = " $ipv6_address / $ipv6_prefixlen " ;
$valid = $full_address . " - " . $interface_id ;
$valid_v6 [ $valid ] = 1 ;
}
2010-01-01 14:14:09 +00:00
}
2010-01-03 20:13:10 +00:00
function get_astext ( $asn )
{
2011-03-23 09:54:56 +00:00
global $config , $cache ;
2010-01-09 17:06:34 +00:00
2011-03-23 09:54:56 +00:00
if ( isset ( $config [ 'astext' ][ $asn ]))
{
return $config [ 'astext' ][ $asn ];
}
else
{
if ( isset ( $cache [ 'astext' ][ $asn ]))
2010-01-10 14:48:00 +00:00
{
2011-03-23 09:54:56 +00:00
return $cache [ 'astext' ][ $asn ];
2010-01-10 14:48:00 +00:00
}
else
{
2011-03-23 09:54:56 +00:00
$result = dns_get_record ( " AS $asn .asn.cymru.com " , DNS_TXT );
$txt = explode ( '|' , $result [ 0 ][ 'txt' ]);
$result = trim ( str_replace ( '"' , '' , $txt [ 4 ]));
$cache [ 'astext' ][ $asn ] = $result ;
return $result ;
2010-01-10 14:48:00 +00:00
}
2011-03-23 09:54:56 +00:00
}
2010-01-03 20:13:10 +00:00
}
2010-01-07 16:50:52 +00:00
2011-03-30 16:28:24 +00:00
# FIXME DEPRECATED -- only used in dead file includes/polling/interfaces.inc.php - if we no longer need that one, this can go too.
2010-01-07 16:50:52 +00:00
function eventlog ( $eventtext , $device_id = " " , $interface_id = " " )
{
2011-03-23 09:54:56 +00:00
$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-07 16:50:52 +00:00
}
2010-01-13 15:54:03 +00:00
2010-07-07 13:58:11 +00:00
# Use this function to write to the eventlog table
2011-03-08 17:12:43 +00:00
function log_event ( $text , $device = NULL , $type = NULL , $reference = NULL )
2010-02-21 14:57:36 +00:00
{
2011-03-23 09:54:56 +00:00
global $debug ;
2010-02-21 14:57:36 +00:00
2011-03-28 20:29:34 +00:00
if ( ! is_array ( $device )) { $device = device_by_id_cache ( $device ); }
2010-06-20 21:37:05 +00:00
2011-03-23 09:54:56 +00:00
$event_query = " INSERT INTO eventlog (host, reference, type, datetime, message) VALUES ( " . ( $device [ 'device_id' ] ? $device [ 'device_id' ] : " NULL " );
$event_query .= " , ' " . ( $reference ? $reference : " NULL " ) . " ', ' " . ( $type ? $type : " NULL " ) . " ', NOW(), ' " . mres ( $text ) . " ') " ;
if ( $debug ) { echo ( $event_query . " \n " ); }
mysql_query ( $event_query );
2011-03-08 17:12:43 +00:00
}
2010-02-17 11:02:18 +00:00
2010-01-13 15:54:03 +00:00
function notify ( $device , $title , $message )
{
2011-03-23 09:54:56 +00:00
global $config ;
2010-01-13 15:54:03 +00:00
2011-03-23 09:54:56 +00:00
if ( $config [ 'alerts' ][ 'email' ][ 'enable' ])
{
2011-03-30 16:28:24 +00:00
if ( ! get_dev_attrib ( $device , 'disable_notify' ))
2010-07-11 19:11:46 +00:00
{
2011-03-30 16:28:24 +00:00
if ( $config [ 'alerts' ][ 'email' ][ 'default_only' ])
2011-03-23 09:54:56 +00:00
{
$email = $config [ 'alerts' ][ 'email' ][ 'default' ];
2011-03-30 16:28:24 +00:00
} else {
if ( get_dev_attrib ( $device , 'override_sysContact_bool' ))
{
$email = get_dev_attrib ( $device , 'override_sysContact_string' );
}
elseif ( $device [ 'sysContact' ])
{
$email = $device [ 'sysContact' ];
} else {
$email = $config [ 'alerts' ][ 'email' ][ 'default' ];
}
}
if ( $email )
{
mail ( $email , $title , $message , $config [ 'email_headers' ]);
2011-03-23 09:54:56 +00:00
}
}
}
2010-01-13 15:54:03 +00:00
}
2010-02-21 17:02:20 +00:00
function formatCiscoHardware ( & $device , $short = false )
{
2011-03-23 09:54:56 +00:00
if ( $device [ 'os' ] == " ios " )
{
if ( $device [ 'hardware' ])
2011-03-11 18:03:49 +00:00
{
2011-03-23 09:54:56 +00:00
if ( preg_match ( " /^WS-C([A-Za-z0-9]+).*/ " , $device [ 'hardware' ], $matches ))
{
if ( ! $short )
2011-03-11 18:03:49 +00:00
{
2011-03-23 09:54:56 +00:00
$device [ 'hardware' ] = " Cisco " . $matches [ 1 ] . " ( " . $device [ 'hardware' ] . " ) " ;
2011-03-11 18:03:49 +00:00
}
else
{
2011-03-23 09:54:56 +00:00
$device [ 'hardware' ] = " Cisco " . $matches [ 1 ];
2010-02-21 17:02:20 +00:00
}
2011-03-23 09:54:56 +00:00
}
elseif ( preg_match ( " /^CISCO([0-9]+) $ / " , $device [ 'hardware' ], $matches ))
{
$device [ 'hardware' ] = " Cisco " . $matches [ 1 ];
}
2010-02-21 17:02:20 +00:00
}
2011-03-23 09:54:56 +00:00
else
{
if ( preg_match ( " /Cisco IOS Software, C([A-Za-z0-9]+) Software.*/ " , $device [ 'sysDescr' ], $matches ))
{
$device [ 'hardware' ] = " Cisco " . $matches [ 1 ];
}
elseif ( preg_match ( " /Cisco IOS Software, ([0-9]+) Software.*/ " , $device [ 'sysDescr' ], $matches ))
{
$device [ 'hardware' ] = " Cisco " . $matches [ 1 ];
}
}
}
2010-02-21 17:02:20 +00:00
}
2010-06-18 16:38:41 +00:00
# from http://ditio.net/2008/11/04/php-string-to-hex-and-hex-to-string-functions/
function hex2str ( $hex )
{
2011-03-23 09:54:56 +00:00
$string = '' ;
2011-03-11 18:03:49 +00:00
2011-03-26 19:12:24 +00:00
for ( $i = 0 ; $i < strlen ( $hex ) - 1 ; $i += 2 )
2011-03-23 09:54:56 +00:00
{
$string .= chr ( hexdec ( $hex [ $i ] . $hex [ $i + 1 ]));
}
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $string ;
2010-06-18 16:38:41 +00:00
}
2010-07-05 15:27:01 +00:00
2010-07-07 13:58:11 +00:00
# Convert an SNMP hex string to regular string
2010-07-05 15:27:01 +00:00
function snmp_hexstring ( $hex )
{
2011-03-23 09:54:56 +00:00
return hex2str ( str_replace ( ' ' , '' , str_replace ( ' 00' , '' , $hex )));
2010-07-05 15:27:01 +00:00
}
2010-07-07 13:58:11 +00:00
# Check if the supplied string is an SNMP hex string
2010-07-05 15:27:01 +00:00
function isHexString ( $str )
{
2011-03-23 09:54:56 +00:00
return preg_match ( " /^[a-f0-9][a-f0-9]( [a-f0-9][a-f0-9])* $ /is " , trim ( $str ));
2010-07-05 15:27:01 +00:00
}
2010-07-09 22:38:46 +00:00
# Include all .inc.php files in $dir
2010-12-15 17:54:42 +00:00
function include_dir ( $dir , $regex = " " )
2010-07-09 22:38:46 +00:00
{
2011-03-23 09:54:56 +00:00
global $device , $config , $debug ;
if ( $regex == " " )
{
$regex = " / \ .inc \ .php $ / " ;
}
if ( $handle = opendir ( $config [ 'install_dir' ] . '/' . $dir ))
{
while ( false !== ( $file = readdir ( $handle )))
{
if ( filetype ( $config [ 'install_dir' ] . '/' . $dir . '/' . $file ) == 'file' && preg_match ( $regex , $file ))
{
if ( $debug ) { echo ( " Including: " . $config [ 'install_dir' ] . '/' . $dir . '/' . $file . " \n " ); }
include ( $config [ 'install_dir' ] . '/' . $dir . '/' . $file );
}
2010-07-09 22:38:46 +00:00
}
2011-03-23 09:54:56 +00:00
closedir ( $handle );
}
2010-07-09 22:38:46 +00:00
}
2011-03-30 16:28:24 +00:00
?>