2009-09-07 11:07:59 +00:00
< ? php
2007-04-03 14:10:23 +00:00
2012-05-21 16:52:02 +00:00
/**
* Observium
*
* This file is part of Observium.
*
* @package observium
* @subpackage functions
* @author Adam Armstrong <adama@memetic.org>
* @copyright (C) 2006 - 2012 Adam Armstrong
*
*/
2012-05-25 12:24:34 +00:00
// Include from PEAR
2008-04-13 16:27:05 +00:00
include_once ( " Net/IPv4.php " );
include_once ( " Net/IPv6.php " );
2012-05-25 12:24:34 +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 " );
2011-05-12 11:08:56 +00:00
include_once ( $config [ 'install_dir' ] . " /includes/dbFacile.php " );
2011-09-08 12:24:18 +00:00
include_once ( $config [ 'install_dir' ] . " /includes/console_colour.php " );
2012-05-02 22:02:30 +00:00
2015-03-01 17:06:38 +00:00
$console_color = new Console_Color2 ();
2012-04-26 12:11:03 +00:00
if ( $config [ 'alerts' ][ 'email' ][ 'enable' ])
{
include_once ( $config [ 'install_dir' ] . " /includes/phpmailer/class.phpmailer.php " );
include_once ( $config [ 'install_dir' ] . " /includes/phpmailer/class.smtp.php " );
}
2011-09-08 12:24:18 +00:00
2012-05-25 10:34:01 +00:00
function array_sort ( $array , $on , $order = SORT_ASC )
2012-04-08 16:21:52 +00:00
{
2012-05-25 10:34:01 +00:00
$new_array = array ();
$sortable_array = array ();
if ( count ( $array ) > 0 ) {
foreach ( $array as $k => $v ) {
if ( is_array ( $v )) {
foreach ( $v as $k2 => $v2 ) {
if ( $k2 == $on ) {
$sortable_array [ $k ] = $v2 ;
}
}
} else {
$sortable_array [ $k ] = $v ;
}
}
switch ( $order ) {
case SORT_ASC :
asort ( $sortable_array );
break ;
case SORT_DESC :
arsort ( $sortable_array );
break ;
}
foreach ( $sortable_array as $k => $v ) {
$new_array [ $k ] = $array [ $k ];
}
}
return $new_array ;
2012-04-08 16:21:52 +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 ;
2011-09-16 10:31:48 +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 getHostOS ( $device )
{
2011-04-15 16:35:39 +00:00
global $config , $debug ;
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-04-15 16:35:39 +00:00
if ( $debug )
{
echo ( " | $sysDescr | $sysObjectId | " );
}
2010-07-11 20:12:24 +00:00
2015-02-22 19:28:09 +00:00
$path = $config [ 'install_dir' ] . " /includes/discovery/os " ;
$dir_handle = @ opendir ( $path ) or die ( " Unable to open $path " );
2011-03-23 09:54:56 +00:00
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 ;
2015-02-22 16:45:40 +00:00
$errors = array ();
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 ));
2015-02-22 19:28:09 +00:00
$in_errors = 0 ;
$out_errors = 0 ;
2011-03-23 09:54:56 +00:00
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
}
2012-04-24 22:50:15 +00:00
function getImage ( $device )
2008-11-27 12:36:37 +00:00
{
2011-03-23 09:54:56 +00:00
global $config ;
2011-09-16 10:31:48 +00:00
2012-04-24 22:50:15 +00:00
$device [ 'os' ] = strtolower ( $device [ 'os' ]);
if ( $device [ 'icon' ] && file_exists ( $config [ 'html_dir' ] . " /images/os/ " . $device [ 'icon' ] . " .png " ))
2011-03-23 09:54:56 +00:00
{
2012-04-24 22:50:15 +00:00
$image = '<img src="' . $config [ 'base_url' ] . '/images/os/' . $device [ 'icon' ] . '.png" />' ;
}
2015-03-01 17:06:38 +00:00
elseif ( isset ( $config [ 'os' ][ $device [ 'os' ]][ 'icon' ]) && $config [ 'os' ][ $device [ 'os' ]][ 'icon' ] && file_exists ( $config [ 'html_dir' ] . " /images/os/ " . $config [ 'os' ][ $device [ 'os' ]][ 'icon' ] . " .png " ))
2011-03-23 09:54:56 +00:00
{
2012-04-24 22:50:15 +00:00
$image = '<img src="' . $config [ 'base_url' ] . '/images/os/' . $config [ 'os' ][ $device [ 'os' ]][ 'icon' ] . '.png" />' ;
2011-03-23 09:54:56 +00:00
} else {
2012-04-24 22:50:15 +00:00
if ( file_exists ( $config [ 'html_dir' ] . '/images/os/' . $device [ 'os' ] . '.png' ))
{
$image = '<img src="' . $config [ 'base_url' ] . '/images/os/' . $device [ 'os' ] . '.png" />' ;
}
if ( $device [ 'os' ] == " linux " )
2011-03-23 09:54:56 +00:00
{
2012-04-24 22:50:15 +00:00
$features = strtolower ( trim ( $device [ 'features' ]));
2011-04-11 09:22:12 +00:00
list ( $distro ) = explode ( " " , $features );
2012-04-24 22:50:15 +00:00
if ( file_exists ( $config [ 'html_dir' ] . " /images/os/ $distro " . " .png " ))
{
$image = '<img src="' . $config [ 'base_url' ] . '/images/os/' . $distro . '.png" />' ;
}
2010-07-17 14:40:02 +00:00
}
2015-02-22 19:28:09 +00:00
if ( empty ( $image )) {
$image = '<img src="' . $config [ 'base_url' ] . '/images/os/generic.png" />' ;
}
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
}
2014-01-13 10:05:19 +00:00
function getImageSrc ( $device )
{
global $config ;
$device [ 'os' ] = strtolower ( $device [ 'os' ]);
if ( $device [ 'icon' ] && file_exists ( $config [ 'html_dir' ] . " /images/os/ " . $device [ 'icon' ] . " .png " ))
{
$image = $config [ 'base_url' ] . '/images/os/' . $device [ 'icon' ] . '.png' ;
}
elseif ( $config [ 'os' ][ $device [ 'os' ]][ 'icon' ] && file_exists ( $config [ 'html_dir' ] . " /images/os/ " . $config [ 'os' ][ $device [ 'os' ]][ 'icon' ] . " .png " ))
{
$image = $config [ 'base_url' ] . '/images/os/' . $config [ 'os' ][ $device [ 'os' ]][ 'icon' ] . '.png' ;
} else {
if ( file_exists ( $config [ 'html_dir' ] . '/images/os/' . $device [ 'os' ] . '.png' ))
{
$image = $config [ 'base_url' ] . '/images/os/' . $device [ 'os' ] . '.png' ;
}
if ( $device [ 'os' ] == " linux " )
{
$features = strtolower ( trim ( $device [ 'features' ]));
list ( $distro ) = explode ( " " , $features );
if ( file_exists ( $config [ 'html_dir' ] . " /images/os/ $distro " . " .png " ))
{
$image = $config [ 'base_url' ] . '/images/os/' . $distro . '.png' ;
}
}
2015-02-22 19:28:09 +00:00
if ( empty ( $image )) {
$image = $config [ 'base_url' ] . '/images/os/generic.png' ;
}
2014-01-13 10:05:19 +00:00
}
return $image ;
}
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
2012-05-25 12:24:34 +00:00
// FIXME does not check if destination exists!
2011-05-12 17:59:00 +00:00
$host = dbFetchCell ( " SELECT `hostname` FROM `devices` WHERE `device_id` = ? " , array ( $id ));
2015-05-19 10:54:24 +01:00
if ( rename ( $config [ 'rrd_dir' ] . " / $host " , $config [ 'rrd_dir' ] . " / $new " ) === TRUE ) {
dbUpdate ( array ( 'hostname' => $new ), 'devices' , 'device_id=?' , array ( $id ));
log_event ( " Hostname changed -> $new ( $source ) " , $id , 'system' );
} else {
echo " Renaming of $host failed \n " ;
log_event ( " Renaming of $host failed " , $id , 'system' );
}
2008-11-19 12:12:54 +00:00
}
2011-03-08 17:12:43 +00:00
function delete_device ( $id )
2010-04-24 23:16:12 +00:00
{
2015-03-12 15:51:40 +00:00
global $config , $debug ;
2015-02-22 19:28:09 +00:00
$ret = '' ;
2011-03-26 19:12:24 +00:00
2011-05-12 17:59:00 +00:00
$host = dbFetchCell ( " SELECT hostname FROM devices WHERE device_id = ? " , array ( $id ));
2015-05-19 14:25:17 +00:00
if ( empty ( $host ) ) {
return " No such host. " ;
}
2011-05-12 17:59:00 +00:00
foreach ( dbFetch ( " SELECT * FROM `ports` WHERE `device_id` = ? " , array ( $id )) as $int_data )
2011-03-23 09:54:56 +00:00
{
$int_if = $int_data [ 'ifDescr' ];
2012-05-16 13:25:50 +00:00
$int_id = $int_data [ 'port_id' ];
2011-03-23 09:54:56 +00:00
delete_port ( $int_id );
$ret .= " Removed interface $int_id ( $int_if ) \n " ;
}
2011-07-12 21:59:17 +00:00
2015-03-05 22:57:02 +00:00
$fields = array ( 'device_id' , 'host' );
foreach ( $fields as $field ) {
foreach ( dbFetch ( " SELECT table_name FROM information_schema.columns WHERE table_schema = ? AND column_name = ? " , array ( $config [ 'db_name' ], $field )) as $table ) {
2015-03-12 15:51:40 +00:00
$table = $table [ 'table_name' ];
$entries = ( int ) dbDelete ( $table , " ` $field ` = ? " , array ( $id ));
if ( $entries > 0 && $debug === true ) {
$ret .= " $field @ $table = # $entries\n " ;
}
2015-03-05 22:57:02 +00:00
}
2011-05-12 17:59:00 +00:00
}
2011-03-26 19:12:24 +00:00
2015-05-19 14:25:17 +00:00
$ex = shell_exec ( " bash -c '( [ ! -d " . trim ( $config [ 'rrd_dir' ]) . " / " . $host . " ] || rm -vrf " . trim ( $config [ 'rrd_dir' ]) . " / " . $host . " 2>&1 ) && echo -n OK' " );
$tmp = explode ( " \n " , $ex );
if ( $tmp [ sizeof ( $tmp ) - 1 ] != " OK " ) {
$ret .= " Could not remove files: \n $ex\n " ;
}
2011-03-26 19:12:24 +00:00
2015-03-12 15:51:40 +00:00
$ret .= " Removed device $host\n " ;
2011-03-23 09:54:56 +00:00
return $ret ;
2007-04-03 14:10:23 +00:00
}
2015-03-19 22:06:13 +00:00
function addHost ( $host , $snmpver , $port = '161' , $transport = 'udp' , $quiet = '0' , $poller_group = '0' , $force_add = '0' )
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 );
2012-05-25 12:24:34 +00:00
// Test Database Exists
2011-09-08 12:24:18 +00:00
if ( dbFetchCell ( " SELECT COUNT(*) FROM `devices` WHERE `hostname` = ? " , array ( $host )) == '0' )
2011-03-23 09:54:56 +00:00
{
2012-05-25 12:24:34 +00:00
// Test reachability
2015-03-23 16:14:31 +00:00
if ( $force_add == 1 || isPingable ( $host ))
2011-03-23 09:54:56 +00:00
{
2012-05-09 16:18:23 +00:00
if ( empty ( $snmpver ))
{
// Try SNMPv2c
$snmpver = 'v2c' ;
2015-04-30 21:39:10 +01:00
$ret = addHost ( $host , $snmpver , $port , $transport , $quiet , $poller_group , $force_add );
2012-05-20 23:19:28 +00:00
if ( ! $ret )
2012-05-09 16:18:23 +00:00
{
//Try SNMPv3
$snmpver = 'v3' ;
2015-04-30 21:39:10 +01:00
$ret = addHost ( $host , $snmpver , $port , $transport , $quiet , $poller_group , $force_add );
2012-05-20 23:19:28 +00:00
if ( ! $ret )
2012-05-09 16:18:23 +00:00
{
// Try SNMPv1
$snmpver = 'v1' ;
2015-04-30 21:39:10 +01:00
return addHost ( $host , $snmpver , $port , $transport , $quiet , $poller_group , $force_add );
2012-05-20 23:19:28 +00:00
} else {
return $ret ;
}
} else {
return $ret ;
}
2012-05-09 16:18:23 +00:00
}
if ( $snmpver === " v3 " )
2011-03-08 17:12:43 +00:00
{
2012-05-09 16:18:23 +00:00
// Try each set of parameters from config
foreach ( $config [ 'snmp' ][ 'v3' ] as $v3 )
2011-09-08 12:24:18 +00:00
{
2012-05-09 16:18:23 +00:00
$device = deviceArray ( $host , NULL , $snmpver , $port , $transport , $v3 );
2014-07-16 01:02:55 +01:00
if ( $quiet == '0' ) { print_message ( " Trying v3 parameters " . $v3 [ 'authname' ] . " / " . $v3 [ 'authlevel' ] . " ... " ); }
2015-03-23 16:14:31 +00:00
if ( $force_add == 1 || isSNMPable ( $device ))
2011-09-08 12:24:18 +00:00
{
2012-05-09 16:18:23 +00:00
$snmphost = snmp_get ( $device , " sysName.0 " , " -Oqv " , " SNMPv2-MIB " );
if ( empty ( $snmphost ) or ( $snmphost == $host || $hostshort = $host ))
{
2015-03-19 12:00:03 +00:00
$device_id = createHost ( $host , NULL , $snmpver , $port , $transport , $v3 , $poller_group );
2012-05-09 16:18:23 +00:00
return $device_id ;
} else {
2014-07-16 01:02:55 +01:00
if ( $quiet == '0' ) { print_error ( " Given hostname does not match SNMP-read hostname ( $snmphost )! " ); }
2012-05-09 16:18:23 +00:00
}
2011-09-16 10:31:48 +00:00
} else {
2014-07-16 01:02:55 +01:00
if ( $quiet == '0' ) { print_error ( " No reply on credentials " . $v3 [ 'authname' ] . " / " . $v3 [ 'authlevel' ] . " using $snmpver " ); }
2011-09-08 12:24:18 +00:00
}
}
}
2012-05-09 16:18:23 +00:00
elseif ( $snmpver === " v2c " or $snmpver === " v1 " )
{
2012-05-25 12:24:34 +00:00
// try each community from config
2012-05-09 16:18:23 +00:00
foreach ( $config [ 'snmp' ][ 'community' ] as $community )
{
$device = deviceArray ( $host , $community , $snmpver , $port , $transport , NULL );
2014-07-16 01:02:55 +01:00
if ( $quiet == '0' ) { print_message ( " Trying community $community ... " ); }
2015-03-23 16:14:31 +00:00
if ( $force_add == 1 || isSNMPable ( $device ))
2012-05-09 16:18:23 +00:00
{
$snmphost = snmp_get ( $device , " sysName.0 " , " -Oqv " , " SNMPv2-MIB " );
2015-02-22 19:28:09 +00:00
if ( empty ( $snmphost ) || ( $snmphost && ( $snmphost == $host || $hostshort = $host )))
2012-05-09 16:18:23 +00:00
{
2015-03-19 12:00:03 +00:00
$device_id = createHost ( $host , $community , $snmpver , $port , $transport , array (), $poller_group );
2012-05-09 16:18:23 +00:00
return $device_id ;
} else {
2014-07-16 01:02:55 +01:00
if ( $quiet == '0' ) { print_error ( " Given hostname does not match SNMP-read hostname ( $snmphost )! " ); }
2012-05-09 16:18:23 +00:00
}
} else {
2014-07-16 01:02:55 +01:00
if ( $quiet == '0' ) { print_error ( " No reply on community $community using $snmpver " ); }
2012-05-09 16:18:23 +00:00
}
}
}
else
{
2014-07-16 01:02:55 +01:00
if ( $quiet == '0' ) { print_error ( " Unsupported SNMP Version \" $snmpver\ " . " ); }
2012-05-09 16:18:23 +00:00
}
2011-09-16 10:31:48 +00:00
if (! $device_id )
{
2012-05-25 12:24:34 +00:00
// Failed SNMP
2014-07-16 01:02:55 +01:00
if( $quiet == '0') { print_error( " Could not reach $host with given SNMP community using $snmpver " ); }
2011-09-14 17:26:59 +00:00
}
2011-09-16 10:31:48 +00:00
} else {
2012-05-25 12:24:34 +00:00
// failed Reachability
2014-07-16 01:02:55 +01:00
if( $quiet == '0') { print_error( " Could not ping $host " ); }
}
2011-09-16 10:31:48 +00:00
} else {
2012-05-25 12:24:34 +00:00
// found in database
2014-07-16 01:02:55 +01:00
if( $quiet == '0') { print_error( " Already got host $host " ); }
2012-05-09 16:18:23 +00:00
}
2012-05-20 23:19:28 +00:00
2012-05-09 16:18:23 +00:00
return 0;
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();
$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
}
2012-05-09 16:18:23 +00:00
function deviceArray( $host , $community , $snmpver , $port = 161, $transport = 'udp', $v3 )
2011-03-15 16:12:44 +00:00
{
2011-03-23 09:54:56 +00:00
$device = array();
$device['hostname'] = $host ;
$device['port'] = $port ;
$device['transport'] = $transport ;
2011-03-15 16:12:44 +00:00
2012-05-09 16:18:23 +00:00
$device['snmpver'] = $snmpver ;
if ( $snmpver === " v2c " or $snmpver === " v1 " )
{
$device['community'] = $community ;
}
elseif ( $snmpver === " v3 " )
{
$device['authlevel'] = $v3['authlevel'] ;
$device['authname'] = $v3['authname'] ;
$device['authpass'] = $v3['authpass'] ;
$device['authalgo'] = $v3['authalgo'] ;
$device['cryptopass'] = $v3['cryptopass'] ;
$device['cryptoalgo'] = $v3['cryptoalgo'] ;
}
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
}
2015-02-22 19:28:09 +00:00
function cidr2netmask( $netmask )
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 " );
2015-01-05 21:06:49 +00:00
if(empty( $pos )) {
// Support for Hikvision
$pos = snmp_get( $device , " SNMPv2 - SMI :: enterprises . 39165.1 . 1.0 " , " - Oqv " , " SNMPv2 - MIB " );
}
2011-03-23 09:54:56 +00:00
if ( $pos === '' || $pos === false)
{
return false;
} else {
return true;
}
2007-04-03 14:10:23 +00:00
}
2014-09-30 01:50:23 +01:00
function isPingable( $hostname , $device_id = FALSE)
2011-03-11 18:03:49 +00:00
{
2011-03-23 09:54:56 +00:00
global $config ;
2011-09-20 14:22:34 +00:00
2014-10-16 21:52:35 +01:00
$fping_params = '';
if(is_numeric( $config['fping_options'] ['retries']) || $config['fping_options'] ['retries'] > 1) {
$fping_params .= ' -r ' . $config['fping_options'] ['retries'];
}
if(is_numeric( $config['fping_options'] ['timeout']) || $config['fping_options'] ['timeout'] > 1) {
$fping_params .= ' -t ' . $config['fping_options'] ['timeout'];
}
$status = shell_exec( $config['fping'] . " $fping_params - e $hostname 2 >/ dev / null " );
2014-09-30 01:50:23 +01:00
$response = array();
2011-03-23 09:54:56 +00:00
if (strstr( $status , " alive " ))
{
2014-09-29 23:10:00 +01:00
$response['result'] = TRUE;
2011-03-23 09:54:56 +00:00
} else {
2014-10-16 21:52:35 +01:00
$status = shell_exec( $config['fping6'] . " $fping_params - e $hostname 2 >/ dev / null " );
2011-03-23 09:54:56 +00:00
if (strstr( $status , " alive " ))
{
2014-09-29 23:03:59 +01:00
$response['result'] = TRUE;
2011-03-23 09:54:56 +00:00
} else {
2014-09-29 23:03:59 +01:00
$response['result'] = FALSE;
2011-03-23 09:54:56 +00:00
}
}
2014-09-29 23:10:00 +01:00
if(is_numeric( $device_id ) && !empty( $device_id ))
{
preg_match('/( \ d+ \ .* \ d*) (ms)/', $status , $time );
$response['last_ping_timetaken'] = $time[1] ;
}
2014-09-29 23:03:59 +01:00
return( $response );
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-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
}
2015-03-19 12:00:03 +00:00
function createHost( $host , $community = NULL, $snmpver , $port = 161, $transport = 'udp', $v3 = array(), $poller_group ='0')
2010-02-07 22:39:02 +00:00
{
2015-05-24 20:48:54 +01:00
global $config ;
2011-03-23 09:54:56 +00:00
$host = trim(strtolower( $host ));
2011-09-14 17:26:59 +00:00
2015-05-24 20:48:54 +01:00
if (is_numeric( $poller_group ) === FALSE) {
$poller_group = $config['distributed_poller_group'] ;
}
2011-09-14 17:26:59 +00:00
$device = array('hostname' => $host ,
'sysName' => $host ,
'community' => $community ,
'port' => $port ,
'transport' => $transport ,
'status' => '1',
2015-03-19 12:00:03 +00:00
'snmpver' => $snmpver ,
'poller_group' => $poller_group
2012-05-09 16:18:23 +00:00
);
$device = array_merge( $device , $v3 );
2011-09-14 17:26:59 +00:00
2011-05-12 14:10:50 +00:00
$device['os'] = getHostOS( $device );
2011-03-15 16:12:44 +00:00
2011-05-12 14:10:50 +00:00
if ( $device['os'] )
2011-03-23 09:54:56 +00:00
{
2011-05-12 14:10:50 +00:00
2011-09-14 17:26:59 +00:00
$device_id = dbInsert( $device , 'devices');
2011-05-12 14:10:50 +00:00
2011-09-14 17:26:59 +00:00
if ( $device_id )
2010-02-07 22:39:02 +00:00
{
2011-09-14 17:26:59 +00:00
return( $device_id );
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-05-12 17:59:00 +00:00
return dbFetchCell( " SELECT `status` FROM `devices` WHERE `device_id` = ? " , array( $id ));
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 );
2015-02-22 16:45:40 +00:00
$ipv6_2 = array();
2011-04-21 17:11:58 +00:00
# Workaround stupid Microsoft bug in Windows 2008 -- this is fixed length!
# < fenestro> " because whoever implemented this mib for Microsoft was ignorant of RFC 2578 section 7.7 ( 2 ) "
if (count( $ipv6 ) == 17 && $ipv6[0] == 16)
{
array_shift( $ipv6 );
}
2011-03-23 09:54:56 +00:00
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 )
{
2015-02-22 16:45:40 +00:00
$ipv6_split = array();
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-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
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-05-12 17:59:00 +00:00
$insert = array(' host ' => ($device[' device_id '] ? $device[' device_id '] : "NULL"),
' reference ' => ($reference ? $reference : "NULL"),
' type ' => ($type ? $type : "NULL"),
2011-09-16 10:31:48 +00:00
' datetime ' => array("NOW()"),
2011-05-12 17:59:00 +00:00
' message ' => $text);
2011-09-16 10:31:48 +00:00
dbInsert($insert, ' eventlog ');
2011-03-08 17:12:43 +00:00
}
2010-02-17 11:02:18 +00:00
2012-04-26 12:11:03 +00:00
// Parse string with emails. Return array with email (as key) and name (as value)
function parse_email($emails)
{
$result = array();
$regex = ' /^ [ \ " \ ']?([^ \" \ ']+)[ \" \ ']? \ s { 0,}<([^@]+@[^>]+)> $ /';
if (is_string( $emails ))
{
$emails = preg_split('/[,;] \ s { 0,}/', $emails );
foreach ( $emails as $email )
{
if (preg_match( $regex , $email , $out , PREG_OFFSET_CAPTURE))
{
$result[$out[2] [0]] = $out[1] [0];
} else {
if (strpos( $email , " @ " )) { $result[$email] = NULL; }
}
}
} else {
// Return FALSE if input not string
return FALSE;
}
return $result ;
}
2014-03-07 22:30:07 +01:00
function send_mail( $emails , $subject , $message , $html =false) {
global $config ;
if( is_array( $emails ) || ( $emails = parse_email( $emails )) ) {
if( !class_exists( " PHPMailer " ,false) )
include_once( $config['install_dir'] . " / includes / phpmailer / class . phpmailer . php " );
$mail = new PHPMailer();
$mail->Hostname = php_uname('n');
if (empty( $config['email_from'] ))
$config['email_from'] = ' " ' . $config[' project_name '] . ' " <' . $config['email_user'] . '@'. $mail->Hostname .'>';
foreach (parse_email( $config['email_from'] ) as $from => $from_name )
$mail->setFrom ( $from , $from_name );
foreach ( $emails as $email => $email_name )
$mail->addAddress ( $email , $email_name );
$mail->Subject = $subject ;
$mail->XMailer = $config['project_name_version'] ;
$mail->CharSet = 'utf-8';
$mail->WordWrap = 76;
$mail->Body = $message ;
if( $html )
$mail->isHTML (true);
switch (strtolower(trim( $config['email_backend'] ))) {
case 'sendmail':
$mail->Mailer = 'sendmail';
$mail->Sendmail = $config['email_sendmail_path'] ;
break;
case 'smtp':
$mail->isSMTP ();
$mail->Host = $config['email_smtp_host'] ;
$mail->Timeout = $config['email_smtp_timeout'] ;
$mail->SMTPAuth = $config['email_smtp_auth'] ;
$mail->SMTPSecure = $config['email_smtp_secure'] ;
$mail->Port = $config['email_smtp_port'] ;
$mail->Username = $config['email_smtp_username'] ;
$mail->Password = $config['email_smtp_password'] ;
$mail->SMTPDebug = false;
break;
default:
$mail->Mailer = 'mail';
break;
}
return $mail->send () ? true : $mail->ErrorInfo ;
}
}
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'];
}
}
2012-04-26 12:11:03 +00:00
$emails = parse_email( $email );
if ( $emails )
2011-03-30 16:28:24 +00:00
{
2013-10-29 08:08:23 +10:00
$message_header = $config['page_title_prefix'] . " \n\n " ; // FIXME: use different config element
2012-04-27 17:18:26 +00:00
$message_footer = " \n\nE - mail sent to : " ;
2012-04-26 12:11:03 +00:00
$i = 0;
$count = count( $emails );
foreach ( $emails as $email => $email_name )
{
$i ++;
$message_footer .= $email ;
if ( $i < $count )
{
$message_footer .= " , " ;
} else {
$message_footer .= " \n " ;
}
}
$message_footer .= " E - mail sent at : " . date( $config['timestamp_format'] ) . " \n " ;
2014-03-07 22:30:07 +01:00
if( ( $err = send_mail( $emails , $title , $message_header . $message . $message_footer )) !== true) { echo " Mailer Error : " . $err . " \n " ; }
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-05-04 10:45:38 +00:00
global $device , $config , $debug , $valid ;
2011-09-16 10:31:48 +00:00
2011-03-23 09:54:56 +00:00
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 " ); }
2012-04-05 16:44:58 +00:00
2011-03-23 09:54:56 +00:00
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-05-18 23:08:45 +00:00
function is_port_valid( $port , $device )
{
2012-05-13 16:58:28 +00:00
global $config , $debug ;
2011-05-18 23:08:45 +00:00
2012-05-11 15:21:42 +00:00
if (strstr( $port['ifDescr'] , " irtual " ))
2011-05-18 23:08:45 +00:00
{
2012-05-11 15:21:42 +00:00
$valid = 0;
} else {
2011-05-18 23:08:45 +00:00
$valid = 1;
$if = strtolower( $port['ifDescr'] );
2015-05-07 10:22:37 +00:00
$fringe = $config['bad_if'] ;
if( is_array( $config['os'] [ $device['os'] ]['bad_if']) ) {
$fringe = array_merge( $config['bad_if'] , $config['os'] [ $device['os'] ]['bad_if']);
}
foreach ( $fringe as $bi )
2011-05-18 23:08:45 +00:00
{
if (strstr( $if , $bi ))
{
$valid = 0;
2011-09-20 14:22:34 +00:00
if ( $debug ) { echo( " ignored : $bi : $if " ); }
2011-05-18 23:08:45 +00:00
}
}
if (is_array( $config['bad_if_regexp'] ))
{
2015-05-07 10:22:37 +00:00
$fringe = $config['bad_if_regexp'] ;
if( is_array( $config['os'] [ $device['os'] ]['bad_if_regexp']) ) {
$fringe = array_merge( $config['bad_if_regexp'] , $config['os'] [ $device['os'] ]['bad_if_regexp']);
}
foreach ( $fringe as $bi )
2011-05-18 23:08:45 +00:00
{
if (preg_match( $bi . " i " , $if ))
{
$valid = 0;
2011-09-20 14:22:34 +00:00
if ( $debug ) { echo( " ignored : $bi : " . $if ); }
2011-05-18 23:08:45 +00:00
}
}
}
2012-03-29 12:05:45 +00:00
if (is_array( $config['bad_iftype'] ))
{
2015-05-07 10:22:37 +00:00
$fringe = $config['bad_iftype'] ;
if( is_array( $config['os'] [ $device['os'] ]['bad_iftype']) ) {
$fringe = array_merge( $config['bad_iftype'] , $config['os'] [ $device['os'] ]['bad_iftype']);
}
foreach ( $fringe as $bi )
2012-03-29 12:05:45 +00:00
{
if (strstr( $port['ifType'] , $bi ))
{
$valid = 0;
if ( $debug ) { echo( " ignored ifType : " . $port['ifType'] . " ( matched : " . $bi . " ) " ); }
}
}
}
2011-05-18 23:08:45 +00:00
if (empty( $port['ifDescr'] )) { $valid = 0; }
if ( $device['os'] == " catos " && strstr( $if , " vlan " )) { $valid = 0; }
2014-11-07 08:50:59 -08:00
if ( $device['os'] == " dlink " ) { $valid = 1; }
2011-05-18 23:08:45 +00:00
}
return $valid ;
}
2014-02-25 12:51:07 +00:00
function scan_new_plugins()
{
global $config , $debug ;
$installed = 0; // Track how many plugins we install.
if(file_exists( $config['plugin_dir'] ))
{
$plugin_files = scandir( $config['plugin_dir'] );
foreach( $plugin_files as $name )
{
if(is_dir( $config['plugin_dir'] .'/'. $name ))
{
if( $name != '.' && $name != '..')
{
if(is_file( $config['plugin_dir'] .'/'. $name .'/'. $name .'.php') && is_file( $config['plugin_dir'] .'/'. $name .'/'. $name .'.inc.php'))
{
$plugin_id = dbFetchRow( " SELECT `plugin_id` FROM `plugins` WHERE `plugin_name` = '$name' " );
if(empty( $plugin_id ))
{
if(dbInsert(array('plugin_name' => $name , 'plugin_active' => '0'), 'plugins'))
{
$installed ++;
}
}
}
}
}
}
}
return( $installed );
}
2014-06-24 16:14:42 +01:00
function validate_device_id( $id )
{
global $config ;
2014-06-26 00:33:00 +01:00
if(empty( $id ) || !is_numeric( $id ))
2014-06-24 16:14:42 +01:00
{
2014-06-26 00:33:00 +01:00
$return = false;
2014-06-24 16:14:42 +01:00
}
else
{
2014-06-26 00:33:00 +01:00
$device_id = dbFetchCell( " SELECT `device_id` FROM `devices` WHERE `device_id` = ? " , array( $id ));
if( $device_id == $id )
{
$return = true;
}
else
{
$return = false;
}
2014-06-24 16:14:42 +01:00
}
return( $return );
}
2014-09-17 21:03:02 +01:00
2014-10-07 11:29:54 +01:00
// The original source of this code is from Stackoverflow (www.stackoverflow.com).
// http://stackoverflow.com/questions/6054033/pretty-printing-json-with-php
// Answer provided by stewe (http://stackoverflow.com/users/3202187/ulk200
2014-10-02 20:17:45 +01:00
if (!defined('JSON_UNESCAPED_SLASHES'))
define('JSON_UNESCAPED_SLASHES', 64);
if (!defined('JSON_PRETTY_PRINT'))
define('JSON_PRETTY_PRINT', 128);
if (!defined('JSON_UNESCAPED_UNICODE'))
define('JSON_UNESCAPED_UNICODE', 256);
function _json_encode( $data , $options = 448)
{
2014-10-25 09:16:07 +10:00
if (version_compare(PHP_VERSION, '5.4', '>=')) {
2014-10-02 20:17:45 +01:00
return json_encode( $data , $options );
}
2014-10-25 09:16:07 +10:00
else {
return _json_format(json_encode( $data ), $options );
}
2014-10-02 20:17:45 +01:00
}
function _json_format( $json , $options = 448)
{
$prettyPrint = (bool) ( $options & JSON_PRETTY_PRINT);
$unescapeUnicode = (bool) ( $options & JSON_UNESCAPED_UNICODE);
$unescapeSlashes = (bool) ( $options & JSON_UNESCAPED_SLASHES);
if (! $prettyPrint && ! $unescapeUnicode && ! $unescapeSlashes )
{
return $json ;
}
$result = '';
$pos = 0;
$strLen = strlen( $json );
$indentStr = ' ';
$newLine = " \n " ;
$outOfQuotes = true;
$buffer = '';
$noescape = true;
for ( $i = 0; $i < $strLen ; $i ++)
{
// Grab the next character in the string
$char = substr( $json , $i , 1);
// Are we inside a quoted string?
if (' " ' === $char && $noescape)
{
$outOfQuotes = !$outOfQuotes;
}
if (!$outOfQuotes)
{
$buffer .= $char;
$noescape = ' \\ ' === $char ? !$noescape : true;
continue;
}
elseif (' ' !== $buffer)
{
if ($unescapeSlashes)
{
$buffer = str_replace(' \\ / ', ' / ', $buffer);
}
if ($unescapeUnicode && function_exists(' mb_convert_encoding '))
{
// http://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha
$buffer = preg_replace_callback(' / \\\\u ([ 0 - 9 a - f ]{ 4 }) / i ',
function ($match)
{
return mb_convert_encoding(pack(' H * ', $match[1]), ' UTF - 8 ', ' UCS - 2 BE ');
}, $buffer);
}
$result .= $buffer . $char;
$buffer = ' ';
continue;
}
elseif(false !== strpos(" \t\r\n", $char))
{
continue;
}
if (' : ' === $char)
{
// Add a space after the : character
$char .= ' ';
}
elseif ((' } ' === $char || ' ] ' === $char))
{
$pos--;
$prevChar = substr($json, $i - 1, 1);
if (' { ' !== $prevChar && ' [ ' !== $prevChar)
{
// If this character is the end of an element,
// output a new line and indent the next line
$result .= $newLine;
for ($j = 0; $j < $pos; $j++)
{
$result .= $indentStr;
}
}
else
{
// Collapse empty {} and []
$result = rtrim($result) . "\n\n" . $indentStr;
}
}
$result .= $char;
// If the last character was the beginning of an element,
// output a new line and indent the next line
if (' , ' === $char || ' { ' === $char || ' [ ' === $char)
{
$result .= $newLine;
if (' { ' === $char || ' [ ' === $char)
{
$pos++;
}
for ($j = 0; $j < $pos; $j++)
{
$result .= $indentStr;
}
}
}
// If buffer not empty after formating we have an unclosed quote
if (strlen($buffer) > 0)
{
//json is incorrectly formatted
$result = false;
}
return $result;
}
2014-11-30 17:49:52 +00:00
function convert_delay($delay) {
$delay = preg_replace(' / \s / ',' ',$delay);
if(strstr($delay, ' m ',TRUE)) {
$delay_sec = $delay * 60;
} elseif(strstr($delay, ' h ',TRUE)) {
$delay_sec = $delay * 3600;
} elseif(strstr($delay, ' d ',TRUE)) {
$delay_sec = $delay * 86400;
} elseif(is_numeric($delay)) {
$delay_sec = $delay;
} else {
2015-02-26 01:25:25 +00:00
$delay_sec = 300;
2014-11-30 17:49:52 +00:00
}
return($delay_sec);
}
2015-04-10 17:00:32 +01:00
function guidv4($data) {
// http://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid#15875555
// From: Jack http://stackoverflow.com/users/1338292/ja%CD%A2ck
assert(strlen($data) == 16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
return vsprintf(' % s % s -% s -% s -% s -% s % s % s ', str_split(bin2hex($data), 4));
}
2015-04-13 19:51:16 +10:00
function set_curl_proxy($post)
{
global $config;
if (isset($_ENV[' https_proxy '])) {
$tmp = rtrim($_ENV[' https_proxy '], "/");
$proxystr = str_replace(array("http://", "https://"), "", $tmp);
$config[' callback_proxy '] = $proxystr;
echo "Setting proxy to ".$proxystr." (from https_proxy=".$_ENV[' https_proxy '].")\n";
}
if (isset($config[' callback_proxy '])) {
echo "Using ".$config[' callback_proxy ']." as proxy\n";
curl_setopt($post, CURLOPT_PROXY, $config[' callback_proxy ']);
}
}
2015-04-19 20:23:34 +01:00
function target_to_id($target) {
if( $target[0].$target[1] == "g:" ) {
$target = "g".dbFetchCell(' SELECT id FROM device_groups WHERE name = ? ',array(substr($target,2)));
} else {
$target = dbFetchCell(' SELECT device_id FROM devices WHERE hostname = ? ',array($target));
}
return $target;
}
function id_to_target($id) {
if( $id[0] == "g" ) {
$id = ' g : '.dbFetchCell("SELECT name FROM device_groups WHERE id = ?",array(substr($id,1)));
} else {
$id = dbFetchCell("SELECT hostname FROM devices WHERE device_id = ?",array($id));
}
return $id;
}
2015-05-26 23:58:29 +10:00
function first_oid_match($device, $list) {
foreach ($list as $item) {
$tmp = trim(snmp_get($device, $item, "-Ovq"), ' " ');
if (!empty( $tmp )) {
return $tmp ;
}
}
}
2015-06-02 17:36:10 +01:00
function fix_integer_value( $value ) {
if ( $value < 0) {
$return = 4294967296+ $value ;
} else {
$return = $value ;
}
return $return ;
}