Merge pull request #3339 from jkohen/patch-1
[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 #include "common.h"
29 #include "plugin.h"
30
31 #include <routeros_api.h>
32
33 struct cr_data_s
34 {
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 {
54         value_t values[2];
55         value_list_t vl = VALUE_LIST_INIT;
56
57         values[0].derive = rx;
58         values[1].derive = tx;
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 {
73   if (i == NULL)
74     return;
75
76   if (!i->running)
77   {
78     submit_interface (rd, i->next);
79     return;
80   }
81
82   cr_submit_io (rd, "if_packets", i->name,
83       (derive_t) i->rx_packets, (derive_t) i->tx_packets);
84   cr_submit_io (rd, "if_octets", i->name,
85       (derive_t) i->rx_bytes, (derive_t) i->tx_bytes);
86   cr_submit_io (rd, "if_errors", i->name,
87       (derive_t) i->rx_errors, (derive_t) i->tx_errors);
88   cr_submit_io (rd, "if_dropped", i->name,
89       (derive_t) i->rx_drops, (derive_t) i->tx_drops);
90
91   submit_interface (rd, i->next);
92 } /* }}} void submit_interface */
93
94 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
95     const ros_interface_t *i, void *user_data)
96 {
97   if ((i == NULL) || (user_data == NULL))
98     return (EINVAL);
99
100   submit_interface (user_data, i);
101   return (0);
102 } /* }}} int handle_interface */
103
104 static void cr_submit_gauge (cr_data_t *rd, const char *type, /* {{{ */
105     const char *type_instance, gauge_t value)
106 {
107         value_t values[1];
108         value_list_t vl = VALUE_LIST_INIT;
109
110         values[0].gauge = value;
111
112         vl.values = values;
113         vl.values_len = STATIC_ARRAY_SIZE (values);
114         sstrncpy (vl.host, rd->node, sizeof (vl.host));
115         sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
116         sstrncpy (vl.type, type, sizeof (vl.type));
117         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
118
119         plugin_dispatch_values (&vl);
120 } /* }}} void cr_submit_gauge */
121
122 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
123 static void cr_submit_counter (cr_data_t *rd, const char *type, /* {{{ */
124     const char *type_instance, derive_t value)
125 {
126         value_t values[1];
127         value_list_t vl = VALUE_LIST_INIT;
128
129         values[0].derive = value;
130
131         vl.values = values;
132         vl.values_len = STATIC_ARRAY_SIZE (values);
133         sstrncpy (vl.host, rd->node, sizeof (vl.host));
134         sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
135         sstrncpy (vl.type, type, sizeof (vl.type));
136         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
137
138         plugin_dispatch_values (&vl);
139 } /* }}} void cr_submit_gauge */
140 #endif
141
142 static void submit_regtable (cr_data_t *rd, /* {{{ */
143     const ros_registration_table_t *r)
144 {
145   char type_instance[DATA_MAX_NAME_LEN];
146
147   if (r == NULL)
148     return;
149
150   /*** RX ***/
151   ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
152       r->interface, r->radio_name);
153   cr_submit_gauge (rd, "bitrate", type_instance,
154       (gauge_t) (1000000.0 * r->rx_rate));
155   cr_submit_gauge (rd, "signal_power", type_instance,
156       (gauge_t) r->rx_signal_strength);
157   cr_submit_gauge (rd, "signal_quality", type_instance,
158       (gauge_t) r->rx_ccq);
159
160   /*** TX ***/
161   ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
162       r->interface, r->radio_name);
163   cr_submit_gauge (rd, "bitrate", type_instance,
164       (gauge_t) (1000000.0 * r->tx_rate));
165   cr_submit_gauge (rd, "signal_power", type_instance,
166       (gauge_t) r->tx_signal_strength);
167   cr_submit_gauge (rd, "signal_quality", type_instance,
168       (gauge_t) r->tx_ccq);
169
170   /*** RX / TX ***/
171   ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
172       r->interface, r->radio_name);
173   cr_submit_io (rd, "if_octets", type_instance,
174       (derive_t) r->rx_bytes, (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)) ros_connection_t *c, /* {{{ */
181     const ros_registration_table_t *r, void *user_data)
182 {
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)) ros_connection_t *c, /* {{{ */
192         const ros_system_resource_t *r,
193         __attribute__((unused)) void *user_data)
194 {
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   {
206     cr_submit_gauge (rd, "memory", "used",
207         (gauge_t) (r->total_memory - r->free_memory));
208     cr_submit_gauge (rd, "memory", "free", (gauge_t) r->free_memory);
209   }
210
211   if (rd->collect_df)
212   {
213     cr_submit_gauge (rd, "df_complex", "used",
214         (gauge_t) (r->total_memory - r->free_memory));
215     cr_submit_gauge (rd, "df_complex", "free", (gauge_t) r->free_memory);
216   }
217
218   if (rd->collect_disk)
219   {
220     cr_submit_counter (rd, "counter", "secors_written", (derive_t) r->write_sect_total);
221     cr_submit_gauge (rd, "gauge", "bad_blocks", (gauge_t) r->bad_blocks);
222   }
223
224   return (0);
225 } /* }}} int handle_system_resource */
226 #endif
227
228 static int cr_read (user_data_t *user_data) /* {{{ */
229 {
230   int status;
231   cr_data_t *rd;
232
233   if (user_data == NULL)
234     return (EINVAL);
235
236   rd = user_data->data;
237   if (rd == NULL)
238     return (EINVAL);
239
240   if (rd->connection == NULL)
241   {
242     rd->connection = ros_connect (rd->node, rd->service,
243         rd->username, rd->password);
244     if (rd->connection == NULL)
245     {
246       char errbuf[128];
247       ERROR ("routeros plugin: ros_connect failed: %s",
248           sstrerror (errno, errbuf, sizeof (errbuf)));
249       return (-1);
250     }
251   }
252   assert (rd->connection != NULL);
253
254   if (rd->collect_interface)
255   {
256     status = ros_interface (rd->connection, handle_interface,
257         /* user data = */ rd);
258     if (status != 0)
259     {
260       char errbuf[128];
261       ERROR ("routeros plugin: ros_interface 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 (rd->collect_regtable)
270   {
271     status = ros_registration_table (rd->connection, handle_regtable,
272         /* user data = */ rd);
273     if (status != 0)
274     {
275       char errbuf[128];
276       ERROR ("routeros plugin: ros_registration_table failed: %s",
277           sstrerror (status, errbuf, sizeof (errbuf)));
278       ros_disconnect (rd->connection);
279       rd->connection = NULL;
280       return (-1);
281     }
282   }
283
284 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
285   if (rd->collect_cpu_load
286       || rd->collect_memory
287       || rd->collect_df
288       || rd->collect_disk)
289   {
290     status = ros_system_resource (rd->connection, handle_system_resource,
291         /* user data = */ rd);
292     if (status != 0)
293     {
294       char errbuf[128];
295       ERROR ("routeros plugin: ros_system_resource failed: %s",
296           sstrerror (status, errbuf, sizeof (errbuf)));
297       ros_disconnect (rd->connection);
298       rd->connection = NULL;
299       return (-1);
300     }
301   }
302 #endif
303
304   return (0);
305 } /* }}} int cr_read */
306
307 static void cr_free_data (cr_data_t *ptr) /* {{{ */
308 {
309   if (ptr == NULL)
310     return;
311
312   ros_disconnect (ptr->connection);
313   ptr->connection = NULL;
314
315   sfree (ptr->node);
316   sfree (ptr->service);
317   sfree (ptr->username);
318   sfree (ptr->password);
319
320   sfree (ptr);
321 } /* }}} void cr_free_data */
322
323 static int cr_config_router (oconfig_item_t *ci) /* {{{ */
324 {
325   cr_data_t *router_data;
326   char read_name[128];
327   user_data_t user_data;
328   int status;
329   int i;
330
331   router_data = malloc (sizeof (*router_data));
332   if (router_data == NULL)
333     return (-1);
334   memset (router_data, 0, sizeof (router_data));
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 (i = 0; i < ci->children_num; i++)
343   {
344     oconfig_item_t *child = ci->children + i;
345
346     if (strcasecmp ("Host", child->key) == 0)
347       status = cf_util_get_string (child, &router_data->node);
348     else if (strcasecmp ("Port", child->key) == 0)
349       status = cf_util_get_service (child, &router_data->service);
350     else if (strcasecmp ("User", child->key) == 0)
351       status = cf_util_get_string (child, &router_data->username);
352     else if (strcasecmp ("Password", child->key) == 0)
353       status = cf_util_get_string (child, &router_data->password);
354     else if (strcasecmp ("CollectInterface", child->key) == 0)
355       cf_util_get_boolean (child, &router_data->collect_interface);
356     else if (strcasecmp ("CollectRegistrationTable", child->key) == 0)
357       cf_util_get_boolean (child, &router_data->collect_regtable);
358 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
359     else if (strcasecmp ("CollectCPULoad", child->key) == 0)
360       cf_util_get_boolean (child, &router_data->collect_cpu_load);
361     else if (strcasecmp ("CollectMemory", child->key) == 0)
362       cf_util_get_boolean (child, &router_data->collect_memory);
363     else if (strcasecmp ("CollectDF", child->key) == 0)
364       cf_util_get_boolean (child, &router_data->collect_df);
365     else if (strcasecmp ("CollectDisk", child->key) == 0)
366       cf_util_get_boolean (child, &router_data->collect_disk);
367 #endif
368     else
369     {
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   {
379     if (router_data->node == NULL)
380     {
381       ERROR ("routeros plugin: No `Host' option within a `Router' block. "
382           "Where should I connect to?");
383       status = -1;
384     }
385
386     if (router_data->password == NULL)
387     {
388       ERROR ("routeros plugin: No `Password' option within a `Router' block. "
389           "How should I authenticate?");
390       status = -1;
391     }
392
393     if (!router_data->collect_interface
394         && !router_data->collect_regtable)
395     {
396       ERROR ("routeros plugin: No `Collect*' option within a `Router' block. "
397           "What statistics should I collect?");
398       status = -1;
399     }
400   }
401
402   if ((status == 0) && (router_data->username == NULL))
403   {
404     router_data->username = sstrdup ("admin");
405     if (router_data->username == NULL)
406     {
407       ERROR ("routeros plugin: sstrdup failed.");
408       status = -1;
409     }
410   }
411
412   ssnprintf (read_name, sizeof (read_name), "routeros/%s", router_data->node);
413   user_data.data = router_data;
414   user_data.free_func = (void *) cr_free_data;
415   if (status == 0)
416     status = plugin_register_complex_read (/* group = */ NULL, read_name,
417         cr_read, /* interval = */ NULL, &user_data);
418
419   if (status != 0)
420     cr_free_data (router_data);
421
422   return (status);
423 } /* }}} int cr_config_router */
424
425 static int cr_config (oconfig_item_t *ci)
426 {
427   int i;
428
429   for (i = 0; i < ci->children_num; i++)
430   {
431     oconfig_item_t *child = ci->children + i;
432
433     if (strcasecmp ("Router", child->key) == 0)
434       cr_config_router (child);
435     else
436     {
437       WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
438     }
439   }
440
441   return (0);
442 } /* }}} int cr_config */
443
444 void module_register (void)
445 {
446   plugin_register_complex_config ("routeros", cr_config);
447 } /* void module_register */
448
449 /* vim: set sw=2 noet fdm=marker : */