routeros: Fixed check of 'Collect*' options
[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 "common.h"
30 #include "plugin.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   /*** RX ***/
145   snprintf(type_instance, sizeof(type_instance), "%s-%s-rx", r->interface,
146            r->radio_name ? r->radio_name : "default");
147   cr_submit_gauge(rd, "bitrate", type_instance,
148                   (gauge_t)(1000000.0 * r->rx_rate));
149   cr_submit_gauge(rd, "signal_power", type_instance,
150                   (gauge_t)r->rx_signal_strength);
151   cr_submit_gauge(rd, "signal_quality", type_instance, (gauge_t)r->rx_ccq);
152
153   /*** TX ***/
154   snprintf(type_instance, sizeof(type_instance), "%s-%s-tx", r->interface,
155            r->radio_name ? r->radio_name : "default");
156   cr_submit_gauge(rd, "bitrate", type_instance,
157                   (gauge_t)(1000000.0 * r->tx_rate));
158   cr_submit_gauge(rd, "signal_power", type_instance,
159                   (gauge_t)r->tx_signal_strength);
160   cr_submit_gauge(rd, "signal_quality", type_instance, (gauge_t)r->tx_ccq);
161
162   /*** RX / TX ***/
163   snprintf(type_instance, sizeof(type_instance), "%s-%s", r->interface,
164            r->radio_name ? r->radio_name : "default");
165   cr_submit_io(rd, "if_octets", type_instance, (derive_t)r->rx_bytes,
166                (derive_t)r->tx_bytes);
167   cr_submit_gauge(rd, "snr", type_instance, (gauge_t)r->signal_to_noise);
168
169   submit_regtable(rd, r->next);
170 } /* }}} void submit_regtable */
171
172 static int handle_regtable(__attribute__((unused))
173                            ros_connection_t *c, /* {{{ */
174                            const ros_registration_table_t *r, void *user_data) {
175   if ((r == NULL) || (user_data == NULL))
176     return EINVAL;
177
178   submit_regtable(user_data, r);
179   return 0;
180 } /* }}} int handle_regtable */
181
182 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
183 static int handle_system_resource(__attribute__((unused))
184                                   ros_connection_t *c, /* {{{ */
185                                   const ros_system_resource_t *r,
186                                   __attribute__((unused)) void *user_data) {
187   cr_data_t *rd;
188
189   if ((r == NULL) || (user_data == NULL))
190     return EINVAL;
191   rd = user_data;
192
193   if (rd->collect_cpu_load)
194     cr_submit_gauge(rd, "gauge", "cpu_load", (gauge_t)r->cpu_load);
195
196   if (rd->collect_memory) {
197     cr_submit_gauge(rd, "memory", "used",
198                     (gauge_t)(r->total_memory - r->free_memory));
199     cr_submit_gauge(rd, "memory", "free", (gauge_t)r->free_memory);
200   }
201
202   if (rd->collect_df) {
203     cr_submit_gauge(rd, "df_complex", "used",
204                     (gauge_t)(r->total_memory - r->free_memory));
205     cr_submit_gauge(rd, "df_complex", "free", (gauge_t)r->free_memory);
206   }
207
208   if (rd->collect_disk) {
209     cr_submit_counter(rd, "counter", "secors_written",
210                       (derive_t)r->write_sect_total);
211     cr_submit_gauge(rd, "gauge", "bad_blocks", (gauge_t)r->bad_blocks);
212   }
213
214   return 0;
215 } /* }}} int handle_system_resource */
216
217 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
218 static int handle_system_health(__attribute__((unused))
219                                 ros_connection_t *c, /* {{{ */
220                                 const ros_system_health_t *r,
221                                 __attribute__((unused)) void *user_data) {
222   cr_data_t *rd;
223
224   if ((r == NULL) || (user_data == NULL))
225     return (EINVAL);
226   rd = user_data;
227
228   cr_submit_gauge(rd, "gauge", "voltage", (gauge_t)r->voltage);
229   cr_submit_gauge(rd, "gauge", "temperature", (gauge_t)r->temperature);
230
231   return (0);
232 } /* }}} int handle_system_health */
233 #endif
234 #endif
235
236 static int cr_read(user_data_t *user_data) /* {{{ */
237 {
238   int status;
239   cr_data_t *rd;
240
241   if (user_data == NULL)
242     return EINVAL;
243
244   rd = user_data->data;
245   if (rd == NULL)
246     return EINVAL;
247
248   if (rd->connection == NULL) {
249     rd->connection =
250         ros_connect(rd->node, rd->service, rd->username, rd->password);
251     if (rd->connection == NULL) {
252       ERROR("routeros plugin: ros_connect failed: %s", STRERRNO);
253       return -1;
254     }
255   }
256   assert(rd->connection != NULL);
257
258   if (rd->collect_interface) {
259     status = ros_interface(rd->connection, handle_interface,
260                            /* user data = */ rd);
261     if (status != 0) {
262       ERROR("routeros plugin: ros_interface failed: %s", STRERROR(status));
263       ros_disconnect(rd->connection);
264       rd->connection = NULL;
265       return -1;
266     }
267   }
268
269   if (rd->collect_regtable) {
270     status = ros_registration_table(rd->connection, handle_regtable,
271                                     /* user data = */ rd);
272     if (status != 0) {
273       ERROR("routeros plugin: ros_registration_table failed: %s",
274             STRERROR(status));
275       ros_disconnect(rd->connection);
276       rd->connection = NULL;
277       return -1;
278     }
279   }
280
281 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
282   if (rd->collect_cpu_load || rd->collect_memory || rd->collect_df ||
283       rd->collect_disk) {
284     status = ros_system_resource(rd->connection, handle_system_resource,
285                                  /* user data = */ rd);
286     if (status != 0) {
287       ERROR("routeros plugin: ros_system_resource failed: %s",
288             STRERROR(status));
289       ros_disconnect(rd->connection);
290       rd->connection = NULL;
291       return -1;
292     }
293   }
294
295 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
296   if (rd->collect_health) {
297     status = ros_system_health(rd->connection, handle_system_health,
298                                /* user data = */ rd);
299     if (status != 0) {
300       char errbuf[128];
301       ERROR("routeros plugin: ros_system_health failed: %s",
302             sstrerror(status, errbuf, sizeof(errbuf)));
303       ros_disconnect(rd->connection);
304       rd->connection = NULL;
305       return (-1);
306     }
307   }
308 #endif
309 #endif
310
311   return 0;
312 } /* }}} int cr_read */
313
314 static void cr_free_data(cr_data_t *ptr) /* {{{ */
315 {
316   if (ptr == NULL)
317     return;
318
319   ros_disconnect(ptr->connection);
320   ptr->connection = NULL;
321
322   sfree(ptr->node);
323   sfree(ptr->service);
324   sfree(ptr->username);
325   sfree(ptr->password);
326
327   sfree(ptr);
328 } /* }}} void cr_free_data */
329
330 static int cr_config_router(oconfig_item_t *ci) /* {{{ */
331 {
332   cr_data_t *router_data;
333   char read_name[128];
334   int status;
335
336   router_data = calloc(1, sizeof(*router_data));
337   if (router_data == NULL)
338     return -1;
339   router_data->connection = NULL;
340   router_data->node = NULL;
341   router_data->service = NULL;
342   router_data->username = NULL;
343   router_data->password = NULL;
344
345   status = 0;
346   for (int i = 0; i < ci->children_num; i++) {
347     oconfig_item_t *child = ci->children + i;
348
349     if (strcasecmp("Host", child->key) == 0)
350       status = cf_util_get_string(child, &router_data->node);
351     else if (strcasecmp("Port", child->key) == 0)
352       status = cf_util_get_service(child, &router_data->service);
353     else if (strcasecmp("User", child->key) == 0)
354       status = cf_util_get_string(child, &router_data->username);
355     else if (strcasecmp("Password", child->key) == 0)
356       status = cf_util_get_string(child, &router_data->password);
357     else if (strcasecmp("CollectInterface", child->key) == 0)
358       cf_util_get_boolean(child, &router_data->collect_interface);
359     else if (strcasecmp("CollectRegistrationTable", child->key) == 0)
360       cf_util_get_boolean(child, &router_data->collect_regtable);
361 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
362     else if (strcasecmp("CollectCPULoad", child->key) == 0)
363       cf_util_get_boolean(child, &router_data->collect_cpu_load);
364     else if (strcasecmp("CollectMemory", child->key) == 0)
365       cf_util_get_boolean(child, &router_data->collect_memory);
366     else if (strcasecmp("CollectDF", child->key) == 0)
367       cf_util_get_boolean(child, &router_data->collect_df);
368     else if (strcasecmp("CollectDisk", child->key) == 0)
369       cf_util_get_boolean(child, &router_data->collect_disk);
370 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
371     else if (strcasecmp("CollectHealth", child->key) == 0)
372       cf_util_get_boolean(child, &router_data->collect_health);
373 #endif
374 #endif
375     else {
376       WARNING("routeros plugin: Unknown config option `%s'.", child->key);
377     }
378
379     if (status != 0)
380       break;
381   }
382
383   if (status == 0) {
384     if (router_data->node == NULL) {
385       ERROR("routeros plugin: No `Host' option within a `Router' block. "
386             "Where should I connect to?");
387       status = -1;
388     }
389
390     if (router_data->password == NULL) {
391       ERROR("routeros plugin: No `Password' option within a `Router' block. "
392             "How should I authenticate?");
393       status = -1;
394     }
395
396     int report = 0;
397     if (router_data->collect_interface)
398       report++;
399     if (router_data->collect_regtable)
400       report++;
401 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
402     if (router_data->collect_cpu_load)
403       report++;
404     if (router_data->collect_memory)
405       report++;
406     if (router_data->collect_df)
407       report++;
408     if (router_data->collect_disk)
409       report++;
410 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
411     if (router_data->collect_health)
412       report++;
413 #endif
414 #endif
415
416     if (!report) {
417       ERROR("routeros plugin: No `Collect*' option within a `Router' block. "
418             "What statistics should I collect?");
419       status = -1;
420     }
421   }
422
423   if ((status == 0) && (router_data->username == NULL)) {
424     router_data->username = sstrdup("admin");
425     if (router_data->username == NULL) {
426       ERROR("routeros plugin: sstrdup failed.");
427       status = -1;
428     }
429   }
430
431   if (status != 0) {
432     cr_free_data(router_data);
433     return status;
434   }
435
436   snprintf(read_name, sizeof(read_name), "routeros/%s", router_data->node);
437   return plugin_register_complex_read(
438       /* group = */ NULL, read_name, cr_read, /* interval = */ 0,
439       &(user_data_t){
440           .data = router_data, .free_func = (void *)cr_free_data,
441       });
442 } /* }}} int cr_config_router */
443
444 static int cr_config(oconfig_item_t *ci) {
445   for (int i = 0; i < ci->children_num; i++) {
446     oconfig_item_t *child = ci->children + i;
447
448     if (strcasecmp("Router", child->key) == 0)
449       cr_config_router(child);
450     else {
451       WARNING("routeros plugin: Unknown config option `%s'.", child->key);
452     }
453   }
454
455   return 0;
456 } /* }}} int cr_config */
457
458 void module_register(void) {
459   plugin_register_complex_config("routeros", cr_config);
460 } /* void module_register */