Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/plugins/check_network.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014,2015,2020 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin that displays some network interfaces statistics.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include <getopt.h>
24 #include <stdlib.h>
25 #include <linux/ethtool.h>
26 #ifdef HAVE_LINUX_IF_LINK_H
27 # include <linux/if_link.h>
28 #else
29 # include <linux/rtnetlink.h>
30 #endif
31 #include <string.h>
32
33 #include "common.h"
34 #include "logging.h"
35 #include "messages.h"
36 #include "netinfo.h"
37 #include "progname.h"
38 #include "progversion.h"
39 #include "string-macros.h"
40 #include "system.h"
41 #include "thresholds.h"
42 #include "xalloc.h"
43 #include "xasprintf.h"
44 #include "xstrton.h"
45
46 #define MAX_PRINTED_INTERFACES 5
47
48 static const char *program_copyright =
49 "Copyright (C) 2014,2015,2020 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
50
51 static struct option const longopts[] = {
52 {(char *) "check-link", no_argument, NULL, 'k'},
53 {(char *) "ifname", required_argument, NULL, 'i'},
54 {(char *) "ifname-debug", no_argument, NULL, 0},
55 {(char *) "no-bytes", no_argument, NULL, 'b'},
56 {(char *) "no-collisions", no_argument, NULL, 'C'},
57 {(char *) "no-drops", no_argument, NULL, 'd'},
58 {(char *) "no-errors", no_argument, NULL, 'e'},
59 {(char *) "no-loopback", no_argument, NULL, 'l'},
60 {(char *) "no-multicast", no_argument, NULL, 'm'},
61 {(char *) "no-packets", no_argument, NULL, 'p'},
62 {(char *) "no-wireless", no_argument, NULL, 'W'},
63 {(char *) "perc", no_argument, NULL, '%'},
64 {(char *) "rx-only", no_argument, NULL, 'r'},
65 {(char *) "tx-only", no_argument, NULL, 't'},
66 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
67 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
68 {NULL, 0, NULL, 0}
69 };
70
71 typedef enum network_check
72 {
73 CHECK_BYTES,
74 CHECK_DEFAULT = CHECK_BYTES,
75 CHECK_COLLISIONS,
76 CHECK_DROPPED,
77 CHECK_ERRORS,
78 CHECK_MULTICAST
79 } network_check;
80
81 static _Noreturn void
82 usage (FILE * out)
83 {
84 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
85 fputs ("This plugin displays some network interfaces statistics.\n", out);
86 fputs (program_copyright, out);
87 fputs (USAGE_HEADER, out);
88 fprintf (out, " %s [-klW] [-bCdemp] [-i <ifname-regex>] [delay]\n",
89 program_name);
90 fprintf (out, " %s [-klW] [-bCdemp] [-i <ifname-regex>] --ifname-debug\n",
91 program_name);
92 fputs (USAGE_OPTIONS, out);
93 fputs (" -i, --ifname only display interfaces matching a regular "
94 "expression\n", out);
95 fputs (" --ifname-debug display the list of metric keys and exit\n",
96 out);
97 fputs (" -k, --check-link report an error if at least a link is down\n",
98 out);
99 fputs (" -l, --no-loopback skip the loopback interface\n", out);
100 fputs (" -W, --no-wireless skip the wireless interfaces\n", out);
101 fputs (" -%, --perc return percentage metrics if possible\n",
102 out);
103 fputs (" -w, --warning COUNTER warning threshold\n", out);
104 fputs (" -c, --critical COUNTER critical threshold\n", out);
105 fputs (USAGE_HELP, out);
106 fputs (USAGE_VERSION, out);
107 fprintf (out, " delay is the delay between the two network snapshots "
108 "in seconds (default: %dsec)\n", DELAY_DEFAULT);
109 fputs (" By default all the counters are reported in the output but it's "
110 "possible to select a subset of them:\n", out);
111 fputs (" -b --no-bytes omit the rx/tx bytes counter from perfdata\n",
112 out);
113 fputs (" -C, --no-collisions omit the collisions counter from perfdata\n",
114 out);
115 fputs (" -d --no-drops omit the rx/tx drop counters from perfdata\n",
116 out);
117 fputs (" -e --no-errors omit the rx/tx errors counters "
118 "from perfdata\n", out);
119 fputs (" -m, --no-multicast omit the multicast counter from perfdata\n",
120 out);
121 fputs (" -p, --no-packets omit the rx/tx packets counter from "
122 "perfdata\n", out);
123 fputs (" -r, --rx-only consider the received traffic only in "
124 "the thresholds\n", out);
125 fputs (" -t, --tx-only consider the transmitted traffic only in "
126 "the thresholds\n", out);
127 fputs (USAGE_NOTE, out);
128 fputs (" - The option --ifname supports the POSIX Extended Regular Expression "
129 "syntax.\n", out);
130 fputs (" See: https://man7.org/linux/man-pages/man7/regex.7.html\n", out);
131 fputs (" - You cannot select both the options r/rx-only and t/tx-only.\n",
132 out);
133 fputs (USAGE_EXAMPLES, out);
134 fprintf (out, " %s\n", program_name);
135 fprintf (out, " %s --check-link --ifname \"^(enp|eth)\" 15\n", program_name);
136 fprintf (out, " %s --check-link --ifname ^wlp --warning 645120 15\n",
137 program_name);
138 fprintf (out, " %s --ifname \"^(enp|wlp)\" --ifname-debug -Cdm\n",
139 program_name);
140 fprintf (out, " %s --perc --ifname \"^(enp|eth)\" -w 80%% 15\n",
141 program_name);
142 fprintf (out, " %s --no-loopback --no-wireless 15\n", program_name);
143
144 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
145 }
146
147 static _Noreturn void
148 print_version (void)
149 {
150 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
151 fputs (program_copyright, stdout);
152 fputs (GPLv3_DISCLAIMER, stdout);
153
154 exit (STATE_OK);
155 }
156
157 static inline unsigned int
158 get_threshold_metric (unsigned int tx, unsigned int rx,
159 bool tx_only, bool rx_only)
160 {
161 dbg ("call to get_threshold_metric "
162 "with tx:%u rx:%u, tx_only:%s, rx_only:%s\n"
163 , tx, rx
164 , tx_only ? "true" : "false"
165 , rx_only ? "true" : "false");
166 return (tx_only ? 0 : rx)
167 + (rx_only ? 0 : tx);
168 }
169
170 /* performance data format:
171 * 'label'=value[UOM];[warn];[crit];[min];[max] */
172 static inline double
173 ratio_over_speed (unsigned int counter, unsigned long long speed)
174 {
175 return (double)(100.0 / speed) * counter;
176 }
177
178 static inline char *
179 fmt_perfdata_bytes (const char *ifname, const char *label,
180 unsigned int counter, unsigned long long speed, bool perc)
181 {
182 char *perfdata;
183
184 if (perc && (speed > 0))
185 {
186 double counter_perc = ratio_over_speed (counter, speed);
187 perfdata =
188 xasprintf ("%s_%s/s=%.2f%%;;;0;100.0", ifname, label, counter_perc);
189 }
190 else if (!perc && (speed > 0))
191 perfdata = xasprintf("%s_%s/s=%u;;;0;%llu", ifname, label, counter, speed);
192 else
193 perfdata = xasprintf ("%s_%s/s=%u", ifname, label, counter);
194
195 return perfdata;
196 }
197
198 int
199 main (int argc, char **argv)
200 {
201 int c, option_index = 0;
202 nagstatus status;
203 bool ifname_debug = false,
204 pd_bytes = true,
205 pd_collisions = true,
206 pd_drops = true,
207 pd_errors = true,
208 pd_multicast = true,
209 pd_packets = true,
210 report_perc = false,
211 rx_only = false,
212 tx_only = false;
213 char *p = NULL, *plugin_progname,
214 *critical = NULL, *warning = NULL,
215 *bp, *ifname_regex = NULL;
216 size_t size;
217 unsigned int options = 0;
218 unsigned long delay, len;
219 FILE *perfdata;
220 network_check check = CHECK_DEFAULT;
221 thresholds *my_threshold = NULL;
222
223 set_program_name (argv[0]);
224
225 while ((c = getopt_long (argc, argv,
226 "Cc:bdei:klmpWw:%" GETOPT_HELP_VERSION_STRING,
227 longopts, &option_index)) != -1)
228 {
229 switch (c)
230 {
231 default:
232 usage (stderr);
233 case 0:
234 if (STREQ (longopts[option_index].name, "ifname-debug"))
235 ifname_debug = true;
236 break;
237 case 'b':
238 options |= NO_BYTES;
239 pd_bytes = false;
240 break;
241 case 'C':
242 options |= NO_COLLISIONS;
243 pd_collisions = false;
244 break;
245 case 'c':
246 critical = optarg;
247 break;
248 case 'd':
249 options |= NO_DROPS;
250 pd_drops = false;
251 break;
252 case 'e':
253 options |= NO_ERRORS;
254 pd_errors = false;
255 break;
256 case 'i':
257 ifname_regex = xstrdup (optarg);
258 break;
259 case 'k':
260 options |= CHECK_LINK;
261 break;
262 case 'l':
263 options |= NO_LOOPBACK;
264 break;
265 case 'm':
266 options |= NO_MULTICAST;
267 pd_multicast = false;
268 break;
269 case 'p':
270 options |= NO_PACKETS;
271 pd_packets = false;
272 break;
273 case 'r':
274 options |= RX_ONLY;
275 rx_only = true;
276 break;
277 case 't':
278 options |= TX_ONLY;
279 tx_only = true;
280 break;
281 case 'W':
282 options |= NO_WIRELESS;
283 break;
284 case 'w':
285 warning = optarg;
286 break;
287 case '%':
288 report_perc = true;
289 break;
290 case_GETOPT_HELP_CHAR
291 case_GETOPT_VERSION_CHAR
292
293 }
294 }
295
296 delay = ifname_debug ? 0 : DELAY_DEFAULT;
297 if (optind < argc)
298 {
299 if (ifname_debug)
300 usage (stderr);
301
302 delay = strtol_or_err (argv[optind++], "failed to parse argument");
303 if (delay < 1)
304 plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer");
305 else if (DELAY_MAX < delay)
306 plugin_error (STATE_UNKNOWN, 0,
307 "too large delay value (greater than %d)", DELAY_MAX);
308 }
309
310 if (tx_only && rx_only)
311 usage (stderr);
312
313 len = strlen (program_name);
314 if (len > 6 && STRPREFIX (program_name, "check_"))
315 p = (char *) program_name + 6;
316 else
317 plugin_error (STATE_UNKNOWN, 0,
318 "bug: the plugin does not have a standard name");
319
320 if (STRPREFIX (p, "network_collisions"))
321 {
322 check = CHECK_COLLISIONS;
323 if (!pd_collisions)
324 usage (stderr);
325 plugin_progname = xstrdup ("network collisions");
326 }
327 else if (STRPREFIX (p, "network_dropped"))
328 {
329 check = CHECK_DROPPED;
330 if (!pd_drops)
331 usage (stderr);
332 plugin_progname = xstrdup ("network dropped");
333 }
334 else if (STRPREFIX (p, "network_errors"))
335 {
336 check = CHECK_ERRORS;
337 if (!pd_errors)
338 usage (stderr);
339 plugin_progname = xstrdup ("network errors");
340 }
341 else if (STRPREFIX (p, "network_multicast"))
342 {
343 check = CHECK_MULTICAST;
344 if (!pd_multicast)
345 usage (stderr);
346 plugin_progname = xstrdup ("network multicast");
347 }
348 else
349 plugin_progname = xstrdup ("network");
350
351 unsigned int ninterfaces;
352 struct iflist *ifl, *iflhead =
353 netinfo (options, ifname_regex, delay, &ninterfaces);
354
355 /* just print the list of matching interfaces and exit */
356 if (ifname_debug)
357 {
358 print_ifname_debug (iflhead, options);
359 exit (STATE_UNKNOWN);
360 }
361
362 status = set_thresholds (&my_threshold, warning, critical);
363 if (status == NP_RANGE_UNPARSEABLE)
364 usage (stderr);
365
366 perfdata = open_memstream (&bp, &size);
367 status = STATE_OK;
368
369 iflist_foreach (ifl, iflhead)
370 {
371 const char *ifname = iflist_get_ifname (ifl);
372 double counter;
373 unsigned long long speed =
374 (iflist_get_speed (ifl) > 0) ? iflist_get_speed (ifl) * 1000*1000/8 : 0;
375 nagstatus iface_status;
376
377 /* If the output in percentages is selected and a thresholds has been
378 * set, but the interface is down or its physical speed is not
379 * available, make the plugin exit with an unknown state.
380 * Otherwise the thresholds will be used for percentages and counters
381 * which does not make sense. */
382 if (report_perc && (warning || critical) && !(speed > 0))
383 plugin_error (STATE_UNKNOWN, 0,
384 "metrics of %s cannot be converted into percentages%s"
385 , ifname
386 , if_flags_UP (iflist_get_flags (ifl))
387 && if_flags_RUNNING (iflist_get_flags (ifl)) ?
388 (0 == speed ?
389 ": physical speed is not available" : "")
390 : ": link is not UP/RUNNING");
391 if (DUPLEX_HALF == iflist_get_duplex (ifl))
392 speed /= 2;
393
394 switch (check)
395 {
396 default:
397 counter = get_threshold_metric (iflist_get_tx_bytes (ifl),
398 iflist_get_rx_bytes (ifl),
399 tx_only, rx_only);
400 if (report_perc && speed > 0)
401 counter = ratio_over_speed (counter, speed);
402 break;
403 case CHECK_COLLISIONS:
404 counter = iflist_get_collisions (ifl);
405 break;
406 case CHECK_DROPPED:
407 counter = get_threshold_metric (iflist_get_tx_dropped (ifl),
408 iflist_get_rx_dropped (ifl),
409 tx_only, rx_only);
410 break;
411 case CHECK_ERRORS:
412 counter = get_threshold_metric (iflist_get_tx_errors (ifl),
413 iflist_get_rx_errors (ifl),
414 tx_only, rx_only);
415 break;
416 case CHECK_MULTICAST:
417 counter = iflist_get_multicast (ifl);
418 break;
419 }
420 dbg ("threshold counter: %g with w:%s c:%s\n"
421 , counter
422 , warning ? warning : "unset"
423 , critical ? critical : "unset");
424
425 iface_status = get_status (counter, my_threshold);
426 if (iface_status > status)
427 status = iface_status;
428
429 if (pd_bytes)
430 {
431 char *perfdata_txbyte =
432 fmt_perfdata_bytes (ifname, "txbyte", iflist_get_tx_bytes (ifl),
433 speed, report_perc);
434 char *perfdata_rxbyte =
435 fmt_perfdata_bytes (ifname, "rxbyte", iflist_get_rx_bytes (ifl),
436 speed, report_perc);
437 fprintf (perfdata, "%s %s ", perfdata_txbyte, perfdata_rxbyte);
438 free (perfdata_txbyte);
439 free (perfdata_rxbyte);
440 }
441 if (pd_errors)
442 fprintf (perfdata
443 , "%s_txerr/s=%u %s_rxerr/s=%u "
444 , ifname, iflist_get_tx_errors (ifl)
445 , ifname, iflist_get_rx_errors (ifl));
446 if (pd_drops)
447 fprintf (perfdata
448 , "%s_txdrop/s=%u %s_rxdrop/s=%u "
449 , ifname, iflist_get_tx_dropped (ifl)
450 , ifname, iflist_get_rx_dropped (ifl));
451 if (pd_packets)
452 fprintf (perfdata
453 , "%s_txpck/s=%u %s_rxpck/s=%u "
454 , ifname, iflist_get_tx_packets (ifl)
455 , ifname, iflist_get_rx_packets (ifl));
456 if (pd_collisions)
457 fprintf (perfdata, "%s_coll/s=%u "
458 , ifname, iflist_get_collisions (ifl));
459 if (pd_multicast)
460 fprintf (perfdata, "%s_mcast/s=%u "
461 , ifname, iflist_get_multicast (ifl));
462 }
463
464 fclose (perfdata);
465
466 if (ninterfaces < 1)
467 status = STATE_UNKNOWN;
468
469 int i = 0;
470 printf ("%s %s - found %u interface(s): "
471 , plugin_progname
472 , state_text (status)
473 , ninterfaces);
474 iflist_foreach (ifl, iflhead)
475 if (i++ < MAX_PRINTED_INTERFACES)
476 printf ("%s%s", i < 2 ? "" : ",", iflist_get_ifname (ifl));
477 else
478 {
479 printf (",...");
480 break;
481 }
482 printf (" | %s\n", bp);
483
484 freeiflist (iflhead);
485 free (my_threshold);
486
487 return status;
488 }