From e44c47cabda4068632cef84cfe1b494e8c7ac0d9 Mon Sep 17 00:00:00 2001 From: Adam Amstrong Date: Wed, 4 Apr 2012 18:10:45 +0000 Subject: [PATCH] Add support for memcached to poller for current bps/pps for ports and ability to disable writing these values to MySQL. HIGHLY RECOMMENDED FOR LARGE INSTALLS. Also PROCEED WITH CAUTION. git-svn-id: http://www.observium.org/svn/observer/trunk@2955 61d68cd4-352d-0410-923a-c4978735b2b8 --- config.php.default | 5 ++ includes/defaults.inc.php | 10 ++++ includes/polling/functions.inc.php | 2 +- includes/polling/ports.inc.php | 81 ++++++++++++++++++++++++------ includes/static-config.php | 11 ++-- 5 files changed, 87 insertions(+), 22 deletions(-) diff --git a/config.php.default b/config.php.default index 7a39c92037..ed7c838a45 100755 --- a/config.php.default +++ b/config.php.default @@ -8,6 +8,11 @@ $config['db_user'] = "USERNAME"; $config['db_pass'] = "PASSWORD"; $config['db_name'] = "observium"; +### Memcached config - We use this to store realtime usage +$config['memcached']['enable'] = FALSE; +$config['memcached']['host'] = "localhost"; +$config['memcached']['port'] = 11211; + ### Locations $config['install_dir'] = "/opt/observium"; $config['html_dir'] = $config['install_dir'] . "/html"; diff --git a/includes/defaults.inc.php b/includes/defaults.inc.php index 0420a18f36..8393a99b30 100644 --- a/includes/defaults.inc.php +++ b/includes/defaults.inc.php @@ -37,6 +37,16 @@ $config['unflatten'] = "/usr/bin/unflatten"; $config['neato'] = "/usr/bin/neato"; $config['sfdp'] = "/usr/bin/sfdp"; + +### Memcached - Keep immediate statistics + +$config['memcached']['enable'] = FALSE; +$config['memcached']['host'] = "localhost"; +$config['memcached']['port'] = 11211; + +$config['slow_statistics'] = TRUE; ### THIS WILL CHANGE TO FALSE IN FUTURE + + ### RRDCacheD - Make sure it can write to your RRD dir! #$config['rrdcached'] = "unix:/var/run/rrdcached.sock"; diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index 5b8c2b370e..f4e2687912 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -87,7 +87,7 @@ function poll_sensor($device, $class, $unit) function poll_device($device, $options) { - global $config, $device, $polled_devices, $db_stats; + global $config, $device, $polled_devices, $db_stats, $memcache; $attribs = get_dev_attribs($device['device_id']); diff --git a/includes/polling/ports.inc.php b/includes/polling/ports.inc.php index 1683745275..f6586af4f7 100755 --- a/includes/polling/ports.inc.php +++ b/includes/polling/ports.inc.php @@ -160,12 +160,29 @@ foreach ($ports as $port) $this_port = &$port_stats[$port['ifIndex']]; if ($device['os'] == "vmware" && preg_match("/Device ([a-z0-9]+) at .*/", $this_port['ifDescr'], $matches)) { $this_port['ifDescr'] = $matches[1]; } + + if($config['memcached']['enable']) + { + $port['poll_time'] = $memcache->get('port-'.$port['interface_id'].'-poll_time'); +# echo("time".$port['poll_time']); + } + + $polled_period = $polled - $port['poll_time']; $port['update'] = array(); - $port['update']['poll_time'] = $polled; - $port['update']['poll_prev'] = $port['poll_time']; - $port['update']['poll_period'] = $polled_period; + if($config['slow_statistics'] == TRUE) { + $port['update']['poll_time'] = $polled; + $port['update']['poll_prev'] = $port['poll_time']; + $port['update']['poll_period'] = $polled_period; + } + + if($config['memcached']['enable']) + { + $memcache->set('port-'.$port['interface_id'].'-poll_time', $polled); + $memcache->set('port-'.$port['interface_id'].'-poll_prev', $port['poll_time']); + $memcache->set('port-'.$port['interface_id'].'-poll_period', $polled_period); + } /// Copy ifHC[In|Out]Octets values to non-HC if they exist if ($this_port['ifHCInOctets'] > 0 && is_numeric($this_port['ifHCInOctets']) && $this_port['ifHCOutOctets'] > 0 && is_numeric($this_port['ifHCOutOctets'])) @@ -262,24 +279,56 @@ foreach ($ports as $port) /// Update IF-MIB metrics foreach ($stat_oids_db as $oid) { - $port['update'][$oid] = $this_port[$oid]; - $port['update'][$oid.'_prev'] = $port[$oid]; +# echo("\n".$oid.":"); + + if($config['slow_statistics'] == TRUE) { + $port['update'][$oid] = $this_port[$oid]; + $port['update'][$oid.'_prev'] = $port[$oid]; + } + + if($config['memcached']['enable']) + { + $port[$oid] = $memcache->get('port-'.$port['interface_id'].'-'.$oid); + $memcache->set('port-'.$port['interface_id'].'-'.$oid, $this_port[$oid]); + $memcache->set('port-'.$port['interface_id'].'-'.$oid.'_prev', $port[$oid]); +# echo("MEMCACHE!"); + } + +# echo(" old[".$port[$oid]."]"); +# echo(" new[".$this_port[$oid]."]"); + $oid_prev = $oid . "_prev"; - if ($port[$oid]) + if (isset($port[$oid])) { $oid_diff = $this_port[$oid] - $port[$oid]; +# echo(" diff[".$oid_diff."]"); $oid_rate = $oid_diff / $polled_period; +# echo(" rate[".$oid_rate."]"); if ($oid_rate < 0) { $oid_rate = "0"; echo("negative $oid"); } - $port['update'][$oid.'_rate'] = $oid_rate; - $port['update'][$oid.'_delta'] = $oid_diff; + $port['stats'][$oid.'_rate'] = $oid_rate; + $port['stats'][$oid.'_diff'] = $oid_diff; + + if($config['slow_statistics'] == TRUE) { + $port['update'][$oid.'_rate'] = $oid_rate; + $port['update'][$oid.'_delta'] = $oid_diff; + } + + if($config['memcached']['enable']) + { + $memcache->set('port-'.$port['interface_id'].'-'.$oid.'_rate', $oid_rate); + $memcache->set('port-'.$port['interface_id'].'-'.$oid.'_delta', $oid_diff); +# echo($oid."[".$oid_rate."]"); + } + if ($debug) {echo("\n $oid ($oid_diff B) $oid_rate Bps $polled_period secs\n"); } } } - $port['ifInBits_rate'] = $port['update']['ifInOctets_rate'] * 8; - $port['ifOutBits_rate'] = $port['update']['ifOutOctets_rate'] * 8; - echo('bits('.formatRates($port['ifInBits_rate']).'/'.formatRates($port['ifOutBits_rate']).')'); - echo('pkts('.format_si($port['update']['ifInUcastPkts_rate']).'pps/'.format_si($port['update']['ifOutUcastPkts_rate']).'pps)'); + $port['stats']['ifInBits_rate'] = $port['stats']['ifInOctets_rate'] * 8; + $port['stats']['ifOutBits_rate'] = $port['stats']['ifOutOctets_rate'] * 8; + echo('bps('.formatRates($port['stats']['ifInBits_rate']).'/'.formatRates($port['stats']['ifOutBits_rate']).')'); + echo('bytes('.formatStorage($port['stats']['ifInOctets_diff']).'/'.formatStorage($port['stats']['ifOutOctets_diff']).')'); + echo('pkts('.format_si($port['stats']['ifInUcastPkts_rate']).'pps/'.format_si($port['stats']['ifOutUcastPkts_rate']).'pps)'); ### Port utilisation % threshold alerting. ## Fixme allow setting threshold per-port. probably 90% of ports we don't care about. if($config['alerts']['port_util_alert']) @@ -287,11 +336,11 @@ foreach ($ports as $port) // Check for port saturation of $config['alerts']['port_util_perc'] or higher. Alert if we see this. // Check both inbound and outbound rates $saturation_threshold = $this_port['ifSpeed'] * ( $config['alerts']['port_util_perc'] / 100 ); - echo("IN: " . $port['ifInBits_rate'] . " OUT: " . $port['ifOutBits_rate'] . " THRESH: " . $saturation_threshold); - if (($port['ifInBits_rate'] >= $saturation_threshold || $port['ifOutBits_rate'] >= $saturation_threshold) && $saturation_threshold > 0) + echo("IN: " . $port['stats']['ifInBits_rate'] . " OUT: " . $port['stats']['ifOutBits_rate'] . " THRESH: " . $saturation_threshold); + if (($port['stats']['ifInBits_rate'] >= $saturation_threshold || $port['stats']['ifOutBits_rate'] >= $saturation_threshold) && $saturation_threshold > 0) { - log_event('Port reached saturation threshold: ' . formatRates($port['ifInBits_rate']) . '/' . formatRates($port['ifOutBits_rate']) . ' - ifspeed: ' . formatRates( $this_port['ifSpeed']) , $device, 'interface', $port['interface_id']); - notify($device, 'Port saturation threshold reached on ' . $device['hostname'] , 'Port saturation threshold alarm: ' . $device['hostname'] . ' on ' . $port['ifDescr'] . "\nRates:" . formatRates($port['ifInBits_rate']) . '/' . formatRates($port['ifOutBits_rate']) . ' - ifspeed: ' . formatRates( $this_port['ifSpeed']) . "\nTimestamp: " . date($config['timestamp_format'])); + log_event('Port reached saturation threshold: ' . formatRates($port['stats']['ifInBits_rate']) . '/' . formatRates($port['stats']['ifOutBits_rate']) . ' - ifspeed: ' . formatRates( $this_port['stats']['ifSpeed']) , $device, 'interface', $port['interface_id']); + notify($device, 'Port saturation threshold reached on ' . $device['hostname'] , 'Port saturation threshold alarm: ' . $device['hostname'] . ' on ' . $port['ifDescr'] . "\nRates:" . formatRates($port['stats']['ifInBits_rate']) . '/' . formatRates($port['stats']['ifOutBits_rate']) . ' - ifspeed: ' . formatRates( $this_port['ifSpeed']) . "\nTimestamp: " . date($config['timestamp_format'])); } } diff --git a/includes/static-config.php b/includes/static-config.php index 670b575416..5e1b6ccdfb 100644 --- a/includes/static-config.php +++ b/includes/static-config.php @@ -1125,11 +1125,12 @@ if (!$observium_link) } $observium_db = mysql_select_db($config['db_name'], $observium_link); -#try { -# $db = new PDO('mysql:host='.$config['db_host'].';dbname='.$config['db_name'], $config['db_user'], $config['db_pass']); -#} catch (PDOException $e) { -# print "Error!: " . $e->getMessage() . "
"; -#} +if($config['memcached']['enable']) +{ + $memcache = new Memcached(); + $memcache->addServer($config['memcached']['host'], $config['memcached']['port']); + if($debug) { print_r($memcache->getStats()); } +} # Set some times needed by loads of scripts (it's dynamic, so we do it here!)