Add snprintf wrapper for GCC 8.2/3
[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}, {.derive = tx},
57   };
58
59   vl.values = values;
60   vl.values_len = STATIC_ARRAY_SIZE(values);
61   sstrncpy(vl.host, rd->node, sizeof(vl.host));
62   sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
63   sstrncpy(vl.type, type, sizeof(vl.type));
64   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
65
66   plugin_dispatch_values(&vl);
67 } /* }}} void cr_submit_io */
68
69 static void submit_interface(cr_data_t *rd, /* {{{ */
70                              const ros_interface_t *i) {
71   if (i == NULL)
72     return;
73
74   if (!i->running) {
75     submit_interface(rd, i->next);
76     return;
77   }
78
79   cr_submit_io(rd, "if_packets", i->name, (derive_t)i->rx_packets,
80                (derive_t)i->tx_packets);
81   cr_submit_io(rd, "if_octets", i->name, (derive_t)i->rx_bytes,
82                (derive_t)i->tx_bytes);
83   cr_submit_io(rd, "if_errors", i->name, (derive_t)i->rx_errors,
84                (derive_t)i->tx_errors);
85   cr_submit_io(rd, "if_dropped", i->name, (derive_t)i->rx_drops,
86                (derive_t)i->tx_drops);
87
88   submit_interface(rd, i->next);
89 } /* }}} void submit_interface */
90
91 static int handle_interface(__attribute__((unused))
92                             ros_connection_t *c, /* {{{ */
93                             const ros_interface_t *i, void *user_data) {
94   if ((i == NULL) || (user_data == NULL))
95     return EINVAL;
96
97   submit_interface(user_data, i);
98   return 0;
99 } /* }}} int handle_interface */
100
101 static void cr_submit_gauge(cr_data_t *rd, const char *type, /* {{{ */
102                             const char *type_instance, gauge_t value) {
103   value_t values[1];
104   value_list_t vl = VALUE_LIST_INIT;
105
106   values[0].gauge = value;
107
108   vl.values = values;
109   vl.values_len = STATIC_ARRAY_SIZE(values);
110   sstrncpy(vl.host, rd->node, sizeof(vl.host));
111   sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
112   sstrncpy(vl.type, type, sizeof(vl.type));
113   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
114
115   plugin_dispatch_values(&vl);
116 } /* }}} void cr_submit_gauge */
117
118 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
119 static void cr_submit_counter(cr_data_t *rd, const char *type, /* {{{ */
120                               const char *type_instance, derive_t value) {
121   value_t values[1];
122   value_list_t vl = VALUE_LIST_INIT;
123
124   values[0].derive = value;
125
126   vl.values = values;
127   vl.values_len = STATIC_ARRAY_SIZE(values);
128   sstrncpy(vl.host, rd->node, sizeof(vl.host));
129   sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
130   sstrncpy(vl.type, type, sizeof(vl.type));
131   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
132
133   plugin_dispatch_values(&vl);
134 } /* }}} void cr_submit_gauge */
135 #endif
136
137 static void submit_regtable(cr_data_t *rd, /* {{{ */
138                             const ros_registration_table_t *r) {
139   char type_instance[DATA_MAX_NAME_LEN];
140
141   if (r == NULL)
142     return;
143
144   const char *name = r->radio_name;
145 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
146   if (name == NULL)
147     name = r->mac_address;
148 #endif
149   if (name == NULL)
150     name = "default";
151
152   /*** RX ***/
153   ssnprintf(type_instance, sizeof(type_instance), "%s-%s-rx", r->interface,
154            name);
155   cr_submit_gauge(rd, "bitrate", type_instance,
156                   (gauge_t)(1000000.0 * r->rx_rate));
157   cr_submit_gauge(rd, "signal_power", type_instance,
158                   (gauge_t)r->rx_signal_strength);
159   cr_submit_gauge(rd, "signal_quality", type_instance, (gauge_t)r->rx_ccq);
160
161   /*** TX ***/
162   ssnprintf(type_instance, sizeof(type_instance), "%s-%s-tx", r->interface,
163            name);
164   cr_submit_gauge(rd, "bitrate", type_instance,
165                   (gauge_t)(1000000.0 * r->tx_rate));
166   cr_submit_gauge(rd, "signal_power", type_instance,
167                   (gauge_t)r->tx_signal_strength);
168   cr_submit_gauge(rd, "signal_quality", type_instance, (gauge_t)r->tx_ccq);
169
170   /*** RX / TX ***/
171   ssnprintf(type_instance, sizeof(type_instance), "%s-%s", r->interface, name);
172   cr_submit_io(rd, "if_octets", type_instance, (derive_t)r->rx_bytes,
173                (derive_t)r->tx_bytes);
174   cr_submit_gauge(rd, "snr", type_instance, (gauge_t)r->signal_to_noise);
175
176   submit_regtable(rd, r->next);
177 } /* }}} void submit_regtable */
178
179 static int handle_regtable(__attribute__((unused))
180                            ros_connection_t *c, /* {{{ */
181                            const ros_registration_table_t *r, void *user_data) {
182   if ((r == NULL) || (user_data == NULL))
183     return EINVAL;
184
185   submit_regtable(user_data, r);
186   return 0;
187 } /* }}} int handle_regtable */
188
189 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
190 static int handle_system_resource(__attribute__((unused))
191                                   ros_connection_t *c, /* {{{ */
192                                   const ros_system_resource_t *r,
193                                   __attribute__((unused)) void *user_data) {
194   cr_data_t *rd;
195
196   if ((r == NULL) || (user_data == NULL))
197     return EINVAL;
198   rd = user_data;
199
200   if (rd->collect_cpu_load)
201     cr_submit_gauge(rd, "gauge", "cpu_load", (gauge_t)r->cpu_load);
202
203   if (rd->collect_memory) {
204     cr_submit_gauge(rd, "memory", "used",
205                     (gauge_t)(r->total_memory - r->free_memory));
206     cr_submit_gauge(rd, "memory", "free", (gauge_t)r->free_memory);
207   }
208
209   if (rd->collect_df) {
210     cr_submit_gauge(rd, "df_complex", "used",
211                     (gauge_t)(r->total_memory - r->free_memory));
212     cr_submit_gauge(rd, "df_complex", "free", (gauge_t)r->free_memory);
213   }
214
215   if (rd->collect_disk) {
216     cr_submit_counter(rd, "counter", "sectors_written",
217                       (derive_t)r->write_sect_total);
218     cr_submit_gauge(rd, "gauge", "bad_blocks", (gauge_t)r->bad_blocks);
219   }
220
221   return 0;
222 } /* }}} int handle_system_resource */
223
224 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
225 static int handle_system_health(__attribute__((unused))
226                                 ros_connection_t *c, /* {{{ */
227                                 const ros_system_health_t *r,
228                                 __attribute__((unused)) void *user_data) {
229
230   if ((r == NULL) || (user_data == NULL))
231     return EINVAL;
232
233   cr_data_t *rd = user_data;
234
235   cr_submit_gauge(rd, "voltage", "system", (gauge_t)r->voltage);
236   cr_submit_gauge(rd, "temperature", "system", (gauge_t)r->temperature);
237
238   return 0;
239 } /* }}} int handle_system_health */
240 #endif
241 #endif
242
243 static int cr_read(user_data_t *user_data) /* {{{ */
244 {
245   int status;
246   cr_data_t *rd;
247
248   if (user_data == NULL)
249     return EINVAL;
250
251   rd = user_data->data;
252   if (rd == NULL)
253     return EINVAL;
254
255   if (rd->connection == NULL) {
256     rd->connection =
257         ros_connect(rd->node, rd->service, rd->username, rd->password);
258     if (rd->connection == NULL) {
259       ERROR("routeros plugin: ros_connect failed: %s", STRERRNO);
260       return -1;
261     }
262   }
263   assert(rd->connection != NULL);
264
265   if (rd->collect_interface) {
266     status = ros_interface(rd->connection, handle_interface,
267                            /* user data = */ rd);
268     if (status != 0) {
269       ERROR("routeros plugin: ros_interface failed: %s", STRERROR(status));
270       ros_disconnect(rd->connection);
271       rd->connection = NULL;
272       return -1;
273     }
274   }
275
276   if (rd->collect_regtable) {
277     status = ros_registration_table(rd->connection, handle_regtable,
278                                     /* user data = */ rd);
279     if (status != 0) {
280       ERROR("routeros plugin: ros_registration_table failed: %s",
281             STRERROR(status));
282       ros_disconnect(rd->connection);
283       rd->connection = NULL;
284       return -1;
285     }
286   }
287
288 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
289   if (rd->collect_cpu_load || rd->collect_memory || rd->collect_df ||
290       rd->collect_disk) {
291     status = ros_system_resource(rd->connection, handle_system_resource,
292                                  /* user data = */ rd);
293     if (status != 0) {
294       ERROR("routeros plugin: ros_system_resource failed: %s",
295             STRERROR(status));
296       ros_disconnect(rd->connection);
297       rd->connection = NULL;
298       return -1;
299     }
300   }
301
302 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
303   if (rd->collect_health) {
304     status = ros_system_health(rd->connection, handle_system_health,
305                                /* user data = */ rd);
306     if (status != 0) {
307       ERROR("routeros plugin: ros_system_health failed: %s", STRERROR(status));
308       ros_disconnect(rd->connection);
309       rd->connection = NULL;
310       return -1;
311     }
312   }
313 #endif
314 #endif
315
316   return 0;
317 } /* }}} int cr_read */
318
319 static void cr_free_data(cr_data_t *ptr) /* {{{ */
320 {
321   if (ptr == NULL)
322     return;
323
324   ros_disconnect(ptr->connection);
325   ptr->connection = NULL;
326
327   sfree(ptr->node);
328   sfree(ptr->service);
329   sfree(ptr->username);
330   sfree(ptr->password);
331
332   sfree(ptr);
333 } /* }}} void cr_free_data */
334
335 static int cr_config_router(oconfig_item_t *ci) /* {{{ */
336 {
337   cr_data_t *router_data;
338   char read_name[128];
339   int status;
340
341   router_data = calloc(1, sizeof(*router_data));
342   if (router_data == NULL)
343     return -1;
344   router_data->connection = NULL;
345   router_data->node = NULL;
346   router_data->service = NULL;
347   router_data->username = NULL;
348   router_data->password = NULL;
349
350   status = 0;
351   for (int i = 0; i < ci->children_num; i++) {
352     oconfig_item_t *child = ci->children + i;
353
354     if (strcasecmp("Host", child->key) == 0)
355       status = cf_util_get_string(child, &router_data->node);
356     else if (strcasecmp("Port", child->key) == 0)
357       status = cf_util_get_service(child, &router_data->service);
358     else if (strcasecmp("User", child->key) == 0)
359       status = cf_util_get_string(child, &router_data->username);
360     else if (strcasecmp("Password", child->key) == 0)
361       status = cf_util_get_string(child, &router_data->password);
362     else if (strcasecmp("CollectInterface", child->key) == 0)
363       cf_util_get_boolean(child, &router_data->collect_interface);
364     else if (strcasecmp("CollectRegistrationTable", child->key) == 0)
365       cf_util_get_boolean(child, &router_data->collect_regtable);
366 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
367     else if (strcasecmp("CollectCPULoad", child->key) == 0)
368       cf_util_get_boolean(child, &router_data->collect_cpu_load);
369     else if (strcasecmp("CollectMemory", child->key) == 0)
370       cf_util_get_boolean(child, &router_data->collect_memory);
371     else if (strcasecmp("CollectDF", child->key) == 0)
372       cf_util_get_boolean(child, &router_data->collect_df);
373     else if (strcasecmp("CollectDisk", child->key) == 0)
374       cf_util_get_boolean(child, &router_data->collect_disk);
375 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
376     else if (strcasecmp("CollectHealth", child->key) == 0)
377       cf_util_get_boolean(child, &router_data->collect_health);
378 #endif
379 #endif
380     else {
381       WARNING("routeros plugin: Unknown config option `%s'.", child->key);
382     }
383
384     if (status != 0)
385       break;
386   }
387
388   if (status == 0) {
389     if (router_data->node == NULL) {
390       ERROR("routeros plugin: No `Host' option within a `Router' block. "
391             "Where should I connect to?");
392       status = -1;
393     }
394
395     if (router_data->password == NULL) {
396       ERROR("routeros plugin: No `Password' option within a `Router' block. "
397             "How should I authenticate?");
398       status = -1;
399     }
400
401     int report = 0;
402     if (router_data->collect_interface)
403       report++;
404     if (router_data->collect_regtable)
405       report++;
406 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
407     if (router_data->collect_cpu_load)
408       report++;
409     if (router_data->collect_memory)
410       report++;
411     if (router_data->collect_df)
412       report++;
413     if (router_data->collect_disk)
414       report++;
415 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
416     if (router_data->collect_health)
417       report++;
418 #endif
419 #endif
420
421     if (!report) {
422       ERROR("routeros plugin: No `Collect*' option within a `Router' block. "
423             "What statistics should I collect?");
424       status = -1;
425     }
426   }
427
428   if ((status == 0) && (router_data->username == NULL)) {
429     router_data->username = sstrdup("admin");
430     if (router_data->username == NULL) {
431       ERROR("routeros plugin: sstrdup failed.");
432       status = -1;
433     }
434   }
435
436   if (status != 0) {
437     cr_free_data(router_data);
438     return status;
439   }
440
441   ssnprintf(read_name, sizeof(read_name), "routeros/%s", router_data->node);
442   return plugin_register_complex_read(
443       /* group = */ NULL, read_name, cr_read, /* interval = */ 0,
444       &(user_data_t){
445           .data = router_data, .free_func = (void *)cr_free_data,
446       });
447 } /* }}} int cr_config_router */
448
449 static int cr_config(oconfig_item_t *ci) {
450   for (int i = 0; i < ci->children_num; i++) {
451     oconfig_item_t *child = ci->children + i;
452
453     if (strcasecmp("Router", child->key) == 0)
454       cr_config_router(child);
455     else {
456       WARNING("routeros plugin: Unknown config option `%s'.", child->key);
457     }
458   }
459
460   return 0;
461 } /* }}} int cr_config */
462
463 void module_register(void) {
464   plugin_register_complex_config("routeros", cr_config);
465 } /* void module_register */