Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014,2015,2019,2022 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin to check the CPU frequency characteristics.
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 * This software is based on the source code of the tool "vmstat".
22 */
23
24 #include <getopt.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "common.h"
29 #include "cpudesc.h"
30 #include "cpufreq.h"
31 #include "cpustats.h"
32 #include "cputopology.h"
33 #include "messages.h"
34 #include "progname.h"
35 #include "progversion.h"
36 #include "sysfsparser.h"
37 #include "thresholds.h"
38 #include "units.h"
39 #include "xalloc.h"
40 #include "xasprintf.h"
41
42 static const char *program_copyright =
43 "Copyright (C) 2014,2015,2019,2022 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
44
45 static struct option const longopts[] = {
46 {(char *) "no-cpu-model", no_argument, NULL, 'm'},
47 {(char *) "Hz", no_argument, NULL, 'H'},
48 {(char *) "kHz", no_argument, NULL, 'K'},
49 {(char *) "mHz", no_argument, NULL, 'M'},
50 {(char *) "gHz", no_argument, NULL, 'G'},
51 {(char *) "critical", required_argument, NULL, 'c'},
52 {(char *) "warning", required_argument, NULL, 'w'},
53 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
54 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
55 {NULL, 0, NULL, 0}
56 };
57
58 static _Noreturn void
59 usage (FILE * out)
60 {
61 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
62 fputs ("This plugin displays the CPU frequency characteristics.\n", out);
63 fputs (program_copyright, out);
64 fputs (USAGE_HEADER, out);
65 fprintf (out, " %s [-m] [-H,-K,-M,-G] [-w COUNTER] [-c COUNTER]\n",
66 program_name);
67 fputs (USAGE_OPTIONS, out);
68 fputs (" -m, --no-cpu-model "
69 "do not display the CPU model in the output message\n", out);
70 fputs (" -H,-K,-M,-G "
71 "show output in Hz, kHz (the default), mHz, or gHz\n", out);
72 fputs (" -w, --warning COUNTER (kHz) warning threshold\n", out);
73 fputs (" -c, --critical COUNTER (kHz) critical threshold\n", out);
74 fputs (USAGE_HELP, out);
75 fputs (USAGE_VERSION, out);
76 fputs (USAGE_EXAMPLES, out);
77 fprintf (out, " %s -m -w 800000:\n", program_name);
78
79 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
80 }
81
82 static _Noreturn void
83 print_version (void)
84 {
85 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
86 fputs (program_copyright, stdout);
87 fputs (GPLv3_DISCLAIMER, stdout);
88
89 exit (STATE_OK);
90 }
91
92 int
93 main (int argc, char **argv)
94 {
95 int c, err;
96 bool cpu_model;
97 char *critical = NULL, *warning = NULL;
98 float factor = 1.0;
99 nagstatus currstatus, status = STATE_OK;
100 thresholds *my_threshold = NULL;
101 struct cpu_desc *cpudesc = NULL;
102 unsigned long freq_min, freq_max, freq_kernel;
103
104 set_program_name (argv[0]);
105
106 sysfsparser_check_for_sysfs ();
107
108 err = cpu_desc_new (&cpudesc);
109 if (err < 0)
110 plugin_error (STATE_UNKNOWN, err, "memory exhausted");
111
112 /* default values */
113 cpu_model = true;
114
115 while ((c = getopt_long (
116 argc, argv, "c:w:mHKMG"
117 GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1)
118 {
119 switch (c)
120 {
121 default:
122 usage (stderr);
123 case 'm':
124 cpu_model = false;
125 break;
126 case 'c':
127 critical = optarg;
128 break;
129 case 'w':
130 warning = optarg;
131 break;
132 case 'H': factor = 1000.0; break;
133 case 'K': factor = 1.0; break;
134 case 'M': factor = 1.0/1000; break;
135 case 'G': factor = 1.0/1000000; break;
136
137 case_GETOPT_HELP_CHAR
138 case_GETOPT_VERSION_CHAR
139
140 }
141 }
142
143 status = set_thresholds (&my_threshold, warning, critical);
144 if (status == NP_RANGE_UNPARSEABLE)
145 usage (stderr);
146
147 int ncpus = get_processor_number_total ();
148
149 cpu_desc_read (cpudesc);
150 char *cpu_model_str =
151 cpu_model ? xasprintf ("(%s) ",
152 cpu_desc_get_model_name (cpudesc)) : NULL;
153
154 for (c = 0; c < ncpus; c++)
155 if ((freq_kernel = cpufreq_get_freq_kernel (c)) > 0)
156 {
157 currstatus = get_status (freq_kernel, my_threshold);
158 if (currstatus > status)
159 status = currstatus;
160 }
161
162 printf ("%s %s%s |"
163 , program_name_short, cpu_model ? cpu_model_str : ""
164 , state_text (status));
165 #define unit_convert(_val, _factor) (unsigned long long)(_val * _factor)
166 for (c = 0; c < ncpus; c++)
167 {
168 if (0 == cpufreq_get_hardware_limits (c, &freq_min, &freq_max))
169 {
170 freq_kernel = cpufreq_get_freq_kernel (c);
171 /* expected format for the Nagios performance data:
172 * 'label'=value[UOM];[warn];[crit];[min];[max] */
173 if (freq_kernel)
174 printf (" cpu%d_freq=%llu;;;%llu;%llu",
175 c,
176 unit_convert(freq_kernel, factor),
177 unit_convert(freq_min, factor),
178 unit_convert(freq_max, factor));
179 }
180 }
181 #undef unit_convert
182 putchar ('\n');
183 cpu_desc_unref (cpudesc);
184
185 return status;
186 }