| 
									
										
										
										
											2015-08-19 20:58:02 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * LibreNMS | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify it | 
					
						
							|  |  |  |  * under the terms of the GNU General Public License as published by the | 
					
						
							|  |  |  |  * Free Software Foundation, either version 3 of the License, or (at your | 
					
						
							|  |  |  |  * option) any later version.  Please see LICENSE.txt at the top level of | 
					
						
							|  |  |  |  * the source code distribution for details. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 02:46:26 -05:00
										 |  |  | if (isset($config['influxdb']['enable']) && $config['influxdb']['enable'] === true) { | 
					
						
							| 
									
										
										
										
											2015-08-19 20:58:02 +00:00
										 |  |  |     include $config['install_dir'].'/lib/influxdb-php/vendor/autoload.php'; | 
					
						
							|  |  |  | }//end if
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-28 12:32:58 -05:00
										 |  |  | function influxdb_connect() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-19 20:58:02 +00:00
										 |  |  |     global $config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $influxdb_cred = ''; | 
					
						
							|  |  |  |     if (!empty($config['influxdb']['username']) && !empty($config['influxdb']['password'])) { | 
					
						
							|  |  |  |         $influxdb_cred = $config['influxdb']['username'].':'.$config['influxdb']['password'].'@'; | 
					
						
							|  |  |  |         d_echo('Using authentication for InfluxDB'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-21 21:02:05 +00:00
										 |  |  |     $influxdb_url = $influxdb_cred.$config['influxdb']['host'].':'.$config['influxdb']['port'].'/'.$config['influxdb']['db']; | 
					
						
							| 
									
										
										
										
											2015-08-19 20:58:02 +00:00
										 |  |  |     d_echo($config['influxdb']['transport'] . " transport being used"); | 
					
						
							|  |  |  |     if ($config['influxdb']['transport'] == 'http') { | 
					
						
							|  |  |  |         $influxdb_conn = 'influxdb'; | 
					
						
							| 
									
										
										
										
											2016-08-28 12:32:58 -05:00
										 |  |  |     } elseif ($config['influxdb']['transport'] == 'https') { | 
					
						
							| 
									
										
										
										
											2016-04-01 17:53:56 +03:00
										 |  |  |         $influxdb_conn = 'https+influxdb'; | 
					
						
							| 
									
										
										
										
											2016-08-28 12:32:58 -05:00
										 |  |  |     } elseif ($config['influxdb']['transport'] == 'udp') { | 
					
						
							| 
									
										
										
										
											2015-08-19 20:58:02 +00:00
										 |  |  |         $influxdb_conn = 'udp+influxdb'; | 
					
						
							| 
									
										
										
										
											2016-08-28 12:32:58 -05:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2015-08-19 20:58:02 +00:00
										 |  |  |         echo 'InfluxDB support enabled but no valid transport details provided'; | 
					
						
							| 
									
										
										
										
											2015-12-18 11:03:00 +00:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2015-08-19 20:58:02 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-01 17:53:56 +03:00
										 |  |  |     $db = \InfluxDB\Client::fromDSN($influxdb_conn.'://'.$influxdb_url, $config['influxdb']['timeout'], $config['influxdb']['verifySSL']); | 
					
						
							| 
									
										
										
										
											2015-08-19 20:58:02 +00:00
										 |  |  |     return($db); | 
					
						
							|  |  |  | }// end influxdb_connect
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-28 12:32:58 -05:00
										 |  |  | function influx_update($device, $measurement, $tags, $fields) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:52:36 -05:00
										 |  |  |     global $influxdb, $config; | 
					
						
							| 
									
										
										
										
											2015-12-18 11:15:56 +00:00
										 |  |  |     if ($influxdb !== false) { | 
					
						
							|  |  |  |         $tmp_fields = array(); | 
					
						
							|  |  |  |         $tmp_tags['hostname'] = $device['hostname']; | 
					
						
							|  |  |  |         foreach ($tags as $k => $v) { | 
					
						
							| 
									
										
										
										
											2016-08-28 12:32:58 -05:00
										 |  |  |             $v = preg_replace(array('/ /','/,/','/=/'), array('\ ','\,','\='), $v); | 
					
						
							| 
									
										
										
										
											2015-12-23 15:12:09 +00:00
										 |  |  |             if (empty($v)) { | 
					
						
							|  |  |  |                 $v = '_blank_'; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-12-18 11:15:56 +00:00
										 |  |  |             $tmp_tags[$k] = $v; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         foreach ($fields as $k => $v) { | 
					
						
							| 
									
										
										
										
											2016-04-12 10:01:20 +01:00
										 |  |  |             $tmp_fields[$k] = force_influx_data($v); | 
					
						
							| 
									
										
										
										
											2016-08-28 12:32:58 -05:00
										 |  |  |             if ($tmp_fields[$k] === null) { | 
					
						
							|  |  |  |                 unset($tmp_fields[$k]); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-12-18 11:15:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-08-19 20:58:02 +00:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2015-12-18 11:15:56 +00:00
										 |  |  |         d_echo("\nInfluxDB data:\n"); | 
					
						
							|  |  |  |         d_echo($measurement); | 
					
						
							|  |  |  |         d_echo($tmp_tags); | 
					
						
							|  |  |  |         d_echo($tmp_fields); | 
					
						
							|  |  |  |         d_echo("\nEND\n"); | 
					
						
							| 
									
										
										
										
											2015-08-19 20:58:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-18 11:15:56 +00:00
										 |  |  |         if ($config['noinfluxdb'] !== true) { | 
					
						
							|  |  |  |             $points = array( | 
					
						
							|  |  |  |                 new InfluxDB\Point( | 
					
						
							|  |  |  |                     $measurement, | 
					
						
							|  |  |  |                     null, // the measurement value
 | 
					
						
							|  |  |  |                     $tmp_tags, | 
					
						
							|  |  |  |                     $tmp_fields // optional additional fields
 | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2016-04-12 10:01:20 +01:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 $result = $influxdb->writePoints($points); | 
					
						
							|  |  |  |             } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2016-08-26 09:37:00 -05:00
										 |  |  |                 d_echo("Caught exception: " . $e->getMessage() . PHP_EOL); | 
					
						
							| 
									
										
										
										
											2016-04-12 10:01:20 +01:00
										 |  |  |                 d_echo($e->getTrace()); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-08-28 12:32:58 -05:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:52:36 -05:00
										 |  |  |             c_echo("[%gInfluxDB Disabled%n]\n"); | 
					
						
							| 
									
										
										
										
											2015-12-18 11:15:56 +00:00
										 |  |  |         }//end if
 | 
					
						
							| 
									
										
										
										
											2015-08-19 20:58:02 +00:00
										 |  |  |     }//end if
 | 
					
						
							|  |  |  | }// end influx_update
 |