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"
29 #include "common.h"
30 #include "plugin.h"
32 #include <routeros_api.h>
34 struct cr_data_s
35 {
36 ros_connection_t *connection;
38 char *node;
39 char *service;
40 char *username;
41 char *password;
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;
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;
58 values[0].derive = rx;
59 values[1].derive = tx;
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));
68 plugin_dispatch_values (&vl);
69 } /* }}} void cr_submit_io */
71 static void submit_interface (cr_data_t *rd, /* {{{ */
72 const ros_interface_t *i)
73 {
74 if (i == NULL)
75 return;
77 if (!i->running)
78 {
79 submit_interface (rd, i->next);
80 return;
81 }
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);
92 submit_interface (rd, i->next);
93 } /* }}} void submit_interface */
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);
101 submit_interface (user_data, i);
102 return (0);
103 } /* }}} int handle_interface */
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;
111 values[0].gauge = value;
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));
120 plugin_dispatch_values (&vl);
121 } /* }}} void cr_submit_gauge */
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;
130 values[0].derive = value;
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));
139 plugin_dispatch_values (&vl);
140 } /* }}} void cr_submit_gauge */
141 #endif
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];
148 if (r == NULL)
149 return;
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);
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);
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);
178 submit_regtable (rd, r->next);
179 } /* }}} void submit_regtable */
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);
187 submit_regtable (user_data, r);
188 return (0);
189 } /* }}} int handle_regtable */
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;
198 if ((r == NULL) || (user_data == NULL))
199 return (EINVAL);
200 rd = user_data;
202 if (rd->collect_cpu_load)
203 cr_submit_gauge (rd, "gauge", "cpu_load", (gauge_t) r->cpu_load);
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 }
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 }
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 }
225 return (0);
226 } /* }}} int handle_system_resource */
227 #endif
229 static int cr_read (user_data_t *user_data) /* {{{ */
230 {
231 int status;
232 cr_data_t *rd;
234 if (user_data == NULL)
235 return (EINVAL);
237 rd = user_data->data;
238 if (rd == NULL)
239 return (EINVAL);
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);
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 }
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 }
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
305 return (0);
306 } /* }}} int cr_read */
308 static void cr_free_data (cr_data_t *ptr) /* {{{ */
309 {
310 if (ptr == NULL)
311 return;
313 ros_disconnect (ptr->connection);
314 ptr->connection = NULL;
316 sfree (ptr->node);
317 sfree (ptr->service);
318 sfree (ptr->username);
319 sfree (ptr->password);
321 sfree (ptr);
322 } /* }}} void cr_free_data */
324 static int cr_config_router (oconfig_item_t *ci) /* {{{ */
325 {
326 cr_data_t *router_data;
327 char read_name[128];
328 int status;
330 router_data = calloc (1, sizeof (*router_data));
331 if (router_data == NULL)
332 return (-1);
333 router_data->connection = NULL;
334 router_data->node = NULL;
335 router_data->service = NULL;
336 router_data->username = NULL;
337 router_data->password = NULL;
339 status = 0;
340 for (int i = 0; i < ci->children_num; i++)
341 {
342 oconfig_item_t *child = ci->children + i;
344 if (strcasecmp ("Host", child->key) == 0)
345 status = cf_util_get_string (child, &router_data->node);
346 else if (strcasecmp ("Port", child->key) == 0)
347 status = cf_util_get_service (child, &router_data->service);
348 else if (strcasecmp ("User", child->key) == 0)
349 status = cf_util_get_string (child, &router_data->username);
350 else if (strcasecmp ("Password", child->key) == 0)
351 status = cf_util_get_string (child, &router_data->password);
352 else if (strcasecmp ("CollectInterface", child->key) == 0)
353 cf_util_get_boolean (child, &router_data->collect_interface);
354 else if (strcasecmp ("CollectRegistrationTable", child->key) == 0)
355 cf_util_get_boolean (child, &router_data->collect_regtable);
356 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
357 else if (strcasecmp ("CollectCPULoad", child->key) == 0)
358 cf_util_get_boolean (child, &router_data->collect_cpu_load);
359 else if (strcasecmp ("CollectMemory", child->key) == 0)
360 cf_util_get_boolean (child, &router_data->collect_memory);
361 else if (strcasecmp ("CollectDF", child->key) == 0)
362 cf_util_get_boolean (child, &router_data->collect_df);
363 else if (strcasecmp ("CollectDisk", child->key) == 0)
364 cf_util_get_boolean (child, &router_data->collect_disk);
365 #endif
366 else
367 {
368 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
369 }
371 if (status != 0)
372 break;
373 }
375 if (status == 0)
376 {
377 if (router_data->node == NULL)
378 {
379 ERROR ("routeros plugin: No `Host' option within a `Router' block. "
380 "Where should I connect to?");
381 status = -1;
382 }
384 if (router_data->password == NULL)
385 {
386 ERROR ("routeros plugin: No `Password' option within a `Router' block. "
387 "How should I authenticate?");
388 status = -1;
389 }
391 if (!router_data->collect_interface
392 && !router_data->collect_regtable)
393 {
394 ERROR ("routeros plugin: No `Collect*' option within a `Router' block. "
395 "What statistics should I collect?");
396 status = -1;
397 }
398 }
400 if ((status == 0) && (router_data->username == NULL))
401 {
402 router_data->username = sstrdup ("admin");
403 if (router_data->username == NULL)
404 {
405 ERROR ("routeros plugin: sstrdup failed.");
406 status = -1;
407 }
408 }
410 ssnprintf (read_name, sizeof (read_name), "routeros/%s", router_data->node);
411 if (status == 0)
412 status = plugin_register_complex_read (/* group = */ NULL, read_name,
413 cr_read, /* interval = */ 0, &(user_data_t) {
414 .data = router_data,
415 .free_func = (void *) cr_free_data,
416 });
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 for (int i = 0; i < ci->children_num; i++)
427 {
428 oconfig_item_t *child = ci->children + i;
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 }
438 return (0);
439 } /* }}} int cr_config */
441 void module_register (void)
442 {
443 plugin_register_complex_config ("routeros", cr_config);
444 } /* void module_register */
446 /* vim: set sw=2 noet fdm=marker : */