Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014-2021,2022 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin that monitors the hardware's temperature.
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 * Based on the code of the acpiclient tool:
22 * <http://sourceforge.net/p/acpiclient/code/ci/master/tree/acpi.c>
23 *
24 * See also the official kernel documentation:
25 * <https://www.kernel.org/doc/Documentation/thermal/sysfs-api.txt>
26 */
27
28 #include <errno.h>
29 #include <getopt.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include "common.h"
36 #include "messages.h"
37 #include "progname.h"
38 #include "progversion.h"
39 #include "sysfsparser.h"
40 #include "thresholds.h"
41 #include "xalloc.h"
42
43 enum
44 {
45 TEMP_KELVIN,
46 TEMP_CELSIUS,
47 TEMP_FAHRENHEIT
48 };
49
50 static const char *program_copyright =
51 "Copyright (C) 2014-2021,2022 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
52
53 static struct option const longopts[] = {
54 {(char *) "fahrenheit", required_argument, NULL, 'f'},
55 {(char *) "kelvin", required_argument, NULL, 'k'},
56 {(char *) "list", no_argument, NULL, 'l'},
57 {(char *) "thermal_zone", required_argument, NULL, 't'},
58 {(char *) "critical", required_argument, NULL, 'c'},
59 {(char *) "warning", required_argument, NULL, 'w'},
60 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
61 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
62 {NULL, 0, NULL, 0}
63 };
64
65 static _Noreturn void
66 usage (FILE * out)
67 {
68 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
69 fputs ("This plugin monitors the hardware's temperature.\n", out);
70 fprintf (out, "It requires the sysfs tree %s to be mounted and readable.\n",
71 sysfsparser_thermal_sysfs_path ());
72 fputs (program_copyright, out);
73 fputs (USAGE_HEADER, out);
74 fprintf (out, " %s [-f|-k] [-t <thermal_zone_num>] "
75 "[-w COUNTER] [-c COUNTER]\n", program_name);
76 fputs (USAGE_OPTIONS, out);
77 fputs (" -f, --fahrenheit use fahrenheit as the temperature unit\n", out);
78 fputs (" -k, --kelvin use kelvin as the temperature unit\n", out);
79 fputs (" -l, --list list all the thermal sensors reported by the"
80 " kernel\n", out);
81 fputs (" -t, --thermal_zone only consider a specific thermal zone\n", out);
82 fputs (" -w, --warning COUNTER warning threshold\n", out);
83 fputs (" -c, --critical COUNTER critical threshold\n", out);
84 fputs (USAGE_HELP, out);
85 fputs (USAGE_VERSION, out);
86 fputs (USAGE_NOTE, out);
87 fputs (" If the option '-t|--thermal_zone' is not specified, then the"
88 " thermal zone\n"
89 " with the highest temperature is selected. Note that this zone"
90 " may change\n"
91 " at each plugin execution.\n", out);
92 fputs (USAGE_EXAMPLES, out);
93 fprintf (out, " %s --list\n", program_name);
94 fprintf (out, " %s -w 80 -c 90\n", program_name);
95 fprintf (out, " %s -t 0 -w 80 -c 90\n", program_name);
96
97 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
98 }
99
100 static _Noreturn void
101 print_version (void)
102 {
103 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
104 fputs (program_copyright, stdout);
105 fputs (GPLv3_DISCLAIMER, stdout);
106
107 exit (STATE_OK);
108 }
109
110 static double
111 get_real_temp (unsigned long temperature, char **scale, int temp_units)
112 {
113 double real_temp = (double) temperature / 1000.0;
114 const double absolute_zero = 273.1;
115
116 switch (temp_units)
117 {
118 case TEMP_CELSIUS:
119 *scale = "°C";
120 break;
121 case TEMP_FAHRENHEIT:
122 real_temp = (real_temp * 1.8) + 32;
123 *scale = "°F";
124 break;
125 case TEMP_KELVIN:
126 default:
127 real_temp += absolute_zero;
128 *scale = "°K";
129 break;
130 }
131
132 return (real_temp);
133 }
134
135 #ifndef NPL_TESTING
136 int
137 main (int argc, char **argv)
138 {
139 int c, temperature_unit = TEMP_CELSIUS;
140 unsigned int thermal_zone, selected_thermal_zone = ALL_THERMAL_ZONES;
141 char *critical = NULL, *warning = NULL,
142 *end, *type, *scale;
143 nagstatus status = STATE_OK;
144 thresholds *my_threshold = NULL;
145
146 set_program_name (argv[0]);
147
148 while ((c = getopt_long (argc, argv,
149 "fklt:c:w:v" GETOPT_HELP_VERSION_STRING,
150 longopts, NULL)) != -1)
151 {
152 switch (c)
153 {
154 default:
155 usage (stderr);
156 case 'f':
157 temperature_unit = TEMP_FAHRENHEIT;
158 break;
159 case 'k':
160 temperature_unit = TEMP_KELVIN;
161 break;
162 case 'l':
163 sysfsparser_thermal_listall ();
164 return STATE_UNKNOWN;
165 case 't':
166 errno = 0;
167 selected_thermal_zone = strtoul (optarg, &end, 10);
168 if (errno != 0 || optarg == end || (end != NULL && *end != '\0'))
169 plugin_error (STATE_UNKNOWN, 0,
170 "the option '-t' requires an integer");
171 break;
172 case 'c':
173 critical = optarg;
174 break;
175 case 'w':
176 warning = optarg;
177 break;
178
179 case_GETOPT_HELP_CHAR
180 case_GETOPT_VERSION_CHAR
181
182 }
183 }
184
185 status = set_thresholds (&my_threshold, warning, critical);
186 if (status == NP_RANGE_UNPARSEABLE)
187 usage (stderr);
188
189 sysfsparser_check_for_sysfs ();
190
191 int max_temp =
192 sysfsparser_thermal_get_temperature (selected_thermal_zone,
193 &thermal_zone, &type);
194 double real_temp = get_real_temp (max_temp, &scale, temperature_unit);
195
196 status = get_status (real_temp, my_threshold);
197 free (my_threshold);
198
199 /* check for the related critical temperature, if any */
200 int crit_temp =
201 sysfsparser_thermal_get_critical_temperature (thermal_zone) / 1000;
202
203 printf ("%s %s - +%.1f%s (thermal zone: %u [%s], type: \"%s\") | temp=%u%c",
204 program_name_short, state_text (status), real_temp, scale,
205 thermal_zone, sysfsparser_thermal_get_device (thermal_zone),
206 type ? type : "n/a", (unsigned int) real_temp,
207 (temperature_unit == TEMP_KELVIN) ? 'K' :
208 (temperature_unit == TEMP_FAHRENHEIT) ? 'F' : 'C');
209
210 if (crit_temp > 0 && ALL_THERMAL_ZONES != selected_thermal_zone)
211 printf (";0;%d", crit_temp);
212
213 putchar ('\n');
214
215 return status;
216 }
217 #endif /* NPL_TESTING */