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