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