mirror of
https://github.com/stylersnico/librenms.git
synced 2026-07-28 08:02:41 +02:00
Merge pull request #2007 from QuxLabs/qux-issue-8
Added working memcached support
This commit is contained in:
+30
-10
@@ -238,8 +238,15 @@ function dbDelete($table, $where=null, $parameters=array()) {
|
||||
* */
|
||||
|
||||
|
||||
function dbFetchRows($sql, $parameters=array()) {
|
||||
global $db_stats;
|
||||
function dbFetchRows($sql, $parameters=array(), $nocache=false) {
|
||||
global $db_stats, $config;
|
||||
|
||||
if ($config['memcached']['enable'] && $nocache === false) {
|
||||
$result = $config['memcached']['resource']->get(hash('sha512',$sql.'|'.serialize($parameters)));
|
||||
if (!empty($result)) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
$time_start = microtime(true);
|
||||
$result = dbQuery($sql, $parameters);
|
||||
@@ -251,6 +258,9 @@ function dbFetchRows($sql, $parameters=array()) {
|
||||
}
|
||||
|
||||
mysql_free_result($result);
|
||||
if ($config['memcached']['enable'] && $nocache === false) {
|
||||
$config['memcached']['resource']->set(hash('sha512',$sql.'|'.serialize($parameters)),$rows,$config['memcached']['ttl']);
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
@@ -273,8 +283,8 @@ function dbFetchRows($sql, $parameters=array()) {
|
||||
* */
|
||||
|
||||
|
||||
function dbFetch($sql, $parameters=array()) {
|
||||
return dbFetchRows($sql, $parameters);
|
||||
function dbFetch($sql, $parameters=array(), $nocache=false) {
|
||||
return dbFetchRows($sql, $parameters, $nocache);
|
||||
/*
|
||||
// for now, don't do the iterator thing
|
||||
$result = dbQuery($sql, $parameters);
|
||||
@@ -295,8 +305,15 @@ function dbFetch($sql, $parameters=array()) {
|
||||
* */
|
||||
|
||||
|
||||
function dbFetchRow($sql=null, $parameters=array()) {
|
||||
global $db_stats;
|
||||
function dbFetchRow($sql=null, $parameters=array(), $nocache=false) {
|
||||
global $db_stats, $config;
|
||||
|
||||
if ($config['memcached']['enable'] && $nocache === false) {
|
||||
$result = $config['memcached']['resource']->get(hash('sha512',$sql.'|'.serialize($parameters)));
|
||||
if (!empty($result)) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
$time_start = microtime(true);
|
||||
$result = dbQuery($sql, $parameters);
|
||||
@@ -308,6 +325,9 @@ function dbFetchRow($sql=null, $parameters=array()) {
|
||||
$db_stats['fetchrow_sec'] += number_format(($time_end - $time_start), 8);
|
||||
$db_stats['fetchrow']++;
|
||||
|
||||
if ($config['memcached']['enable'] && $nocache === false) {
|
||||
$config['memcached']['resource']->set(hash('sha512',$sql.'|'.serialize($parameters)),$row,$config['memcached']['ttl']);
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
else {
|
||||
@@ -324,10 +344,10 @@ function dbFetchRow($sql=null, $parameters=array()) {
|
||||
* */
|
||||
|
||||
|
||||
function dbFetchCell($sql, $parameters=array()) {
|
||||
function dbFetchCell($sql, $parameters=array(), $nocache=false) {
|
||||
global $db_stats;
|
||||
$time_start = microtime(true);
|
||||
$row = dbFetchRow($sql, $parameters);
|
||||
$row = dbFetchRow($sql, $parameters, $nocache);
|
||||
if ($row) {
|
||||
return array_shift($row);
|
||||
// shift first field off first row
|
||||
@@ -349,11 +369,11 @@ function dbFetchCell($sql, $parameters=array()) {
|
||||
* */
|
||||
|
||||
|
||||
function dbFetchColumn($sql, $parameters=array()) {
|
||||
function dbFetchColumn($sql, $parameters=array(), $nocache=false) {
|
||||
global $db_stats;
|
||||
$time_start = microtime(true);
|
||||
$cells = array();
|
||||
foreach (dbFetch($sql, $parameters) as $row) {
|
||||
foreach (dbFetch($sql, $parameters, $nocache) as $row) {
|
||||
$cells[] = array_shift($row);
|
||||
}
|
||||
|
||||
|
||||
@@ -239,8 +239,15 @@ function dbDelete($table, $where=null, $parameters=array()) {
|
||||
* */
|
||||
|
||||
|
||||
function dbFetchRows($sql, $parameters=array()) {
|
||||
global $db_stats;
|
||||
function dbFetchRows($sql, $parameters=array(), $nocache=false) {
|
||||
global $db_stats, $config;
|
||||
|
||||
if ($config['memcached']['enable'] && $nocache === false) {
|
||||
$result = $config['memcached']['resource']->get(hash('sha512',$sql.'|'.serialize($parameters)));
|
||||
if (!empty($result)) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
$time_start = microtime(true);
|
||||
$result = dbQuery($sql, $parameters);
|
||||
@@ -252,6 +259,9 @@ function dbFetchRows($sql, $parameters=array()) {
|
||||
}
|
||||
|
||||
mysqli_free_result($result);
|
||||
if ($config['memcached']['enable'] && $nocache === false) {
|
||||
$config['memcached']['resource']->set(hash('sha512',$sql.'|'.serialize($parameters)),$rows,$config['memcached']['ttl']);
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
@@ -274,8 +284,8 @@ function dbFetchRows($sql, $parameters=array()) {
|
||||
* */
|
||||
|
||||
|
||||
function dbFetch($sql, $parameters=array()) {
|
||||
return dbFetchRows($sql, $parameters);
|
||||
function dbFetch($sql, $parameters=array(), $nocache=false) {
|
||||
return dbFetchRows($sql, $parameters, $nocache);
|
||||
/*
|
||||
// for now, don't do the iterator thing
|
||||
$result = dbQuery($sql, $parameters);
|
||||
@@ -296,8 +306,15 @@ function dbFetch($sql, $parameters=array()) {
|
||||
* */
|
||||
|
||||
|
||||
function dbFetchRow($sql=null, $parameters=array()) {
|
||||
global $db_stats;
|
||||
function dbFetchRow($sql=null, $parameters=array(), $nocache=false) {
|
||||
global $db_stats, $config;
|
||||
|
||||
if ($config['memcached']['enable'] && $nocache === false) {
|
||||
$result = $config['memcached']['resource']->get(hash('sha512',$sql.'|'.serialize($parameters)));
|
||||
if (!empty($result)) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
$time_start = microtime(true);
|
||||
$result = dbQuery($sql, $parameters);
|
||||
@@ -309,6 +326,9 @@ function dbFetchRow($sql=null, $parameters=array()) {
|
||||
$db_stats['fetchrow_sec'] += number_format(($time_end - $time_start), 8);
|
||||
$db_stats['fetchrow']++;
|
||||
|
||||
if ($config['memcached']['enable'] && $nocache === false) {
|
||||
$config['memcached']['resource']->set(hash('sha512',$sql.'|'.serialize($parameters)),$row,$config['memcached']['ttl']);
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
else {
|
||||
@@ -325,10 +345,11 @@ function dbFetchRow($sql=null, $parameters=array()) {
|
||||
* */
|
||||
|
||||
|
||||
function dbFetchCell($sql, $parameters=array()) {
|
||||
global $db_stats;
|
||||
function dbFetchCell($sql, $parameters=array(), $nocache=false) {
|
||||
global $db_stats, $config;
|
||||
|
||||
$time_start = microtime(true);
|
||||
$row = dbFetchRow($sql, $parameters);
|
||||
$row = dbFetchRow($sql, $parameters, $nocache);
|
||||
if ($row) {
|
||||
return array_shift($row);
|
||||
// shift first field off first row
|
||||
@@ -350,11 +371,11 @@ function dbFetchCell($sql, $parameters=array()) {
|
||||
* */
|
||||
|
||||
|
||||
function dbFetchColumn($sql, $parameters=array()) {
|
||||
function dbFetchColumn($sql, $parameters=array(), $nocache=false) {
|
||||
global $db_stats;
|
||||
$time_start = microtime(true);
|
||||
$cells = array();
|
||||
foreach (dbFetch($sql, $parameters) as $row) {
|
||||
foreach (dbFetch($sql, $parameters, $nocache) as $row) {
|
||||
$cells[] = array_shift($row);
|
||||
}
|
||||
|
||||
@@ -375,9 +396,9 @@ function dbFetchColumn($sql, $parameters=array()) {
|
||||
*/
|
||||
|
||||
|
||||
function dbFetchKeyValue($sql, $parameters=array()) {
|
||||
function dbFetchKeyValue($sql, $parameters=array(), $nocache=false) {
|
||||
$data = array();
|
||||
foreach (dbFetch($sql, $parameters) as $row) {
|
||||
foreach (dbFetch($sql, $parameters, $nocache) as $row) {
|
||||
$key = array_shift($row);
|
||||
if (sizeof($row) == 1) {
|
||||
// if there were only 2 fields in the result
|
||||
|
||||
@@ -75,6 +75,7 @@ $config['sfdp'] = '/usr/bin/sfdp';
|
||||
$config['memcached']['enable'] = false;
|
||||
$config['memcached']['host'] = 'localhost';
|
||||
$config['memcached']['port'] = 11211;
|
||||
$config['memcached']['ttl'] = 240;
|
||||
|
||||
$config['slow_statistics'] = true;
|
||||
// THIS WILL CHANGE TO FALSE IN FUTURE
|
||||
|
||||
@@ -30,6 +30,19 @@ else {
|
||||
$database_db = mysql_select_db($config['db_name'], $database_link);
|
||||
}
|
||||
|
||||
if ($config['memcached']['enable'] === true) {
|
||||
if (class_exists('Memcached')) {
|
||||
$config['memcached']['ttl'] = 60;
|
||||
$config['memcached']['resource'] = new Memcached();
|
||||
$config['memcached']['resource']->addServer($config['memcached']['host'], $config['memcached']['port']);
|
||||
}
|
||||
else {
|
||||
echo "WARNING: You have enabled memcached but have not installed the PHP bindings. Disabling memcached support.\n";
|
||||
echo "Try 'apt-get install php5-memcached' or 'pecl install memcached'. You will need the php5-dev and libmemcached-dev packages to use pecl.\n\n";
|
||||
$config['memcached']['enable'] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
$clone = $config;
|
||||
foreach (dbFetchRows('select config_name,config_value from config') as $obj) {
|
||||
$clone = array_replace_recursive($clone, mergecnf($obj));
|
||||
@@ -1703,19 +1716,6 @@ if (isset($_SERVER['HTTPS'])) {
|
||||
$config['base_url'] = preg_replace('/^http:/', 'https:', $config['base_url']);
|
||||
}
|
||||
|
||||
if ($config['memcached']['enable'] === true) {
|
||||
if (class_exists('Memcached')) {
|
||||
$memcache = new Memcached();
|
||||
$memcache->addServer($config['memcached']['host'], $config['memcached']['port']);
|
||||
$memcache->getStats();
|
||||
}
|
||||
else {
|
||||
echo "WARNING: You have enabled memcached but have not installed the PHP bindings. Disabling memcached support.\n";
|
||||
echo "Try 'apt-get install php5-memcached' or 'pecl install memcached'. You will need the php5-dev and libmemcached-dev packages to use pecl.\n\n";
|
||||
$config['memcached']['enable'] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Set some times needed by loads of scripts (it's dynamic, so we do it here!)
|
||||
$config['time']['now'] = time();
|
||||
$config['time']['now'] -= ($config['time']['now'] % 300);
|
||||
|
||||
@@ -119,12 +119,7 @@ function poll_sensor($device, $class, $unit) {
|
||||
log_event(ucfirst($class).' '.$sensor['sensor_descr'].' above threshold: '.$sensor_value." $unit (> ".$sensor['sensor_limit']." $unit)", $device, $class, $sensor['sensor_id']);
|
||||
}
|
||||
|
||||
if ($config['memcached']['enable'] === true) {
|
||||
$memcache->set('sensor-'.$sensor['sensor_id'].'-value', $sensor_value);
|
||||
}
|
||||
else {
|
||||
dbUpdate(array('sensor_current' => $sensor_value), 'sensors', '`sensor_class` = ? AND `sensor_id` = ?', array($class, $sensor['sensor_id']));
|
||||
}
|
||||
dbUpdate(array('sensor_current' => $sensor_value), 'sensors', '`sensor_class` = ? AND `sensor_id` = ?', array($class, $sensor['sensor_id']));
|
||||
}//end foreach
|
||||
|
||||
}//end poll_sensor()
|
||||
|
||||
@@ -47,14 +47,7 @@ foreach (dbFetchRows('SELECT * FROM mempools WHERE device_id = ?', array($device
|
||||
$mempool['state']['mempool_lowestfree'] = $mempool['lowestfree'];
|
||||
}
|
||||
|
||||
if ($config['memcached']['enable'] === true) {
|
||||
d_echo($mempool['state']);
|
||||
|
||||
$memcache->set('mempool-'.$mempool['mempool_id'].'-value', $mempool['state']);
|
||||
}
|
||||
else {
|
||||
dbUpdate($mempool['state'], 'mempools', '`mempool_id` = ?', array($mempool['mempool_id']));
|
||||
}
|
||||
dbUpdate($mempool['state'], 'mempools', '`mempool_id` = ?', array($mempool['mempool_id']));
|
||||
|
||||
echo "\n";
|
||||
}//end foreach
|
||||
|
||||
@@ -244,17 +244,6 @@ foreach ($ports as $port) {
|
||||
$this_port['ifDescr'] = $matches[1];
|
||||
}
|
||||
|
||||
if ($config['memcached']['enable'] === true) {
|
||||
$state = $memcache->get('port-'.$port['port_id'].'-state');
|
||||
d_echo($state);
|
||||
|
||||
if (is_array($state)) {
|
||||
$port = array_merge($port, $state);
|
||||
}
|
||||
|
||||
unset($state);
|
||||
}
|
||||
|
||||
$polled_period = ($polled - $port['poll_time']);
|
||||
|
||||
$port['update'] = array();
|
||||
@@ -266,12 +255,6 @@ foreach ($ports as $port) {
|
||||
$port['update']['poll_period'] = $polled_period;
|
||||
}
|
||||
|
||||
if ($config['memcached']['enable'] === true) {
|
||||
$port['state']['poll_time'] = $polled;
|
||||
$port['state']['poll_prev'] = $port['poll_time'];
|
||||
$port['state']['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'])) {
|
||||
echo 'HC ';
|
||||
@@ -400,11 +383,6 @@ foreach ($ports as $port) {
|
||||
$port['update'][$oid.'_prev'] = $port[$oid];
|
||||
}
|
||||
|
||||
if ($config['memcached']['enable'] === true) {
|
||||
$port['state'][$oid] = $this_port[$oid];
|
||||
$port['state'][$oid.'_prev'] = $port[$oid];
|
||||
}
|
||||
|
||||
$oid_prev = $oid.'_prev';
|
||||
if (isset($port[$oid])) {
|
||||
$oid_diff = ($this_port[$oid] - $port[$oid]);
|
||||
@@ -422,11 +400,6 @@ foreach ($ports as $port) {
|
||||
$port['update'][$oid.'_delta'] = $oid_diff;
|
||||
}
|
||||
|
||||
if ($config['memcached']['enable'] === true) {
|
||||
$port['state'][$oid.'_rate'] = $oid_rate;
|
||||
$port['state'][$oid.'_delta'] = $oid_diff;
|
||||
}
|
||||
|
||||
d_echo("\n $oid ($oid_diff B) $oid_rate Bps $polled_period secs\n");
|
||||
}//end if
|
||||
}//end foreach
|
||||
@@ -451,13 +424,6 @@ foreach ($ports as $port) {
|
||||
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)';
|
||||
|
||||
// Store aggregate in/out state
|
||||
if ($config['memcached']['enable'] === true) {
|
||||
$port['state']['ifOctets_rate'] = ($port['stats']['ifOutOctets_rate'] + $port['stats']['ifInOctets_rate']);
|
||||
$port['state']['ifUcastPkts_rate'] = ($port['stats']['ifOutUcastPkts_rate'] + $port['stats']['ifInUcastPkts_rate']);
|
||||
$port['state']['ifErrors_rate'] = ($port['stats']['ifOutErrors_rate'] + $port['stats']['ifInErrors_rate']);
|
||||
}
|
||||
|
||||
// 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'] && $port['ignore'] == '0') {
|
||||
// Check for port saturation of $config['alerts']['port_util_perc'] or higher. Alert if we see this.
|
||||
@@ -547,13 +513,6 @@ foreach ($ports as $port) {
|
||||
include 'port-alcatel.inc.php';
|
||||
}
|
||||
|
||||
// Update Memcached
|
||||
if ($config['memcached']['enable'] === true) {
|
||||
d_echo($port['state']);
|
||||
|
||||
$memcache->set('port-'.$port['port_id'].'-state', $port['state']);
|
||||
}
|
||||
|
||||
foreach ($port['update'] as $key => $val_check) {
|
||||
if (!isset($val_check)) {
|
||||
unset($port['update'][$key]);
|
||||
|
||||
@@ -37,16 +37,7 @@ foreach (dbFetchRows('SELECT * FROM storage WHERE device_id = ?', array($device[
|
||||
|
||||
rrdtool_update($storage_rrd, $fields);
|
||||
|
||||
if ($config['memcached']['enable'] === true) {
|
||||
$memcache->set('storage-'.$storage['storage_id'].'-used', $storage['used']);
|
||||
$memcache->set('storage-'.$storage['storage_id'].'-free', $storage['free']);
|
||||
$memcache->set('storage-'.$storage['storage_id'].'-size', $storage['size']);
|
||||
$memcache->set('storage-'.$storage['storage_id'].'-units', $storage['units']);
|
||||
$memcache->set('storage-'.$storage['storage_id'].'-perc', $percent);
|
||||
}
|
||||
else {
|
||||
$update = dbUpdate(array('storage_used' => $storage['used'], 'storage_free' => $storage['free'], 'storage_size' => $storage['size'], 'storage_units' => $storage['units'], 'storage_perc' => $percent), 'storage', '`storage_id` = ?', array($storage['storage_id']));
|
||||
}
|
||||
$update = dbUpdate(array('storage_used' => $storage['used'], 'storage_free' => $storage['free'], 'storage_size' => $storage['size'], 'storage_units' => $storage['units'], 'storage_perc' => $percent), 'storage', '`storage_id` = ?', array($storage['storage_id']));
|
||||
|
||||
echo "\n";
|
||||
}//end foreach
|
||||
|
||||
Reference in New Issue
Block a user