1 /******************************************************************************
2 *
3 * CHECK_UPS.C
4 *
5 * Program: UPS monitor plugin for Nagios
6 * License: GPL
7 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8 *
9 * Last Modified: $Date$
10 *
11 * Command line: CHECK_UPS <host_address> [-u ups] [-p port] [-v variable] \
12 * [-wv warn_value] [-cv crit_value] [-to to_sec]
13 *
14 * Description:
15 *
17 * This plugin attempts to determine the status of an UPS
18 * (Uninterruptible Power Supply) on a remote host (or the local host)
19 * that is being monitored with Russel Kroll's "Smarty UPS Tools"
20 * package. If the UPS is online or calibrating, the plugin will
21 * return an OK state. If the battery is on it will return a WARNING
22 * state. If the UPS is off or has a low battery the plugin will
23 * return a CRITICAL state. You may also specify a variable to check
24 * (such as temperature, utility voltage, battery load, etc.) as well
25 * as warning and critical thresholds for the value of that variable.
26 * If the remote host has multiple UPS that are being monitored you
27 * will have to use the [ups] option to specify which UPS to check.
28 *
29 * Notes:
30 *
31 * This plugin requires that the UPSD daemon distributed with Russel
32 * Kroll's "Smart UPS Tools" be installed on the remote host. If you
33 * don't have the package installed on your system, you can download
34 * it from http://www.exploits.org/~rkroll/smartupstools
35 *
36 * License Information:
37 *
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version.
42 *
43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
47 *
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
51 *
52 ******************************************************************************/
54 #include "config.h"
55 #include "common.h"
56 #include "netutils.h"
57 #include "utils.h"
59 #define PROGNAME "check_ups"
61 #define CHECK_NONE 0
63 #define PORT 3305
65 #define UPS_NONE 0 /* no supported options */
66 #define UPS_UTILITY 1 /* supports utility line voltage */
67 #define UPS_BATTPCT 2 /* supports percent battery remaining */
68 #define UPS_STATUS 4 /* supports UPS status */
69 #define UPS_TEMP 8 /* supports UPS temperature */
70 #define UPS_LOADPCT 16 /* supports load percent */
72 #define UPSSTATUS_NONE 0
73 #define UPSSTATUS_OFF 1
74 #define UPSSTATUS_OL 2
75 #define UPSSTATUS_OB 4
76 #define UPSSTATUS_LB 8
77 #define UPSSTATUS_CAL 16
78 #define UPSSTATUS_UNKOWN 32
80 int server_port = PORT;
81 char *server_address = NULL;
82 char *ups_name = NULL;
83 double warning_value = 0.0L;
84 double critical_value = 0.0L;
85 int check_warning_value = FALSE;
86 int check_critical_value = FALSE;
87 int check_variable = UPS_NONE;
88 int supported_options = UPS_NONE;
89 int status = UPSSTATUS_NONE;
91 double ups_utility_voltage = 0.0L;
92 double ups_battery_percent = 0.0L;
93 double ups_load_percent = 0.0L;
94 double ups_temperature = 0.0L;
95 char ups_status[MAX_INPUT_BUFFER] = "N/A";
97 int determine_status (void);
98 int determine_supported_vars (void);
99 int get_ups_variable (const char *, char *, int);
101 int process_arguments (int, char **);
102 int call_getopt (int, char **);
103 int validate_arguments (void);
104 void print_help (void);
105 void print_usage (void);
107 int
108 main (int argc, char **argv)
109 {
110 int result = STATE_OK;
111 char output_message[MAX_INPUT_BUFFER];
112 char temp_buffer[MAX_INPUT_BUFFER];
114 double ups_utility_deviation = 0.0L;
116 if (process_arguments (argc, argv) != OK)
117 usage ("Invalid command arguments supplied\n");
119 /* initialize alarm signal handling */
120 signal (SIGALRM, socket_timeout_alarm_handler);
122 /* set socket timeout */
123 alarm (socket_timeout);
125 /* determine what variables the UPS supports */
126 if (determine_supported_vars () != OK)
127 return STATE_CRITICAL;
129 /* get the ups status if possible */
130 if (supported_options & UPS_STATUS) {
132 if (determine_status () != OK)
133 return STATE_CRITICAL;
134 ups_status[0] = 0;
135 result = STATE_OK;
137 if (status & UPSSTATUS_OFF) {
138 strcpy (ups_status, "Off");
139 result = STATE_CRITICAL;
140 }
141 else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) ==
142 (UPSSTATUS_OB | UPSSTATUS_LB)) {
143 strcpy (ups_status, "On Battery, Low Battery");
144 result = STATE_CRITICAL;
145 }
146 else {
147 if (status & UPSSTATUS_OL) {
148 strcat (ups_status, "Online");
149 }
150 if (status & UPSSTATUS_OB) {
151 strcat (ups_status, "On Battery");
152 result = STATE_WARNING;
153 }
154 if (status & UPSSTATUS_LB) {
155 strcat (ups_status, ", Low Battery");
156 result = STATE_WARNING;
157 }
158 if (status & UPSSTATUS_CAL) {
159 strcat (ups_status, ", Calibrating");
160 }
161 if (status & UPSSTATUS_UNKOWN) {
162 strcat (ups_status, ", Unknown");
163 }
164 }
165 }
167 /* get the ups utility voltage if possible */
168 if (supported_options & UPS_UTILITY) {
170 if (get_ups_variable ("UTILITY", temp_buffer, sizeof (temp_buffer)) != OK)
171 return STATE_CRITICAL;
173 ups_utility_voltage = atof (temp_buffer);
175 if (ups_utility_voltage > 120.0)
176 ups_utility_deviation = 120.0 - ups_utility_voltage;
177 else
178 ups_utility_deviation = ups_utility_voltage - 120.0;
180 if (check_variable == UPS_UTILITY) {
181 if (check_critical_value == TRUE
182 && ups_utility_deviation >= critical_value) result = STATE_CRITICAL;
183 else if (check_warning_value == TRUE
184 && ups_utility_deviation >= warning_value
185 && result < STATE_WARNING) result = STATE_WARNING;
186 }
187 }
189 /* get the ups battery percent if possible */
190 if (supported_options & UPS_BATTPCT) {
192 if (get_ups_variable ("BATTPCT", temp_buffer, sizeof (temp_buffer)) != OK)
193 return STATE_CRITICAL;
195 ups_battery_percent = atof (temp_buffer);
197 if (check_variable == UPS_BATTPCT) {
198 if (check_critical_value == TRUE
199 && ups_battery_percent <= critical_value) result = STATE_CRITICAL;
200 else if (check_warning_value == TRUE
201 && ups_battery_percent <= warning_value
202 && result < STATE_WARNING) result = STATE_WARNING;
203 }
204 }
206 /* get the ups load percent if possible */
207 if (supported_options & UPS_LOADPCT) {
209 if (get_ups_variable ("LOADPCT", temp_buffer, sizeof (temp_buffer)) != OK)
210 return STATE_CRITICAL;
212 ups_load_percent = atof (temp_buffer);
214 if (check_variable == UPS_LOADPCT) {
215 if (check_critical_value == TRUE && ups_load_percent >= critical_value)
216 result = STATE_CRITICAL;
217 else if (check_warning_value == TRUE
218 && ups_load_percent >= warning_value && result < STATE_WARNING)
219 result = STATE_WARNING;
220 }
221 }
223 /* get the ups temperature if possible */
224 if (supported_options & UPS_TEMP) {
226 if (get_ups_variable ("UPSTEMP", temp_buffer, sizeof (temp_buffer)) != OK)
227 return STATE_CRITICAL;
229 ups_temperature = (atof (temp_buffer) * 1.8) + 32;
231 if (check_variable == UPS_TEMP) {
232 if (check_critical_value == TRUE && ups_temperature >= critical_value)
233 result = STATE_CRITICAL;
234 else if (check_warning_value == TRUE && ups_temperature >= warning_value
235 && result < STATE_WARNING)
236 result = STATE_WARNING;
237 }
238 }
240 /* if the UPS does not support any options we are looking for, report an error */
241 if (supported_options == UPS_NONE)
242 result = STATE_CRITICAL;
244 /* reset timeout */
245 alarm (0);
248 sprintf (output_message, "UPS %s - ",
249 (result == STATE_OK) ? "ok" : "problem");
251 if (supported_options & UPS_STATUS) {
252 sprintf (temp_buffer, "Status=%s ", ups_status);
253 strcat (output_message, temp_buffer);
254 }
255 if (supported_options & UPS_UTILITY) {
256 sprintf (temp_buffer, "Utility=%3.1fV ", ups_utility_voltage);
257 strcat (output_message, temp_buffer);
258 }
259 if (supported_options & UPS_BATTPCT) {
260 sprintf (temp_buffer, "Batt=%3.1f%% ", ups_battery_percent);
261 strcat (output_message, temp_buffer);
262 }
263 if (supported_options & UPS_LOADPCT) {
264 sprintf (temp_buffer, "Load=%3.1f%% ", ups_load_percent);
265 strcat (output_message, temp_buffer);
266 }
267 if (supported_options & UPS_TEMP) {
268 sprintf (temp_buffer, "Temp=%3.1fF", ups_temperature);
269 strcat (output_message, temp_buffer);
270 }
271 if (supported_options == UPS_NONE) {
272 sprintf (temp_buffer,
273 "UPS does not appear to support any available options\n");
274 strcat (output_message, temp_buffer);
275 }
277 printf ("%s\n", output_message);
279 return result;
280 }
284 /* determines what options are supported by the UPS */
285 int
286 determine_status (void)
287 {
288 char recv_buffer[MAX_INPUT_BUFFER];
289 char temp_buffer[MAX_INPUT_BUFFER];
290 char *ptr;
292 if (get_ups_variable ("STATUS", recv_buffer, sizeof (recv_buffer)) !=
293 STATE_OK) {
294 printf ("Invalid response received from hostn");
295 return ERROR;
296 }
298 recv_buffer[strlen (recv_buffer) - 1] = 0;
300 strcpy (temp_buffer, recv_buffer);
301 for (ptr = (char *) strtok (temp_buffer, " "); ptr != NULL;
302 ptr = (char *) strtok (NULL, " ")) {
303 if (!strcmp (ptr, "OFF"))
304 status |= UPSSTATUS_OFF;
305 else if (!strcmp (ptr, "OL"))
306 status |= UPSSTATUS_OL;
307 else if (!strcmp (ptr, "OB"))
308 status |= UPSSTATUS_OB;
309 else if (!strcmp (ptr, "LB"))
310 status |= UPSSTATUS_LB;
311 else if (!strcmp (ptr, "CAL"))
312 status |= UPSSTATUS_CAL;
313 else
314 status |= UPSSTATUS_UNKOWN;
315 }
317 return OK;
318 }
321 /* determines what options are supported by the UPS */
322 int
323 determine_supported_vars (void)
324 {
325 char send_buffer[MAX_INPUT_BUFFER];
326 char recv_buffer[MAX_INPUT_BUFFER];
327 char temp_buffer[MAX_INPUT_BUFFER];
328 char *ptr;
331 /* get the list of variables that this UPS supports */
332 if (ups_name)
333 sprintf (send_buffer, "LISTVARS %s\r\n", ups_name);
334 else
335 sprintf (send_buffer, "LISTVARS\r\n");
336 if (process_tcp_request
337 (server_address, server_port, send_buffer, recv_buffer,
338 sizeof (recv_buffer)) != STATE_OK) {
339 printf ("Invalid response received from host\n");
340 return ERROR;
341 }
343 recv_buffer[strlen (recv_buffer) - 1] = 0;
345 if (ups_name)
346 ptr = recv_buffer + 5 + strlen (ups_name) + 2;
347 else
348 ptr = recv_buffer + 5;
350 strcpy (temp_buffer, recv_buffer);
352 for (ptr = (char *) strtok (temp_buffer, " "); ptr != NULL;
353 ptr = (char *) strtok (NULL, " ")) {
354 if (!strcmp (ptr, "UTILITY"))
355 supported_options |= UPS_UTILITY;
356 else if (!strcmp (ptr, "BATTPCT"))
357 supported_options |= UPS_BATTPCT;
358 else if (!strcmp (ptr, "LOADPCT"))
359 supported_options |= UPS_LOADPCT;
360 else if (!strcmp (ptr, "STATUS"))
361 supported_options |= UPS_STATUS;
362 else if (!strcmp (ptr, "UPSTEMP"))
363 supported_options |= UPS_TEMP;
364 }
366 return OK;
367 }
370 /* gets a variable value for a specific UPS */
371 int
372 get_ups_variable (const char *varname, char *buf, int buflen)
373 {
374 /* char command[MAX_INPUT_BUFFER]; */
375 char temp_buffer[MAX_INPUT_BUFFER];
376 char send_buffer[MAX_INPUT_BUFFER];
377 char *ptr;
379 /* create the command string to send to the UPS daemon */
380 if (ups_name)
381 sprintf (send_buffer, "REQ %s@%s\n", varname, ups_name);
382 else
383 sprintf (send_buffer, "REQ %s\n", varname);
385 /* send the command to the daemon and get a response back */
386 if (process_tcp_request
387 (server_address, server_port, send_buffer, temp_buffer,
388 sizeof (temp_buffer)) != STATE_OK) {
389 printf ("Invalid response received from host\n");
390 return ERROR;
391 }
393 if (ups_name)
394 ptr = temp_buffer + strlen (varname) + 5 + strlen (ups_name) + 1;
395 else
396 ptr = temp_buffer + strlen (varname) + 5;
398 if (!strcmp (ptr, "NOT-SUPPORTED")) {
399 printf ("Error: Variable '%s' is not supported\n", varname);
400 return ERROR;
401 }
403 if (!strcmp (ptr, "DATA-STALE")) {
404 printf ("Error: UPS data is stale\n");
405 return ERROR;
406 }
408 if (!strcmp (ptr, "UNKNOWN-UPS")) {
409 if (ups_name)
410 printf ("Error: UPS '%s' is unknown\n", ups_name);
411 else
412 printf ("Error: UPS is unknown\n");
413 return ERROR;
414 }
416 strncpy (buf, ptr, buflen - 1);
417 buf[buflen - 1] = 0;
419 return OK;
420 }
426 /* Command line: CHECK_UPS <host_address> [-u ups] [-p port] [-v variable]
427 [-wv warn_value] [-cv crit_value] [-to to_sec] */
430 /* process command-line arguments */
431 int
432 process_arguments (int argc, char **argv)
433 {
434 int c;
436 if (argc < 2)
437 return ERROR;
439 for (c = 1; c < argc; c++) {
440 if (strcmp ("-to", argv[c]) == 0)
441 strcpy (argv[c], "-t");
442 else if (strcmp ("-wt", argv[c]) == 0)
443 strcpy (argv[c], "-w");
444 else if (strcmp ("-ct", argv[c]) == 0)
445 strcpy (argv[c], "-c");
446 }
448 c = 0;
449 while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
451 if (is_option (argv[c]))
452 continue;
454 if (server_address == NULL) {
455 if (is_host (argv[c])) {
456 server_address = argv[c];
457 }
458 else {
459 usage ("Invalid host name");
460 }
461 }
462 }
464 if (server_address == NULL)
465 server_address = strscpy (NULL, "127.0.0.1");
467 return validate_arguments ();
468 }
475 int
476 call_getopt (int argc, char **argv)
477 {
478 int c, i = 0;
480 #ifdef HAVE_GETOPT_H
481 int option_index = 0;
482 static struct option long_options[] = {
483 {"hostname", required_argument, 0, 'H'},
484 {"ups", required_argument, 0, 'u'},
485 {"port", required_argument, 0, 'p'},
486 {"critical", required_argument, 0, 'c'},
487 {"warning", required_argument, 0, 'w'},
488 {"timeout", required_argument, 0, 't'},
489 {"variable", required_argument, 0, 'v'},
490 {"version", no_argument, 0, 'V'},
491 {"help", no_argument, 0, 'h'},
492 {0, 0, 0, 0}
493 };
494 #endif
496 while (1) {
497 #ifdef HAVE_GETOPT_H
498 c =
499 getopt_long (argc, argv, "+hVH:u:p:v:c:w:t:", long_options,
500 &option_index);
501 #else
502 c = getopt (argc, argv, "+?hVH:u:p:v:c:w:t:");
503 #endif
505 i++;
507 if (c == -1 || c == EOF || c == 1)
508 break;
510 switch (c) {
511 case 'H':
512 case 'u':
513 case 'p':
514 case 'v':
515 case 'c':
516 case 'w':
517 case 't':
518 i++;
519 }
521 switch (c) {
522 case '?': /* help */
523 usage ("Invalid argument\n");
524 case 'H': /* hostname */
525 if (is_host (optarg)) {
526 server_address = optarg;
527 }
528 else {
529 usage ("Invalid host name\n");
530 }
531 break;
532 case 'u': /* ups name */
533 ups_name = optarg;
534 break;
535 case 'p': /* port */
536 if (is_intpos (optarg)) {
537 server_port = atoi (optarg);
538 }
539 else {
540 usage ("Server port must be a positive integer\n");
541 }
542 break;
543 case 'c': /* critical time threshold */
544 if (is_intnonneg (optarg)) {
545 critical_value = atoi (optarg);
546 check_critical_value = TRUE;
547 }
548 else {
549 usage ("Critical time must be a nonnegative integer\n");
550 }
551 break;
552 case 'w': /* warning time threshold */
553 if (is_intnonneg (optarg)) {
554 warning_value = atoi (optarg);
555 check_warning_value = TRUE;
556 }
557 else {
558 usage ("Warning time must be a nonnegative integer\n");
559 }
560 break;
561 case 'v': /* variable */
562 if (!strcmp (optarg, "LINE"))
563 check_variable = UPS_UTILITY;
564 else if (!strcmp (optarg, "TEMP"))
565 check_variable = UPS_TEMP;
566 else if (!strcmp (optarg, "BATTPCT"))
567 check_variable = UPS_BATTPCT;
568 else if (!strcmp (optarg, "LOADPCT"))
569 check_variable = UPS_LOADPCT;
570 else
571 usage ("Unrecognized UPS variable\n");
572 break;
573 case 't': /* timeout */
574 if (is_intnonneg (optarg)) {
575 socket_timeout = atoi (optarg);
576 }
577 else {
578 usage ("Time interval must be a nonnegative integer\n");
579 }
580 break;
581 case 'V': /* version */
582 print_revision (PROGNAME, "$Revision$");
583 exit (STATE_OK);
584 case 'h': /* help */
585 print_help ();
586 exit (STATE_OK);
587 }
588 }
589 return i;
590 }
596 int
597 validate_arguments (void)
598 {
599 return OK;
600 }
606 void
607 print_help (void)
608 {
609 print_revision (PROGNAME, "$Revision$");
610 printf
611 ("Copyright (c) 2000 Tom Shields/Karl DeBisschop\n\n"
612 "This plugin tests the UPS service on the specified host.\n"
613 "Newtork UPS Tools for www.exploits.org must be running for this plugin to work.\n\n");
614 print_usage ();
615 printf
616 ("\nOptions:\n"
617 " -H, --hostname=STRING or IPADDRESS\n"
618 " Check server on the indicated host\n"
619 " -p, --port=INTEGER\n"
620 " Make connection on the indicated port (default: %d)\n"
621 " -u, --ups=STRING\n"
622 " Name of UPS\n"
623 " -w, --warning=INTEGER\n"
624 " Seconds necessary to result in a warning status\n"
625 " -c, --critical=INTEGER\n"
626 " Seconds necessary to result in a critical status\n"
627 " -t, --timeout=INTEGER\n"
628 " Seconds before connection attempt times out (default: %d)\n"
629 " -v, --verbose\n"
630 " Print extra information (command-line use only)\n"
631 " -h, --help\n"
632 " Print detailed help screen\n"
633 " -V, --version\n"
634 " Print version information\n\n", PORT, DEFAULT_SOCKET_TIMEOUT);
635 support ();
636 }
642 void
643 print_usage (void)
644 {
645 printf
646 ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n"
647 " [-t timeout] [-v]\n"
648 " %s --help\n"
649 " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
650 }