Code

c711de20f4ff8bd2709b8ccd5c3ef1db7a235a03
[nagiosplug.git] / plugins / check_ups.c
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"
60 #define REVISION "$Revision$"
61 #define COPYRIGHT "1999-2002"
62 #define AUTHOR "Ethan Galstad"
63 #define EMAIL "nagios@nagios.org"
65 #define CHECK_NONE      0
67 #define PORT    3305
69 #define UPS_NONE        0                                                       /* no supported options */
70 #define UPS_UTILITY     1                                               /* supports utility line voltage */
71 #define UPS_BATTPCT     2                                               /* supports percent battery remaining */
72 #define UPS_STATUS      4                                               /* supports UPS status */
73 #define UPS_TEMP        8                                                       /* supports UPS temperature */
74 #define UPS_LOADPCT     16                                      /* supports load percent */
76 #define UPSSTATUS_NONE          0
77 #define UPSSTATUS_OFF           1
78 #define UPSSTATUS_OL            2
79 #define UPSSTATUS_OB            4
80 #define UPSSTATUS_LB            8
81 #define UPSSTATUS_CAL           16
82 #define UPSSTATUS_UNKOWN        32
84 int server_port = PORT;
85 char *server_address = NULL;
86 char *ups_name = NULL;
87 double warning_value = 0.0L;
88 double critical_value = 0.0L;
89 int check_warning_value = FALSE;
90 int check_critical_value = FALSE;
91 int check_variable = UPS_NONE;
92 int supported_options = UPS_NONE;
93 int status = UPSSTATUS_NONE;
95 double ups_utility_voltage = 0.0L;
96 double ups_battery_percent = 0.0L;
97 double ups_load_percent = 0.0L;
98 double ups_temperature = 0.0L;
99 char ups_status[MAX_INPUT_BUFFER] = "N/A";
101 int determine_status (void);
102 int determine_supported_vars (void);
103 int get_ups_variable (const char *, char *, int);
105 int process_arguments (int, char **);
106 int call_getopt (int, char **);
107 int validate_arguments (void);
108 void print_help (void);
109 void print_usage (void);
111 int
112 main (int argc, char **argv)
114         int result = STATE_OK;
115         char output_message[MAX_INPUT_BUFFER];
116         char temp_buffer[MAX_INPUT_BUFFER];
118         double ups_utility_deviation = 0.0L;
120         if (process_arguments (argc, argv) != OK)
121                 usage ("Invalid command arguments supplied\n");
123         /* initialize alarm signal handling */
124         signal (SIGALRM, socket_timeout_alarm_handler);
126         /* set socket timeout */
127         alarm (socket_timeout);
129         /* determine what variables the UPS supports */
130         if (determine_supported_vars () != OK)
131                 return STATE_CRITICAL;
133         /* get the ups status if possible */
134         if (supported_options & UPS_STATUS) {
136                 if (determine_status () != OK)
137                         return STATE_CRITICAL;
138                 ups_status[0] = 0;
139                 result = STATE_OK;
141                 if (status & UPSSTATUS_OFF) {
142                         strcpy (ups_status, "Off");
143                         result = STATE_CRITICAL;
144                 }
145                 else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) ==
146                                                  (UPSSTATUS_OB | UPSSTATUS_LB)) {
147                         strcpy (ups_status, "On Battery, Low Battery");
148                         result = STATE_CRITICAL;
149                 }
150                 else {
151                         if (status & UPSSTATUS_OL) {
152                                 strcat (ups_status, "Online");
153                         }
154                         if (status & UPSSTATUS_OB) {
155                                 strcat (ups_status, "On Battery");
156                                 result = STATE_WARNING;
157                         }
158                         if (status & UPSSTATUS_LB) {
159                                 strcat (ups_status, ", Low Battery");
160                                 result = STATE_WARNING;
161                         }
162                         if (status & UPSSTATUS_CAL) {
163                                 strcat (ups_status, ", Calibrating");
164                         }
165                         if (status & UPSSTATUS_UNKOWN) {
166                                 strcat (ups_status, ", Unknown");
167                         }
168                 }
169         }
171         /* get the ups utility voltage if possible */
172         if (supported_options & UPS_UTILITY) {
174                 if (get_ups_variable ("UTILITY", temp_buffer, sizeof (temp_buffer)) != OK)
175                         return STATE_CRITICAL;
177                 ups_utility_voltage = atof (temp_buffer);
179                 if (ups_utility_voltage > 120.0)
180                         ups_utility_deviation = 120.0 - ups_utility_voltage;
181                 else
182                         ups_utility_deviation = ups_utility_voltage - 120.0;
184                 if (check_variable == UPS_UTILITY) {
185                         if (check_critical_value == TRUE
186                                         && ups_utility_deviation >= critical_value) result = STATE_CRITICAL;
187                         else if (check_warning_value == TRUE
188                                                          && ups_utility_deviation >= warning_value
189                                                          && result < STATE_WARNING) result = STATE_WARNING;
190                 }
191         }
193         /* get the ups battery percent if possible */
194         if (supported_options & UPS_BATTPCT) {
196                 if (get_ups_variable ("BATTPCT", temp_buffer, sizeof (temp_buffer)) != OK)
197                         return STATE_CRITICAL;
199                 ups_battery_percent = atof (temp_buffer);
201                 if (check_variable == UPS_BATTPCT) {
202                         if (check_critical_value == TRUE
203                                         && ups_battery_percent <= critical_value) result = STATE_CRITICAL;
204                         else if (check_warning_value == TRUE
205                                                          && ups_battery_percent <= warning_value
206                                                          && result < STATE_WARNING) result = STATE_WARNING;
207                 }
208         }
210         /* get the ups load percent if possible */
211         if (supported_options & UPS_LOADPCT) {
213                 if (get_ups_variable ("LOADPCT", temp_buffer, sizeof (temp_buffer)) != OK)
214                         return STATE_CRITICAL;
216                 ups_load_percent = atof (temp_buffer);
218                 if (check_variable == UPS_LOADPCT) {
219                         if (check_critical_value == TRUE && ups_load_percent >= critical_value)
220                                 result = STATE_CRITICAL;
221                         else if (check_warning_value == TRUE
222                                                          && ups_load_percent >= warning_value && result < STATE_WARNING)
223                                 result = STATE_WARNING;
224                 }
225         }
227         /* get the ups temperature if possible */
228         if (supported_options & UPS_TEMP) {
230                 if (get_ups_variable ("UPSTEMP", temp_buffer, sizeof (temp_buffer)) != OK)
231                         return STATE_CRITICAL;
233                 ups_temperature = (atof (temp_buffer) * 1.8) + 32;
235                 if (check_variable == UPS_TEMP) {
236                         if (check_critical_value == TRUE && ups_temperature >= critical_value)
237                                 result = STATE_CRITICAL;
238                         else if (check_warning_value == TRUE && ups_temperature >= warning_value
239                                                          && result < STATE_WARNING)
240                                 result = STATE_WARNING;
241                 }
242         }
244         /* if the UPS does not support any options we are looking for, report an error */
245         if (supported_options == UPS_NONE)
246                 result = STATE_CRITICAL;
248         /* reset timeout */
249         alarm (0);
252         sprintf (output_message, "UPS %s - ",
253                                          (result == STATE_OK) ? "ok" : "problem");
255         if (supported_options & UPS_STATUS) {
256                 sprintf (temp_buffer, "Status=%s ", ups_status);
257                 strcat (output_message, temp_buffer);
258         }
259         if (supported_options & UPS_UTILITY) {
260                 sprintf (temp_buffer, "Utility=%3.1fV ", ups_utility_voltage);
261                 strcat (output_message, temp_buffer);
262         }
263         if (supported_options & UPS_BATTPCT) {
264                 sprintf (temp_buffer, "Batt=%3.1f%% ", ups_battery_percent);
265                 strcat (output_message, temp_buffer);
266         }
267         if (supported_options & UPS_LOADPCT) {
268                 sprintf (temp_buffer, "Load=%3.1f%% ", ups_load_percent);
269                 strcat (output_message, temp_buffer);
270         }
271         if (supported_options & UPS_TEMP) {
272                 sprintf (temp_buffer, "Temp=%3.1fF", ups_temperature);
273                 strcat (output_message, temp_buffer);
274         }
275         if (supported_options == UPS_NONE) {
276                 sprintf (temp_buffer,
277                                                  "UPS does not appear to support any available options\n");
278                 strcat (output_message, temp_buffer);
279         }
281         printf ("%s\n", output_message);
283         return result;
288 /* determines what options are supported by the UPS */
289 int
290 determine_status (void)
292         char recv_buffer[MAX_INPUT_BUFFER];
293         char temp_buffer[MAX_INPUT_BUFFER];
294         char *ptr;
296         if (get_ups_variable ("STATUS", recv_buffer, sizeof (recv_buffer)) !=
297                         STATE_OK) {
298                 printf ("Invalid response received from hostn");
299                 return ERROR;
300         }
302         recv_buffer[strlen (recv_buffer) - 1] = 0;
304         strcpy (temp_buffer, recv_buffer);
305         for (ptr = (char *) strtok (temp_buffer, " "); ptr != NULL;
306                          ptr = (char *) strtok (NULL, " ")) {
307                 if (!strcmp (ptr, "OFF"))
308                         status |= UPSSTATUS_OFF;
309                 else if (!strcmp (ptr, "OL"))
310                         status |= UPSSTATUS_OL;
311                 else if (!strcmp (ptr, "OB"))
312                         status |= UPSSTATUS_OB;
313                 else if (!strcmp (ptr, "LB"))
314                         status |= UPSSTATUS_LB;
315                 else if (!strcmp (ptr, "CAL"))
316                         status |= UPSSTATUS_CAL;
317                 else
318                         status |= UPSSTATUS_UNKOWN;
319         }
321         return OK;
325 /* determines what options are supported by the UPS */
326 int
327 determine_supported_vars (void)
329         char send_buffer[MAX_INPUT_BUFFER];
330         char recv_buffer[MAX_INPUT_BUFFER];
331         char temp_buffer[MAX_INPUT_BUFFER];
332         char *ptr;
335         /* get the list of variables that this UPS supports */
336         if (ups_name)
337                 sprintf (send_buffer, "LISTVARS %s\r\n", ups_name);
338         else
339                 sprintf (send_buffer, "LISTVARS\r\n");
340         if (process_tcp_request
341                         (server_address, server_port, send_buffer, recv_buffer,
342                          sizeof (recv_buffer)) != STATE_OK) {
343                 printf ("Invalid response received from host\n");
344                 return ERROR;
345         }
347         recv_buffer[strlen (recv_buffer) - 1] = 0;
349         if (ups_name)
350                 ptr = recv_buffer + 5 + strlen (ups_name) + 2;
351         else
352                 ptr = recv_buffer + 5;
354         strcpy (temp_buffer, recv_buffer);
356         for (ptr = (char *) strtok (temp_buffer, " "); ptr != NULL;
357                          ptr = (char *) strtok (NULL, " ")) {
358                 if (!strcmp (ptr, "UTILITY"))
359                         supported_options |= UPS_UTILITY;
360                 else if (!strcmp (ptr, "BATTPCT"))
361                         supported_options |= UPS_BATTPCT;
362                 else if (!strcmp (ptr, "LOADPCT"))
363                         supported_options |= UPS_LOADPCT;
364                 else if (!strcmp (ptr, "STATUS"))
365                         supported_options |= UPS_STATUS;
366                 else if (!strcmp (ptr, "UPSTEMP"))
367                         supported_options |= UPS_TEMP;
368         }
370         return OK;
374 /* gets a variable value for a specific UPS  */
375 int
376 get_ups_variable (const char *varname, char *buf, int buflen)
378         /*  char command[MAX_INPUT_BUFFER]; */
379         char temp_buffer[MAX_INPUT_BUFFER];
380         char send_buffer[MAX_INPUT_BUFFER];
381         char *ptr;
383         /* create the command string to send to the UPS daemon */
384         if (ups_name)
385                 sprintf (send_buffer, "REQ %s@%s\n", varname, ups_name);
386         else
387                 sprintf (send_buffer, "REQ %s\n", varname);
389         /* send the command to the daemon and get a response back */
390         if (process_tcp_request
391                         (server_address, server_port, send_buffer, temp_buffer,
392                          sizeof (temp_buffer)) != STATE_OK) {
393                 printf ("Invalid response received from host\n");
394                 return ERROR;
395         }
397         if (ups_name)
398                 ptr = temp_buffer + strlen (varname) + 5 + strlen (ups_name) + 1;
399         else
400                 ptr = temp_buffer + strlen (varname) + 5;
402         if (!strcmp (ptr, "NOT-SUPPORTED")) {
403                 printf ("Error: Variable '%s' is not supported\n", varname);
404                 return ERROR;
405         }
407         if (!strcmp (ptr, "DATA-STALE")) {
408                 printf ("Error: UPS data is stale\n");
409                 return ERROR;
410         }
412         if (!strcmp (ptr, "UNKNOWN-UPS")) {
413                 if (ups_name)
414                         printf ("Error: UPS '%s' is unknown\n", ups_name);
415                 else
416                         printf ("Error: UPS is unknown\n");
417                 return ERROR;
418         }
420         strncpy (buf, ptr, buflen - 1);
421         buf[buflen - 1] = 0;
423         return OK;
430 /* Command line: CHECK_UPS <host_address> [-u ups] [-p port] [-v variable] 
431                            [-wv warn_value] [-cv crit_value] [-to to_sec] */
434 /* process command-line arguments */
435 int
436 process_arguments (int argc, char **argv)
438         int c;
440 #ifdef HAVE_GETOPT_H
441         int option_index = 0;
442         static struct option long_options[] = {
443                 {"hostname", required_argument, 0, 'H'},
444                 {"ups", required_argument, 0, 'u'},
445                 {"port", required_argument, 0, 'p'},
446                 {"critical", required_argument, 0, 'c'},
447                 {"warning", required_argument, 0, 'w'},
448                 {"timeout", required_argument, 0, 't'},
449                 {"variable", required_argument, 0, 'v'},
450                 {"version", no_argument, 0, 'V'},
451                 {"help", no_argument, 0, 'h'},
452                 {0, 0, 0, 0}
453         };
454 #endif
456         if (argc < 2)
457                 return ERROR;
459         for (c = 1; c < argc; c++) {
460                 if (strcmp ("-to", argv[c]) == 0)
461                         strcpy (argv[c], "-t");
462                 else if (strcmp ("-wt", argv[c]) == 0)
463                         strcpy (argv[c], "-w");
464                 else if (strcmp ("-ct", argv[c]) == 0)
465                         strcpy (argv[c], "-c");
466         }
468         while (1) {
469 #ifdef HAVE_GETOPT_H
470                 c =
471                         getopt_long (argc, argv, "hVH:u:p:v:c:w:t:", long_options,
472                                                                          &option_index);
473 #else
474                 c = getopt (argc, argv, "hVH:u:p:v:c:w:t:");
475 #endif
477                 if (c == -1 || c == EOF)
478                         break;
480                 switch (c) {
481                 case '?':                                                                       /* help */
482                         usage3 ("Unknown option", optopt);
483                 case 'H':                                                                       /* hostname */
484                         if (is_host (optarg)) {
485                                 server_address = optarg;
486                         }
487                         else {
488                                 usage2 ("Invalid host name", optarg);
489                         }
490                         break;
491                 case 'u':                                                                       /* ups name */
492                         ups_name = optarg;
493                         break;
494                 case 'p':                                                                       /* port */
495                         if (is_intpos (optarg)) {
496                                 server_port = atoi (optarg);
497                         }
498                         else {
499                                 usage2 ("Server port must be a positive integer", optarg);
500                         }
501                         break;
502                 case 'c':                                                                       /* critical time threshold */
503                         if (is_intnonneg (optarg)) {
504                                 critical_value = atoi (optarg);
505                                 check_critical_value = TRUE;
506                         }
507                         else {
508                                 usage2 ("Critical time must be a nonnegative integer", optarg);
509                         }
510                         break;
511                 case 'w':                                                                       /* warning time threshold */
512                         if (is_intnonneg (optarg)) {
513                                 warning_value = atoi (optarg);
514                                 check_warning_value = TRUE;
515                         }
516                         else {
517                                 usage2 ("Warning time must be a nonnegative integer", optarg);
518                         }
519                         break;
520                 case 'v':                                                                       /* variable */
521                         if (!strcmp (optarg, "LINE"))
522                                 check_variable = UPS_UTILITY;
523                         else if (!strcmp (optarg, "TEMP"))
524                                 check_variable = UPS_TEMP;
525                         else if (!strcmp (optarg, "BATTPCT"))
526                                 check_variable = UPS_BATTPCT;
527                         else if (!strcmp (optarg, "LOADPCT"))
528                                 check_variable = UPS_LOADPCT;
529                         else
530                                 usage2 ("Unrecognized UPS variable", optarg);
531                         break;
532                 case 't':                                                                       /* timeout */
533                         if (is_intnonneg (optarg)) {
534                                 socket_timeout = atoi (optarg);
535                         }
536                         else {
537                                 usage ("Time interval must be a nonnegative integer\n");
538                         }
539                         break;
540                 case 'V':                                                                       /* version */
541                         print_revision (PROGNAME, "$Revision$");
542                         exit (STATE_OK);
543                 case 'h':                                                                       /* help */
544                         print_help ();
545                         exit (STATE_OK);
546                 }
547         }
550         if (server_address == NULL) {
551                 if (optind >= argc) {
552                         server_address = strscpy (NULL, "127.0.0.1");
553                 }
554                 else if (is_host (argv[optind])) {
555                         server_address = argv[optind++];
556                 }
557                 else {
558                         usage ("Invalid host name");
559                 }
560         }
561         return validate_arguments();
568 int
569 validate_arguments (void)
571         return OK;
578 void
579 print_help (void)
581         print_revision (PROGNAME, "$Revision$");
582         printf
583                 ("Copyright (c) 2000 Tom Shields/Karl DeBisschop\n\n"
584                  "This plugin tests the UPS service on the specified host.\n"
585                  "Newtork UPS Tools for www.exploits.org must be running for this plugin to work.\n\n");
586         print_usage ();
587         printf
588                 ("\nOptions:\n"
589                  " -H, --hostname=STRING or IPADDRESS\n"
590                  "   Check server on the indicated host\n"
591                  " -p, --port=INTEGER\n"
592                  "   Make connection on the indicated port (default: %d)\n"
593                  " -u, --ups=STRING\n"
594                  "   Name of UPS\n"
595                  " -w, --warning=INTEGER\n"
596                  "   Seconds necessary to result in a warning status\n"
597                  " -c, --critical=INTEGER\n"
598                  "   Seconds necessary to result in a critical status\n"
599                  " -t, --timeout=INTEGER\n"
600                  "   Seconds before connection attempt times out (default: %d)\n"
601                  " -v, --verbose\n"
602                  "   Print extra information (command-line use only)\n"
603                  " -h, --help\n"
604                  "   Print detailed help screen\n"
605                  " -V, --version\n"
606                  "   Print version information\n\n", PORT, DEFAULT_SOCKET_TIMEOUT);
607         support ();
614 void
615 print_usage (void)
617         printf
618                 ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n"
619                  "            [-t timeout] [-v]\n"
620                  "       %s --help\n"
621                  "       %s --version\n", PROGNAME, PROGNAME, PROGNAME);