2009-09-07 11:07:59 +00:00
< ? php
2009-05-07 13:47:51 +00:00
## Common Functions
2011-04-28 21:12:16 +00:00
function sgn ( $int )
{
if ( $int < 0 )
{
2011-04-28 13:06:39 +00:00
return - 1 ;
2011-04-28 21:12:16 +00:00
} elseif ( $int == 0 ) {
2011-04-28 13:06:39 +00:00
return 0 ;
2011-04-28 21:12:16 +00:00
} else {
2011-04-28 13:06:39 +00:00
return 1 ;
2011-04-28 21:12:16 +00:00
}
2011-04-28 13:06:39 +00:00
}
2011-04-26 15:07:52 +00:00
function get_sensor_rrd ( $device , $sensor )
{
global $config ;
2011-04-28 21:12:16 +00:00
# For IPMI, sensors tend to change order, and there is no index, so we prefer to use the description as key here.
if ( $config [ 'os' ][ $device [ 'os' ]][ 'sensor_descr' ] || $sensor [ 'poller_type' ] == " ipmi " )
2011-04-26 15:07:52 +00:00
{
$rrd_file = $config [ 'rrd_dir' ] . " / " . $device [ 'hostname' ] . " / " . safename ( " sensor- " . $sensor [ 'sensor_class' ] . " - " . $sensor [ 'sensor_type' ] . " - " . $sensor [ 'sensor_descr' ] . " .rrd " );
} else {
$rrd_file = $config [ 'rrd_dir' ] . " / " . $device [ 'hostname' ] . " / " . safename ( " sensor- " . $sensor [ 'sensor_class' ] . " - " . $sensor [ 'sensor_type' ] . " - " . $sensor [ 'sensor_index' ] . " .rrd " );
}
return ( $rrd_file );
}
2010-07-28 19:43:02 +00:00
function get_port_by_id ( $port_id )
{
2011-03-23 09:54:56 +00:00
if ( is_numeric ( $port_id ))
{
$port = mysql_fetch_assoc ( mysql_query ( " SELECT * FROM `ports` WHERE `interface_id` = ' " . $port_id . " ' " ));
}
if ( is_array ( $port ))
{
return $port ;
} else {
return FALSE ;
}
2010-08-01 14:17:06 +00:00
}
2010-07-28 19:43:02 +00:00
2010-08-01 14:17:06 +00:00
function get_application_by_id ( $application_id )
{
2011-03-23 09:54:56 +00:00
if ( is_numeric ( $application_id ))
{
$application = mysql_fetch_assoc ( mysql_query ( " SELECT * FROM `applications` WHERE `app_id` = ' " . $application_id . " ' " ));
}
if ( is_array ( $application ))
{
return $application ;
} else {
return FALSE ;
}
2010-08-01 14:17:06 +00:00
}
function get_sensor_by_id ( $sensor_id )
{
2011-03-23 09:54:56 +00:00
if ( is_numeric ( $sensor_id ))
{
$sensor = mysql_fetch_assoc ( mysql_query ( " SELECT * FROM `sensors` WHERE `sensor_id` = ' " . $sensor_id . " ' " ));
}
if ( is_array ( $sensor ))
{
return $sensor ;
} else {
return FALSE ;
}
2010-08-01 14:17:06 +00:00
}
2011-03-11 18:03:49 +00:00
function get_device_id_by_interface_id ( $interface_id )
{
2011-03-23 09:54:56 +00:00
if ( is_numeric ( $interface_id ))
{
$device_id = mysql_result ( mysql_query ( " SELECT `device_id` FROM `ports` WHERE `interface_id` = ' " . $interface_id . " ' " ), 0 );
}
if ( is_numeric ( $device_id ))
{
return $device_id ;
} else {
return FALSE ;
}
2010-07-28 19:43:02 +00:00
}
function ifclass ( $ifOperStatus , $ifAdminStatus )
{
2011-03-23 09:54:56 +00:00
$ifclass = " interface-upup " ;
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
if ( $ifAdminStatus == " down " ) { $ifclass = " interface-admindown " ; }
if ( $ifAdminStatus == " up " && $ifOperStatus == " down " ) { $ifclass = " interface-updown " ; }
if ( $ifAdminStatus == " up " && $ifOperStatus == " up " ) { $ifclass = " interface-upup " ; }
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $ifclass ;
2010-07-28 19:43:02 +00:00
}
2010-04-20 15:46:17 +00:00
function device_by_id_cache ( $device_id )
{
2011-03-23 09:54:56 +00:00
global $device_cache ;
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
if ( is_array ( $device_cache [ $device_id ]))
{
$device = $device_cache [ $device_id ];
} else {
$device = mysql_fetch_assoc ( mysql_query ( " SELECT * FROM `devices` WHERE `device_id` = ' " . $device_id . " ' " ));
2011-03-31 17:19:54 +00:00
if ( get_dev_attrib ( $device , 'override_sysLocation_bool' ))
{
$device [ 'real_location' ] = $device [ 'location' ];
$device [ 'location' ] = get_dev_attrib ( $device , 'override_sysLocation_string' );
}
2011-03-23 09:54:56 +00:00
$device_cache [ $device_id ] = $device ;
}
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $device ;
2010-04-20 15:46:17 +00:00
}
2011-03-11 18:03:49 +00:00
function truncate ( $substring , $max = 50 , $rep = '...' )
{
2011-03-23 09:54:56 +00:00
if ( strlen ( $substring ) < 1 ){ $string = $rep ; } else { $string = $substring ; }
$leave = $max - strlen ( $rep );
if ( strlen ( $string ) > $max ){ return substr_replace ( $string , $rep , $leave ); } else { return $string ; }
2009-10-28 13:49:37 +00:00
}
2011-03-11 18:03:49 +00:00
function mres ( $string )
{ // short function wrapper because the real one is stupidly long and ugly. aestetics.
2011-03-23 09:54:56 +00:00
return mysql_real_escape_string ( $string );
2009-06-19 10:43:02 +00:00
}
2011-03-11 18:03:49 +00:00
function getifhost ( $id )
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query ( " SELECT `device_id` from `ports` WHERE `interface_id` = ' $id ' " );
$result = @ mysql_result ( $sql , 0 );
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result ;
2009-05-11 13:43:59 +00:00
}
2011-03-11 18:03:49 +00:00
function gethostbyid ( $id )
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query ( " SELECT `hostname` FROM `devices` WHERE `device_id` = ' $id ' " );
$result = @ mysql_result ( $sql , 0 );
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result ;
2009-05-07 13:47:51 +00:00
}
function strgen ( $length = 16 )
{
2011-03-23 09:54:56 +00:00
$entropy = array ( 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 'a' , 'A' , 'b' , 'B' , 'c' , 'C' , 'd' , 'D' , 'e' ,
'E' , 'f' , 'F' , 'g' , 'G' , 'h' , 'H' , 'i' , 'I' , 'j' , 'J' , 'k' , 'K' , 'l' , 'L' , 'm' , 'M' , 'n' ,
'N' , 'o' , 'O' , 'p' , 'P' , 'q' , 'Q' , 'r' , 'R' , 's' , 'S' , 't' , 'T' , 'u' , 'U' , 'v' , 'V' , 'w' ,
'W' , 'x' , 'X' , 'y' , 'Y' , 'z' , 'Z' );
$string = " " ;
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
for ( $i = 0 ; $i < $length ; $i ++ )
{
$key = mt_rand ( 0 , 61 );
$string .= $entropy [ $key ];
}
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $string ;
2009-05-07 13:47:51 +00:00
}
2011-03-11 18:03:49 +00:00
function getpeerhost ( $id )
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query ( " SELECT `device_id` from `bgpPeers` WHERE `bgpPeer_id` = ' $id ' " );
$result = @ mysql_result ( $sql , 0 );
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result ;
2009-05-07 13:47:51 +00:00
}
2011-03-11 18:03:49 +00:00
function getifindexbyid ( $id )
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query ( " SELECT `ifIndex` FROM `ports` WHERE `interface_id` = ' $id ' " );
$result = @ mysql_result ( $sql , 0 );
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result ;
2009-05-07 13:47:51 +00:00
}
2011-04-27 17:41:35 +00:00
function get_port_by_ifIndex ( $device , $ifIndex )
{
$sql = mysql_query ( " SELECT * FROM `ports` WHERE `device_id` = ' " . $device [ 'device_id' ] . " ' AND `ifIndex` = ' $ifIndex ' " );
$result = @ mysql_fetch_assoc ( $sql );
return $result ;
}
2011-03-11 18:03:49 +00:00
function getifbyid ( $id )
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query ( " SELECT * FROM `ports` WHERE `interface_id` = ' $id ' " );
2011-04-06 13:54:50 +00:00
$result = @ mysql_fetch_assoc ( $sql );
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result ;
2010-01-08 23:09:25 +00:00
}
2011-03-11 18:03:49 +00:00
function getifdescrbyid ( $id )
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query ( " SELECT `ifDescr` FROM `ports` WHERE `interface_id` = ' $id ' " );
$result = @ mysql_result ( $sql , 0 );
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result ;
2009-05-07 13:47:51 +00:00
}
2011-03-11 18:03:49 +00:00
function getidbyname ( $domain )
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query ( " SELECT `device_id` FROM `devices` WHERE `hostname` = ' $domain ' " );
$result = @ mysql_result ( $sql , 0 );
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result ;
2009-05-07 13:47:51 +00:00
}
2011-03-11 18:03:49 +00:00
function gethostosbyid ( $id )
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query ( " SELECT `os` FROM `devices` WHERE `device_id` = ' $id ' " );
$result = @ mysql_result ( $sql , 0 );
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result ;
2009-05-07 13:47:51 +00:00
}
2011-03-15 15:39:57 +00:00
function safename ( $name )
2010-02-05 22:10:06 +00:00
{
2011-03-23 09:54:56 +00:00
return preg_replace ( '/[^a-zA-Z0-9,._\-]/' , '_' , $name );
2010-02-05 22:10:06 +00:00
}
2009-05-07 13:47:51 +00:00
2011-03-28 20:29:34 +00:00
function zeropad ( $num , $length = 2 )
{
while ( strlen ( $num ) < $length )
{
$num = '0' . $num ;
}
return $num ;
}
2011-03-31 17:19:54 +00:00
function set_dev_attrib ( $device , $attrib_type , $attrib_value )
{
$count_sql = " SELECT COUNT(*) FROM devices_attribs WHERE `device_id` = ' " . mres ( $device [ 'device_id' ]) . " ' AND `attrib_type` = ' $attrib_type ' " ;
if ( mysql_result ( mysql_query ( $count_sql ), 0 ))
{
$update_sql = " UPDATE devices_attribs SET attrib_value = ' $attrib_value ' WHERE `device_id` = ' " . mres ( $device [ 'device_id' ]) . " ' AND `attrib_type` = ' $attrib_type ' " ;
mysql_query ( $update_sql );
}
else
{
$insert_sql = " INSERT INTO devices_attribs (`device_id`, `attrib_type`, `attrib_value`) VALUES (' " . mres ( $device [ 'device_id' ]) . " ', ' $attrib_type ', ' $attrib_value ') " ;
mysql_query ( $insert_sql );
}
return mysql_affected_rows ();
}
2011-05-03 20:13:15 +00:00
function get_dev_attribs ( $device )
{
$attribs = array ();
2011-05-03 21:17:04 +00:00
$sql = " SELECT * FROM devices_attribs WHERE `device_id` = ' " . mres ( $device ) . " ' " ;
$query = mysql_query ( $sql );
while ( $entry = mysql_fetch_assoc ( $query ))
2011-05-03 20:13:15 +00:00
{
2011-05-03 21:17:04 +00:00
$attribs [ $entry [ 'attrib_type' ]] = $entry [ 'attrib_value' ];
2011-05-03 20:13:15 +00:00
}
return $attribs ;
}
2011-03-31 17:19:54 +00:00
function get_dev_attrib ( $device , $attrib_type )
{
$sql = " SELECT attrib_value FROM devices_attribs WHERE `device_id` = ' " . mres ( $device [ 'device_id' ]) . " ' AND `attrib_type` = ' $attrib_type ' " ;
if ( $row = mysql_fetch_assoc ( mysql_query ( $sql )))
{
return $row [ 'attrib_value' ];
}
else
{
return NULL ;
}
}
function del_dev_attrib ( $device , $attrib_type )
{
$sql = " DELETE FROM devices_attribs WHERE `device_id` = ' " . mres ( $device [ 'device_id' ]) . " ' AND `attrib_type` = ' $attrib_type ' " ;
return mysql_query ( $sql );
}
2011-03-28 20:29:34 +00:00
2011-04-05 13:29:57 +00:00
function formatRates ( $rate )
{
$rate = format_si ( $rate ) . " bps " ;
return $rate ;
}
function formatStorage ( $rate , $round = '2' )
{
$rate = format_bi ( $rate , $round ) . " B " ;
return $rate ;
}
function format_si ( $rate )
{
if ( $rate >= " 0.1 " )
{
$sizes = Array ( '' , 'k' , 'M' , 'G' , 'T' , 'P' , 'E' );
$round = Array ( '2' , '2' , '2' , '2' , '2' , '2' , '2' , '2' , '2' );
$ext = $sizes [ 0 ];
for ( $i = 1 ; (( $i < count ( $sizes )) && ( $rate >= 1000 )); $i ++ ) { $rate = $rate / 1000 ; $ext = $sizes [ $i ]; }
}
else
{
$sizes = Array ( '' , 'm' , 'u' , 'n' );
$round = Array ( '2' , '2' , '2' , '2' );
$ext = $sizes [ 0 ];
for ( $i = 1 ; (( $i < count ( $sizes )) && ( $rate != 0 ) && ( $rate <= 0.1 )); $i ++ ) { $rate = $rate * 1000 ; $ext = $sizes [ $i ]; }
}
return round ( $rate , $round [ $i ]) . $ext ;
}
function format_bi ( $size , $round = '2' )
{
$sizes = Array ( '' , 'k' , 'M' , 'G' , 'T' , 'P' , 'E' );
$ext = $sizes [ 0 ];
for ( $i = 1 ; (( $i < count ( $sizes )) && ( $size >= 1024 )); $i ++ ) { $size = $size / 1024 ; $ext = $sizes [ $i ]; }
return round ( $size , $round ) . $ext ;
}
?>