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 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
31 #include <routeros_api.h>
33 struct cr_data_s
34 {
35 ros_connection_t *connection;
37 char *node;
38 char *service;
39 char *username;
40 char *password;
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;
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;
57 values[0].derive = rx;
58 values[1].derive = tx;
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));
67 plugin_dispatch_values (&vl);
68 } /* }}} void cr_submit_io */
70 static void submit_interface (cr_data_t *rd, /* {{{ */
71 const ros_interface_t *i)
72 {
73 if (i == NULL)
74 return;
76 if (!i->running)
77 {
78 submit_interface (rd, i->next);
79 return;
80 }
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);
91 submit_interface (rd, i->next);
92 } /* }}} void submit_interface */
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);
100 submit_interface (user_data, i);
101 return (0);
102 } /* }}} int handle_interface */
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;
110 values[0].gauge = value;
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));
119 plugin_dispatch_values (&vl);
120 } /* }}} void cr_submit_gauge */
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;
129 values[0].derive = value;
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));
138 plugin_dispatch_values (&vl);
139 } /* }}} void cr_submit_gauge */
140 #endif
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];
147 if (r == NULL)
148 return;
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);
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);
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);
177 submit_regtable (rd, r->next);
178 } /* }}} void submit_regtable */
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);
186 submit_regtable (user_data, r);
187 return (0);
188 } /* }}} int handle_regtable */
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;
197 if ((r == NULL) || (user_data == NULL))
198 return (EINVAL);
199 rd = user_data;
201 if (rd->collect_cpu_load)
202 cr_submit_gauge (rd, "gauge", "cpu_load", (gauge_t) r->cpu_load);
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 }
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 }
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 }
224 return (0);
225 } /* }}} int handle_system_resource */
226 #endif
228 static int cr_read (user_data_t *user_data) /* {{{ */
229 {
230 int status;
231 cr_data_t *rd;
233 if (user_data == NULL)
234 return (EINVAL);
236 rd = user_data->data;
237 if (rd == NULL)
238 return (EINVAL);
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);
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 }
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 }
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
304 return (0);
305 } /* }}} int cr_read */
307 static void cr_free_data (cr_data_t *ptr) /* {{{ */
308 {
309 if (ptr == NULL)
310 return;
312 ros_disconnect (ptr->connection);
313 ptr->connection = NULL;
315 sfree (ptr->node);
316 sfree (ptr->service);
317 sfree (ptr->username);
318 sfree (ptr->password);
320 sfree (ptr);
321 } /* }}} void cr_free_data */
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;
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;
340 status = 0;
341 for (i = 0; i < ci->children_num; i++)
342 {
343 oconfig_item_t *child = ci->children + i;
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 }
372 if (status != 0)
373 break;
374 }
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 }
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 }
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 }
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 }
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);
418 if (status != 0)
419 cr_free_data (router_data);
421 return (status);
422 } /* }}} int cr_config_router */
424 static int cr_config (oconfig_item_t *ci)
425 {
426 int i;
428 for (i = 0; i < ci->children_num; i++)
429 {
430 oconfig_item_t *child = ci->children + i;
432 if (strcasecmp ("Router", child->key) == 0)
433 cr_config_router (child);
434 else
435 {
436 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
437 }
438 }
440 return (0);
441 } /* }}} int cr_config */
443 void module_register (void)
444 {
445 plugin_register_complex_config ("routeros", cr_config);
446 } /* void module_register */
448 /* vim: set sw=2 noet fdm=marker : */