2ade3bb9c9d2cceb90a71f8950353dd214f2c80a
[collectd.git] / src / routeros.c
1 /**
2  * collectd - src/routeros.c
3  * Copyright (C) 2009,2010  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at collectd.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #include <routeros_api.h>
27
28 struct cr_data_s
29 {
30   ros_connection_t *connection;
31
32   char *node;
33   char *service;
34   char *username;
35   char *password;
36
37   _Bool collect_interface;
38   _Bool collect_regtable;
39   _Bool collect_cpu_load;
40   _Bool collect_memory;
41   _Bool collect_df;
42   _Bool collect_disk;
43 };
44 typedef struct cr_data_s cr_data_t;
45
46 static void cr_submit_io (cr_data_t *rd, const char *type, /* {{{ */
47     const char *type_instance, derive_t rx, derive_t tx)
48 {
49         value_t values[2];
50         value_list_t vl = VALUE_LIST_INIT;
51
52         values[0].derive = rx;
53         values[1].derive = tx;
54
55         vl.values = values;
56         vl.values_len = STATIC_ARRAY_SIZE (values);
57         sstrncpy (vl.host, rd->node, sizeof (vl.host));
58         sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
59         sstrncpy (vl.type, type, sizeof (vl.type));
60         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
61
62         plugin_dispatch_values (&vl);
63 } /* }}} void cr_submit_io */
64
65 static void submit_interface (cr_data_t *rd, /* {{{ */
66     const ros_interface_t *i)
67 {
68   if (i == NULL)
69     return;
70
71   if (!i->running)
72   {
73     submit_interface (rd, i->next);
74     return;
75   }
76
77   cr_submit_io (rd, "if_packets", i->name,
78       (derive_t) i->rx_packets, (derive_t) i->tx_packets);
79   cr_submit_io (rd, "if_octets", i->name,
80       (derive_t) i->rx_bytes, (derive_t) i->tx_bytes);
81   cr_submit_io (rd, "if_errors", i->name,
82       (derive_t) i->rx_errors, (derive_t) i->tx_errors);
83   cr_submit_io (rd, "if_dropped", i->name,
84       (derive_t) i->rx_drops, (derive_t) i->tx_drops);
85
86   submit_interface (rd, i->next);
87 } /* }}} void submit_interface */
88
89 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
90     const ros_interface_t *i, void *user_data)
91 {
92   if ((i == NULL) || (user_data == NULL))
93     return (EINVAL);
94
95   submit_interface (user_data, i);
96   return (0);
97 } /* }}} int handle_interface */
98
99 static void cr_submit_gauge (cr_data_t *rd, const char *type, /* {{{ */
100     const char *type_instance, gauge_t value)
101 {
102         value_t values[1];
103         value_list_t vl = VALUE_LIST_INIT;
104
105         values[0].gauge = value;
106
107         vl.values = values;
108         vl.values_len = STATIC_ARRAY_SIZE (values);
109         sstrncpy (vl.host, rd->node, sizeof (vl.host));
110         sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
111         sstrncpy (vl.type, type, sizeof (vl.type));
112         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
113
114         plugin_dispatch_values (&vl);
115 } /* }}} void cr_submit_gauge */
116
117 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
118 static void cr_submit_counter (cr_data_t *rd, const char *type, /* {{{ */
119     const char *type_instance, derive_t value)
120 {
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 {
140   char type_instance[DATA_MAX_NAME_LEN];
141
142   if (r == NULL)
143     return;
144
145   /*** RX ***/
146   ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
147       r->interface, r->radio_name);
148   cr_submit_gauge (rd, "bitrate", type_instance,
149       (gauge_t) (1000000.0 * r->rx_rate));
150   cr_submit_gauge (rd, "signal_power", type_instance,
151       (gauge_t) r->rx_signal_strength);
152   cr_submit_gauge (rd, "signal_quality", type_instance,
153       (gauge_t) r->rx_ccq);
154
155   /*** TX ***/
156   ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
157       r->interface, r->radio_name);
158   cr_submit_gauge (rd, "bitrate", type_instance,
159       (gauge_t) (1000000.0 * r->tx_rate));
160   cr_submit_gauge (rd, "signal_power", type_instance,
161       (gauge_t) r->tx_signal_strength);
162   cr_submit_gauge (rd, "signal_quality", type_instance,
163       (gauge_t) r->tx_ccq);
164
165   /*** RX / TX ***/
166   ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
167       r->interface, r->radio_name);
168   cr_submit_io (rd, "if_octets", type_instance,
169       (derive_t) r->rx_bytes, (derive_t) r->tx_bytes);
170   cr_submit_gauge (rd, "snr", type_instance, (gauge_t) r->signal_to_noise);
171
172   submit_regtable (rd, r->next);
173 } /* }}} void submit_regtable */
174
175 static int handle_regtable (__attribute__((unused)) ros_connection_t *c, /* {{{ */
176     const ros_registration_table_t *r, void *user_data)
177 {
178   if ((r == NULL) || (user_data == NULL))
179     return (EINVAL);
180
181   submit_regtable (user_data, r);
182   return (0);
183 } /* }}} int handle_regtable */
184
185 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
186 static int handle_system_resource (__attribute__((unused)) ros_connection_t *c, /* {{{ */
187         const ros_system_resource_t *r,
188         __attribute__((unused)) void *user_data)
189 {
190   cr_data_t *rd;
191
192   if ((r == NULL) || (user_data == NULL))
193     return (EINVAL);
194   rd = user_data;
195
196   if (rd->collect_cpu_load)
197     cr_submit_gauge (rd, "gauge", "cpu_load", (gauge_t) r->cpu_load);
198
199   if (rd->collect_memory)
200   {
201     cr_submit_gauge (rd, "memory", "used",
202         (gauge_t) (r->total_memory - r->free_memory));
203     cr_submit_gauge (rd, "memory", "free", (gauge_t) r->free_memory);
204   }
205
206   if (rd->collect_df)
207   {
208     cr_submit_gauge (rd, "df_complex", "used",
209         (gauge_t) (r->total_memory - r->free_memory));
210     cr_submit_gauge (rd, "df_complex", "free", (gauge_t) r->free_memory);
211   }
212
213   if (rd->collect_disk)
214   {
215     cr_submit_counter (rd, "counter", "secors_written", (derive_t) r->write_sect_total);
216     cr_submit_gauge (rd, "gauge", "bad_blocks", (gauge_t) r->bad_blocks);
217   }
218
219   return (0);
220 } /* }}} int handle_system_resource */
221 #endif
222
223 static int cr_read (user_data_t *user_data) /* {{{ */
224 {
225   int status;
226   cr_data_t *rd;
227
228   if (user_data == NULL)
229     return (EINVAL);
230
231   rd = user_data->data;
232   if (rd == NULL)
233     return (EINVAL);
234
235   if (rd->connection == NULL)
236   {
237     rd->connection = ros_connect (rd->node, rd->service,
238         rd->username, rd->password);
239     if (rd->connection == NULL)
240     {
241       char errbuf[128];
242       ERROR ("routeros plugin: ros_connect failed: %s",
243           sstrerror (errno, errbuf, sizeof (errbuf)));
244       return (-1);
245     }
246   }
247   assert (rd->connection != NULL);
248
249   if (rd->collect_interface)
250   {
251     status = ros_interface (rd->connection, handle_interface,
252         /* user data = */ rd);
253     if (status != 0)
254     {
255       char errbuf[128];
256       ERROR ("routeros plugin: ros_interface failed: %s",
257           sstrerror (status, errbuf, sizeof (errbuf)));
258       ros_disconnect (rd->connection);
259       rd->connection = NULL;
260       return (-1);
261     }
262   }
263
264   if (rd->collect_regtable)
265   {
266     status = ros_registration_table (rd->connection, handle_regtable,
267         /* user data = */ rd);
268     if (status != 0)
269     {
270       char errbuf[128];
271       ERROR ("routeros plugin: ros_registration_table failed: %s",
272           sstrerror (status, errbuf, sizeof (errbuf)));
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
281       || rd->collect_memory
282       || rd->collect_df
283       || rd->collect_disk)
284   {
285     status = ros_system_resource (rd->connection, handle_system_resource,
286         /* user data = */ rd);
287     if (status != 0)
288     {
289       char errbuf[128];
290       ERROR ("routeros plugin: ros_system_resource failed: %s",
291           sstrerror (status, errbuf, sizeof (errbuf)));
292       ros_disconnect (rd->connection);
293       rd->connection = NULL;
294       return (-1);
295     }
296   }
297 #endif
298
299   return (0);
300 } /* }}} int cr_read */
301
302 static void cr_free_data (cr_data_t *ptr) /* {{{ */
303 {
304   if (ptr == NULL)
305     return;
306
307   ros_disconnect (ptr->connection);
308   ptr->connection = NULL;
309
310   sfree (ptr->node);
311   sfree (ptr->service);
312   sfree (ptr->username);
313   sfree (ptr->password);
314
315   sfree (ptr);
316 } /* }}} void cr_free_data */
317
318 static int cr_config_router (oconfig_item_t *ci) /* {{{ */
319 {
320   cr_data_t *router_data;
321   char read_name[128];
322   user_data_t user_data;
323   int status;
324   int i;
325
326   router_data = malloc (sizeof (*router_data));
327   if (router_data == NULL)
328     return (-1);
329   memset (router_data, 0, sizeof (router_data));
330   router_data->connection = NULL;
331   router_data->node = NULL;
332   router_data->service = NULL;
333   router_data->username = NULL;
334   router_data->password = NULL;
335
336   status = 0;
337   for (i = 0; i < ci->children_num; i++)
338   {
339     oconfig_item_t *child = ci->children + i;
340
341     if (strcasecmp ("Host", child->key) == 0)
342       status = cf_util_get_string (child, &router_data->node);
343     else if (strcasecmp ("Port", child->key) == 0)
344       status = cf_util_get_service (child, &router_data->service);
345     else if (strcasecmp ("User", child->key) == 0)
346       status = cf_util_get_string (child, &router_data->username);
347     else if (strcasecmp ("Password", child->key) == 0)
348       status = cf_util_get_string (child, &router_data->password);
349     else if (strcasecmp ("CollectInterface", child->key) == 0)
350       cf_util_get_boolean (child, &router_data->collect_interface);
351     else if (strcasecmp ("CollectRegistrationTable", child->key) == 0)
352       cf_util_get_boolean (child, &router_data->collect_regtable);
353 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
354     else if (strcasecmp ("CollectCPULoad", child->key) == 0)
355       cf_util_get_boolean (child, &router_data->collect_cpu_load);
356     else if (strcasecmp ("CollectMemory", child->key) == 0)
357       cf_util_get_boolean (child, &router_data->collect_memory);
358     else if (strcasecmp ("CollectDF", child->key) == 0)
359       cf_util_get_boolean (child, &router_data->collect_df);
360     else if (strcasecmp ("CollectDisk", child->key) == 0)
361       cf_util_get_boolean (child, &router_data->collect_disk);
362 #endif
363     else
364     {
365       WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
366     }
367
368     if (status != 0)
369       break;
370   }
371
372   if (status == 0)
373   {
374     if (router_data->node == NULL)
375     {
376       ERROR ("routeros plugin: No `Host' option within a `Router' block. "
377           "Where should I connect to?");
378       status = -1;
379     }
380
381     if (router_data->password == NULL)
382     {
383       ERROR ("routeros plugin: No `Password' option within a `Router' block. "
384           "How should I authenticate?");
385       status = -1;
386     }
387
388     if (!router_data->collect_interface
389         && !router_data->collect_regtable)
390     {
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   {
399     router_data->username = sstrdup ("admin");
400     if (router_data->username == NULL)
401     {
402       ERROR ("routeros plugin: sstrdup failed.");
403       status = -1;
404     }
405   }
406
407   ssnprintf (read_name, sizeof (read_name), "routeros/%s", router_data->node);
408   user_data.data = router_data;
409   user_data.free_func = (void *) cr_free_data;
410   if (status == 0)
411     status = plugin_register_complex_read (/* group = */ NULL, read_name,
412         cr_read, /* interval = */ NULL, &user_data);
413
414   if (status != 0)
415     cr_free_data (router_data);
416
417   return (status);
418 } /* }}} int cr_config_router */
419
420 static int cr_config (oconfig_item_t *ci)
421 {
422   int i;
423
424   for (i = 0; i < ci->children_num; i++)
425   {
426     oconfig_item_t *child = ci->children + i;
427
428     if (strcasecmp ("Router", child->key) == 0)
429       cr_config_router (child);
430     else
431     {
432       WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
433     }
434   }
435
436   return (0);
437 } /* }}} int cr_config */
438
439 void module_register (void)
440 {
441   plugin_register_complex_config ("routeros", cr_config);
442 } /* void module_register */
443
444 /* vim: set sw=2 noet fdm=marker : */