routeros: add support for collecting health data
[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 static int handle_system_health(__attribute__((unused))
218                                 ros_connection_t *c, /* {{{ */
219                                 const ros_system_health_t *r,
220                                 __attribute__((unused)) void *user_data) {
221   cr_data_t *rd;
222
223   if ((r == NULL) || (user_data == NULL))
224     return (EINVAL);
225   rd = user_data;
226
227   cr_submit_gauge(rd, "gauge", "voltage", (gauge_t)r->voltage);
228   cr_submit_gauge(rd, "gauge", "temperature", (gauge_t)r->temperature);
229
230   return (0);
231 } /* }}} int handle_system_health */
232 #endif
233
234 static int cr_read(user_data_t *user_data) /* {{{ */
235 {
236   int status;
237   cr_data_t *rd;
238
239   if (user_data == NULL)
240     return EINVAL;
241
242   rd = user_data->data;
243   if (rd == NULL)
244     return EINVAL;
245
246   if (rd->connection == NULL) {
247     rd->connection =
248         ros_connect(rd->node, rd->service, rd->username, rd->password);
249     if (rd->connection == NULL) {
250       ERROR("routeros plugin: ros_connect failed: %s", STRERRNO);
251       return -1;
252     }
253   }
254   assert(rd->connection != NULL);
255
256   if (rd->collect_interface) {
257     status = ros_interface(rd->connection, handle_interface,
258                            /* user data = */ rd);
259     if (status != 0) {
260       ERROR("routeros plugin: ros_interface failed: %s", STRERROR(status));
261       ros_disconnect(rd->connection);
262       rd->connection = NULL;
263       return -1;
264     }
265   }
266
267   if (rd->collect_regtable) {
268     status = ros_registration_table(rd->connection, handle_regtable,
269                                     /* user data = */ rd);
270     if (status != 0) {
271       ERROR("routeros plugin: ros_registration_table failed: %s",
272             STRERROR(status));
273       ros_disconnect(rd->connection);
274       rd->connection = NULL;
275       return -1;
276     }
277   }
278
279 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
280   if (rd->collect_cpu_load || rd->collect_memory || rd->collect_df ||
281       rd->collect_disk) {
282     status = ros_system_resource(rd->connection, handle_system_resource,
283                                  /* user data = */ rd);
284     if (status != 0) {
285       ERROR("routeros plugin: ros_system_resource failed: %s",
286             STRERROR(status));
287       ros_disconnect(rd->connection);
288       rd->connection = NULL;
289       return -1;
290     }
291   }
292
293   if (rd->collect_health) {
294     status = ros_system_health(rd->connection, handle_system_health,
295                                /* user data = */ rd);
296     if (status != 0) {
297       char errbuf[128];
298       ERROR("routeros plugin: ros_system_health failed: %s",
299             sstrerror(status, errbuf, sizeof(errbuf)));
300       ros_disconnect(rd->connection);
301       rd->connection = NULL;
302       return (-1);
303     }
304   }
305 #endif
306
307   return 0;
308 } /* }}} int cr_read */
309
310 static void cr_free_data(cr_data_t *ptr) /* {{{ */
311 {
312   if (ptr == NULL)
313     return;
314
315   ros_disconnect(ptr->connection);
316   ptr->connection = NULL;
317
318   sfree(ptr->node);
319   sfree(ptr->service);
320   sfree(ptr->username);
321   sfree(ptr->password);
322
323   sfree(ptr);
324 } /* }}} void cr_free_data */
325
326 static int cr_config_router(oconfig_item_t *ci) /* {{{ */
327 {
328   cr_data_t *router_data;
329   char read_name[128];
330   int status;
331
332   router_data = calloc(1, sizeof(*router_data));
333   if (router_data == NULL)
334     return -1;
335   router_data->connection = NULL;
336   router_data->node = NULL;
337   router_data->service = NULL;
338   router_data->username = NULL;
339   router_data->password = NULL;
340
341   status = 0;
342   for (int i = 0; i < ci->children_num; i++) {
343     oconfig_item_t *child = ci->children + i;
344
345     if (strcasecmp("Host", child->key) == 0)
346       status = cf_util_get_string(child, &router_data->node);
347     else if (strcasecmp("Port", child->key) == 0)
348       status = cf_util_get_service(child, &router_data->service);
349     else if (strcasecmp("User", child->key) == 0)
350       status = cf_util_get_string(child, &router_data->username);
351     else if (strcasecmp("Password", child->key) == 0)
352       status = cf_util_get_string(child, &router_data->password);
353     else if (strcasecmp("CollectInterface", child->key) == 0)
354       cf_util_get_boolean(child, &router_data->collect_interface);
355     else if (strcasecmp("CollectRegistrationTable", child->key) == 0)
356       cf_util_get_boolean(child, &router_data->collect_regtable);
357 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
358     else if (strcasecmp("CollectCPULoad", child->key) == 0)
359       cf_util_get_boolean(child, &router_data->collect_cpu_load);
360     else if (strcasecmp("CollectMemory", child->key) == 0)
361       cf_util_get_boolean(child, &router_data->collect_memory);
362     else if (strcasecmp("CollectDF", child->key) == 0)
363       cf_util_get_boolean(child, &router_data->collect_df);
364     else if (strcasecmp("CollectDisk", child->key) == 0)
365       cf_util_get_boolean(child, &router_data->collect_disk);
366     else if (strcasecmp("CollectHealth", child->key) == 0)
367       cf_util_get_boolean(child, &router_data->collect_health);
368 #endif
369     else {
370       WARNING("routeros plugin: Unknown config option `%s'.", child->key);
371     }
372
373     if (status != 0)
374       break;
375   }
376
377   if (status == 0) {
378     if (router_data->node == NULL) {
379       ERROR("routeros plugin: No `Host' option within a `Router' block. "
380             "Where should I connect to?");
381       status = -1;
382     }
383
384     if (router_data->password == NULL) {
385       ERROR("routeros plugin: No `Password' option within a `Router' block. "
386             "How should I authenticate?");
387       status = -1;
388     }
389
390     if (!router_data->collect_interface && !router_data->collect_regtable) {
391       ERROR("routeros plugin: No `Collect*' option within a `Router' block. "
392             "What statistics should I collect?");
393       status = -1;
394     }
395   }
396
397   if ((status == 0) && (router_data->username == NULL)) {
398     router_data->username = sstrdup("admin");
399     if (router_data->username == NULL) {
400       ERROR("routeros plugin: sstrdup failed.");
401       status = -1;
402     }
403   }
404
405   if (status != 0) {
406     cr_free_data(router_data);
407     return status;
408   }
409
410   snprintf(read_name, sizeof(read_name), "routeros/%s", router_data->node);
411   return plugin_register_complex_read(
412       /* group = */ NULL, read_name, cr_read, /* interval = */ 0,
413       &(user_data_t){
414           .data = router_data, .free_func = (void *)cr_free_data,
415       });
416 } /* }}} int cr_config_router */
417
418 static int cr_config(oconfig_item_t *ci) {
419   for (int i = 0; i < ci->children_num; i++) {
420     oconfig_item_t *child = ci->children + i;
421
422     if (strcasecmp("Router", child->key) == 0)
423       cr_config_router(child);
424     else {
425       WARNING("routeros plugin: Unknown config option `%s'.", child->key);
426     }
427   }
428
429   return 0;
430 } /* }}} int cr_config */
431
432 void module_register(void) {
433   plugin_register_complex_config("routeros", cr_config);
434 } /* void module_register */