Merge pull request #3329 from efuss/fix-3311
[collectd.git] / src / routeros.c
1 /**
2  * collectd - src/routeros.c
3  * Copyright (C) 2009,2010  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28
29 #include "plugin.h"
30 #include "utils/common/common.h"
31
32 #include <routeros_api.h>
33
34 struct cr_data_s {
35   ros_connection_t *connection;
36
37   char *node;
38   char *service;
39   char *username;
40   char *password;
41
42   bool collect_interface;
43   bool collect_regtable;
44   bool collect_cpu_load;
45   bool collect_memory;
46   bool collect_df;
47   bool collect_disk;
48   bool collect_health;
49 };
50 typedef struct cr_data_s cr_data_t;
51
52 static void cr_submit_io(cr_data_t *rd, const char *type, /* {{{ */
53                          const char *type_instance, derive_t rx, derive_t tx) {
54   value_list_t vl = VALUE_LIST_INIT;
55   value_t values[] = {
56       {.derive = rx},
57       {.derive = tx},
58   };
59
60   vl.values = values;
61   vl.values_len = STATIC_ARRAY_SIZE(values);
62   sstrncpy(vl.host, rd->node, sizeof(vl.host));
63   sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
64   sstrncpy(vl.type, type, sizeof(vl.type));
65   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
66
67   plugin_dispatch_values(&vl);
68 } /* }}} void cr_submit_io */
69
70 static void submit_interface(cr_data_t *rd, /* {{{ */
71                              const ros_interface_t *i) {
72   if (i == NULL)
73     return;
74
75   if (!i->running) {
76     submit_interface(rd, i->next);
77     return;
78   }
79
80   cr_submit_io(rd, "if_packets", i->name, (derive_t)i->rx_packets,
81                (derive_t)i->tx_packets);
82   cr_submit_io(rd, "if_octets", i->name, (derive_t)i->rx_bytes,
83                (derive_t)i->tx_bytes);
84   cr_submit_io(rd, "if_errors", i->name, (derive_t)i->rx_errors,
85                (derive_t)i->tx_errors);
86   cr_submit_io(rd, "if_dropped", i->name, (derive_t)i->rx_drops,
87                (derive_t)i->tx_drops);
88
89   submit_interface(rd, i->next);
90 } /* }}} void submit_interface */
91
92 static int handle_interface(__attribute__((unused))
93                             ros_connection_t *c, /* {{{ */
94                             const ros_interface_t *i, void *user_data) {
95   if ((i == NULL) || (user_data == NULL))
96     return EINVAL;
97
98   submit_interface(user_data, i);
99   return 0;
100 } /* }}} int handle_interface */
101
102 static void cr_submit_gauge(cr_data_t *rd, const char *type, /* {{{ */
103                             const char *type_instance, gauge_t value) {
104   value_t values[1];
105   value_list_t vl = VALUE_LIST_INIT;
106
107   values[0].gauge = value;
108
109   vl.values = values;
110   vl.values_len = STATIC_ARRAY_SIZE(values);
111   sstrncpy(vl.host, rd->node, sizeof(vl.host));
112   sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
113   sstrncpy(vl.type, type, sizeof(vl.type));
114   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
115
116   plugin_dispatch_values(&vl);
117 } /* }}} void cr_submit_gauge */
118
119 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
120 static void cr_submit_counter(cr_data_t *rd, const char *type, /* {{{ */
121                               const char *type_instance, derive_t value) {
122   value_t values[1];
123   value_list_t vl = VALUE_LIST_INIT;
124
125   values[0].derive = value;
126
127   vl.values = values;
128   vl.values_len = STATIC_ARRAY_SIZE(values);
129   sstrncpy(vl.host, rd->node, sizeof(vl.host));
130   sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
131   sstrncpy(vl.type, type, sizeof(vl.type));
132   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
133
134   plugin_dispatch_values(&vl);
135 } /* }}} void cr_submit_gauge */
136 #endif
137
138 static void submit_regtable(cr_data_t *rd, /* {{{ */
139                             const ros_registration_table_t *r) {
140   char type_instance[DATA_MAX_NAME_LEN];
141
142   if (r == NULL)
143     return;
144
145   const char *name = r->radio_name;
146 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
147   if (name == NULL)
148     name = r->mac_address;
149 #endif
150   if (name == NULL)
151     name = "default";
152
153   /*** RX ***/
154   ssnprintf(type_instance, sizeof(type_instance), "%s-%s-rx", r->interface,
155             name);
156   cr_submit_gauge(rd, "bitrate", type_instance,
157                   (gauge_t)(1000000.0 * r->rx_rate));
158   cr_submit_gauge(rd, "signal_power", type_instance,
159                   (gauge_t)r->rx_signal_strength);
160   cr_submit_gauge(rd, "signal_quality", type_instance, (gauge_t)r->rx_ccq);
161
162   /*** TX ***/
163   ssnprintf(type_instance, sizeof(type_instance), "%s-%s-tx", r->interface,
164             name);
165   cr_submit_gauge(rd, "bitrate", type_instance,
166                   (gauge_t)(1000000.0 * r->tx_rate));
167   cr_submit_gauge(rd, "signal_power", type_instance,
168                   (gauge_t)r->tx_signal_strength);
169   cr_submit_gauge(rd, "signal_quality", type_instance, (gauge_t)r->tx_ccq);
170
171   /*** RX / TX ***/
172   ssnprintf(type_instance, sizeof(type_instance), "%s-%s", r->interface, name);
173   cr_submit_io(rd, "if_octets", type_instance, (derive_t)r->rx_bytes,
174                (derive_t)r->tx_bytes);
175   cr_submit_gauge(rd, "snr", type_instance, (gauge_t)r->signal_to_noise);
176
177   submit_regtable(rd, r->next);
178 } /* }}} void submit_regtable */
179
180 static int handle_regtable(__attribute__((unused))
181                            ros_connection_t *c, /* {{{ */
182                            const ros_registration_table_t *r, void *user_data) {
183   if ((r == NULL) || (user_data == NULL))
184     return EINVAL;
185
186   submit_regtable(user_data, r);
187   return 0;
188 } /* }}} int handle_regtable */
189
190 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
191 static int handle_system_resource(__attribute__((unused))
192                                   ros_connection_t *c, /* {{{ */
193                                   const ros_system_resource_t *r,
194                                   __attribute__((unused)) void *user_data) {
195   cr_data_t *rd;
196
197   if ((r == NULL) || (user_data == NULL))
198     return EINVAL;
199   rd = user_data;
200
201   if (rd->collect_cpu_load)
202     cr_submit_gauge(rd, "gauge", "cpu_load", (gauge_t)r->cpu_load);
203
204   if (rd->collect_memory) {
205     cr_submit_gauge(rd, "memory", "used",
206                     (gauge_t)(r->total_memory - r->free_memory));
207     cr_submit_gauge(rd, "memory", "free", (gauge_t)r->free_memory);
208   }
209
210   if (rd->collect_df) {
211     cr_submit_gauge(rd, "df_complex", "used",
212                     (gauge_t)(r->total_memory - r->free_memory));
213     cr_submit_gauge(rd, "df_complex", "free", (gauge_t)r->free_memory);
214   }
215
216   if (rd->collect_disk) {
217     cr_submit_counter(rd, "counter", "sectors_written",
218                       (derive_t)r->write_sect_total);
219     cr_submit_gauge(rd, "gauge", "bad_blocks", (gauge_t)r->bad_blocks);
220   }
221
222   return 0;
223 } /* }}} int handle_system_resource */
224
225 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
226 static int handle_system_health(__attribute__((unused))
227                                 ros_connection_t *c, /* {{{ */
228                                 const ros_system_health_t *r,
229                                 __attribute__((unused)) void *user_data) {
230
231   if ((r == NULL) || (user_data == NULL))
232     return EINVAL;
233
234   cr_data_t *rd = user_data;
235
236   cr_submit_gauge(rd, "voltage", "system", (gauge_t)r->voltage);
237   cr_submit_gauge(rd, "temperature", "system", (gauge_t)r->temperature);
238
239   return 0;
240 } /* }}} int handle_system_health */
241 #endif
242 #endif
243
244 static int cr_read(user_data_t *user_data) /* {{{ */
245 {
246   int status;
247   cr_data_t *rd;
248
249   if (user_data == NULL)
250     return EINVAL;
251
252   rd = user_data->data;
253   if (rd == NULL)
254     return EINVAL;
255
256   if (rd->connection == NULL) {
257     rd->connection =
258         ros_connect(rd->node, rd->service, rd->username, rd->password);
259     if (rd->connection == NULL) {
260       ERROR("routeros plugin: ros_connect failed: %s", STRERRNO);
261       return -1;
262     }
263   }
264   assert(rd->connection != NULL);
265
266   if (rd->collect_interface) {
267     status = ros_interface(rd->connection, handle_interface,
268                            /* user data = */ rd);
269     if (status != 0) {
270       ERROR("routeros plugin: ros_interface failed: %s", STRERROR(status));
271       ros_disconnect(rd->connection);
272       rd->connection = NULL;
273       return -1;
274     }
275   }
276
277   if (rd->collect_regtable) {
278     status = ros_registration_table(rd->connection, handle_regtable,
279                                     /* user data = */ rd);
280     if (status != 0) {
281       ERROR("routeros plugin: ros_registration_table failed: %s",
282             STRERROR(status));
283       ros_disconnect(rd->connection);
284       rd->connection = NULL;
285       return -1;
286     }
287   }
288
289 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
290   if (rd->collect_cpu_load || rd->collect_memory || rd->collect_df ||
291       rd->collect_disk) {
292     status = ros_system_resource(rd->connection, handle_system_resource,
293                                  /* user data = */ rd);
294     if (status != 0) {
295       ERROR("routeros plugin: ros_system_resource failed: %s",
296             STRERROR(status));
297       ros_disconnect(rd->connection);
298       rd->connection = NULL;
299       return -1;
300     }
301   }
302
303 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
304   if (rd->collect_health) {
305     status = ros_system_health(rd->connection, handle_system_health,
306                                /* user data = */ rd);
307     if (status != 0) {
308       ERROR("routeros plugin: ros_system_health failed: %s", STRERROR(status));
309       ros_disconnect(rd->connection);
310       rd->connection = NULL;
311       return -1;
312     }
313   }
314 #endif
315 #endif
316
317   return 0;
318 } /* }}} int cr_read */
319
320 static void cr_free_data(cr_data_t *ptr) /* {{{ */
321 {
322   if (ptr == NULL)
323     return;
324
325   ros_disconnect(ptr->connection);
326   ptr->connection = NULL;
327
328   sfree(ptr->node);
329   sfree(ptr->service);
330   sfree(ptr->username);
331   sfree(ptr->password);
332
333   sfree(ptr);
334 } /* }}} void cr_free_data */
335
336 static int cr_config_router(oconfig_item_t *ci) /* {{{ */
337 {
338   cr_data_t *router_data;
339   char read_name[128];
340   int status;
341
342   router_data = calloc(1, sizeof(*router_data));
343   if (router_data == NULL)
344     return -1;
345   router_data->connection = NULL;
346   router_data->node = NULL;
347   router_data->service = NULL;
348   router_data->username = NULL;
349   router_data->password = NULL;
350
351   status = 0;
352   for (int i = 0; i < ci->children_num; i++) {
353     oconfig_item_t *child = ci->children + i;
354
355     if (strcasecmp("Host", child->key) == 0)
356       status = cf_util_get_string(child, &router_data->node);
357     else if (strcasecmp("Port", child->key) == 0)
358       status = cf_util_get_service(child, &router_data->service);
359     else if (strcasecmp("User", child->key) == 0)
360       status = cf_util_get_string(child, &router_data->username);
361     else if (strcasecmp("Password", child->key) == 0)
362       status = cf_util_get_string(child, &router_data->password);
363     else if (strcasecmp("CollectInterface", child->key) == 0)
364       cf_util_get_boolean(child, &router_data->collect_interface);
365     else if (strcasecmp("CollectRegistrationTable", child->key) == 0)
366       cf_util_get_boolean(child, &router_data->collect_regtable);
367 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
368     else if (strcasecmp("CollectCPULoad", child->key) == 0)
369       cf_util_get_boolean(child, &router_data->collect_cpu_load);
370     else if (strcasecmp("CollectMemory", child->key) == 0)
371       cf_util_get_boolean(child, &router_data->collect_memory);
372     else if (strcasecmp("CollectDF", child->key) == 0)
373       cf_util_get_boolean(child, &router_data->collect_df);
374     else if (strcasecmp("CollectDisk", child->key) == 0)
375       cf_util_get_boolean(child, &router_data->collect_disk);
376 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
377     else if (strcasecmp("CollectHealth", child->key) == 0)
378       cf_util_get_boolean(child, &router_data->collect_health);
379 #endif
380 #endif
381     else {
382       WARNING("routeros plugin: Unknown config option `%s'.", child->key);
383     }
384
385     if (status != 0)
386       break;
387   }
388
389   if (status == 0) {
390     if (router_data->node == NULL) {
391       ERROR("routeros plugin: No `Host' option within a `Router' block. "
392             "Where should I connect to?");
393       status = -1;
394     }
395
396     if (router_data->password == NULL) {
397       ERROR("routeros plugin: No `Password' option within a `Router' block. "
398             "How should I authenticate?");
399       status = -1;
400     }
401
402     int report = 0;
403     if (router_data->collect_interface)
404       report++;
405     if (router_data->collect_regtable)
406       report++;
407 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
408     if (router_data->collect_cpu_load)
409       report++;
410     if (router_data->collect_memory)
411       report++;
412     if (router_data->collect_df)
413       report++;
414     if (router_data->collect_disk)
415       report++;
416 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
417     if (router_data->collect_health)
418       report++;
419 #endif
420 #endif
421
422     if (!report) {
423       ERROR("routeros plugin: No `Collect*' option within a `Router' block. "
424             "What statistics should I collect?");
425       status = -1;
426     }
427   }
428
429   if ((status == 0) && (router_data->username == NULL)) {
430     router_data->username = sstrdup("admin");
431     if (router_data->username == NULL) {
432       ERROR("routeros plugin: sstrdup failed.");
433       status = -1;
434     }
435   }
436
437   if (status != 0) {
438     cr_free_data(router_data);
439     return status;
440   }
441
442   ssnprintf(read_name, sizeof(read_name), "routeros/%s", router_data->node);
443   return plugin_register_complex_read(
444       /* group = */ NULL, read_name, cr_read, /* interval = */ 0,
445       &(user_data_t){
446           .data = router_data,
447           .free_func = (void *)cr_free_data,
448       });
449 } /* }}} int cr_config_router */
450
451 static int cr_config(oconfig_item_t *ci) {
452   for (int i = 0; i < ci->children_num; i++) {
453     oconfig_item_t *child = ci->children + i;
454
455     if (strcasecmp("Router", child->key) == 0)
456       cr_config_router(child);
457     else {
458       WARNING("routeros plugin: Unknown config option `%s'.", child->key);
459     }
460   }
461
462   return 0;
463 } /* }}} int cr_config */
464
465 void module_register(void) {
466   plugin_register_complex_config("routeros", cr_config);
467 } /* void module_register */