From a988e83df77c434b04be98a2cdde1fc15dc30be1 Mon Sep 17 00:00:00 2001 From: David Bell Date: Tue, 12 Apr 2016 10:01:20 +0100 Subject: [PATCH 1/4] Version 2 of sending the correct data to influx. Also catching the exception that would stop updates working. --- includes/functions.php | 24 +++++++++++++++++++++--- includes/influxdb.inc.php | 12 ++++++++++-- includes/polling/netstats-snmp.inc.php | 2 +- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index b9da4a670..66985ecd9 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1252,10 +1252,28 @@ function function_check($function) { return function_exists($function); } -function force_influx_data($type,$data) { - if ($type == 'f' || $type == 'float') { - return(sprintf("%.1f",$data)); +function force_influx_data($data) { + /* + * It is not trivial to detect if something is a float or an integer, and + * therefore may cause breakages on inserts. + * Just setting every number to a float gets around this, but may introduce + * inefficiencies. + * I've left the detection statement in there for a possible change in future, + * but currently everything just gets set to a float. + */ + + if (is_numeric($data)) { + // If it is an Integer + if (ctype_digit($data)) { + return floatval($data); + // Else it is a float + } else { + return floatval($data); + } + } else { + return $data; } + }// end force_influx_data /** diff --git a/includes/influxdb.inc.php b/includes/influxdb.inc.php index 80785f100..f0fa6bdf8 100644 --- a/includes/influxdb.inc.php +++ b/includes/influxdb.inc.php @@ -58,7 +58,10 @@ function influx_update($device,$measurement,$tags=array(),$fields) { $tmp_tags[$k] = $v; } foreach ($fields as $k => $v) { - $tmp_fields[$k] = force_influx_data('f',$v); + $tmp_fields[$k] = force_influx_data($v); + if( $tmp_fields[$k] === null) { + unset($tmp_fields[$k]); + } } d_echo("\nInfluxDB data:\n"); @@ -76,7 +79,12 @@ function influx_update($device,$measurement,$tags=array(),$fields) { $tmp_fields // optional additional fields ) ); - $result = $influxdb->writePoints($points); + try { + $result = $influxdb->writePoints($points); + } catch (Exception $e) { + d_echo("Caught exception: ", $e->getMessage(), "\n"); + d_echo($e->getTrace()); + } } else { print $console_color->convert('[%gInfluxDB Disabled%n] ', false); diff --git a/includes/polling/netstats-snmp.inc.php b/includes/polling/netstats-snmp.inc.php index 776f79457..6732de393 100644 --- a/includes/polling/netstats-snmp.inc.php +++ b/includes/polling/netstats-snmp.inc.php @@ -54,7 +54,7 @@ if ($device['os'] != 'Snom') { $value = $data_array[0][$oid]; } else { - $value = 'U'; + $value = null; } $fields[$oid] = $value; } From f9fda59abd887f9e0c7eba89e59bab234dca210d Mon Sep 17 00:00:00 2001 From: David Bell Date: Tue, 19 Apr 2016 11:45:03 +0100 Subject: [PATCH 2/4] Corrected formatting. --- includes/functions.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/functions.php b/includes/functions.php index 66985ecd9..c0271e67e 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1267,7 +1267,8 @@ function force_influx_data($data) { if (ctype_digit($data)) { return floatval($data); // Else it is a float - } else { + } + else { return floatval($data); } } else { From 933e163222d527c6a9e9e869b22b0307903b4762 Mon Sep 17 00:00:00 2001 From: David Bell Date: Tue, 19 Apr 2016 11:53:06 +0100 Subject: [PATCH 3/4] Corrected more formatting. I don't like your coding style! :) --- includes/functions.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/functions.php b/includes/functions.php index c0271e67e..d7597c8ae 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1271,7 +1271,8 @@ function force_influx_data($data) { else { return floatval($data); } - } else { + } + else { return $data; } From 761f55b8db2ebefeaafea8ba1b6b8959fbef22b7 Mon Sep 17 00:00:00 2001 From: David Bell Date: Tue, 19 Apr 2016 16:04:24 +0100 Subject: [PATCH 4/4] Removed large unrequired loop. An example output that shows this is still working: ... snmpOutTraps.0 = 0 snmpEnableAuthenTraps.0 = enabled snmpSilentDrops.0 = 0 snmpProxyDrops.0 = 0 RRD Disabled RRD update /opt/librenms/rrd/xxx/netstats-snmp.rrd N:173688066:172651761:0:50736:0:0:0:0:0:0:0:210621835:0:167870188:2601931:0:0:0:4:74625425:0:0:0:0:0:172651443:0:U:0:0 ... (Note the U 3rd from the end) --- includes/polling/netstats-snmp.inc.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/includes/polling/netstats-snmp.inc.php b/includes/polling/netstats-snmp.inc.php index 6732de393..c1d6b461c 100644 --- a/includes/polling/netstats-snmp.inc.php +++ b/includes/polling/netstats-snmp.inc.php @@ -48,16 +48,7 @@ if ($device['os'] != 'Snom') { $data_array = snmpwalk_cache_oid($device, 'snmp', array(), 'SNMPv2-MIB'); - $fields = array(); - foreach ($oids as $oid) { - if (is_numeric($data_array[0][$oid])) { - $value = $data_array[0][$oid]; - } - else { - $value = null; - } - $fields[$oid] = $value; - } + $fields = $data_array[0]; if (isset($data_array[0]['snmpInPkts']) && isset($data_array[0]['snmpOutPkts'])) { if (!file_exists($rrd_file)) {