routeros plugin: Add support for the "system resource" high-level interface.
[collectd.git] / src / routeros.c
1 /**
2  * collectd - src/routeros.c
3  * Copyright (C) 2009  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 verplant.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 (const char *type, const char *type_instance, /* {{{ */
47     counter_t rx, counter_t tx)
48 {
49         value_t values[2];
50         value_list_t vl = VALUE_LIST_INIT;
51
52         values[0].counter = rx;
53         values[1].counter = tx;
54
55         vl.values = values;
56         vl.values_len = STATIC_ARRAY_SIZE (values);
57         sstrncpy (vl.host, hostname_g, 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 (const ros_interface_t *i) /* {{{ */
66 {
67   if (i == NULL)
68     return;
69
70   if (!i->running)
71   {
72     submit_interface (i->next);
73     return;
74   }
75
76   cr_submit_io ("if_packets", i->name,
77       (counter_t) i->rx_packets, (counter_t) i->tx_packets);
78   cr_submit_io ("if_octets", i->name,
79       (counter_t) i->rx_bytes, (counter_t) i->tx_bytes);
80   cr_submit_io ("if_errors", i->name,
81       (counter_t) i->rx_errors, (counter_t) i->tx_errors);
82   cr_submit_io ("if_dropped", i->name,
83       (counter_t) i->rx_drops, (counter_t) i->tx_drops);
84
85   submit_interface (i->next);
86 } /* }}} void submit_interface */
87
88 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
89     const ros_interface_t *i, __attribute__((unused)) void *user_data)
90 {
91   submit_interface (i);
92   return (0);
93 } /* }}} int handle_interface */
94
95 static void cr_submit_gauge (const char *type, /* {{{ */
96     const char *type_instance, gauge_t value)
97 {
98         value_t values[1];
99         value_list_t vl = VALUE_LIST_INIT;
100
101         values[0].gauge = value;
102
103         vl.values = values;
104         vl.values_len = STATIC_ARRAY_SIZE (values);
105         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
106         sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
107         sstrncpy (vl.type, type, sizeof (vl.type));
108         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
109
110         plugin_dispatch_values (&vl);
111 } /* }}} void cr_submit_gauge */
112
113 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
114 static void cr_submit_counter (const char *type, /* {{{ */
115     const char *type_instance, counter_t value)
116 {
117         value_t values[1];
118         value_list_t vl = VALUE_LIST_INIT;
119
120         values[0].counter = value;
121
122         vl.values = values;
123         vl.values_len = STATIC_ARRAY_SIZE (values);
124         sstrncpy (vl.host, hostname_g, sizeof (vl.host)); /* FIXME */
125         sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
126         sstrncpy (vl.type, type, sizeof (vl.type));
127         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
128
129         plugin_dispatch_values (&vl);
130 } /* }}} void cr_submit_gauge */
131 #endif
132
133 static void submit_regtable (const ros_registration_table_t *r) /* {{{ */
134 {
135   char type_instance[DATA_MAX_NAME_LEN];
136
137   if (r == NULL)
138     return;
139
140   /*** RX ***/
141   ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
142       r->interface, r->radio_name);
143   cr_submit_gauge ("bitrate", type_instance,
144       (gauge_t) (1000000.0 * r->rx_rate));
145   cr_submit_gauge ("signal_power", type_instance,
146       (gauge_t) r->rx_signal_strength);
147   cr_submit_gauge ("signal_quality", type_instance,
148       (gauge_t) r->rx_ccq);
149
150   /*** TX ***/
151   ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
152       r->interface, r->radio_name);
153   cr_submit_gauge ("bitrate", type_instance,
154       (gauge_t) (1000000.0 * r->tx_rate));
155   cr_submit_gauge ("signal_power", type_instance,
156       (gauge_t) r->tx_signal_strength);
157   cr_submit_gauge ("signal_quality", type_instance,
158       (gauge_t) r->tx_ccq);
159
160   /*** RX / TX ***/
161   ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
162       r->interface, r->radio_name);
163   cr_submit_io ("if_octets", type_instance,
164       (counter_t) r->rx_bytes, (counter_t) r->tx_bytes);
165   cr_submit_gauge ("snr", type_instance, (gauge_t) r->signal_to_noise);
166
167   submit_regtable (r->next);
168 } /* }}} void submit_regtable */
169
170 static int handle_regtable (__attribute__((unused)) ros_connection_t *c, /* {{{ */
171     const ros_registration_table_t *r,
172     __attribute__((unused)) void *user_data)
173 {
174   submit_regtable (r);
175   return (0);
176 } /* }}} int handle_regtable */
177
178 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0) /* FIXME */
179 static int handle_system_resource (__attribute__((unused)) ros_connection_t *c, /* {{{ */
180         const ros_system_resource_t *r,
181         __attribute__((unused)) void *user_data)
182 {
183   cr_data_t *rd;
184
185   if ((r == NULL) || (user_data == NULL))
186     return (EINVAL);
187   rd = user_data;
188
189   if (rd->collect_cpu_load)
190     cr_submit_gauge ("gauge", "cpu_load", (gauge_t) r->cpu_load);
191
192   if (rd->collect_memory)
193   {
194     cr_submit_gauge ("memory", "used",
195         (gauge_t) (r->total_memory - r->free_memory));
196     cr_submit_gauge ("memory", "free", (gauge_t) r->free_memory);
197   }
198
199   if (rd->collect_df)
200   {
201     cr_submit_gauge ("df_complex", "used",
202         (gauge_t) (r->total_memory - r->free_memory));
203     cr_submit_gauge ("df_complex", "free", (gauge_t) r->free_memory);
204   }
205
206   if (rd->collect_disk)
207   {
208     cr_submit_counter ("counter", "secors_written", (counter_t) r->write_sect_total);
209     cr_submit_gauge ("gauge", "bad_blocks", (gauge_t) r->bad_blocks);
210   }
211
212   return (0);
213 } /* }}} int handle_system_resource */
214 #endif
215
216 static int cr_read (user_data_t *user_data) /* {{{ */
217 {
218   int status;
219   cr_data_t *rd;
220
221   if (user_data == NULL)
222     return (EINVAL);
223
224   rd = user_data->data;
225   if (rd == NULL)
226     return (EINVAL);
227
228   if (rd->connection == NULL)
229   {
230     rd->connection = ros_connect (rd->node, rd->service,
231         rd->username, rd->password);
232     if (rd->connection == NULL)
233     {
234       char errbuf[128];
235       ERROR ("routeros plugin: ros_connect failed: %s",
236           sstrerror (errno, errbuf, sizeof (errbuf)));
237       return (-1);
238     }
239   }
240   assert (rd->connection != NULL);
241
242   if (rd->collect_interface)
243   {
244     status = ros_interface (rd->connection, handle_interface,
245         /* user data = */ NULL);
246     if (status != 0)
247     {
248       char errbuf[128];
249       ERROR ("routeros plugin: ros_interface failed: %s",
250           sstrerror (status, errbuf, sizeof (errbuf)));
251       ros_disconnect (rd->connection);
252       rd->connection = NULL;
253       return (-1);
254     }
255   }
256
257   if (rd->collect_regtable)
258   {
259     status = ros_registration_table (rd->connection, handle_regtable,
260         /* user data = */ NULL);
261     if (status != 0)
262     {
263       char errbuf[128];
264       ERROR ("routeros plugin: ros_registration_table failed: %s",
265           sstrerror (status, errbuf, sizeof (errbuf)));
266       ros_disconnect (rd->connection);
267       rd->connection = NULL;
268       return (-1);
269     }
270   }
271
272 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0) /* FIXME */
273   if (rd->collect_cpu_load
274       || rd->collect_memory
275       || rd->collect_df
276       || rd->collect_disk)
277   {
278     status = ros_system_resource (rd->connection, handle_system_resource,
279         /* user data = */ rd);
280     if (status != 0)
281     {
282       char errbuf[128];
283       ERROR ("routeros plugin: ros_system_resource failed: %s",
284           sstrerror (status, errbuf, sizeof (errbuf)));
285       ros_disconnect (rd->connection);
286       rd->connection = NULL;
287       return (-1);
288     }
289   }
290 #endif
291
292   return (0);
293 } /* }}} int cr_read */
294
295 static void cr_free_data (cr_data_t *ptr) /* {{{ */
296 {
297   if (ptr == NULL)
298     return;
299
300   ros_disconnect (ptr->connection);
301   ptr->connection = NULL;
302
303   sfree (ptr->node);
304   sfree (ptr->service);
305   sfree (ptr->username);
306   sfree (ptr->password);
307
308   sfree (ptr);
309 } /* }}} void cr_free_data */
310
311 static int cr_config_router (oconfig_item_t *ci) /* {{{ */
312 {
313   cr_data_t *router_data;
314   char read_name[128];
315   user_data_t user_data;
316   int status;
317   int i;
318
319   router_data = malloc (sizeof (*router_data));
320   if (router_data == NULL)
321     return (-1);
322   memset (router_data, 0, sizeof (router_data));
323   router_data->connection = NULL;
324   router_data->node = NULL;
325   router_data->service = NULL;
326   router_data->username = NULL;
327   router_data->password = NULL;
328
329   status = 0;
330   for (i = 0; i < ci->children_num; i++)
331   {
332     oconfig_item_t *child = ci->children + i;
333
334     if (strcasecmp ("Host", child->key) == 0)
335       status = cf_util_get_string (child, &router_data->node);
336     else if (strcasecmp ("Port", child->key) == 0)
337       status = cf_util_get_string (child, &router_data->service);
338     else if (strcasecmp ("User", child->key) == 0)
339       status = cf_util_get_string (child, &router_data->username);
340     else if (strcasecmp ("Password", child->key) == 0)
341       status = cf_util_get_string (child, &router_data->password);
342     else if (strcasecmp ("CollectInterface", child->key) == 0)
343       cf_util_get_boolean (child, &router_data->collect_interface);
344     else if (strcasecmp ("CollectRegistrationTable", child->key) == 0)
345       cf_util_get_boolean (child, &router_data->collect_regtable);
346 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0) /* FIXME */
347     else if (strcasecmp ("CollectCPULoad", child->key) == 0)
348       cf_util_get_boolean (child, &router_data->collect_cpu_load);
349     else if (strcasecmp ("CollectMemory", child->key) == 0)
350       cf_util_get_boolean (child, &router_data->collect_memory);
351     else if (strcasecmp ("CollectDF", child->key) == 0)
352       cf_util_get_boolean (child, &router_data->collect_df);
353     else if (strcasecmp ("CollectDisk", child->key) == 0)
354       cf_util_get_boolean (child, &router_data->collect_disk);
355 #endif
356     else
357     {
358       WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
359     }
360
361     if (status != 0)
362       break;
363   }
364
365   if (status == 0)
366   {
367     if (router_data->node == NULL)
368     {
369       ERROR ("routeros plugin: No `Host' option within a `Router' block. "
370           "Where should I connect to?");
371       status = -1;
372     }
373
374     if (router_data->password == NULL)
375     {
376       ERROR ("routeros plugin: No `Password' option within a `Router' block. "
377           "How should I authenticate?");
378       status = -1;
379     }
380
381     if (!router_data->collect_interface
382         && !router_data->collect_regtable)
383     {
384       ERROR ("routeros plugin: No `Collect*' option within a `Router' block. "
385           "What statistics should I collect?");
386       status = -1;
387     }
388   }
389
390   if ((status == 0) && (router_data->username == NULL))
391   {
392     router_data->username = sstrdup ("admin");
393     if (router_data->username == NULL)
394     {
395       ERROR ("routeros plugin: sstrdup failed.");
396       status = -1;
397     }
398   }
399
400   ssnprintf (read_name, sizeof (read_name), "routeros/%s", router_data->node);
401   user_data.data = router_data;
402   user_data.free_func = (void *) cr_free_data;
403   if (status == 0)
404     status = plugin_register_complex_read (read_name, cr_read,
405         /* interval = */ NULL, &user_data);
406
407   if (status != 0)
408     cr_free_data (router_data);
409
410   return (status);
411 } /* }}} int cr_config_router */
412
413 static int cr_config (oconfig_item_t *ci)
414 {
415   int i;
416
417   for (i = 0; i < ci->children_num; i++)
418   {
419     oconfig_item_t *child = ci->children + i;
420
421     if (strcasecmp ("Router", child->key) == 0)
422       cr_config_router (child);
423     else
424     {
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 {
434   plugin_register_complex_config ("routeros", cr_config);
435 } /* void module_register */
436
437 /* vim: set sw=2 noet fdm=marker : */