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