From 62f88a4ec49c737ce3138553b20983eddf8b28e2 Mon Sep 17 00:00:00 2001 From: laf Date: Mon, 17 Aug 2015 14:31:21 +0000 Subject: [PATCH] More updates --- includes/functions.php | 6 +++++ includes/polling/system.inc.php | 3 +-- includes/rrdtool.inc.php | 39 +++++++++++++++++++++++++-------- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 13498595b..b96c25495 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1238,3 +1238,9 @@ function fping($host,$params) { function function_check($function) { return function_exists($function); } + +function force_influx_data($type,$data) { + if ($type == 'f' || $type == 'float') { + return(sprintf("%.1f",$data)); + } +}// end force_influx_data diff --git a/includes/polling/system.inc.php b/includes/polling/system.inc.php index fa706e701..181318f12 100644 --- a/includes/polling/system.inc.php +++ b/includes/polling/system.inc.php @@ -75,8 +75,7 @@ if (is_numeric($uptime)) { } $measurement = 'uptime'; - echo "BOOM " . sprintf("%.1f",$uptime) . " YAH\n"; - $fields = array('uptime' => $uptime); + $fields = array('uptime' => force_influx_data('f',$uptime)); influx_update($device,$measurement,$tags,$fields); rrdtool_update($uptime_rrd, 'N:'.$uptime); diff --git a/includes/rrdtool.inc.php b/includes/rrdtool.inc.php index d2be944a2..2a0fec55c 100644 --- a/includes/rrdtool.inc.php +++ b/includes/rrdtool.inc.php @@ -304,9 +304,34 @@ function rrdtool_escape($string, $maxlength=null){ } +function influx_connect() { + global $config; + if ($config['influxdb']['transport'] == 'http') { + + $influx_url = $config['influxdb']['url'] . '/write?db=' . $config['influxdb']['db']; + + $ch = curl_init($influx_url); + + if (!empty($config['influxdb']['username']) && !empty($config['influxdb']['password'])) { + curl_setopt($ch, CURLOPT_USERPWD, $config['influxdb']['username'] . ":" . $config['influxdb']['password']); + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + } + curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); + return($ch); + } +}// end influx_connect + +function influx_write($ch,$data) { + global $config; + if ($config['influxdb']['transport'] == 'http') { + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + return(curl_exec($ch)); + } +}// end influx_write + function influx_update($device,$measurement,$tags=array(),$fields) { $tmp_fields = array(); - $tmp_tags[] = "host=".$device['hostname']; + $tmp_tags[] = "hostname=".$device['hostname']; foreach ($tags as $k => $v) { $tmp_tags[] = "$k=$v"; } @@ -317,13 +342,9 @@ function influx_update($device,$measurement,$tags=array(),$fields) { $tags = implode(',', $tmp_tags); $fields = implode(',', $tmp_fields); - $influx_url = 'http://localhost:8086/write?db=librenms'; + $influx_conn = influx_connect(); - $ch = curl_init($influx_url); - $post = "$measurement,$tags $fields"; - curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); - curl_setopt($ch, CURLOPT_POSTFIELDS, $post); - $response = curl_exec($ch); - echo "YEAH $response END\n"; + $data = "$measurement,$tags $fields"; + $response = influx_write($influx_conn,$data); d_echo($response); -} +}// end influx_update