summaryrefslogtreecommitdiffstats
path: root/plugins/check_ups.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_ups.c')
-rw-r--r--plugins/check_ups.c649
1 files changed, 649 insertions, 0 deletions
diff --git a/plugins/check_ups.c b/plugins/check_ups.c
new file mode 100644
index 0000000..4632bdc
--- /dev/null
+++ b/plugins/check_ups.c
@@ -0,0 +1,649 @@
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*
16
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******************************************************************************/
53
54#include "config.h"
55#include "common.h"
56#include "netutils.h"
57#include "utils.h"
58
59#define PROGNAME "check_ups"
60
61#define CHECK_NONE 0
62
63#define PORT 3305
64
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 */
71
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
79
80int server_port = PORT;
81char *server_address = NULL;
82char *ups_name = NULL;
83double warning_value = 0.0L;
84double critical_value = 0.0L;
85int check_warning_value = FALSE;
86int check_critical_value = FALSE;
87int check_variable = UPS_NONE;
88int supported_options = UPS_NONE;
89int status = UPSSTATUS_NONE;
90
91double ups_utility_voltage = 0.0L;
92double ups_battery_percent = 0.0L;
93double ups_load_percent = 0.0L;
94double ups_temperature = 0.0L;
95char ups_status[MAX_INPUT_BUFFER] = "N/A";
96
97int determine_status (void);
98int determine_supported_vars (void);
99int get_ups_variable (const char *, char *, int);
100
101int process_arguments (int, char **);
102int call_getopt (int, char **);
103int validate_arguments (void);
104void print_help (void);
105void print_usage (void);
106
107int
108main (int argc, char **argv)
109{
110 int result = STATE_OK;
111 char output_message[MAX_INPUT_BUFFER];
112 char temp_buffer[MAX_INPUT_BUFFER];
113
114 double ups_utility_deviation = 0.0L;
115
116 if (process_arguments (argc, argv) != OK)
117 usage ("Invalid command arguments supplied\n");
118
119 /* initialize alarm signal handling */
120 signal (SIGALRM, socket_timeout_alarm_handler);
121
122 /* set socket timeout */
123 alarm (socket_timeout);
124
125 /* determine what variables the UPS supports */
126 if (determine_supported_vars () != OK)
127 return STATE_CRITICAL;
128
129 /* get the ups status if possible */
130 if (supported_options & UPS_STATUS) {
131
132 if (determine_status () != OK)
133 return STATE_CRITICAL;
134 ups_status[0] = 0;
135 result = STATE_OK;
136
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 }
166
167 /* get the ups utility voltage if possible */
168 if (supported_options & UPS_UTILITY) {
169
170 if (get_ups_variable ("UTILITY", temp_buffer, sizeof (temp_buffer)) != OK)
171 return STATE_CRITICAL;
172
173 ups_utility_voltage = atof (temp_buffer);
174
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;
179
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 }
188
189 /* get the ups battery percent if possible */
190 if (supported_options & UPS_BATTPCT) {
191
192 if (get_ups_variable ("BATTPCT", temp_buffer, sizeof (temp_buffer)) != OK)
193 return STATE_CRITICAL;
194
195 ups_battery_percent = atof (temp_buffer);
196
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 }
205
206 /* get the ups load percent if possible */
207 if (supported_options & UPS_LOADPCT) {
208
209 if (get_ups_variable ("LOADPCT", temp_buffer, sizeof (temp_buffer)) != OK)
210 return STATE_CRITICAL;
211
212 ups_load_percent = atof (temp_buffer);
213
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 }
222
223 /* get the ups temperature if possible */
224 if (supported_options & UPS_TEMP) {
225
226 if (get_ups_variable ("UPSTEMP", temp_buffer, sizeof (temp_buffer)) != OK)
227 return STATE_CRITICAL;
228
229 ups_temperature = (atof (temp_buffer) * 1.8) + 32;
230
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 }
239
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;
243
244 /* reset timeout */
245 alarm (0);
246
247
248 sprintf (output_message, "UPS %s - ",
249 (result == STATE_OK) ? "ok" : "problem");
250
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 }
276
277 printf ("%s\n", output_message);
278
279 return result;
280}
281
282
283
284/* determines what options are supported by the UPS */
285int
286determine_status (void)
287{
288 char recv_buffer[MAX_INPUT_BUFFER];
289 char temp_buffer[MAX_INPUT_BUFFER];
290 char *ptr;
291
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 }
297
298 recv_buffer[strlen (recv_buffer) - 1] = 0;
299
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 }
316
317 return OK;
318}
319
320
321/* determines what options are supported by the UPS */
322int
323determine_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;
329
330
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 }
342
343 recv_buffer[strlen (recv_buffer) - 1] = 0;
344
345 if (ups_name)
346 ptr = recv_buffer + 5 + strlen (ups_name) + 2;
347 else
348 ptr = recv_buffer + 5;
349
350 strcpy (temp_buffer, recv_buffer);
351
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 }
365
366 return OK;
367}
368
369
370/* gets a variable value for a specific UPS */
371int
372get_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;
378
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);
384
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 }
392
393 if (ups_name)
394 ptr = temp_buffer + strlen (varname) + 5 + strlen (ups_name) + 1;
395 else
396 ptr = temp_buffer + strlen (varname) + 5;
397
398 if (!strcmp (ptr, "NOT-SUPPORTED")) {
399 printf ("Error: Variable '%s' is not supported\n", varname);
400 return ERROR;
401 }
402
403 if (!strcmp (ptr, "DATA-STALE")) {
404 printf ("Error: UPS data is stale\n");
405 return ERROR;
406 }
407
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 }
415
416 strncpy (buf, ptr, buflen - 1);
417 buf[buflen - 1] = 0;
418
419 return OK;
420}
421
422
423
424
425
426/* Command line: CHECK_UPS <host_address> [-u ups] [-p port] [-v variable]
427 [-wv warn_value] [-cv crit_value] [-to to_sec] */
428
429
430/* process command-line arguments */
431int
432process_arguments (int argc, char **argv)
433{
434 int c;
435
436 if (argc < 2)
437 return ERROR;
438
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 }
447
448 c = 0;
449 while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
450
451 if (is_option (argv[c]))
452 continue;
453
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 }
463
464 if (server_address == NULL)
465 server_address = strscpy (NULL, "127.0.0.1");
466
467 return validate_arguments ();
468}
469
470
471
472
473
474
475int
476call_getopt (int argc, char **argv)
477{
478 int c, i = 0;
479
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
495
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
504
505 i++;
506
507 if (c == -1 || c == EOF || c == 1)
508 break;
509
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 }
520
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}
591
592
593
594
595
596int
597validate_arguments (void)
598{
599 return OK;
600}
601
602
603
604
605
606void
607print_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\n");
613 print_usage ();
614 printf
615 ("\nOptions:\n"
616 " -H, --hostname=STRING or IPADDRESS\n"
617 " Check server on the indicated host\n"
618 " -p, --port=INTEGER\n"
619 " Make connection on the indicated port (default: %d)\n"
620 " -u, --ups=STRING\n"
621 " Name of UPS\n"
622 " -w, --warning=INTEGER\n"
623 " Seconds necessary to result in a warning status\n"
624 " -c, --critical=INTEGER\n"
625 " Seconds necessary to result in a critical status\n"
626 " -t, --timeout=INTEGER\n"
627 " Seconds before connection attempt times out (default: %d)\n"
628 " -v, --verbose\n"
629 " Print extra information (command-line use only)\n"
630 " -h, --help\n"
631 " Print detailed help screen\n"
632 " -V, --version\n"
633 " Print version information\n\n", PORT, DEFAULT_SOCKET_TIMEOUT);
634 support ();
635}
636
637
638
639
640
641void
642print_usage (void)
643{
644 printf
645 ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n"
646 " [-t timeout] [-v]\n"
647 " %s --help\n"
648 " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
649}