Auto-Merge pull request #2554 from octo/cid/179227
[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 };
49 typedef struct cr_data_s cr_data_t;
50
51 static void cr_submit_io(cr_data_t *rd, const char *type, /* {{{ */
52                          const char *type_instance, derive_t rx, derive_t tx) {
53   value_list_t vl = VALUE_LIST_INIT;
54   value_t values[] = {
55       {.derive = rx}, {.derive = tx},
56   };
57
58   vl.values = values;
59   vl.values_len = STATIC_ARRAY_SIZE(values);
60   sstrncpy(vl.host, rd->node, sizeof(vl.host));
61   sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
62   sstrncpy(vl.type, type, sizeof(vl.type));
63   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
64
65   plugin_dispatch_values(&vl);
66 } /* }}} void cr_submit_io */
67
68 static void submit_interface(cr_data_t *rd, /* {{{ */
69                              const ros_interface_t *i) {
70   if (i == NULL)
71     return;
72
73   if (!i->running) {
74     submit_interface(rd, i->next);
75     return;
76   }
77
78   cr_submit_io(rd, "if_packets", i->name, (derive_t)i->rx_packets,
79                (derive_t)i->tx_packets);
80   cr_submit_io(rd, "if_octets", i->name, (derive_t)i->rx_bytes,
81                (derive_t)i->tx_bytes);
82   cr_submit_io(rd, "if_errors", i->name, (derive_t)i->rx_errors,
83                (derive_t)i->tx_errors);
84   cr_submit_io(rd, "if_dropped", i->name, (derive_t)i->rx_drops,
85                (derive_t)i->tx_drops);
86
87   submit_interface(rd, i->next);
88 } /* }}} void submit_interface */
89
90 static int handle_interface(__attribute__((unused))
91                             ros_connection_t *c, /* {{{ */
92                             const ros_interface_t *i,
93                             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   ssnprintf(type_instance, sizeof(type_instance), "%s-%s-rx", r->interface,
146             r->radio_name);
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   ssnprintf(type_instance, sizeof(type_instance), "%s-%s-tx", r->interface,
155             r->radio_name);
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   ssnprintf(type_instance, sizeof(type_instance), "%s-%s", r->interface,
164             r->radio_name);
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,
175                            void *user_data) {
176   if ((r == NULL) || (user_data == NULL))
177     return (EINVAL);
178
179   submit_regtable(user_data, r);
180   return (0);
181 } /* }}} int handle_regtable */
182
183 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
184 static int handle_system_resource(__attribute__((unused))
185                                   ros_connection_t *c, /* {{{ */
186                                   const ros_system_resource_t *r,
187                                   __attribute__((unused)) void *user_data) {
188   cr_data_t *rd;
189
190   if ((r == NULL) || (user_data == NULL))
191     return (EINVAL);
192   rd = user_data;
193
194   if (rd->collect_cpu_load)
195     cr_submit_gauge(rd, "gauge", "cpu_load", (gauge_t)r->cpu_load);
196
197   if (rd->collect_memory) {
198     cr_submit_gauge(rd, "memory", "used",
199                     (gauge_t)(r->total_memory - r->free_memory));
200     cr_submit_gauge(rd, "memory", "free", (gauge_t)r->free_memory);
201   }
202
203   if (rd->collect_df) {
204     cr_submit_gauge(rd, "df_complex", "used",
205                     (gauge_t)(r->total_memory - r->free_memory));
206     cr_submit_gauge(rd, "df_complex", "free", (gauge_t)r->free_memory);
207   }
208
209   if (rd->collect_disk) {
210     cr_submit_counter(rd, "counter", "secors_written",
211                       (derive_t)r->write_sect_total);
212     cr_submit_gauge(rd, "gauge", "bad_blocks", (gauge_t)r->bad_blocks);
213   }
214
215   return (0);
216 } /* }}} int handle_system_resource */
217 #endif
218
219 static int cr_read(user_data_t *user_data) /* {{{ */
220 {
221   int status;
222   cr_data_t *rd;
223
224   if (user_data == NULL)
225     return (EINVAL);
226
227   rd = user_data->data;
228   if (rd == NULL)
229     return (EINVAL);
230
231   if (rd->connection == NULL) {
232     rd->connection =
233         ros_connect(rd->node, rd->service, rd->username, rd->password);
234     if (rd->connection == NULL) {
235       char errbuf[128];
236       ERROR("routeros plugin: ros_connect failed: %s",
237             sstrerror(errno, errbuf, sizeof(errbuf)));
238       return (-1);
239     }
240   }
241   assert(rd->connection != NULL);
242
243   if (rd->collect_interface) {
244     status = ros_interface(rd->connection, handle_interface,
245                            /* user data = */ rd);
246     if (status != 0) {
247       char errbuf[128];
248       ERROR("routeros plugin: ros_interface failed: %s",
249             sstrerror(status, errbuf, sizeof(errbuf)));
250       ros_disconnect(rd->connection);
251       rd->connection = NULL;
252       return (-1);
253     }
254   }
255
256   if (rd->collect_regtable) {
257     status = ros_registration_table(rd->connection, handle_regtable,
258                                     /* user data = */ rd);
259     if (status != 0) {
260       char errbuf[128];
261       ERROR("routeros plugin: ros_registration_table failed: %s",
262             sstrerror(status, errbuf, sizeof(errbuf)));
263       ros_disconnect(rd->connection);
264       rd->connection = NULL;
265       return (-1);
266     }
267   }
268
269 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
270   if (rd->collect_cpu_load || rd->collect_memory || rd->collect_df ||
271       rd->collect_disk) {
272     status = ros_system_resource(rd->connection, handle_system_resource,
273                                  /* user data = */ rd);
274     if (status != 0) {
275       char errbuf[128];
276       ERROR("routeros plugin: ros_system_resource failed: %s",
277             sstrerror(status, errbuf, sizeof(errbuf)));
278       ros_disconnect(rd->connection);
279       rd->connection = NULL;
280       return (-1);
281     }
282   }
283 #endif
284
285   return (0);
286 } /* }}} int cr_read */
287
288 static void cr_free_data(cr_data_t *ptr) /* {{{ */
289 {
290   if (ptr == NULL)
291     return;
292
293   ros_disconnect(ptr->connection);
294   ptr->connection = NULL;
295
296   sfree(ptr->node);
297   sfree(ptr->service);
298   sfree(ptr->username);
299   sfree(ptr->password);
300
301   sfree(ptr);
302 } /* }}} void cr_free_data */
303
304 static int cr_config_router(oconfig_item_t *ci) /* {{{ */
305 {
306   cr_data_t *router_data;
307   char read_name[128];
308   int status;
309
310   router_data = calloc(1, sizeof(*router_data));
311   if (router_data == NULL)
312     return (-1);
313   router_data->connection = NULL;
314   router_data->node = NULL;
315   router_data->service = NULL;
316   router_data->username = NULL;
317   router_data->password = NULL;
318
319   status = 0;
320   for (int i = 0; i < ci->children_num; i++) {
321     oconfig_item_t *child = ci->children + i;
322
323     if (strcasecmp("Host", child->key) == 0)
324       status = cf_util_get_string(child, &router_data->node);
325     else if (strcasecmp("Port", child->key) == 0)
326       status = cf_util_get_service(child, &router_data->service);
327     else if (strcasecmp("User", child->key) == 0)
328       status = cf_util_get_string(child, &router_data->username);
329     else if (strcasecmp("Password", child->key) == 0)
330       status = cf_util_get_string(child, &router_data->password);
331     else if (strcasecmp("CollectInterface", child->key) == 0)
332       cf_util_get_boolean(child, &router_data->collect_interface);
333     else if (strcasecmp("CollectRegistrationTable", child->key) == 0)
334       cf_util_get_boolean(child, &router_data->collect_regtable);
335 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
336     else if (strcasecmp("CollectCPULoad", child->key) == 0)
337       cf_util_get_boolean(child, &router_data->collect_cpu_load);
338     else if (strcasecmp("CollectMemory", child->key) == 0)
339       cf_util_get_boolean(child, &router_data->collect_memory);
340     else if (strcasecmp("CollectDF", child->key) == 0)
341       cf_util_get_boolean(child, &router_data->collect_df);
342     else if (strcasecmp("CollectDisk", child->key) == 0)
343       cf_util_get_boolean(child, &router_data->collect_disk);
344 #endif
345     else {
346       WARNING("routeros plugin: Unknown config option `%s'.", child->key);
347     }
348
349     if (status != 0)
350       break;
351   }
352
353   if (status == 0) {
354     if (router_data->node == NULL) {
355       ERROR("routeros plugin: No `Host' option within a `Router' block. "
356             "Where should I connect to?");
357       status = -1;
358     }
359
360     if (router_data->password == NULL) {
361       ERROR("routeros plugin: No `Password' option within a `Router' block. "
362             "How should I authenticate?");
363       status = -1;
364     }
365
366     if (!router_data->collect_interface && !router_data->collect_regtable) {
367       ERROR("routeros plugin: No `Collect*' option within a `Router' block. "
368             "What statistics should I collect?");
369       status = -1;
370     }
371   }
372
373   if ((status == 0) && (router_data->username == NULL)) {
374     router_data->username = sstrdup("admin");
375     if (router_data->username == NULL) {
376       ERROR("routeros plugin: sstrdup failed.");
377       status = -1;
378     }
379   }
380
381   ssnprintf(read_name, sizeof(read_name), "routeros/%s", router_data->node);
382   if (status == 0)
383     status = plugin_register_complex_read(
384         /* group = */ NULL, read_name, cr_read, /* interval = */ 0,
385         &(user_data_t){
386             .data = router_data, .free_func = (void *)cr_free_data,
387         });
388
389   if (status != 0)
390     cr_free_data(router_data);
391
392   return (status);
393 } /* }}} int cr_config_router */
394
395 static int cr_config(oconfig_item_t *ci) {
396   for (int i = 0; i < ci->children_num; i++) {
397     oconfig_item_t *child = ci->children + i;
398
399     if (strcasecmp("Router", child->key) == 0)
400       cr_config_router(child);
401     else {
402       WARNING("routeros plugin: Unknown config option `%s'.", child->key);
403     }
404   }
405
406   return (0);
407 } /* }}} int cr_config */
408
409 void module_register(void) {
410   plugin_register_complex_config("routeros", cr_config);
411 } /* void module_register */
412
413 /* vim: set sw=2 noet fdm=marker : */