| 1 |
/** |
| 2 |
* collectd - src/interface.c |
| 3 |
* Copyright (C) 2005-2010 Florian octo Forster |
| 4 |
* Copyright (C) 2009 Manuel Sanmartin |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify it |
| 7 |
* under the terms of the GNU General Public License as published by the |
| 8 |
* Free Software Foundation; only version 2 of the License is applicable. |
| 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 |
* Florian octo Forster <octo at collectd.org> |
| 21 |
* Sune Marcher <sm at flork.dk> |
| 22 |
* Manuel Sanmartin |
| 23 |
**/ |
| 24 |
|
| 25 |
#include "collectd.h" |
| 26 |
|
| 27 |
#include "plugin.h" |
| 28 |
#include "utils/common/common.h" |
| 29 |
#include "utils/ignorelist/ignorelist.h" |
| 30 |
|
| 31 |
#if HAVE_SYS_TYPES_H |
| 32 |
#include <sys/types.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
/* One cannot include both. This sucks. */ |
| 36 |
#if HAVE_LINUX_IF_H |
| 37 |
#include <linux/if.h> |
| 38 |
#elif HAVE_NET_IF_H |
| 39 |
#include <net/if.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#if HAVE_LINUX_NETDEVICE_H |
| 43 |
#include <linux/netdevice.h> |
| 44 |
#endif |
| 45 |
#if HAVE_IFADDRS_H |
| 46 |
#include <ifaddrs.h> |
| 47 |
#endif |
| 48 |
|
| 49 |
#if HAVE_STATGRAB_H |
| 50 |
#include <statgrab.h> |
| 51 |
#endif |
| 52 |
|
| 53 |
#if HAVE_PERFSTAT |
| 54 |
#include <libperfstat.h> |
| 55 |
#include <sys/protosw.h> |
| 56 |
#endif |
| 57 |
|
| 58 |
/* |
| 59 |
* Various people have reported problems with `getifaddrs' and varying versions |
| 60 |
* of `glibc'. That's why it's disabled by default. Since more statistics are |
| 61 |
* available this way one may enable it using the `--enable-getifaddrs' option |
| 62 |
* of the configure script. -octo |
| 63 |
*/ |
| 64 |
#if KERNEL_LINUX |
| 65 |
#if !COLLECT_GETIFADDRS |
| 66 |
#undef HAVE_GETIFADDRS |
| 67 |
#endif /* !COLLECT_GETIFADDRS */ |
| 68 |
#endif /* KERNEL_LINUX */ |
| 69 |
|
| 70 |
#if HAVE_PERFSTAT |
| 71 |
static perfstat_netinterface_t *ifstat; |
| 72 |
static int nif; |
| 73 |
static int pnif; |
| 74 |
#endif /* HAVE_PERFSTAT */ |
| 75 |
|
| 76 |
#if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && \ |
| 77 |
!HAVE_LIBSTATGRAB && !HAVE_PERFSTAT |
| 78 |
#error "No applicable input method." |
| 79 |
#endif |
| 80 |
|
| 81 |
/* |
| 82 |
* (Module-)Global variables |
| 83 |
*/ |
| 84 |
static const char *config_keys[] = { |
| 85 |
"Interface", |
| 86 |
"IgnoreSelected", |
| 87 |
"ReportInactive", |
| 88 |
}; |
| 89 |
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); |
| 90 |
|
| 91 |
static ignorelist_t *ignorelist; |
| 92 |
|
| 93 |
static bool report_inactive = true; |
| 94 |
|
| 95 |
#ifdef HAVE_LIBKSTAT |
| 96 |
#if HAVE_KSTAT_H |
| 97 |
#include <kstat.h> |
| 98 |
#endif |
| 99 |
#define MAX_NUMIF 256 |
| 100 |
extern kstat_ctl_t *kc; |
| 101 |
static kstat_t *ksp[MAX_NUMIF]; |
| 102 |
static int numif; |
| 103 |
static bool unique_name; |
| 104 |
#endif /* HAVE_LIBKSTAT */ |
| 105 |
|
| 106 |
static int interface_config(const char *key, const char *value) { |
| 107 |
if (ignorelist == NULL) |
| 108 |
ignorelist = ignorelist_create(/* invert = */ 1); |
| 109 |
|
| 110 |
if (strcasecmp(key, "Interface") == 0) { |
| 111 |
ignorelist_add(ignorelist, value); |
| 112 |
} else if (strcasecmp(key, "IgnoreSelected") == 0) { |
| 113 |
int invert = 1; |
| 114 |
if (IS_TRUE(value)) |
| 115 |
invert = 0; |
| 116 |
ignorelist_set_invert(ignorelist, invert); |
| 117 |
} else if (strcasecmp(key, "ReportInactive") == 0) |
| 118 |
report_inactive = IS_TRUE(value); |
| 119 |
else if (strcasecmp(key, "UniqueName") == 0) { |
| 120 |
#ifdef HAVE_LIBKSTAT |
| 121 |
if (IS_TRUE(value)) |
| 122 |
unique_name = true; |
| 123 |
#else |
| 124 |
WARNING("interface plugin: the \"UniqueName\" option is only valid on " |
| 125 |
"Solaris."); |
| 126 |
#endif /* HAVE_LIBKSTAT */ |
| 127 |
} else { |
| 128 |
return -1; |
| 129 |
} |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
#if HAVE_LIBKSTAT |
| 135 |
static int interface_init(void) { |
| 136 |
kstat_t *ksp_chain; |
| 137 |
|
| 138 |
numif = 0; |
| 139 |
|
| 140 |
if (kc == NULL) |
| 141 |
return -1; |
| 142 |
|
| 143 |
for (numif = 0, ksp_chain = kc->kc_chain; |
| 144 |
(numif < MAX_NUMIF) && (ksp_chain != NULL); |
| 145 |
ksp_chain = ksp_chain->ks_next) { |
| 146 |
if (strncmp(ksp_chain->ks_class, "net", 3)) |
| 147 |
continue; |
| 148 |
if (ksp_chain->ks_type != KSTAT_TYPE_NAMED) |
| 149 |
continue; |
| 150 |
if (kstat_read(kc, ksp_chain, NULL) == -1) |
| 151 |
continue; |
| 152 |
if (get_kstat_value(ksp_chain, "obytes") == -1LL) |
| 153 |
continue; |
| 154 |
ksp[numif++] = ksp_chain; |
| 155 |
} |
| 156 |
|
| 157 |
return 0; |
| 158 |
} /* int interface_init */ |
| 159 |
#endif /* HAVE_LIBKSTAT */ |
| 160 |
|
| 161 |
static void if_submit(const char *dev, const char *type, derive_t rx, |
| 162 |
derive_t tx) { |
| 163 |
value_list_t vl = VALUE_LIST_INIT; |
| 164 |
value_t values[] = { |
| 165 |
{.derive = rx}, |
| 166 |
{.derive = tx}, |
| 167 |
}; |
| 168 |
|
| 169 |
if (ignorelist_match(ignorelist, dev) != 0) |
| 170 |
return; |
| 171 |
|
| 172 |
vl.values = values; |
| 173 |
vl.values_len = STATIC_ARRAY_SIZE(values); |
| 174 |
sstrncpy(vl.plugin, "interface", sizeof(vl.plugin)); |
| 175 |
sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance)); |
| 176 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 177 |
|
| 178 |
plugin_dispatch_values(&vl); |
| 179 |
} /* void if_submit */ |
| 180 |
|
| 181 |
static int interface_read(void) { |
| 182 |
#if KERNEL_LINUX |
| 183 |
FILE *fh; |
| 184 |
char buffer[1024]; |
| 185 |
derive_t incoming, outgoing; |
| 186 |
char *device; |
| 187 |
|
| 188 |
char *dummy; |
| 189 |
char *fields[16]; |
| 190 |
int numfields; |
| 191 |
|
| 192 |
if ((fh = fopen("/proc/net/dev", "r")) == NULL) { |
| 193 |
WARNING("interface plugin: fopen: %s", STRERRNO); |
| 194 |
return -1; |
| 195 |
} |
| 196 |
|
| 197 |
while (fgets(buffer, 1024, fh) != NULL) { |
| 198 |
if (!(dummy = strchr(buffer, ':'))) |
| 199 |
continue; |
| 200 |
dummy[0] = '\0'; |
| 201 |
dummy++; |
| 202 |
|
| 203 |
device = buffer; |
| 204 |
while (device[0] == ' ') |
| 205 |
device++; |
| 206 |
|
| 207 |
if (device[0] == '\0') |
| 208 |
continue; |
| 209 |
|
| 210 |
numfields = strsplit(dummy, fields, 16); |
| 211 |
|
| 212 |
if (numfields < 11) |
| 213 |
continue; |
| 214 |
|
| 215 |
incoming = atoll(fields[1]); |
| 216 |
outgoing = atoll(fields[9]); |
| 217 |
if (!report_inactive && incoming == 0 && outgoing == 0) |
| 218 |
continue; |
| 219 |
|
| 220 |
if_submit(device, "if_packets", incoming, outgoing); |
| 221 |
|
| 222 |
incoming = atoll(fields[0]); |
| 223 |
outgoing = atoll(fields[8]); |
| 224 |
if_submit(device, "if_octets", incoming, outgoing); |
| 225 |
|
| 226 |
incoming = atoll(fields[2]); |
| 227 |
outgoing = atoll(fields[10]); |
| 228 |
if_submit(device, "if_errors", incoming, outgoing); |
| 229 |
|
| 230 |
incoming = atoll(fields[3]); |
| 231 |
outgoing = atoll(fields[11]); |
| 232 |
if_submit(device, "if_dropped", incoming, outgoing); |
| 233 |
} |
| 234 |
|
| 235 |
fclose(fh); |
| 236 |
/* #endif KERNEL_LINUX */ |
| 237 |
|
| 238 |
#elif HAVE_GETIFADDRS |
| 239 |
struct ifaddrs *if_list; |
| 240 |
|
| 241 |
/* Darwin/Mac OS X and possible other *BSDs */ |
| 242 |
#if HAVE_STRUCT_IF_DATA |
| 243 |
#define IFA_DATA if_data |
| 244 |
#define IFA_RX_BYTES ifi_ibytes |
| 245 |
#define IFA_TX_BYTES ifi_obytes |
| 246 |
#define IFA_RX_PACKT ifi_ipackets |
| 247 |
#define IFA_TX_PACKT ifi_opackets |
| 248 |
#define IFA_RX_ERROR ifi_ierrors |
| 249 |
#define IFA_TX_ERROR ifi_oerrors |
| 250 |
/* #endif HAVE_STRUCT_IF_DATA */ |
| 251 |
|
| 252 |
#elif HAVE_STRUCT_NET_DEVICE_STATS |
| 253 |
#define IFA_DATA net_device_stats |
| 254 |
#define IFA_RX_BYTES rx_bytes |
| 255 |
#define IFA_TX_BYTES tx_bytes |
| 256 |
#define IFA_RX_PACKT rx_packets |
| 257 |
#define IFA_TX_PACKT tx_packets |
| 258 |
#define IFA_RX_ERROR rx_errors |
| 259 |
#define IFA_TX_ERROR tx_errors |
| 260 |
#else |
| 261 |
#error "No suitable type for `struct ifaddrs->ifa_data' found." |
| 262 |
#endif |
| 263 |
|
| 264 |
struct IFA_DATA *if_data; |
| 265 |
|
| 266 |
if (getifaddrs(&if_list) != 0) |
| 267 |
return -1; |
| 268 |
|
| 269 |
for (struct ifaddrs *if_ptr = if_list; if_ptr != NULL; |
| 270 |
if_ptr = if_ptr->ifa_next) { |
| 271 |
if (if_ptr->ifa_addr != NULL && if_ptr->ifa_addr->sa_family == AF_LINK) { |
| 272 |
if_data = (struct IFA_DATA *)if_ptr->ifa_data; |
| 273 |
|
| 274 |
if (!report_inactive && if_data->IFA_RX_PACKT == 0 && |
| 275 |
if_data->IFA_TX_PACKT == 0) |
| 276 |
continue; |
| 277 |
|
| 278 |
if_submit(if_ptr->ifa_name, "if_octets", if_data->IFA_RX_BYTES, |
| 279 |
if_data->IFA_TX_BYTES); |
| 280 |
if_submit(if_ptr->ifa_name, "if_packets", if_data->IFA_RX_PACKT, |
| 281 |
if_data->IFA_TX_PACKT); |
| 282 |
if_submit(if_ptr->ifa_name, "if_errors", if_data->IFA_RX_ERROR, |
| 283 |
if_data->IFA_TX_ERROR); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
freeifaddrs(if_list); |
| 288 |
/* #endif HAVE_GETIFADDRS */ |
| 289 |
|
| 290 |
#elif HAVE_LIBKSTAT |
| 291 |
derive_t rx; |
| 292 |
derive_t tx; |
| 293 |
char iname[DATA_MAX_NAME_LEN]; |
| 294 |
|
| 295 |
if (kc == NULL) |
| 296 |
return -1; |
| 297 |
|
| 298 |
for (int i = 0; i < numif; i++) { |
| 299 |
if (kstat_read(kc, ksp[i], NULL) == -1) |
| 300 |
continue; |
| 301 |
|
| 302 |
if (unique_name) |
| 303 |
ssnprintf(iname, sizeof(iname), "%s_%d_%s", ksp[i]->ks_module, |
| 304 |
ksp[i]->ks_instance, ksp[i]->ks_name); |
| 305 |
else |
| 306 |
sstrncpy(iname, ksp[i]->ks_name, sizeof(iname)); |
| 307 |
|
| 308 |
/* try to get 64bit counters */ |
| 309 |
rx = get_kstat_value(ksp[i], "ipackets64"); |
| 310 |
tx = get_kstat_value(ksp[i], "opackets64"); |
| 311 |
/* or fallback to 32bit */ |
| 312 |
if (rx == -1LL) |
| 313 |
rx = get_kstat_value(ksp[i], "ipackets"); |
| 314 |
if (tx == -1LL) |
| 315 |
tx = get_kstat_value(ksp[i], "opackets"); |
| 316 |
if (!report_inactive && rx == 0 && tx == 0) |
| 317 |
continue; |
| 318 |
if ((rx != -1LL) || (tx != -1LL)) |
| 319 |
if_submit(iname, "if_packets", rx, tx); |
| 320 |
|
| 321 |
/* try to get 64bit counters */ |
| 322 |
rx = get_kstat_value(ksp[i], "rbytes64"); |
| 323 |
tx = get_kstat_value(ksp[i], "obytes64"); |
| 324 |
/* or fallback to 32bit */ |
| 325 |
if (rx == -1LL) |
| 326 |
rx = get_kstat_value(ksp[i], "rbytes"); |
| 327 |
if (tx == -1LL) |
| 328 |
tx = get_kstat_value(ksp[i], "obytes"); |
| 329 |
if ((rx != -1LL) || (tx != -1LL)) |
| 330 |
if_submit(iname, "if_octets", rx, tx); |
| 331 |
|
| 332 |
/* no 64bit error counters yet */ |
| 333 |
rx = get_kstat_value(ksp[i], "ierrors"); |
| 334 |
tx = get_kstat_value(ksp[i], "oerrors"); |
| 335 |
if ((rx != -1LL) || (tx != -1LL)) |
| 336 |
if_submit(iname, "if_errors", rx, tx); |
| 337 |
} |
| 338 |
/* #endif HAVE_LIBKSTAT */ |
| 339 |
|
| 340 |
#elif defined(HAVE_LIBSTATGRAB) |
| 341 |
sg_network_io_stats *ios; |
| 342 |
int num; |
| 343 |
|
| 344 |
ios = sg_get_network_io_stats(&num); |
| 345 |
|
| 346 |
for (int i = 0; i < num; i++) { |
| 347 |
if (!report_inactive && ios[i].rx == 0 && ios[i].tx == 0) |
| 348 |
continue; |
| 349 |
if_submit(ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx); |
| 350 |
} |
| 351 |
/* #endif HAVE_LIBSTATGRAB */ |
| 352 |
|
| 353 |
#elif defined(HAVE_PERFSTAT) |
| 354 |
perfstat_id_t id; |
| 355 |
int ifs; |
| 356 |
|
| 357 |
if ((nif = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), |
| 358 |
0)) < 0) { |
| 359 |
WARNING("interface plugin: perfstat_netinterface: %s", STRERRNO); |
| 360 |
return -1; |
| 361 |
} |
| 362 |
|
| 363 |
if (pnif != nif || ifstat == NULL) { |
| 364 |
free(ifstat); |
| 365 |
ifstat = malloc(nif * sizeof(*ifstat)); |
| 366 |
} |
| 367 |
pnif = nif; |
| 368 |
|
| 369 |
id.name[0] = '\0'; |
| 370 |
if ((ifs = perfstat_netinterface(&id, ifstat, sizeof(perfstat_netinterface_t), |
| 371 |
nif)) < 0) { |
| 372 |
WARNING("interface plugin: perfstat_netinterface (interfaces=%d): %s", nif, |
| 373 |
STRERRNO); |
| 374 |
return -1; |
| 375 |
} |
| 376 |
|
| 377 |
for (int i = 0; i < ifs; i++) { |
| 378 |
if (!report_inactive && ifstat[i].ipackets == 0 && ifstat[i].opackets == 0) |
| 379 |
continue; |
| 380 |
|
| 381 |
if_submit(ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes); |
| 382 |
if_submit(ifstat[i].name, "if_packets", ifstat[i].ipackets, |
| 383 |
ifstat[i].opackets); |
| 384 |
if_submit(ifstat[i].name, "if_errors", ifstat[i].ierrors, |
| 385 |
ifstat[i].oerrors); |
| 386 |
} |
| 387 |
#endif /* HAVE_PERFSTAT */ |
| 388 |
|
| 389 |
return 0; |
| 390 |
} /* int interface_read */ |
| 391 |
|
| 392 |
void module_register(void) { |
| 393 |
plugin_register_config("interface", interface_config, config_keys, |
| 394 |
config_keys_num); |
| 395 |
#if HAVE_LIBKSTAT |
| 396 |
plugin_register_init("interface", interface_init); |
| 397 |
#endif |
| 398 |
plugin_register_read("interface", interface_read); |
| 399 |
} /* void module_register */ |