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