1 | <?php |
||
2 | /** |
||
3 | * InfluxDB.php |
||
4 | * |
||
5 | * -Description- |
||
6 | * |
||
7 | * This program is free software: you can redistribute it and/or modify |
||
8 | * it under the terms of the GNU General Public License as published by |
||
9 | * the Free Software Foundation, either version 3 of the License, or |
||
10 | * (at your option) any later version. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
15 | * GNU General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU General Public License |
||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
19 | * |
||
20 | * @link https://www.librenms.org |
||
21 | * |
||
22 | * @copyright 2020 Tony Murray |
||
23 | * @copyright 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa> |
||
24 | * @author Tony Murray <[email protected]> |
||
25 | */ |
||
26 | |||
27 | namespace LibreNMS\Data\Store; |
||
28 | |||
29 | use InfluxDB\Client; |
||
30 | use InfluxDB\Driver\UDP; |
||
31 | use LibreNMS\Config; |
||
32 | use LibreNMS\Data\Measure\Measurement; |
||
33 | use Log; |
||
34 | |||
35 | class InfluxDB extends BaseDatastore |
||
36 | { |
||
37 | /** @var \InfluxDB\Database */ |
||
38 | private $connection; |
||
39 | |||
40 | public function __construct(\InfluxDB\Database $influx) |
||
41 | { |
||
42 | parent::__construct(); |
||
43 | $this->connection = $influx; |
||
44 | |||
45 | // if the database doesn't exist, create it. |
||
46 | try { |
||
47 | if (! $influx->exists()) { |
||
48 | $influx->create(); |
||
49 | } |
||
50 | } catch (\Exception $e) { |
||
51 | Log::warning('InfluxDB: Could not create database'); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | public function getName() |
||
56 | { |
||
57 | return 'InfluxDB'; |
||
58 | } |
||
59 | |||
60 | public static function isEnabled() |
||
61 | { |
||
62 | return Config::get('influxdb.enable', false); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Datastore-independent function which should be used for all polled metrics. |
||
67 | * |
||
68 | * RRD Tags: |
||
69 | * rrd_def RrdDefinition |
||
70 | * rrd_name array|string: the rrd filename, will be processed with rrd_name() |
||
71 | * rrd_oldname array|string: old rrd filename to rename, will be processed with rrd_name() |
||
72 | * rrd_step int: rrd step, defaults to 300 |
||
73 | * |
||
74 | * @param array $device |
||
75 | * @param string $measurement Name of this measurement |
||
76 | * @param array $tags tags for the data (or to control rrdtool) |
||
77 | * @param array|mixed $fields The data to update in an associative array, the order must be consistent with rrd_def, |
||
78 | * single values are allowed and will be paired with $measurement |
||
79 | */ |
||
80 | public function put($device, $measurement, $tags, $fields) |
||
81 | { |
||
82 | $stat = Measurement::start('write'); |
||
83 | $tmp_fields = []; |
||
84 | $tmp_tags['hostname'] = $device['hostname']; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
85 | foreach ($tags as $k => $v) { |
||
86 | if (empty($v)) { |
||
87 | $v = '_blank_'; |
||
88 | } |
||
89 | $tmp_tags[$k] = $v; |
||
90 | } |
||
91 | foreach ($fields as $k => $v) { |
||
92 | if ($k == 'time') { |
||
93 | $k = 'rtime'; |
||
94 | } |
||
95 | |||
96 | if (($value = $this->forceType($v)) !== null) { |
||
97 | $tmp_fields[$k] = $value; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | if (empty($tmp_fields)) { |
||
102 | Log::warning('All fields empty, skipping update', ['orig_fields' => $fields]); |
||
103 | |||
104 | return; |
||
105 | } |
||
106 | |||
107 | Log::debug('InfluxDB data: ', [ |
||
108 | 'measurement' => $measurement, |
||
109 | 'tags' => $tmp_tags, |
||
110 | 'fields' => $tmp_fields, |
||
111 | ]); |
||
112 | |||
113 | try { |
||
114 | $points = [ |
||
115 | new \InfluxDB\Point( |
||
116 | $measurement, |
||
117 | null, // the measurement value |
||
118 | $tmp_tags, |
||
119 | $tmp_fields // optional additional fields |
||
120 | ), |
||
121 | ]; |
||
122 | |||
123 | $this->connection->writePoints($points); |
||
124 | $this->recordStatistic($stat->end()); |
||
125 | } catch (\InfluxDB\Exception $e) { |
||
126 | Log::error('InfluxDB exception: ' . $e->getMessage()); |
||
127 | Log::debug($e->getTraceAsString()); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Create a new client and select the database |
||
133 | * |
||
134 | * @return \InfluxDB\Database |
||
135 | */ |
||
136 | public static function createFromConfig() |
||
137 | { |
||
138 | $host = Config::get('influxdb.host', 'localhost'); |
||
139 | $transport = Config::get('influxdb.transport', 'http'); |
||
140 | $port = Config::get('influxdb.port', 8086); |
||
141 | $db = Config::get('influxdb.db', 'librenms'); |
||
142 | $username = Config::get('influxdb.username', ''); |
||
143 | $password = Config::get('influxdb.password', ''); |
||
144 | $timeout = Config::get('influxdb.timeout', 0); |
||
145 | $verify_ssl = Config::get('influxdb.verifySSL', false); |
||
146 | |||
147 | $client = new Client($host, $port, $username, $password, $transport == 'https', $verify_ssl, $timeout, $timeout); |
||
148 | |||
149 | if ($transport == 'udp') { |
||
150 | $client->setDriver(new UDP($host, $port)); |
||
151 | } |
||
152 | |||
153 | return $client->selectDB($db); |
||
154 | } |
||
155 | |||
156 | private function forceType($data) |
||
157 | { |
||
158 | /* |
||
159 | * It is not trivial to detect if something is a float or an integer, and |
||
160 | * therefore may cause breakages on inserts. |
||
161 | * Just setting every number to a float gets around this, but may introduce |
||
162 | * inefficiencies. |
||
163 | */ |
||
164 | |||
165 | if (is_numeric($data)) { |
||
166 | return floatval($data); |
||
167 | } |
||
168 | |||
169 | return $data === 'U' ? null : $data; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Checks if the datastore wants rrdtags to be sent when issuing put() |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | public function wantsRrdTags() |
||
178 | { |
||
179 | return false; |
||
180 | } |
||
181 | } |
||
182 |