2 * collectd - src/routeros.c
3 * Copyright (C) 2009 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
26 #include <routeros_api.h>
30 ros_connection_t *connection;
37 _Bool collect_interface;
38 _Bool collect_regtable;
39 _Bool collect_cpu_load;
44 typedef struct cr_data_s cr_data_t;
46 static void cr_submit_io (const char *type, const char *type_instance, /* {{{ */
47 counter_t rx, counter_t tx)
50 value_list_t vl = VALUE_LIST_INIT;
52 values[0].counter = rx;
53 values[1].counter = tx;
56 vl.values_len = STATIC_ARRAY_SIZE (values);
57 sstrncpy (vl.host, hostname_g, 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 (const ros_interface_t *i) /* {{{ */
72 submit_interface (i->next);
76 cr_submit_io ("if_packets", i->name,
77 (counter_t) i->rx_packets, (counter_t) i->tx_packets);
78 cr_submit_io ("if_octets", i->name,
79 (counter_t) i->rx_bytes, (counter_t) i->tx_bytes);
80 cr_submit_io ("if_errors", i->name,
81 (counter_t) i->rx_errors, (counter_t) i->tx_errors);
82 cr_submit_io ("if_dropped", i->name,
83 (counter_t) i->rx_drops, (counter_t) i->tx_drops);
85 submit_interface (i->next);
86 } /* }}} void submit_interface */
88 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
89 const ros_interface_t *i, __attribute__((unused)) void *user_data)
93 } /* }}} int handle_interface */
95 static void cr_submit_gauge (const char *type, /* {{{ */
96 const char *type_instance, gauge_t value)
99 value_list_t vl = VALUE_LIST_INIT;
101 values[0].gauge = value;
104 vl.values_len = STATIC_ARRAY_SIZE (values);
105 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
106 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
107 sstrncpy (vl.type, type, sizeof (vl.type));
108 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
110 plugin_dispatch_values (&vl);
111 } /* }}} void cr_submit_gauge */
113 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
114 static void cr_submit_counter (const char *type, /* {{{ */
115 const char *type_instance, counter_t value)
118 value_list_t vl = VALUE_LIST_INIT;
120 values[0].counter = value;
123 vl.values_len = STATIC_ARRAY_SIZE (values);
124 sstrncpy (vl.host, hostname_g, sizeof (vl.host)); /* FIXME */
125 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
126 sstrncpy (vl.type, type, sizeof (vl.type));
127 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
129 plugin_dispatch_values (&vl);
130 } /* }}} void cr_submit_gauge */
133 static void submit_regtable (const ros_registration_table_t *r) /* {{{ */
135 char type_instance[DATA_MAX_NAME_LEN];
141 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
142 r->interface, r->radio_name);
143 cr_submit_gauge ("bitrate", type_instance,
144 (gauge_t) (1000000.0 * r->rx_rate));
145 cr_submit_gauge ("signal_power", type_instance,
146 (gauge_t) r->rx_signal_strength);
147 cr_submit_gauge ("signal_quality", type_instance,
148 (gauge_t) r->rx_ccq);
151 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
152 r->interface, r->radio_name);
153 cr_submit_gauge ("bitrate", type_instance,
154 (gauge_t) (1000000.0 * r->tx_rate));
155 cr_submit_gauge ("signal_power", type_instance,
156 (gauge_t) r->tx_signal_strength);
157 cr_submit_gauge ("signal_quality", type_instance,
158 (gauge_t) r->tx_ccq);
161 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
162 r->interface, r->radio_name);
163 cr_submit_io ("if_octets", type_instance,
164 (counter_t) r->rx_bytes, (counter_t) r->tx_bytes);
165 cr_submit_gauge ("snr", type_instance, (gauge_t) r->signal_to_noise);
167 submit_regtable (r->next);
168 } /* }}} void submit_regtable */
170 static int handle_regtable (__attribute__((unused)) ros_connection_t *c, /* {{{ */
171 const ros_registration_table_t *r,
172 __attribute__((unused)) void *user_data)
176 } /* }}} int handle_regtable */
178 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0) /* FIXME */
179 static int handle_system_resource (__attribute__((unused)) ros_connection_t *c, /* {{{ */
180 const ros_system_resource_t *r,
181 __attribute__((unused)) void *user_data)
185 if ((r == NULL) || (user_data == NULL))
189 if (rd->collect_cpu_load)
190 cr_submit_gauge ("gauge", "cpu_load", (gauge_t) r->cpu_load);
192 if (rd->collect_memory)
194 cr_submit_gauge ("memory", "used",
195 (gauge_t) (r->total_memory - r->free_memory));
196 cr_submit_gauge ("memory", "free", (gauge_t) r->free_memory);
201 cr_submit_gauge ("df_complex", "used",
202 (gauge_t) (r->total_memory - r->free_memory));
203 cr_submit_gauge ("df_complex", "free", (gauge_t) r->free_memory);
206 if (rd->collect_disk)
208 cr_submit_counter ("counter", "secors_written", (counter_t) r->write_sect_total);
209 cr_submit_gauge ("gauge", "bad_blocks", (gauge_t) r->bad_blocks);
213 } /* }}} int handle_system_resource */
216 static int cr_read (user_data_t *user_data) /* {{{ */
221 if (user_data == NULL)
224 rd = user_data->data;
228 if (rd->connection == NULL)
230 rd->connection = ros_connect (rd->node, rd->service,
231 rd->username, rd->password);
232 if (rd->connection == NULL)
235 ERROR ("routeros plugin: ros_connect failed: %s",
236 sstrerror (errno, errbuf, sizeof (errbuf)));
240 assert (rd->connection != NULL);
242 if (rd->collect_interface)
244 status = ros_interface (rd->connection, handle_interface,
245 /* user data = */ NULL);
249 ERROR ("routeros plugin: ros_interface failed: %s",
250 sstrerror (status, errbuf, sizeof (errbuf)));
251 ros_disconnect (rd->connection);
252 rd->connection = NULL;
257 if (rd->collect_regtable)
259 status = ros_registration_table (rd->connection, handle_regtable,
260 /* user data = */ NULL);
264 ERROR ("routeros plugin: ros_registration_table failed: %s",
265 sstrerror (status, errbuf, sizeof (errbuf)));
266 ros_disconnect (rd->connection);
267 rd->connection = NULL;
272 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0) /* FIXME */
273 if (rd->collect_cpu_load
274 || rd->collect_memory
278 status = ros_system_resource (rd->connection, handle_system_resource,
279 /* user data = */ rd);
283 ERROR ("routeros plugin: ros_system_resource failed: %s",
284 sstrerror (status, errbuf, sizeof (errbuf)));
285 ros_disconnect (rd->connection);
286 rd->connection = NULL;
293 } /* }}} int cr_read */
295 static void cr_free_data (cr_data_t *ptr) /* {{{ */
300 ros_disconnect (ptr->connection);
301 ptr->connection = NULL;
304 sfree (ptr->service);
305 sfree (ptr->username);
306 sfree (ptr->password);
309 } /* }}} void cr_free_data */
311 static int cr_config_router (oconfig_item_t *ci) /* {{{ */
313 cr_data_t *router_data;
315 user_data_t user_data;
319 router_data = malloc (sizeof (*router_data));
320 if (router_data == NULL)
322 memset (router_data, 0, sizeof (router_data));
323 router_data->connection = NULL;
324 router_data->node = NULL;
325 router_data->service = NULL;
326 router_data->username = NULL;
327 router_data->password = NULL;
330 for (i = 0; i < ci->children_num; i++)
332 oconfig_item_t *child = ci->children + i;
334 if (strcasecmp ("Host", child->key) == 0)
335 status = cf_util_get_string (child, &router_data->node);
336 else if (strcasecmp ("Port", child->key) == 0)
337 status = cf_util_get_string (child, &router_data->service);
338 else if (strcasecmp ("User", child->key) == 0)
339 status = cf_util_get_string (child, &router_data->username);
340 else if (strcasecmp ("Password", child->key) == 0)
341 status = cf_util_get_string (child, &router_data->password);
342 else if (strcasecmp ("CollectInterface", child->key) == 0)
343 cf_util_get_boolean (child, &router_data->collect_interface);
344 else if (strcasecmp ("CollectRegistrationTable", child->key) == 0)
345 cf_util_get_boolean (child, &router_data->collect_regtable);
346 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0) /* FIXME */
347 else if (strcasecmp ("CollectCPULoad", child->key) == 0)
348 cf_util_get_boolean (child, &router_data->collect_cpu_load);
349 else if (strcasecmp ("CollectMemory", child->key) == 0)
350 cf_util_get_boolean (child, &router_data->collect_memory);
351 else if (strcasecmp ("CollectDF", child->key) == 0)
352 cf_util_get_boolean (child, &router_data->collect_df);
353 else if (strcasecmp ("CollectDisk", child->key) == 0)
354 cf_util_get_boolean (child, &router_data->collect_disk);
358 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
367 if (router_data->node == NULL)
369 ERROR ("routeros plugin: No `Host' option within a `Router' block. "
370 "Where should I connect to?");
374 if (router_data->password == NULL)
376 ERROR ("routeros plugin: No `Password' option within a `Router' block. "
377 "How should I authenticate?");
381 if (!router_data->collect_interface
382 && !router_data->collect_regtable)
384 ERROR ("routeros plugin: No `Collect*' option within a `Router' block. "
385 "What statistics should I collect?");
390 if ((status == 0) && (router_data->username == NULL))
392 router_data->username = sstrdup ("admin");
393 if (router_data->username == NULL)
395 ERROR ("routeros plugin: sstrdup failed.");
400 ssnprintf (read_name, sizeof (read_name), "routeros/%s", router_data->node);
401 user_data.data = router_data;
402 user_data.free_func = (void *) cr_free_data;
404 status = plugin_register_complex_read (read_name, cr_read,
405 /* interval = */ NULL, &user_data);
408 cr_free_data (router_data);
411 } /* }}} int cr_config_router */
413 static int cr_config (oconfig_item_t *ci)
417 for (i = 0; i < ci->children_num; i++)
419 oconfig_item_t *child = ci->children + i;
421 if (strcasecmp ("Router", child->key) == 0)
422 cr_config_router (child);
425 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
430 } /* }}} int cr_config */
432 void module_register (void)
434 plugin_register_complex_config ("routeros", cr_config);
435 } /* void module_register */
437 /* vim: set sw=2 noet fdm=marker : */