Revert "check_disk - show all disks if state is ok and option error only is used"
[monitoring-plugins.git] / plugins / check_ups.c
blobe9e56a511da10055dcc26f9718c3f1db6626043f
1 /*****************************************************************************
2 *
3 * Monitoring 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 Monitoring Plugins Development Team
11 * Description:
13 * This file contains Network UPS Tools plugin for Monitoring
15 * This plugin tests the UPS service on the specified host. Network UPS Tools
16 * from www.networkupstools.org must be running for this plugin to work.
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 3 of the License, or
22 * (at your option) any later version.
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.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 *****************************************************************************/
35 const char *progname = "check_ups";
36 const char *copyright = "2000-2007";
37 const char *email = "devel@monitoring-plugins.org";
39 #include "common.h"
40 #include "netutils.h"
41 #include "utils.h"
43 enum {
44 PORT = 3493
47 #define CHECK_NONE 0
49 #define UPS_NONE 0 /* no supported options */
50 #define UPS_UTILITY 1 /* supports utility line voltage */
51 #define UPS_BATTPCT 2 /* supports percent battery remaining */
52 #define UPS_STATUS 4 /* supports UPS status */
53 #define UPS_TEMP 8 /* supports UPS temperature */
54 #define UPS_LOADPCT 16 /* supports load percent */
56 #define UPSSTATUS_NONE 0
57 #define UPSSTATUS_OFF 1
58 #define UPSSTATUS_OL 2
59 #define UPSSTATUS_OB 4
60 #define UPSSTATUS_LB 8
61 #define UPSSTATUS_CAL 16
62 #define UPSSTATUS_RB 32 /*Replace Battery */
63 #define UPSSTATUS_BYPASS 64
64 #define UPSSTATUS_OVER 128
65 #define UPSSTATUS_TRIM 256
66 #define UPSSTATUS_BOOST 512
67 #define UPSSTATUS_CHRG 1024
68 #define UPSSTATUS_DISCHRG 2048
69 #define UPSSTATUS_UNKNOWN 4096
71 enum { NOSUCHVAR = ERROR-1 };
73 int server_port = PORT;
74 char *server_address;
75 char *ups_name = NULL;
76 double warning_value = 0.0;
77 double critical_value = 0.0;
78 int check_warn = FALSE;
79 int check_crit = FALSE;
80 int check_variable = UPS_NONE;
81 int supported_options = UPS_NONE;
82 int status = UPSSTATUS_NONE;
84 double ups_utility_voltage = 0.0;
85 double ups_battery_percent = 0.0;
86 double ups_load_percent = 0.0;
87 double ups_temperature = 0.0;
88 char *ups_status;
89 int temp_output_c = 0;
91 int determine_status (void);
92 int get_ups_variable (const char *, char *, size_t);
94 int process_arguments (int, char **);
95 int validate_arguments (void);
96 void print_help (void);
97 void print_usage (void);
99 int
100 main (int argc, char **argv)
102 int result = STATE_UNKNOWN;
103 char *message;
104 char *data;
105 char *tunits;
106 char temp_buffer[MAX_INPUT_BUFFER];
107 double ups_utility_deviation = 0.0;
108 int res;
110 setlocale (LC_ALL, "");
111 bindtextdomain (PACKAGE, LOCALEDIR);
112 textdomain (PACKAGE);
114 ups_status = strdup ("N/A");
115 data = strdup ("");
116 message = strdup ("");
118 /* Parse extra opts if any */
119 argv=np_extra_opts (&argc, argv, progname);
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 xasprintf (&ups_status, "Off");
140 result = STATE_CRITICAL;
142 else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) ==
143 (UPSSTATUS_OB | UPSSTATUS_LB)) {
144 xasprintf (&ups_status, _("On Battery, Low Battery"));
145 result = STATE_CRITICAL;
147 else {
148 if (status & UPSSTATUS_OL) {
149 xasprintf (&ups_status, "%s%s", ups_status, _("Online"));
151 if (status & UPSSTATUS_OB) {
152 xasprintf (&ups_status, "%s%s", ups_status, _("On Battery"));
153 result = STATE_WARNING;
155 if (status & UPSSTATUS_LB) {
156 xasprintf (&ups_status, "%s%s", ups_status, _(", Low Battery"));
157 result = STATE_WARNING;
159 if (status & UPSSTATUS_CAL) {
160 xasprintf (&ups_status, "%s%s", ups_status, _(", Calibrating"));
162 if (status & UPSSTATUS_RB) {
163 xasprintf (&ups_status, "%s%s", ups_status, _(", Replace Battery"));
164 result = STATE_WARNING;
166 if (status & UPSSTATUS_BYPASS) {
167 xasprintf (&ups_status, "%s%s", ups_status, _(", On Bypass"));
169 if (status & UPSSTATUS_OVER) {
170 xasprintf (&ups_status, "%s%s", ups_status, _(", Overload"));
172 if (status & UPSSTATUS_TRIM) {
173 xasprintf (&ups_status, "%s%s", ups_status, _(", Trimming"));
175 if (status & UPSSTATUS_BOOST) {
176 xasprintf (&ups_status, "%s%s", ups_status, _(", Boosting"));
178 if (status & UPSSTATUS_CHRG) {
179 xasprintf (&ups_status, "%s%s", ups_status, _(", Charging"));
181 if (status & UPSSTATUS_DISCHRG) {
182 xasprintf (&ups_status, "%s%s", ups_status, _(", Discharging"));
184 if (status & UPSSTATUS_UNKNOWN) {
185 xasprintf (&ups_status, "%s%s", ups_status, _(", Unknown"));
188 xasprintf (&message, "%sStatus=%s ", message, ups_status);
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 xasprintf (&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;
211 else if (check_warn==TRUE && ups_utility_deviation>=warning_value) {
212 result = max_state (result, STATE_WARNING);
214 xasprintf (&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 xasprintf (&data, "%s",
221 perfdata ("voltage", (long)(1000*ups_utility_voltage), "mV",
222 FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
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 xasprintf (&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;
240 else if (check_warn==TRUE && ups_battery_percent<=warning_value) {
241 result = max_state (result, STATE_WARNING);
243 xasprintf (&data, "%s %s", data,
244 perfdata ("battery", (long)ups_battery_percent, "%",
245 check_warn, (long)(warning_value),
246 check_crit, (long)(critical_value),
247 TRUE, 0, TRUE, 100));
248 } else {
249 xasprintf (&data, "%s %s", data,
250 perfdata ("battery", (long)ups_battery_percent, "%",
251 FALSE, 0, FALSE, 0, TRUE, 0, TRUE, 100));
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 xasprintf (&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;
269 else if (check_warn==TRUE && ups_load_percent>=warning_value) {
270 result = max_state (result, STATE_WARNING);
272 xasprintf (&data, "%s %s", data,
273 perfdata ("load", (long)ups_load_percent, "%",
274 check_warn, (long)(warning_value),
275 check_crit, (long)(critical_value),
276 TRUE, 0, TRUE, 100));
277 } else {
278 xasprintf (&data, "%s %s", data,
279 perfdata ("load", (long)ups_load_percent, "%",
280 FALSE, 0, FALSE, 0, TRUE, 0, TRUE, 100));
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 xasprintf (&message, "%sTemp=%3.1fC", message, ups_temperature);
296 else {
297 tunits="degF";
298 ups_temperature = (atof (temp_buffer) * 1.8) + 32;
299 xasprintf (&message, "%sTemp=%3.1fF", message, ups_temperature);
302 if (check_variable == UPS_TEMP) {
303 if (check_crit==TRUE && ups_temperature>=critical_value) {
304 result = STATE_CRITICAL;
306 else if (check_warn == TRUE && ups_temperature>=warning_value) {
307 result = max_state (result, STATE_WARNING);
309 xasprintf (&data, "%s %s", data,
310 perfdata ("temp", (long)ups_temperature, tunits,
311 check_warn, (long)(warning_value),
312 check_crit, (long)(critical_value),
313 TRUE, 0, FALSE, 0));
314 } else {
315 xasprintf (&data, "%s %s", data,
316 perfdata ("temp", (long)ups_temperature, tunits,
317 FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
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 xasprintf (&message, _("UPS does not support any available options\n"));
327 /* reset timeout */
328 alarm (0);
330 printf ("UPS %s - %s|%s\n", state_text(result), message, data);
331 return result;
336 /* determines what options are supported by the UPS */
338 determine_status (void)
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;
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_UNKNOWN;
385 return OK;
389 /* gets a variable value for a specific UPS */
391 get_ups_variable (const char *varname, char *buf, size_t buflen)
393 /* char command[MAX_INPUT_BUFFER]; */
394 char temp_buffer[MAX_INPUT_BUFFER];
395 char send_buffer[MAX_INPUT_BUFFER];
396 char *ptr;
397 char *logout = "OK Goodbye\n";
398 int logout_len = strlen(logout);
399 int len;
401 *buf=0;
403 /* create the command string to send to the UPS daemon */
404 /* Add LOGOUT to avoid read failure logs */
405 sprintf (send_buffer, "GET VAR %s %s\nLOGOUT\n", ups_name, varname);
407 /* send the command to the daemon and get a response back */
408 if (process_tcp_request
409 (server_address, server_port, send_buffer, temp_buffer,
410 sizeof (temp_buffer)) != STATE_OK) {
411 printf ("%s\n", _("Invalid response received from host"));
412 return ERROR;
415 ptr = temp_buffer;
416 len = strlen(ptr);
417 if (len > logout_len && strcmp (ptr + len - logout_len, logout) == 0) len -= logout_len;
418 if (len > 0 && ptr[len-1] == '\n') ptr[len-1]=0;
419 if (strcmp (ptr, "ERR UNKNOWN-UPS") == 0) {
420 printf (_("CRITICAL - no such UPS '%s' on that host\n"), ups_name);
421 return ERROR;
424 if (strcmp (ptr, "ERR VAR-NOT-SUPPORTED") == 0) {
425 /*printf ("Error: Variable '%s' is not supported\n", varname);*/
426 return NOSUCHVAR;
429 if (strcmp (ptr, "ERR DATA-STALE") == 0) {
430 printf ("%s\n", _("CRITICAL - UPS data is stale"));
431 return ERROR;
434 if (strncmp (ptr, "ERR", 3) == 0) {
435 printf (_("Unknown error: %s\n"), ptr);
436 return ERROR;
439 ptr = temp_buffer + strlen (varname) + strlen (ups_name) + 6;
440 len = strlen(ptr);
441 if (len < 2 || ptr[0] != '"' || ptr[len-1] != '"') {
442 printf ("%s\n", _("Error: unable to parse variable"));
443 return ERROR;
445 strncpy (buf, ptr+1, len - 2);
446 buf[len - 2] = 0;
448 return OK;
452 /* Command line: CHECK_UPS -H <host_address> -u ups [-p port] [-v variable]
453 [-wv warn_value] [-cv crit_value] [-to to_sec] */
456 /* process command-line arguments */
458 process_arguments (int argc, char **argv)
460 int c;
462 int option = 0;
463 static struct option longopts[] = {
464 {"hostname", required_argument, 0, 'H'},
465 {"ups", required_argument, 0, 'u'},
466 {"port", required_argument, 0, 'p'},
467 {"critical", required_argument, 0, 'c'},
468 {"warning", required_argument, 0, 'w'},
469 {"timeout", required_argument, 0, 't'},
470 {"temperature", no_argument, 0, 'T'},
471 {"variable", required_argument, 0, 'v'},
472 {"version", no_argument, 0, 'V'},
473 {"help", no_argument, 0, 'h'},
474 {0, 0, 0, 0}
477 if (argc < 2)
478 return ERROR;
480 for (c = 1; c < argc; c++) {
481 if (strcmp ("-to", argv[c]) == 0)
482 strcpy (argv[c], "-t");
483 else if (strcmp ("-wt", argv[c]) == 0)
484 strcpy (argv[c], "-w");
485 else if (strcmp ("-ct", argv[c]) == 0)
486 strcpy (argv[c], "-c");
489 while (1) {
490 c = getopt_long (argc, argv, "hVTH:u:p:v:c:w:t:", longopts,
491 &option);
493 if (c == -1 || c == EOF)
494 break;
496 switch (c) {
497 case '?': /* help */
498 usage5 ();
499 case 'H': /* hostname */
500 if (is_host (optarg)) {
501 server_address = optarg;
503 else {
504 usage2 (_("Invalid hostname/address"), optarg);
506 break;
507 case 'T': /* FIXME: to be improved (ie "-T C" for Celsius or "-T F" for Farenheit) */
508 temp_output_c = 1;
509 break;
510 case 'u': /* ups name */
511 ups_name = optarg;
512 break;
513 case 'p': /* port */
514 if (is_intpos (optarg)) {
515 server_port = atoi (optarg);
517 else {
518 usage2 (_("Port must be a positive integer"), optarg);
520 break;
521 case 'c': /* critical time threshold */
522 if (is_intnonneg (optarg)) {
523 critical_value = atoi (optarg);
524 check_crit = TRUE;
526 else {
527 usage2 (_("Critical time must be a positive integer"), optarg);
529 break;
530 case 'w': /* warning time threshold */
531 if (is_intnonneg (optarg)) {
532 warning_value = atoi (optarg);
533 check_warn = TRUE;
535 else {
536 usage2 (_("Warning time must be a positive integer"), optarg);
538 break;
539 case 'v': /* variable */
540 if (!strcmp (optarg, "LINE"))
541 check_variable = UPS_UTILITY;
542 else if (!strcmp (optarg, "TEMP"))
543 check_variable = UPS_TEMP;
544 else if (!strcmp (optarg, "BATTPCT"))
545 check_variable = UPS_BATTPCT;
546 else if (!strcmp (optarg, "LOADPCT"))
547 check_variable = UPS_LOADPCT;
548 else
549 usage2 (_("Unrecognized UPS variable"), optarg);
550 break;
551 case 't': /* timeout */
552 if (is_intnonneg (optarg)) {
553 socket_timeout = atoi (optarg);
555 else {
556 usage4 (_("Timeout interval must be a positive integer"));
558 break;
559 case 'V': /* version */
560 print_revision (progname, NP_VERSION);
561 exit (STATE_UNKNOWN);
562 case 'h': /* help */
563 print_help ();
564 exit (STATE_UNKNOWN);
569 if (server_address == NULL && argc > optind) {
570 if (is_host (argv[optind]))
571 server_address = argv[optind++];
572 else
573 usage2 (_("Invalid hostname/address"), optarg);
576 if (server_address == NULL)
577 server_address = strdup("127.0.0.1");
579 return validate_arguments();
584 validate_arguments (void)
586 if (! ups_name) {
587 printf ("%s\n", _("Error : no UPS indicated"));
588 return ERROR;
590 return OK;
594 void
595 print_help (void)
597 char *myport;
598 xasprintf (&myport, "%d", PORT);
600 print_revision (progname, NP_VERSION);
602 printf ("Copyright (c) 2000 Tom Shields\n");
603 printf ("Copyright (c) 2004 Alain Richard <alain.richard@equation.fr>\n");
604 printf ("Copyright (c) 2004 Arnaud Quette <arnaud.quette@mgeups.com>\n");
605 printf (COPYRIGHT, copyright, email);
607 printf ("%s\n", _("This plugin tests the UPS service on the specified host. Network UPS Tools"));
608 printf ("%s\n", _("from www.networkupstools.org must be running for this plugin to work."));
610 printf ("\n\n");
612 print_usage ();
614 printf (UT_HELP_VRSN);
615 printf (UT_EXTRA_OPTS);
617 printf (UT_HOST_PORT, 'p', myport);
619 printf (" %s\n", "-u, --ups=STRING");
620 printf (" %s\n", _("Name of UPS"));
621 printf (" %s\n", "-T, --temperature");
622 printf (" %s\n", _("Output of temperatures in Celsius"));
623 printf (" %s\n", "-v, --variable=STRING");
624 printf (" %s %s\n", _("Valid values for STRING are"), "LINE, TEMP, BATTPCT or LOADPCT");
626 printf (UT_WARN_CRIT);
628 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
630 /* TODO: -v clashing with -v/-variable. Commenting out help text since verbose
631 is unused up to now */
632 /* printf (UT_VERBOSE); */
634 printf ("\n");
635 printf ("%s\n", _("This plugin attempts to determine the status of a UPS (Uninterruptible Power"));
636 printf ("%s\n", _("Supply) on a local or remote host. If the UPS is online or calibrating, the"));
637 printf ("%s\n", _("plugin will return an OK state. If the battery is on it will return a WARNING"));
638 printf ("%s\n", _("state. If the UPS is off or has a low battery the plugin will return a CRITICAL"));
639 printf ("%s\n", _("state."));
641 printf ("\n");
642 printf ("%s\n", _("Notes:"));
643 printf (" %s\n", _("You may also specify a variable to check (such as temperature, utility voltage,"));
644 printf (" %s\n", _("battery load, etc.) as well as warning and critical thresholds for the value"));
645 printf (" %s\n", _("of that variable. If the remote host has multiple UPS that are being monitored"));
646 printf (" %s\n", _("you will have to use the --ups option to specify which UPS to check."));
647 printf ("\n");
648 printf (" %s\n", _("This plugin requires that the UPSD daemon distributed with Russell Kroll's"));
649 printf (" %s\n", _("Network UPS Tools be installed on the remote host. If you do not have the"));
650 printf (" %s\n", _("package installed on your system, you can download it from"));
651 printf (" %s\n", _("http://www.networkupstools.org"));
653 printf (UT_SUPPORT);
657 void
658 print_usage (void)
660 printf ("%s\n", _("Usage:"));
661 printf ("%s -H host -u ups [-p port] [-v variable] [-w warn_value] [-c crit_value] [-to to_sec] [-T]\n", progname);