2 * collectd - src/load.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>
28 static ros_connection_t *connection = NULL;
30 static char *conf_node = "router.example.com";
31 static char *conf_service = NULL;
32 static char *conf_username = "collectd";
33 static char *conf_password = "secret";
35 static void cr_submit_io (const char *type, const char *type_instance, /* {{{ */
36 counter_t rx, counter_t tx)
39 value_list_t vl = VALUE_LIST_INIT;
41 values[0].counter = rx;
42 values[1].counter = tx;
45 vl.values_len = STATIC_ARRAY_SIZE (values);
46 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
47 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
48 sstrncpy (vl.type, type, sizeof (vl.type));
49 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
51 plugin_dispatch_values (&vl);
52 } /* }}} void cr_submit_io */
54 static void submit_interface (const ros_interface_t *i) /* {{{ */
61 submit_interface (i->next);
65 cr_submit_io ("if_packets", i->name,
66 (counter_t) i->rx_packets, (counter_t) i->tx_packets);
67 cr_submit_io ("if_octets", i->name,
68 (counter_t) i->rx_bytes, (counter_t) i->tx_bytes);
69 cr_submit_io ("if_errors", i->name,
70 (counter_t) i->rx_errors, (counter_t) i->tx_errors);
71 cr_submit_io ("if_dropped", i->name,
72 (counter_t) i->rx_drops, (counter_t) i->tx_drops);
74 submit_interface (i->next);
75 } /* }}} void submit_interface */
77 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
78 const ros_interface_t *i, __attribute__((unused)) void *user_data)
82 } /* }}} int handle_interface */
84 static int cr_read (void) /* {{{ */
88 if (connection == NULL)
90 connection = ros_connect (conf_node, conf_service,
91 conf_username, conf_password);
92 if (connection == NULL)
95 ERROR ("routeros plugin: ros_connect failed: %s",
96 sstrerror (errno, errbuf, sizeof (errbuf)));
100 assert (connection != NULL);
102 status = ros_interface (connection, handle_interface, /* user data = */ NULL);
106 ERROR ("routeros plugin: ros_interface failed: %s",
107 sstrerror (status, errbuf, sizeof (errbuf)));
108 ros_disconnect (connection);
114 } /* }}} int cr_read */
116 void module_register (void)
118 plugin_register_read ("routeros", cr_read);
119 } /* void module_register */
121 /* vim: set sw=2 noet fdm=marker : */