1 /*****************************************************************************
2 *
3 * Nagios check_ups plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000 Tom Shields
7 * 2004 Alain Richard <alain.richard@equation.fr>
8 * 2004 Arnaud Quette <arnaud.quette@mgeups.com>
9 * Copyright (c) 2002-2007 Nagios Plugins Development Team
10 *
11 * Last Modified: $Date$
12 *
13 * Description:
14 *
15 * This file contains Network UPS Tools plugin for Nagios
16 *
17 * This plugin tests the UPS service on the specified host.Network UPS Tools
18 * from www.networkupstools.org must be running for thisplugin to work.
19 *
20 *
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 *
34 * $Id$
35 *
36 *****************************************************************************/
38 const char *progname = "check_ups";
39 const char *revision = "$Revision$";
40 const char *copyright = "2000-2007";
41 const char *email = "nagiosplug-devel@lists.sourceforge.net";
43 #include "common.h"
44 #include "netutils.h"
45 #include "utils.h"
47 enum {
48 PORT = 3493
49 };
51 #define CHECK_NONE 0
53 #define UPS_NONE 0 /* no supported options */
54 #define UPS_UTILITY 1 /* supports utility line voltage */
55 #define UPS_BATTPCT 2 /* supports percent battery remaining */
56 #define UPS_STATUS 4 /* supports UPS status */
57 #define UPS_TEMP 8 /* supports UPS temperature */
58 #define UPS_LOADPCT 16 /* supports load percent */
60 #define UPSSTATUS_NONE 0
61 #define UPSSTATUS_OFF 1
62 #define UPSSTATUS_OL 2
63 #define UPSSTATUS_OB 4
64 #define UPSSTATUS_LB 8
65 #define UPSSTATUS_CAL 16
66 #define UPSSTATUS_RB 32 /*Replace Battery */
67 #define UPSSTATUS_BYPASS 64
68 #define UPSSTATUS_OVER 128
69 #define UPSSTATUS_TRIM 256
70 #define UPSSTATUS_BOOST 512
71 #define UPSSTATUS_CHRG 1024
72 #define UPSSTATUS_DISCHRG 2048
73 #define UPSSTATUS_UNKOWN 4096
75 enum { NOSUCHVAR = ERROR-1 };
77 int server_port = PORT;
78 char *server_address;
79 char *ups_name = NULL;
80 double warning_value = 0.0;
81 double critical_value = 0.0;
82 int check_warn = FALSE;
83 int check_crit = FALSE;
84 int check_variable = UPS_NONE;
85 int supported_options = UPS_NONE;
86 int status = UPSSTATUS_NONE;
88 double ups_utility_voltage = 0.0;
89 double ups_battery_percent = 0.0;
90 double ups_load_percent = 0.0;
91 double ups_temperature = 0.0;
92 char *ups_status;
93 int temp_output_c = 0;
95 int determine_status (void);
96 int get_ups_variable (const char *, char *, size_t);
98 int process_arguments (int, char **);
99 int validate_arguments (void);
100 void print_help (void);
101 void print_usage (void);
103 int
104 main (int argc, char **argv)
105 {
106 int result = STATE_UNKNOWN;
107 char *message;
108 char *data;
109 char *tunits;
110 char temp_buffer[MAX_INPUT_BUFFER];
111 double ups_utility_deviation = 0.0;
112 int res;
114 setlocale (LC_ALL, "");
115 bindtextdomain (PACKAGE, LOCALEDIR);
116 textdomain (PACKAGE);
118 ups_status = strdup ("N/A");
119 data = strdup ("");
120 message = strdup ("");
122 if (process_arguments (argc, argv) == ERROR)
123 usage4 (_("Could not parse arguments"));
125 /* initialize alarm signal handling */
126 signal (SIGALRM, socket_timeout_alarm_handler);
128 /* set socket timeout */
129 alarm (socket_timeout);
131 /* get the ups status if possible */
132 if (determine_status () != OK)
133 return STATE_CRITICAL;
134 if (supported_options & UPS_STATUS) {
136 ups_status = strdup ("");
137 result = STATE_OK;
139 if (status & UPSSTATUS_OFF) {
140 asprintf (&ups_status, "Off");
141 result = STATE_CRITICAL;
142 }
143 else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) ==
144 (UPSSTATUS_OB | UPSSTATUS_LB)) {
145 asprintf (&ups_status, _("On Battery, Low Battery"));
146 result = STATE_CRITICAL;
147 }
148 else {
149 if (status & UPSSTATUS_OL) {
150 asprintf (&ups_status, "%s%s", ups_status, _("Online"));
151 }
152 if (status & UPSSTATUS_OB) {
153 asprintf (&ups_status, "%s%s", ups_status, _("On Battery"));
154 result = STATE_WARNING;
155 }
156 if (status & UPSSTATUS_LB) {
157 asprintf (&ups_status, "%s%s", ups_status, _(", Low Battery"));
158 result = STATE_WARNING;
159 }
160 if (status & UPSSTATUS_CAL) {
161 asprintf (&ups_status, "%s%s", ups_status, _(", Calibrating"));
162 }
163 if (status & UPSSTATUS_RB) {
164 asprintf (&ups_status, "%s%s", ups_status, _(", Replace Battery"));
165 result = STATE_WARNING;
166 }
167 if (status & UPSSTATUS_BYPASS) {
168 asprintf (&ups_status, "%s%s", ups_status, _(", On Bypass"));
169 }
170 if (status & UPSSTATUS_OVER) {
171 asprintf (&ups_status, "%s%s", ups_status, _(", Overload"));
172 }
173 if (status & UPSSTATUS_TRIM) {
174 asprintf (&ups_status, "%s%s", ups_status, _(", Trimming"));
175 }
176 if (status & UPSSTATUS_BOOST) {
177 asprintf (&ups_status, "%s%s", ups_status, _(", Boosting"));
178 }
179 if (status & UPSSTATUS_CHRG) {
180 asprintf (&ups_status, "%s%s", ups_status, _(", Charging"));
181 }
182 if (status & UPSSTATUS_DISCHRG) {
183 asprintf (&ups_status, "%s%s", ups_status, _(", Discharging"));
184 }
185 if (status & UPSSTATUS_UNKOWN) {
186 asprintf (&ups_status, "%s%s", ups_status, _(", Unknown"));
187 }
188 }
189 asprintf (&message, "%sStatus=%s ", message, ups_status);
190 }
192 /* get the ups utility voltage if possible */
193 res=get_ups_variable ("input.voltage", temp_buffer, sizeof (temp_buffer));
194 if (res == NOSUCHVAR) supported_options &= ~UPS_UTILITY;
195 else if (res != OK)
196 return STATE_CRITICAL;
197 else {
198 supported_options |= UPS_UTILITY;
200 ups_utility_voltage = atof (temp_buffer);
201 asprintf (&message, "%sUtility=%3.1fV ", message, ups_utility_voltage);
203 if (ups_utility_voltage > 120.0)
204 ups_utility_deviation = 120.0 - ups_utility_voltage;
205 else
206 ups_utility_deviation = ups_utility_voltage - 120.0;
208 if (check_variable == UPS_UTILITY) {
209 if (check_crit==TRUE && ups_utility_deviation>=critical_value) {
210 result = STATE_CRITICAL;
211 }
212 else if (check_warn==TRUE && ups_utility_deviation>=warning_value) {
213 result = max_state (result, STATE_WARNING);
214 }
215 asprintf (&data, "%s",
216 perfdata ("voltage", (long)(1000*ups_utility_voltage), "mV",
217 check_warn, (long)(1000*warning_value),
218 check_crit, (long)(1000*critical_value),
219 TRUE, 0, FALSE, 0));
220 } else {
221 asprintf (&data, "%s",
222 perfdata ("voltage", (long)(1000*ups_utility_voltage), "mV",
223 FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
224 }
225 }
227 /* get the ups battery percent if possible */
228 res=get_ups_variable ("battery.charge", temp_buffer, sizeof (temp_buffer));
229 if (res == NOSUCHVAR) supported_options &= ~UPS_BATTPCT;
230 else if ( res != OK)
231 return STATE_CRITICAL;
232 else {
233 supported_options |= UPS_BATTPCT;
234 ups_battery_percent = atof (temp_buffer);
235 asprintf (&message, "%sBatt=%3.1f%% ", message, ups_battery_percent);
237 if (check_variable == UPS_BATTPCT) {
238 if (check_crit==TRUE && ups_battery_percent <= critical_value) {
239 result = STATE_CRITICAL;
240 }
241 else if (check_warn==TRUE && ups_battery_percent<=warning_value) {
242 result = max_state (result, STATE_WARNING);
243 }
244 asprintf (&data, "%s %s", data,
245 perfdata ("battery", (long)ups_battery_percent, "%",
246 check_warn, (long)(1000*warning_value),
247 check_crit, (long)(1000*critical_value),
248 TRUE, 0, TRUE, 100));
249 } else {
250 asprintf (&data, "%s %s", data,
251 perfdata ("battery", (long)ups_battery_percent, "%",
252 FALSE, 0, FALSE, 0, TRUE, 0, TRUE, 100));
253 }
254 }
256 /* get the ups load percent if possible */
257 res=get_ups_variable ("ups.load", temp_buffer, sizeof (temp_buffer));
258 if ( res == NOSUCHVAR ) supported_options &= ~UPS_LOADPCT;
259 else if ( res != OK)
260 return STATE_CRITICAL;
261 else {
262 supported_options |= UPS_LOADPCT;
263 ups_load_percent = atof (temp_buffer);
264 asprintf (&message, "%sLoad=%3.1f%% ", message, ups_load_percent);
266 if (check_variable == UPS_LOADPCT) {
267 if (check_crit==TRUE && ups_load_percent>=critical_value) {
268 result = STATE_CRITICAL;
269 }
270 else if (check_warn==TRUE && ups_load_percent>=warning_value) {
271 result = max_state (result, STATE_WARNING);
272 }
273 asprintf (&data, "%s %s", data,
274 perfdata ("load", (long)ups_load_percent, "%",
275 check_warn, (long)(1000*warning_value),
276 check_crit, (long)(1000*critical_value),
277 TRUE, 0, TRUE, 100));
278 } else {
279 asprintf (&data, "%s %s", data,
280 perfdata ("load", (long)ups_load_percent, "%",
281 FALSE, 0, FALSE, 0, TRUE, 0, TRUE, 100));
282 }
283 }
285 /* get the ups temperature if possible */
286 res=get_ups_variable ("ups.temperature", temp_buffer, sizeof (temp_buffer));
287 if ( res == NOSUCHVAR ) supported_options &= ~UPS_TEMP;
288 else if ( res != OK)
289 return STATE_CRITICAL;
290 else {
291 supported_options |= UPS_TEMP;
292 if (temp_output_c) {
293 tunits="degC";
294 ups_temperature = atof (temp_buffer);
295 asprintf (&message, "%sTemp=%3.1fC", message, ups_temperature);
296 }
297 else {
298 tunits="degF";
299 ups_temperature = (atof (temp_buffer) * 1.8) + 32;
300 asprintf (&message, "%sTemp=%3.1fF", message, ups_temperature);
301 }
303 if (check_variable == UPS_TEMP) {
304 if (check_crit==TRUE && ups_temperature>=critical_value) {
305 result = STATE_CRITICAL;
306 }
307 else if (check_warn == TRUE && ups_temperature>=warning_value) {
308 result = max_state (result, STATE_WARNING);
309 }
310 asprintf (&data, "%s %s", data,
311 perfdata ("temp", (long)ups_temperature, tunits,
312 check_warn, (long)(1000*warning_value),
313 check_crit, (long)(1000*critical_value),
314 TRUE, 0, FALSE, 0));
315 } else {
316 asprintf (&data, "%s %s", data,
317 perfdata ("temp", (long)ups_temperature, tunits,
318 FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
319 }
320 }
322 /* if the UPS does not support any options we are looking for, report an error */
323 if (supported_options == UPS_NONE) {
324 result = STATE_CRITICAL;
325 asprintf (&message, _("UPS does not support any available options\n"));
326 }
328 /* reset timeout */
329 alarm (0);
331 printf ("UPS %s - %s|%s\n", state_text(result), message, data);
332 return result;
333 }
337 /* determines what options are supported by the UPS */
338 int
339 determine_status (void)
340 {
341 char recv_buffer[MAX_INPUT_BUFFER];
342 char temp_buffer[MAX_INPUT_BUFFER];
343 char *ptr;
344 int res;
346 res=get_ups_variable ("ups.status", recv_buffer, sizeof (recv_buffer));
347 if (res == NOSUCHVAR) return OK;
348 if (res != STATE_OK) {
349 printf ("%s\n", _("Invalid response received from host"));
350 return ERROR;
351 }
353 supported_options |= UPS_STATUS;
355 strcpy (temp_buffer, recv_buffer);
356 for (ptr = (char *) strtok (temp_buffer, " "); ptr != NULL;
357 ptr = (char *) strtok (NULL, " ")) {
358 if (!strcmp (ptr, "OFF"))
359 status |= UPSSTATUS_OFF;
360 else if (!strcmp (ptr, "OL"))
361 status |= UPSSTATUS_OL;
362 else if (!strcmp (ptr, "OB"))
363 status |= UPSSTATUS_OB;
364 else if (!strcmp (ptr, "LB"))
365 status |= UPSSTATUS_LB;
366 else if (!strcmp (ptr, "CAL"))
367 status |= UPSSTATUS_CAL;
368 else if (!strcmp (ptr, "RB"))
369 status |= UPSSTATUS_RB;
370 else if (!strcmp (ptr, "BYPASS"))
371 status |= UPSSTATUS_BYPASS;
372 else if (!strcmp (ptr, "OVER"))
373 status |= UPSSTATUS_OVER;
374 else if (!strcmp (ptr, "TRIM"))
375 status |= UPSSTATUS_TRIM;
376 else if (!strcmp (ptr, "BOOST"))
377 status |= UPSSTATUS_BOOST;
378 else if (!strcmp (ptr, "CHRG"))
379 status |= UPSSTATUS_CHRG;
380 else if (!strcmp (ptr, "DISCHRG"))
381 status |= UPSSTATUS_DISCHRG;
382 else
383 status |= UPSSTATUS_UNKOWN;
384 }
386 return OK;
387 }
390 /* gets a variable value for a specific UPS */
391 int
392 get_ups_variable (const char *varname, char *buf, size_t buflen)
393 {
394 /* char command[MAX_INPUT_BUFFER]; */
395 char temp_buffer[MAX_INPUT_BUFFER];
396 char send_buffer[MAX_INPUT_BUFFER];
397 char *ptr;
398 int len;
400 *buf=0;
402 /* create the command string to send to the UPS daemon */
403 sprintf (send_buffer, "GET VAR %s %s\n", ups_name, varname);
405 /* send the command to the daemon and get a response back */
406 if (process_tcp_request
407 (server_address, server_port, send_buffer, temp_buffer,
408 sizeof (temp_buffer)) != STATE_OK) {
409 printf ("%s\n", _("Invalid response received from host"));
410 return ERROR;
411 }
413 ptr = temp_buffer;
414 len = strlen(ptr);
415 if (len > 0 && ptr[len-1] == '\n') ptr[len-1]=0;
416 if (strcmp (ptr, "ERR UNKNOWN-UPS") == 0) {
417 printf (_("CRITICAL - no such ups '%s' on that host\n"), ups_name);
418 return ERROR;
419 }
421 if (strcmp (ptr, "ERR VAR-NOT-SUPPORTED") == 0) {
422 /*printf ("Error: Variable '%s' is not supported\n", varname);*/
423 return NOSUCHVAR;
424 }
426 if (strcmp (ptr, "ERR DATA-STALE") == 0) {
427 printf ("%s\n", _("CRITICAL - UPS data is stale"));
428 return ERROR;
429 }
431 if (strncmp (ptr, "ERR", 3) == 0) {
432 printf (_("Unknown error: %s\n"), ptr);
433 return ERROR;
434 }
436 ptr = temp_buffer + strlen (varname) + strlen (ups_name) + 6;
437 len = strlen(ptr);
438 if (len < 2 || ptr[0] != '"' || ptr[len-1] != '"') {
439 printf ("%s\n", _("Error: unable to parse variable"));
440 return ERROR;
441 }
442 strncpy (buf, ptr+1, len - 2);
443 buf[len - 2] = 0;
445 return OK;
446 }
449 /* Command line: CHECK_UPS -H <host_address> -u ups [-p port] [-v variable]
450 [-wv warn_value] [-cv crit_value] [-to to_sec] */
453 /* process command-line arguments */
454 int
455 process_arguments (int argc, char **argv)
456 {
457 int c;
459 int option = 0;
460 static struct option longopts[] = {
461 {"hostname", required_argument, 0, 'H'},
462 {"ups", required_argument, 0, 'u'},
463 {"port", required_argument, 0, 'p'},
464 {"critical", required_argument, 0, 'c'},
465 {"warning", required_argument, 0, 'w'},
466 {"timeout", required_argument, 0, 't'},
467 {"temperature", no_argument, 0, 'T'},
468 {"variable", required_argument, 0, 'v'},
469 {"version", no_argument, 0, 'V'},
470 {"help", no_argument, 0, 'h'},
471 {0, 0, 0, 0}
472 };
474 if (argc < 2)
475 return ERROR;
477 for (c = 1; c < argc; c++) {
478 if (strcmp ("-to", argv[c]) == 0)
479 strcpy (argv[c], "-t");
480 else if (strcmp ("-wt", argv[c]) == 0)
481 strcpy (argv[c], "-w");
482 else if (strcmp ("-ct", argv[c]) == 0)
483 strcpy (argv[c], "-c");
484 }
486 while (1) {
487 c = getopt_long (argc, argv, "hVTH:u:p:v:c:w:t:", longopts,
488 &option);
490 if (c == -1 || c == EOF)
491 break;
493 switch (c) {
494 case '?': /* help */
495 usage5 ();
496 case 'H': /* hostname */
497 if (is_host (optarg)) {
498 server_address = optarg;
499 }
500 else {
501 usage2 (_("Invalid hostname/address"), optarg);
502 }
503 break;
504 case 'T': /* FIXME: to be improved (ie "-T C" for Celsius or "-T F" for Farenheit) */
505 temp_output_c = 1;
506 break;
507 case 'u': /* ups name */
508 ups_name = optarg;
509 break;
510 case 'p': /* port */
511 if (is_intpos (optarg)) {
512 server_port = atoi (optarg);
513 }
514 else {
515 usage2 (_("Port must be a positive integer"), optarg);
516 }
517 break;
518 case 'c': /* critical time threshold */
519 if (is_intnonneg (optarg)) {
520 critical_value = atoi (optarg);
521 check_crit = TRUE;
522 }
523 else {
524 usage2 (_("Critical time must be a positive integer"), optarg);
525 }
526 break;
527 case 'w': /* warning time threshold */
528 if (is_intnonneg (optarg)) {
529 warning_value = atoi (optarg);
530 check_warn = TRUE;
531 }
532 else {
533 usage2 (_("Warning time must be a positive integer"), optarg);
534 }
535 break;
536 case 'v': /* variable */
537 if (!strcmp (optarg, "LINE"))
538 check_variable = UPS_UTILITY;
539 else if (!strcmp (optarg, "TEMP"))
540 check_variable = UPS_TEMP;
541 else if (!strcmp (optarg, "BATTPCT"))
542 check_variable = UPS_BATTPCT;
543 else if (!strcmp (optarg, "LOADPCT"))
544 check_variable = UPS_LOADPCT;
545 else
546 usage2 (_("Unrecognized UPS variable"), optarg);
547 break;
548 case 't': /* timeout */
549 if (is_intnonneg (optarg)) {
550 socket_timeout = atoi (optarg);
551 }
552 else {
553 usage4 (_("Timeout interval must be a positive integer"));
554 }
555 break;
556 case 'V': /* version */
557 print_revision (progname, revision);
558 exit (STATE_OK);
559 case 'h': /* help */
560 print_help ();
561 exit (STATE_OK);
562 }
563 }
566 if (server_address == NULL && argc > optind) {
567 if (is_host (argv[optind]))
568 server_address = argv[optind++];
569 else
570 usage2 (_("Invalid hostname/address"), optarg);
571 }
573 if (server_address == NULL)
574 server_address = strdup("127.0.0.1");
576 return validate_arguments();
577 }
580 int
581 validate_arguments (void)
582 {
583 if (! ups_name) {
584 printf ("%s\n", _("Error : no ups indicated"));
585 return ERROR;
586 }
587 return OK;
588 }
591 void
592 print_help (void)
593 {
594 char *myport;
595 asprintf (&myport, "%d", PORT);
597 print_revision (progname, revision);
599 printf ("Copyright (c) 2000 Tom Shields\n");
600 printf ("Copyright (c) 2004 Alain Richard <alain.richard@equation.fr>\n");
601 printf ("Copyright (c) 2004 Arnaud Quette <arnaud.quette@mgeups.com>\n");
602 printf (COPYRIGHT, copyright, email);
604 printf ("%s\n", _("This plugin tests the UPS service on the specified host.Network UPS Tools "));
605 printf ("%s\n", _("from www.networkupstools.org must be running for thisplugin to work."));
607 printf ("\n\n");
609 print_usage ();
611 printf (_(UT_HELP_VRSN));
613 printf (_(UT_HOST_PORT), 'p', myport);
615 printf (" %s\n", "-u, --ups=STRING");
616 printf (" %s\n", _("Name of UPS"));
617 printf (" %s\n", "-T, --temperature");
618 printf (" %s\n", _("Output of temperatures in Celsius"));
619 printf (" %s\n", "-v, --variable=STRING");
620 printf (" %s %s\n", _("Valid values for STRING are"), "LINE, TEMP, BATTPCT or LOADPCT");
622 printf (_(UT_WARN_CRIT));
624 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
626 /* TODO: -v clashing with -v/-variable. Commenting out help text since verbose
627 is unused up to now */
628 /* printf (_(UT_VERBOSE)); */
630 printf ("\n");
631 printf ("%s\n", _("Notes:"));
633 printf ("%s\n", _("This plugin attempts to determine the status of a UPS (Uninterruptible Power"));
634 printf ("%s\n", _("Supply) on a local or remote host. If the UPS is online or calibrating, the"));
635 printf ("%s\n", _("plugin will return an OK state. If the battery is on it will return a WARNING"));
636 printf ("%s\n", _("state.If the UPS is off or has a low battery the plugin will return a CRITICAL"));
637 printf ("%s\n\n", _("state."));
639 printf ("%s\n", _("You may also specify a variable to check [such as temperature, utility voltage,"));
640 printf ("%s\n", _("battery load, etc.] as well as warning and critical thresholds for the value of"));
641 printf ("%s\n", _("that variable. If the remote host has multiple UPS that are being monitored you"));
642 printf ("%s\n", _("will have to use the [ups] option to specify which UPS to check."));
644 printf ("%s\n", _("This plugin requires that the UPSD daemon distributed with Russel Kroll's"));
645 printf ("%s\n", _("Smart UPS Tools be installed on the remote host. If you do not have the"));
646 printf ("%s\n", _("package installed on your system, you can download it from http://www.networkupstools.org"));
648 printf (_(UT_SUPPORT));
649 }
652 void
653 print_usage (void)
654 {
655 printf (_("Usage:"));
656 printf ("%s -H host -u ups [-p port] [-v variable] [-w warn_value] [-c crit_value] [-to to_sec] [-T]\n", progname);
657 }