Merge pull request #2044 from RincewindsHat/fix/fedora-rpm-build
[monitoring-plugins.git] / plugins / check_overcr.c
blobe80d4775c20dd3ec855029f01a9c94ea316235b6
1 /*****************************************************************************
3 * Monitoring check_overcr plugin
5 * License: GPL
6 * Copyright (c) 2000-2024 Monitoring Plugins Development Team
8 * Description:
10 * This file contains the check_overcr plugin
12 * This plugin attempts to contact the Over-CR collector daemon running on the
13 * remote UNIX server in order to gather the requested system information.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *****************************************************************************/
32 const char *progname = "check_overcr";
33 const char *copyright = "2000-2024";
34 const char *email = "devel@monitoring-plugins.org";
36 #include "common.h"
37 #include "netutils.h"
38 #include "utils.h"
40 enum checkvar {
41 NONE,
42 LOAD1,
43 LOAD5,
44 LOAD15,
45 DPU,
46 PROCS,
47 NETSTAT,
48 UPTIME
51 enum {
52 PORT = 2000
55 char *server_address = NULL;
56 int server_port = PORT;
57 double warning_value = 0L;
58 double critical_value = 0L;
59 bool check_warning_value = false;
60 bool check_critical_value = false;
61 enum checkvar vars_to_check = NONE;
62 int cmd_timeout = 1;
64 int netstat_port = 0;
65 char *disk_name = NULL;
66 char *process_name = NULL;
67 char send_buffer[MAX_INPUT_BUFFER];
69 int process_arguments(int, char **);
70 void print_usage(void);
71 void print_help(void);
73 int main(int argc, char **argv) {
74 int result = STATE_UNKNOWN;
75 char recv_buffer[MAX_INPUT_BUFFER];
76 char temp_buffer[MAX_INPUT_BUFFER];
77 char *temp_ptr = NULL;
78 bool found_disk = false;
79 unsigned long percent_used_disk_space = 100;
80 double load;
81 double load_1min;
82 double load_5min;
83 double load_15min;
84 int port_connections = 0;
85 int processes = 0;
86 double uptime_raw_hours;
87 int uptime_raw_minutes = 0;
88 int uptime_days = 0;
89 int uptime_hours = 0;
90 int uptime_minutes = 0;
92 setlocale(LC_ALL, "");
93 bindtextdomain(PACKAGE, LOCALEDIR);
94 textdomain(PACKAGE);
96 /* Parse extra opts if any */
97 argv = np_extra_opts(&argc, argv, progname);
99 if (process_arguments(argc, argv) == ERROR)
100 usage4(_("Could not parse arguments"));
102 /* initialize alarm signal handling */
103 signal(SIGALRM, socket_timeout_alarm_handler);
105 /* set socket timeout */
106 alarm(socket_timeout);
108 result = process_tcp_request2(server_address, server_port, send_buffer, recv_buffer, sizeof(recv_buffer));
110 switch (vars_to_check) {
112 case LOAD1:
113 case LOAD5:
114 case LOAD15:
116 if (result != STATE_OK)
117 die(result, _("Unknown error fetching load data\n"));
119 temp_ptr = (char *)strtok(recv_buffer, "\r\n");
120 if (temp_ptr == NULL)
121 die(STATE_CRITICAL, _("Invalid response from server - no load information\n"));
122 else
123 load_1min = strtod(temp_ptr, NULL);
125 temp_ptr = (char *)strtok(NULL, "\r\n");
126 if (temp_ptr == NULL)
127 die(STATE_CRITICAL, _("Invalid response from server after load 1\n"));
128 else
129 load_5min = strtod(temp_ptr, NULL);
131 temp_ptr = (char *)strtok(NULL, "\r\n");
132 if (temp_ptr == NULL)
133 die(STATE_CRITICAL, _("Invalid response from server after load 5\n"));
134 else
135 load_15min = strtod(temp_ptr, NULL);
137 switch (vars_to_check) {
138 case LOAD1:
139 strcpy(temp_buffer, "1");
140 load = load_1min;
141 break;
142 case LOAD5:
143 strcpy(temp_buffer, "5");
144 load = load_5min;
145 break;
146 default:
147 strcpy(temp_buffer, "15");
148 load = load_15min;
149 break;
152 if (check_critical_value && (load >= critical_value))
153 result = STATE_CRITICAL;
154 else if (check_warning_value && (load >= warning_value))
155 result = STATE_WARNING;
157 die(result, _("Load %s - %s-min load average = %0.2f"), state_text(result), temp_buffer, load);
159 break;
161 case DPU:
163 if (result != STATE_OK)
164 die(result, _("Unknown error fetching disk data\n"));
166 for (temp_ptr = (char *)strtok(recv_buffer, " "); temp_ptr != NULL; temp_ptr = (char *)strtok(NULL, " ")) {
168 if (!strcmp(temp_ptr, disk_name)) {
169 found_disk = true;
170 temp_ptr = (char *)strtok(NULL, "%");
171 if (temp_ptr == NULL)
172 die(STATE_CRITICAL, _("Invalid response from server\n"));
173 else
174 percent_used_disk_space = strtoul(temp_ptr, NULL, 10);
175 break;
178 temp_ptr = (char *)strtok(NULL, "\r\n");
181 /* error if we couldn't find the info for the disk */
182 if (!found_disk)
183 die(STATE_CRITICAL, "CRITICAL - Disk '%s' non-existent or not mounted", disk_name);
185 if (check_critical_value && (percent_used_disk_space >= critical_value))
186 result = STATE_CRITICAL;
187 else if (check_warning_value && (percent_used_disk_space >= warning_value))
188 result = STATE_WARNING;
190 die(result, "Disk %s - %lu%% used on %s", state_text(result), percent_used_disk_space, disk_name);
192 break;
194 case NETSTAT:
196 if (result != STATE_OK)
197 die(result, _("Unknown error fetching network status\n"));
198 else
199 port_connections = strtod(recv_buffer, NULL);
201 if (check_critical_value && (port_connections >= critical_value))
202 result = STATE_CRITICAL;
203 else if (check_warning_value && (port_connections >= warning_value))
204 result = STATE_WARNING;
206 die(result, _("Net %s - %d connection%s on port %d"), state_text(result), port_connections, (port_connections == 1) ? "" : "s",
207 netstat_port);
209 break;
211 case PROCS:
213 if (result != STATE_OK)
214 die(result, _("Unknown error fetching process status\n"));
216 temp_ptr = (char *)strtok(recv_buffer, "(");
217 if (temp_ptr == NULL)
218 die(STATE_CRITICAL, _("Invalid response from server\n"));
220 temp_ptr = (char *)strtok(NULL, ")");
221 if (temp_ptr == NULL)
222 die(STATE_CRITICAL, _("Invalid response from server\n"));
223 else
224 processes = strtod(temp_ptr, NULL);
226 if (check_critical_value && (processes >= critical_value))
227 result = STATE_CRITICAL;
228 else if (check_warning_value && (processes >= warning_value))
229 result = STATE_WARNING;
231 die(result, _("Process %s - %d instance%s of %s running"), state_text(result), processes, (processes == 1) ? "" : "s",
232 process_name);
233 break;
235 case UPTIME:
237 if (result != STATE_OK)
238 return result;
240 uptime_raw_hours = strtod(recv_buffer, NULL);
241 uptime_raw_minutes = (unsigned long)(uptime_raw_hours * 60.0);
243 if (check_critical_value && (uptime_raw_minutes <= critical_value))
244 result = STATE_CRITICAL;
245 else if (check_warning_value && (uptime_raw_minutes <= warning_value))
246 result = STATE_WARNING;
248 uptime_days = uptime_raw_minutes / 1440;
249 uptime_raw_minutes %= 1440;
250 uptime_hours = uptime_raw_minutes / 60;
251 uptime_raw_minutes %= 60;
252 uptime_minutes = uptime_raw_minutes;
254 die(result, _("Uptime %s - Up %d days %d hours %d minutes"), state_text(result), uptime_days, uptime_hours, uptime_minutes);
255 break;
257 default:
258 die(STATE_UNKNOWN, _("Nothing to check!\n"));
259 break;
263 /* process command-line arguments */
264 int process_arguments(int argc, char **argv) {
265 int c;
267 int option = 0;
268 static struct option longopts[] = {
269 {"port", required_argument, 0, 'p'}, {"timeout", required_argument, 0, 't'}, {"critical", required_argument, 0, 'c'},
270 {"warning", required_argument, 0, 'w'}, {"variable", required_argument, 0, 'v'}, {"hostname", required_argument, 0, 'H'},
271 {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
273 /* no options were supplied */
274 if (argc < 2)
275 return ERROR;
277 /* backwards compatibility */
278 if (!is_option(argv[1])) {
279 server_address = argv[1];
280 argv[1] = argv[0];
281 argv = &argv[1];
282 argc--;
285 for (c = 1; c < argc; c++) {
286 if (strcmp("-to", argv[c]) == 0)
287 strcpy(argv[c], "-t");
288 else if (strcmp("-wv", argv[c]) == 0)
289 strcpy(argv[c], "-w");
290 else if (strcmp("-cv", argv[c]) == 0)
291 strcpy(argv[c], "-c");
294 while (1) {
295 c = getopt_long(argc, argv, "+hVH:t:c:w:p:v:", longopts, &option);
297 if (c == -1 || c == EOF || c == 1)
298 break;
300 switch (c) {
301 case '?': /* print short usage statement if args not parsable */
302 usage5();
303 case 'h': /* help */
304 print_help();
305 exit(STATE_UNKNOWN);
306 case 'V': /* version */
307 print_revision(progname, NP_VERSION);
308 exit(STATE_UNKNOWN);
309 case 'H': /* hostname */
310 server_address = optarg;
311 break;
312 case 'p': /* port */
313 if (is_intnonneg(optarg))
314 server_port = atoi(optarg);
315 else
316 die(STATE_UNKNOWN, _("Server port an integer\n"));
317 break;
318 case 'v': /* variable */
319 if (strcmp(optarg, "LOAD") == 0) {
320 strcpy(send_buffer, "LOAD\r\nQUIT\r\n");
321 if (strcmp(optarg, "LOAD1") == 0)
322 vars_to_check = LOAD1;
323 else if (strcmp(optarg, "LOAD5") == 0)
324 vars_to_check = LOAD5;
325 else if (strcmp(optarg, "LOAD15") == 0)
326 vars_to_check = LOAD15;
327 } else if (strcmp(optarg, "UPTIME") == 0) {
328 vars_to_check = UPTIME;
329 strcpy(send_buffer, "UPTIME\r\n");
330 } else if (strstr(optarg, "PROC") == optarg) {
331 vars_to_check = PROCS;
332 process_name = strscpy(process_name, optarg + 4);
333 sprintf(send_buffer, "PROCESS %s\r\n", process_name);
334 } else if (strstr(optarg, "NET") == optarg) {
335 vars_to_check = NETSTAT;
336 netstat_port = atoi(optarg + 3);
337 sprintf(send_buffer, "NETSTAT %d\r\n", netstat_port);
338 } else if (strstr(optarg, "DPU") == optarg) {
339 vars_to_check = DPU;
340 strcpy(send_buffer, "DISKSPACE\r\n");
341 disk_name = strscpy(disk_name, optarg + 3);
342 } else
343 return ERROR;
344 break;
345 case 'w': /* warning threshold */
346 warning_value = strtoul(optarg, NULL, 10);
347 check_warning_value = true;
348 break;
349 case 'c': /* critical threshold */
350 critical_value = strtoul(optarg, NULL, 10);
351 check_critical_value = true;
352 break;
353 case 't': /* timeout */
354 socket_timeout = atoi(optarg);
355 if (socket_timeout <= 0)
356 return ERROR;
359 return OK;
362 void print_help(void) {
363 char *myport;
364 xasprintf(&myport, "%d", PORT);
366 print_revision(progname, NP_VERSION);
368 printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
369 printf(COPYRIGHT, copyright, email);
371 printf("%s\n", _("This plugin attempts to contact the Over-CR collector daemon running on the"));
372 printf("%s\n", _("remote UNIX server in order to gather the requested system information."));
374 printf("\n\n");
376 print_usage();
378 printf(UT_HELP_VRSN);
379 printf(UT_EXTRA_OPTS);
381 printf(UT_HOST_PORT, 'p', myport);
383 printf(" %s\n", "-w, --warning=INTEGER");
384 printf(" %s\n", _("Threshold which will result in a warning status"));
385 printf(" %s\n", "-c, --critical=INTEGER");
386 printf(" %s\n", _("Threshold which will result in a critical status"));
387 printf(" %s\n", "-v, --variable=STRING");
388 printf(" %s\n", _("Variable to check. Valid variables include:"));
389 printf(" %s\n", _("LOAD1 = 1 minute average CPU load"));
390 printf(" %s\n", _("LOAD5 = 5 minute average CPU load"));
391 printf(" %s\n", _("LOAD15 = 15 minute average CPU load"));
392 printf(" %s\n", _("DPU<filesys> = percent used disk space on filesystem <filesys>"));
393 printf(" %s\n", _("PROC<process> = number of running processes with name <process>"));
394 printf(" %s\n", _("NET<port> = number of active connections on TCP port <port>"));
395 printf(" %s\n", _("UPTIME = system uptime in seconds"));
397 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
399 printf(UT_VERBOSE);
401 printf("\n");
402 printf("%s\n", _("This plugin requires that Eric Molitors' Over-CR collector daemon be"));
403 printf("%s\n", _("running on the remote server."));
404 printf("%s\n", _("Over-CR can be downloaded from http://www.molitor.org/overcr"));
405 printf("%s\n", _("This plugin was tested with version 0.99.53 of the Over-CR collector"));
407 printf("\n");
408 printf("%s\n", _("Notes:"));
409 printf(" %s\n", _("For the available options, the critical threshold value should always be"));
410 printf(" %s\n", _("higher than the warning threshold value, EXCEPT with the uptime variable"));
412 printf(UT_SUPPORT);
415 void print_usage(void) {
416 printf("%s\n", _("Usage:"));
417 printf("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname);