Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/plugins/check_cswch.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 total number of context switches
7 * per second across all CPUs.
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 #include <getopt.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28
29 #include "common.h"
30 #include "cpustats.h"
31 #include "messages.h"
32 #include "progname.h"
33 #include "progversion.h"
34 #include "thresholds.h"
35 #include "xstrton.h"
36
37 static const char *program_copyright =
38 "Copyright (C) 2014,2015 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
39
40 static struct option const longopts[] = {
41 {(char *) "critical", required_argument, NULL, 'c'},
42 {(char *) "warning", required_argument, NULL, 'w'},
43 {(char *) "verbose", no_argument, NULL, 'v'},
44 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
45 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
46 {NULL, 0, NULL, 0}
47 };
48
49 static _Noreturn void
50 usage (FILE * out)
51 {
52 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
53 fputs ("This plugin monitors the total number of context switches "
54 "across all CPUs.\n", out);
55 fputs (program_copyright, out);
56 fputs (USAGE_HEADER, out);
57 fprintf (out, " %s [-v] [-w COUNTER] -c [COUNTER] [delay [count]]\n",
58 program_name);
59 fputs (USAGE_OPTIONS, out);
60 fputs (" -w, --warning COUNTER warning threshold\n", out);
61 fputs (" -c, --critical COUNTER critical threshold\n", out);
62 fputs (" -v, --verbose show details for command-line debugging "
63 "(Nagios may truncate output)\n", out);
64 fputs (USAGE_HELP, out);
65 fputs (USAGE_VERSION, out);
66 fprintf (out, " delay is the delay between updates in seconds "
67 "(default: %dsec)\n", DELAY_DEFAULT);
68 fprintf (out, " count is the number of updates "
69 "(default: %d)\n", COUNT_DEFAULT);
70 fputs (USAGE_EXAMPLES, out);
71 fprintf (out, " %s 1 2\n", program_name);
72
73 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
74 }
75
76 static _Noreturn void
77 print_version (void)
78 {
79 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
80 fputs (program_copyright, stdout);
81 fputs (GPLv3_DISCLAIMER, stdout);
82
83 exit (STATE_OK);
84 }
85
86 static unsigned long long
87 get_ctxtdelta (unsigned int count, unsigned int delay, bool verbose)
88 {
89 int tog = 0;
90 unsigned int i;
91 unsigned long long nctxt[2],
92 dnctxt = nctxt[0] = cpu_stats_get_cswch ();
93
94 if (verbose)
95 printf ("ctxt = %llu\n", dnctxt);
96
97 for (i = 1; i < count; i++)
98 {
99 sleep (delay);
100
101 tog = !tog;
102 nctxt[tog] = cpu_stats_get_cswch ();
103 dnctxt = (nctxt[tog] - nctxt[!tog]) / delay;
104
105 if (verbose)
106 printf ("ctxt = %llu --> %llu/s\n", nctxt[tog], dnctxt);
107 }
108
109 return dnctxt;
110 }
111
112 #ifndef NPL_TESTING
113 int
114 main (int argc, char **argv)
115 {
116 int c;
117 bool verbose = false;
118 char *critical = NULL, *warning = NULL;
119 nagstatus status = STATE_OK;
120 thresholds *my_threshold = NULL;
121
122 unsigned long count, delay;
123 unsigned long long dnctxt;
124
125 set_program_name (argv[0]);
126
127 while ((c = getopt_long (argc, argv,
128 "c:w:v" GETOPT_HELP_VERSION_STRING,
129 longopts, NULL)) != -1)
130 {
131 switch (c)
132 {
133 default:
134 usage (stderr);
135 case 'c':
136 critical = optarg;
137 break;
138 case 'w':
139 warning = optarg;
140 break;
141 case 'v':
142 verbose = true;
143 break;
144
145 case_GETOPT_HELP_CHAR
146 case_GETOPT_VERSION_CHAR
147
148 }
149 }
150
151 delay = DELAY_DEFAULT, count = COUNT_DEFAULT;
152 if (optind < argc)
153 {
154 delay = strtol_or_err (argv[optind++], "failed to parse argument");
155
156 if (delay < 1)
157 plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer");
158 else if (DELAY_MAX < delay)
159 plugin_error (STATE_UNKNOWN, 0,
160 "too large delay value (greater than %d)", DELAY_MAX);
161 }
162
163 if (optind < argc)
164 {
165 count = strtol_or_err (argv[optind++], "failed to parse argument");
166 if (COUNT_MAX < count)
167 plugin_error (STATE_UNKNOWN, 0,
168 "too large count value (greater than %d)", COUNT_MAX);
169 }
170
171 status = set_thresholds (&my_threshold, warning, critical);
172 if (status == NP_RANGE_UNPARSEABLE)
173 usage (stderr);
174
175 dnctxt = get_ctxtdelta (count, delay, verbose);
176
177 status = get_status (dnctxt, my_threshold);
178 free (my_threshold);
179
180 char *time_unit = (count > 1) ? "/s" : "";
181 printf ("%s %s - number of context switches%s %llu | cswch%s=%llu\n",
182 program_name_short, state_text (status),
183 time_unit, dnctxt, time_unit, dnctxt);
184
185 return status;
186 }
187 #endif /* NPL_TESTING */