routeros plugin: Added first version of a plugin for MikroTik's RouterOS.
[collectd.git] / src / routeros.c
1 /**
2  * collectd - src/load.c
3  * Copyright (C) 2009  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 verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #include <routeros_api.h>
27
28 static ros_connection_t *connection = NULL;
29
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";
34
35 static void cr_submit_io (const char *type, const char *type_instance, /* {{{ */
36     counter_t rx, counter_t tx)
37 {
38         value_t values[2];
39         value_list_t vl = VALUE_LIST_INIT;
40
41         values[0].counter = rx;
42         values[1].counter = tx;
43
44         vl.values = values;
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));
50
51         plugin_dispatch_values (&vl);
52 } /* }}} void cr_submit_io */
53
54 static void submit_interface (const ros_interface_t *i) /* {{{ */
55 {
56   if (i == NULL)
57     return;
58
59   if (!i->running)
60   {
61     submit_interface (i->next);
62     return;
63   }
64
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);
73
74   submit_interface (i->next);
75 } /* }}} void submit_interface */
76
77 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
78     const ros_interface_t *i, __attribute__((unused)) void *user_data)
79 {
80   submit_interface (i);
81   return (0);
82 } /* }}} int handle_interface */
83
84 static int cr_read (void) /* {{{ */
85 {
86   int status;
87
88   if (connection == NULL)
89   {
90     connection = ros_connect (conf_node, conf_service,
91         conf_username, conf_password);
92     if (connection == NULL)
93     {
94       char errbuf[128];
95       ERROR ("routeros plugin: ros_connect failed: %s",
96           sstrerror (errno, errbuf, sizeof (errbuf)));
97       return (-1);
98     }
99   }
100   assert (connection != NULL);
101
102   status = ros_interface (connection, handle_interface, /* user data = */ NULL);
103   if (status != 0)
104   {
105     char errbuf[128];
106     ERROR ("routeros plugin: ros_interface failed: %s",
107         sstrerror (status, errbuf, sizeof (errbuf)));
108     ros_disconnect (connection);
109     connection = NULL;
110     return (-1);
111   }
112
113   return (0);
114 } /* }}} int cr_read */
115
116 void module_register (void)
117 {
118         plugin_register_read ("routeros", cr_read);
119 } /* void module_register */
120
121 /* vim: set sw=2 noet fdm=marker : */