| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <math.h> |
| 27 |
#include <sys/socket.h> |
| 28 |
#include <linux/ethtool.h> |
| 29 |
#include <linux/if.h> |
| 30 |
#include <linux/wireless.h> |
| 31 |
|
| 32 |
#include "common.h" |
| 33 |
#include "logging.h" |
| 34 |
#include "string-macros.h" |
| 35 |
#include "messages.h" |
| 36 |
#include "netinfo.h" |
| 37 |
#include "netinfo-private.h" |
| 38 |
#include "system.h" |
| 39 |
#include "xasprintf.h" |
| 40 |
|
| 41 |
extern char *const duplex_table[]; |
| 42 |
|
| 43 |
struct iflist * |
| 44 |
netinfo (unsigned int options, const char *ifname_regex, unsigned int seconds, |
| 45 |
unsigned int *ninterfaces) |
| 46 |
{ |
| 47 |
bool opt_check_link = (options & CHECK_LINK); |
| 48 |
char msgbuf[256]; |
| 49 |
int rc; |
| 50 |
regex_t regex; |
| 51 |
struct iflist *iflhead, *ifl, *iflhead2, *ifl2; |
| 52 |
|
| 53 |
if ((rc = |
| 54 |
regcomp (®ex, ifname_regex ? ifname_regex : ".*", REG_EXTENDED))) |
| 55 |
{ |
| 56 |
regerror (rc, ®ex, msgbuf, sizeof (msgbuf)); |
| 57 |
plugin_error (STATE_UNKNOWN, 0, "could not compile regex: %s", msgbuf); |
| 58 |
} |
| 59 |
|
| 60 |
dbg ("getting network informations...\n"); |
| 61 |
iflhead = get_netinfo_snapshot (options, ®ex); |
| 62 |
|
| 63 |
if (seconds > 0) |
| 64 |
{ |
| 65 |
sleep (seconds); |
| 66 |
|
| 67 |
dbg ("getting network informations again (after %us)...\n", seconds); |
| 68 |
iflhead2 = get_netinfo_snapshot (options, ®ex); |
| 69 |
|
| 70 |
*ninterfaces = 0; |
| 71 |
for (ifl = iflhead, ifl2 = iflhead2; ifl != NULL && ifl2 != NULL; |
| 72 |
ifl = ifl->next, ifl2 = ifl2->next) |
| 73 |
{ |
| 74 |
if (STRNEQ (ifl->ifname, ifl2->ifname)) |
| 75 |
plugin_error (STATE_UNKNOWN, 0, |
| 76 |
"bug in netinfo(), please contact the developers"); |
| 77 |
|
| 78 |
dbg ("network interface '%s'\n", ifl->ifname); |
| 79 |
|
| 80 |
bool if_up = if_flags_UP (ifl->flags), |
| 81 |
if_running = if_flags_RUNNING (ifl->flags); |
| 82 |
|
| 83 |
if (ifl->stats) |
| 84 |
{ |
| 85 |
#define DIV(a, b) ceil (((b) - (a)) / (double)seconds) |
| 86 |
dbg ("\ttx_packets : %u %u\n", |
| 87 |
ifl->stats->tx_packets, ifl2->stats->tx_packets); |
| 88 |
ifl->stats->tx_packets = DIV (ifl->stats->tx_packets, |
| 89 |
ifl2->stats->tx_packets); |
| 90 |
|
| 91 |
dbg ("\trx_packets : %u %u\n", |
| 92 |
ifl->stats->rx_packets, ifl2->stats->rx_packets); |
| 93 |
ifl->stats->rx_packets = DIV (ifl->stats->rx_packets, |
| 94 |
ifl2->stats->rx_packets); |
| 95 |
|
| 96 |
dbg ("\ttx_bytes : %u %u\n", |
| 97 |
ifl->stats->tx_bytes, ifl2->stats->tx_bytes); |
| 98 |
ifl->stats->tx_bytes = DIV (ifl->stats->tx_bytes, |
| 99 |
ifl2->stats->tx_bytes); |
| 100 |
|
| 101 |
dbg ("\trx_bytes : %u %u\n", |
| 102 |
ifl->stats->rx_bytes, ifl2->stats->rx_bytes); |
| 103 |
ifl->stats->rx_bytes = DIV (ifl->stats->rx_bytes, |
| 104 |
ifl2->stats->rx_bytes); |
| 105 |
|
| 106 |
dbg ("\ttx_errors : %u %u\n", |
| 107 |
ifl->stats->tx_errors, ifl2->stats->tx_errors); |
| 108 |
ifl->stats->tx_errors = DIV (ifl->stats->tx_errors, |
| 109 |
ifl2->stats->tx_errors); |
| 110 |
|
| 111 |
dbg ("\trx_errors : %u %u\n", |
| 112 |
ifl->stats->rx_errors, ifl2->stats->rx_errors); |
| 113 |
ifl->stats->rx_errors = DIV (ifl->stats->rx_errors, |
| 114 |
ifl2->stats->rx_errors); |
| 115 |
|
| 116 |
dbg ("\ttx_dropped : %u %u\n", |
| 117 |
ifl->stats->tx_dropped, ifl2->stats->tx_dropped); |
| 118 |
ifl->stats->tx_dropped = DIV (ifl->stats->tx_dropped, |
| 119 |
ifl2->stats->tx_dropped); |
| 120 |
|
| 121 |
dbg ("\trx_dropped : %u %u\n", |
| 122 |
ifl->stats->rx_dropped, ifl2->stats->rx_dropped); |
| 123 |
ifl->stats->rx_dropped = DIV (ifl->stats->rx_dropped, |
| 124 |
ifl2->stats->rx_dropped); |
| 125 |
|
| 126 |
dbg ("\tcollisions : %u %u\n", |
| 127 |
ifl->stats->collisions, ifl2->stats->collisions); |
| 128 |
ifl->stats->collisions = DIV (ifl->stats->collisions, |
| 129 |
ifl2->stats->collisions); |
| 130 |
|
| 131 |
dbg ("\tmulticast : %u %u\n", |
| 132 |
ifl->stats->multicast, ifl2->stats->multicast); |
| 133 |
ifl->stats->multicast = DIV (ifl->stats->multicast, |
| 134 |
ifl2->stats->multicast); |
| 135 |
#undef DIV |
| 136 |
} |
| 137 |
|
| 138 |
dbg ("\tlink UP: %s\n", if_up ? "true" : "false"); |
| 139 |
dbg ("\tlink RUNNING: %s\n", if_running ? "true" : "false"); |
| 140 |
|
| 141 |
if (ifl->speed > 0) |
| 142 |
dbg ("\tspeed : %uMbit/s\n", ifl->speed); |
| 143 |
|
| 144 |
if (opt_check_link && !(if_up && if_running)) |
| 145 |
plugin_error (STATE_CRITICAL, 0, |
| 146 |
"%s matches the given regular expression " |
| 147 |
"but is not UP and RUNNING!", ifl->ifname); |
| 148 |
(*ninterfaces)++; |
| 149 |
} |
| 150 |
|
| 151 |
freeiflist (iflhead2); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
regfree (®ex); |
| 156 |
|
| 157 |
return iflhead; |
| 158 |
} |
| 159 |
|
| 160 |
struct iflist * |
| 161 |
iflist_get_next (struct iflist *ifentry) |
| 162 |
{ |
| 163 |
return ifentry->next; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
const char * |
| 169 |
iflist_get_ifname (struct iflist *ifentry) |
| 170 |
{ |
| 171 |
return ifentry->ifname; |
| 172 |
} |
| 173 |
|
| 174 |
uint8_t |
| 175 |
iflist_get_duplex (struct iflist *ifentry) |
| 176 |
{ |
| 177 |
return ifentry->duplex; |
| 178 |
} |
| 179 |
|
| 180 |
uint32_t |
| 181 |
iflist_get_speed (struct iflist *ifentry) |
| 182 |
{ |
| 183 |
return ifentry->speed; |
| 184 |
} |
| 185 |
|
| 186 |
unsigned int |
| 187 |
iflist_get_flags (struct iflist *ifentry) |
| 188 |
{ |
| 189 |
return ifentry->flags; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
#define __iflist_get__(arg) \ |
| 196 |
unsigned int iflist_get_ ## arg (struct iflist *ifentry) \ |
| 197 |
{ return ifentry->stats ? ifentry->stats->arg : 0; } |
| 198 |
|
| 199 |
__iflist_get__(collisions) |
| 200 |
__iflist_get__(multicast) |
| 201 |
__iflist_get__(tx_packets) |
| 202 |
__iflist_get__(rx_packets) |
| 203 |
__iflist_get__(tx_bytes) |
| 204 |
__iflist_get__(rx_bytes) |
| 205 |
__iflist_get__(tx_errors) |
| 206 |
__iflist_get__(rx_errors) |
| 207 |
__iflist_get__(tx_dropped) |
| 208 |
__iflist_get__(rx_dropped) |
| 209 |
#undef __iflist_get__ |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
void print_ifname_debug (struct iflist *iflhead, unsigned int options) |
| 214 |
{ |
| 215 |
bool pd_bytes = (options & NO_BYTES) != NO_BYTES, |
| 216 |
pd_collisions = (options & NO_COLLISIONS) != NO_COLLISIONS, |
| 217 |
pd_drops = (options & NO_DROPS) != NO_DROPS, |
| 218 |
pd_errors = (options & NO_ERRORS) != NO_ERRORS, |
| 219 |
pd_multicast = (options & NO_MULTICAST) != NO_MULTICAST, |
| 220 |
pd_packets = (options & NO_PACKETS) != NO_PACKETS, |
| 221 |
rx_only = options & RX_ONLY, |
| 222 |
tx_only = options & TX_ONLY; |
| 223 |
struct iflist *ifl; |
| 224 |
|
| 225 |
#define __printf_tx_rx__(metric, tx_only, rx_only) \ |
| 226 |
do \ |
| 227 |
{ \ |
| 228 |
fprintf (stdout, " - "); \ |
| 229 |
if (!rx_only) \ |
| 230 |
fprintf (stdout, "%s_tx%s\t ", ifl->ifname, metric); \ |
| 231 |
if (!tx_only) \ |
| 232 |
fprintf (stdout, "%s_rx%s", ifl->ifname, metric); \ |
| 233 |
fprintf (stdout, "\n"); \ |
| 234 |
} \ |
| 235 |
while (0) |
| 236 |
|
| 237 |
for (ifl = iflhead; ifl != NULL; ifl = ifl->next) |
| 238 |
{ |
| 239 |
bool if_up = if_flags_UP (ifl->flags), |
| 240 |
if_running = if_flags_RUNNING (ifl->flags); |
| 241 |
char *ifduplex = NULL, |
| 242 |
*ifspeed = NULL; |
| 243 |
|
| 244 |
if (ifl->speed > 0) |
| 245 |
ifspeed = xasprintf (" link-speed:%uMbps", ifl->speed); |
| 246 |
if (ifl->duplex != _DUP_UNKNOWN) |
| 247 |
ifduplex = xasprintf (" %s-duplex", duplex_table[ifl->duplex]); |
| 248 |
printf ("%s%s%s%s\n" |
| 249 |
, ifl->ifname |
| 250 |
, if_up && !if_running ? " (NO-CARRIER)" |
| 251 |
: if_up ? "" : " (DOWN)" |
| 252 |
, ifspeed ? ifspeed : "" |
| 253 |
, ifduplex ? ifduplex : ""); |
| 254 |
|
| 255 |
if (ifl->stats) |
| 256 |
{ |
| 257 |
if (pd_bytes) |
| 258 |
__printf_tx_rx__ ("byte/s", tx_only, rx_only); |
| 259 |
if (pd_errors) |
| 260 |
__printf_tx_rx__ ("err/s", tx_only, rx_only); |
| 261 |
if (pd_drops) |
| 262 |
__printf_tx_rx__ ("drop/s", tx_only, rx_only); |
| 263 |
if (pd_packets) |
| 264 |
__printf_tx_rx__ ("pck/s", tx_only, rx_only); |
| 265 |
if (pd_collisions) |
| 266 |
fprintf (stdout, " - %s_coll/s\n", ifl->ifname); |
| 267 |
if (pd_multicast) |
| 268 |
fprintf (stdout, " - %s_mcast/s\n", ifl->ifname); |
| 269 |
} |
| 270 |
} |
| 271 |
#undef __printf_tx_rx__ |
| 272 |
} |
| 273 |
|
| 274 |
void |
| 275 |
freeiflist (struct iflist *iflhead) |
| 276 |
{ |
| 277 |
struct iflist *ifl = iflhead, *iflnext; |
| 278 |
|
| 279 |
while (ifl != NULL) |
| 280 |
{ |
| 281 |
iflnext = ifl->next; |
| 282 |
free (ifl->ifname); |
| 283 |
free (ifl); |
| 284 |
ifl = iflnext; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
#define ifa_flags_parser(arg) \ |
| 290 |
bool if_flags_ ## arg (unsigned int flags) \ |
| 291 |
{ return flags & IFF_ ## arg; } |
| 292 |
|
| 293 |
ifa_flags_parser (LOOPBACK); |
| 294 |
ifa_flags_parser (RUNNING); |
| 295 |
ifa_flags_parser (UP); |