Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014,2015 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin that monitors the interrupts serviced per second,
7 * including unnumbered architecture specific interrupts.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /* Note: High number of interrupts per second indicates a problem with
24 * hardware. It could indicate a software bug in the case of software
25 * interrupts. */
26
27 #include <getopt.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32
33 #include "common.h"
34 #include "cpustats.h"
35 #include "interrupts.h"
36 #include "messages.h"
37 #include "progname.h"
38 #include "progversion.h"
39 #include "thresholds.h"
40 #include "xstrton.h"
41
42 #define MIN(a,b) \
43 ({ __typeof__ (a) _a = (a); \
44 __typeof__ (b) _b = (b); \
45 _a < _b ? _a : _b; })
46
47 static const char *program_copyright =
48 "Copyright (C) 2014,2015 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
49
50 static struct option const longopts[] = {
51 {(char *) "critical", required_argument, NULL, 'c'},
52 {(char *) "warning", required_argument, NULL, 'w'},
53 {(char *) "verbose", no_argument, NULL, 'v'},
54 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
55 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
56 {NULL, 0, NULL, 0}
57 };
58
59 static _Noreturn void
60 usage (FILE * out)
61 {
62 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
63 fputs ("This plugin monitors the total number of system interrupts.\n", out);
64 fputs (program_copyright, out);
65 fputs (USAGE_HEADER, out);
66 fprintf (out, " %s [-v] [-w COUNTER] -c [COUNTER] [delay [count]]\n",
67 program_name);
68 fputs (USAGE_OPTIONS, out);
69 fputs (" -w, --warning COUNTER warning threshold\n", out);
70 fputs (" -c, --critical COUNTER critical threshold\n", out);
71 fputs (" -v, --verbose show details for command-line debugging "
72 "(Nagios may truncate output)\n", out);
73 fputs (USAGE_HELP, out);
74 fputs (USAGE_VERSION, out);
75 fprintf (out, " delay is the delay between updates in seconds "
76 "(default: %dsec)\n", DELAY_DEFAULT);
77 fprintf (out, " count is the number of updates "
78 "(default: %d)\n", COUNT_DEFAULT);
79 fputs (USAGE_EXAMPLES, out);
80 fprintf (out, " %s -w 10000 1 2\n", program_name);
81
82 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
83 }
84
85 static _Noreturn void
86 print_version (void)
87 {
88 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
89 fputs (program_copyright, stdout);
90 fputs (GPLv3_DISCLAIMER, stdout);
91
92 exit (STATE_OK);
93 }
94
95 static unsigned long long
96 get_intrdelta (unsigned int *ncpus0, unsigned int *ncpus1,
97 unsigned long *(*vintr)[2], unsigned int count,
98 unsigned int delay, bool verbose)
99 {
100 unsigned long long nintr[2], dnintr;
101 unsigned int i, tog = 0;
102
103 dnintr = nintr[0] = cpu_stats_get_intr ();
104
105 if (verbose)
106 printf ("intr = %llu\n", dnintr);
107
108 if (count <= 2)
109 (*vintr)[0] = proc_interrupts_get_nintr_per_cpu (ncpus0);
110
111 for (i = 1; i < count; i++)
112 {
113 sleep (delay);
114
115 tog = !tog;
116 nintr[tog] = cpu_stats_get_intr ();
117
118 dnintr = (nintr[tog] - nintr[!tog]) / delay;
119 if (verbose)
120 printf ("intr = %llu --> %llu/s\n", nintr[tog], dnintr);
121
122 if (count - 2 == i)
123 (*vintr)[0] = proc_interrupts_get_nintr_per_cpu (ncpus0);
124 else if (count - 1 == i)
125 (*vintr)[1] = proc_interrupts_get_nintr_per_cpu (ncpus1);
126 }
127
128 return dnintr;
129 }
130
131 #ifndef NPL_TESTING
132 int
133 main (int argc, char **argv)
134 {
135 int c;
136 bool verbose = false;
137 char *critical = NULL, *warning = NULL;
138 nagstatus status = STATE_OK;
139 thresholds *my_threshold = NULL;
140
141 unsigned int ncpus0 = 0, ncpus1 = 0;
142 unsigned long i, delay, count, *vintr[2] = { NULL, NULL };
143 unsigned long long dnintr;
144
145 set_program_name (argv[0]);
146
147 while ((c = getopt_long (argc, argv,
148 "c:w:v" GETOPT_HELP_VERSION_STRING,
149 longopts, NULL)) != -1)
150 {
151 switch (c)
152 {
153 default:
154 usage (stderr);
155 case 'c':
156 critical = optarg;
157 break;
158 case 'w':
159 warning = optarg;
160 break;
161 case 'v':
162 verbose = true;
163 break;
164
165 case_GETOPT_HELP_CHAR
166 case_GETOPT_VERSION_CHAR
167 }
168 }
169
170 delay = DELAY_DEFAULT, count = COUNT_DEFAULT;
171 if (optind < argc)
172 {
173 delay = strtol_or_err (argv[optind++], "failed to parse argument");
174
175 if (delay < 1)
176 plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer");
177 else if (DELAY_MAX < delay)
178 plugin_error (STATE_UNKNOWN, 0,
179 "too large delay value (greater than %d)", DELAY_MAX);
180 }
181
182 if (optind < argc)
183 {
184 count = strtol_or_err (argv[optind++], "failed to parse argument");
185 if (COUNT_MAX < count)
186 plugin_error (STATE_UNKNOWN, 0,
187 "too large count value (greater than %d)", COUNT_MAX);
188 }
189
190 status = set_thresholds (&my_threshold, warning, critical);
191 if (status == NP_RANGE_UNPARSEABLE)
192 usage (stderr);
193
194 dnintr = get_intrdelta (&ncpus0, &ncpus1, &vintr, count, delay, verbose);
195
196 status = get_status (dnintr, my_threshold);
197 free (my_threshold);
198
199 char *time_unit = (count > 1) ? "/s" : "";
200 printf ("%s %s - number of interrupts%s %llu | intr%s=%llu",
201 program_name_short, state_text (status),
202 time_unit, dnintr, time_unit, dnintr);
203
204 for (i = 0; i < MIN (ncpus0, ncpus1); i++)
205 printf (" intr_cpu%lu%s=%lu", i, time_unit,
206 (count > 1) ? (vintr[1][i] - vintr[0][i]) / delay : vintr[0][i]);
207 printf ("\n");
208
209 free (vintr[1]);
210 free (vintr[0]);
211
212 return status;
213 }
214 #endif /* NPL_TESTING */