Code

ethstat plugin: Fix includes.
[collectd.git] / src / ethstat.c
1 /**
2  * collectd - src/ethstat.c
3  * Copyright (C) 2011       Cyril Feraudet
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; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Cyril Feraudet <cyril at feraudet.com>
21  **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "ethstat.h"
29 #include <sys/ioctl.h>
30 #include <net/if.h>
31 #include <linux/sockios.h>
33 static int ethstat_config (const char *key, const char *value)
34 {
35         if (strcasecmp (key, "Iface") == 0) {
36                 ifacelist[ifacenumber] = malloc(strlen(value) + 1);
37                 strcpy(ifacelist[ifacenumber++], value);
38                 INFO("ethstat: Registred iface %s", value);
39         }
40         return (0);
41 }
44 static void ethstat_submit_value (char *devname, char *counter, unsigned long long value)
45 {
46         value_t values[1];
47         value_list_t vl = VALUE_LIST_INIT;
49         values[0].counter = value;
51         vl.values = values;
52         vl.values_len = 1;
53         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
54         sstrncpy (vl.plugin, "ethstat", sizeof (vl.plugin));
55         sstrncpy (vl.plugin_instance, devname, sizeof (vl.plugin_instance));
56         sstrncpy (vl.type, "derive", sizeof (vl.type));
57         sstrncpy (vl.type_instance, counter, sizeof (vl.type_instance));
59         plugin_dispatch_values (&vl);
60 }
63 static int getstats(char *devname, struct ifreq *ifr) {
64         int fd;
65         struct ethtool_drvinfo drvinfo;
66         struct ethtool_gstrings *strings;
67         struct ethtool_stats *stats;
68         unsigned int n_stats, sz_str, sz_stats, i;
69         int err;
72         fd = socket(AF_INET, SOCK_DGRAM, 0);
73         if (fd < 0) {
74                 ERROR("ethstat - %s : Cannot get control socket", devname);
75                 return 1;
76         }
78         drvinfo.cmd = ETHTOOL_GDRVINFO;
79         ifr->ifr_data = (caddr_t)&drvinfo;
80         err = ioctl(fd, SIOCETHTOOL, ifr);
81         if (err < 0) {
82                 ERROR("ethstat - %s : Cannot get driver information", devname);
83                 return 1;
84         }
87         n_stats = drvinfo.n_stats;
88         if (n_stats < 1) {
89                 ERROR("ethstat - %s : No stats available", devname);
90                 return 1;
91         }
93         sz_str = n_stats * ETH_GSTRING_LEN;
94         sz_stats = n_stats * sizeof(u64);
96         strings = calloc(1, sz_str + sizeof(struct ethtool_gstrings));
97         stats = calloc(1, sz_stats + sizeof(struct ethtool_stats));
98         if (!strings || !stats) {
99                 ERROR("ethstat - %s No memory available", devname);
100                 return 1;
101         }
103         strings->cmd = ETHTOOL_GSTRINGS;
104         strings->string_set = ETH_SS_STATS;
105         strings->len = n_stats;
106         ifr->ifr_data = (caddr_t) strings;
107         err = ioctl(fd, SIOCETHTOOL, ifr);
108         if (err < 0) {
109                 ERROR("ethstat - %s : Cannot get stats strings information", devname);
110                 free(strings);
111                 free(stats);
112                 return 96;
113         }
115         stats->cmd = ETHTOOL_GSTATS;
116         stats->n_stats = n_stats;
117         ifr->ifr_data = (caddr_t) stats;
118         err = ioctl(fd, SIOCETHTOOL, ifr);
119         if (err < 0) {
120                 ERROR("ethstat - %s : Cannot get stats information", devname);
121                 free(strings);
122                 free(stats);
123                 return 97;
124         }
126         for (i = 0; i < n_stats; i++) {
127                 DEBUG("ethstat - %s : %s: %llu",
128                         devname,
129                         &strings->data[i * ETH_GSTRING_LEN],
130                         stats->data[i]);
131                 ethstat_submit_value (
132                         devname,
133                         (char*)&strings->data[i * ETH_GSTRING_LEN],
134                         stats->data[i]);
135         }
136         free(strings);
137         free(stats);
140         return 0;
144 static int ethstat_read(void)
146         struct ifreq ifr;
147         int i;
149         for (i = 0 ; i < ifacenumber ; i++) {
150                 DEBUG("ethstat - Processing : %s\n", ifacelist[i]);
151                 memset(&ifr, 0, sizeof(ifr));
152                 strcpy(ifr.ifr_name, ifacelist[i]);
153                 getstats(ifacelist[i], &ifr);
154         }
155         return 0;
158 void module_register (void)
160         ifacelist = malloc(sizeof(char*));
161         plugin_register_config ("ethstat", ethstat_config,
162                         config_keys, config_keys_num);
163         plugin_register_read ("ethstat", ethstat_read);