X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=plugins%2Fcheck_ups.c;h=4c9032268f8a940e4cefd758aefd1269dae4e78b;hb=80da8e6dab2b37abc491018b2894414b80cfcbf4;hp=1e1aa39ce6a71637ee2182466c3643377d3dc633;hpb=3f9ccbd2da5bd99967bac0efc7dfc5bb37e72b47;p=nagiosplug.git diff --git a/plugins/check_ups.c b/plugins/check_ups.c index 1e1aa39..4c90322 100644 --- a/plugins/check_ups.c +++ b/plugins/check_ups.c @@ -31,7 +31,7 @@ * This plugin requires that the UPSD daemon distributed with Russel * Kroll's "Smart UPS Tools" be installed on the remote host. If you * don't have the package installed on your system, you can download -* it from http://www.exploits.org/~rkroll/smartupstools +* it from http://www.exploits.org/nut * * License Information: * @@ -56,29 +56,34 @@ #include "netutils.h" #include "utils.h" -#define PROGNAME "check_ups" +const char *progname = "check_ups"; +#define REVISION "$Revision$" +#define COPYRIGHT "1999-2002" +#define AUTHOR "Ethan Galstad" +#define EMAIL "nagios@nagios.org" #define CHECK_NONE 0 -#define PORT 3305 +#define PORT 3493 -#define UPS_NONE 0 /* no supported options */ -#define UPS_UTILITY 1 /* supports utility line voltage */ -#define UPS_BATTPCT 2 /* supports percent battery remaining */ -#define UPS_STATUS 4 /* supports UPS status */ -#define UPS_TEMP 8 /* supports UPS temperature */ -#define UPS_LOADPCT 16 /* supports load percent */ +#define UPS_NONE 0 /* no supported options */ +#define UPS_UTILITY 1 /* supports utility line voltage */ +#define UPS_BATTPCT 2 /* supports percent battery remaining */ +#define UPS_STATUS 4 /* supports UPS status */ +#define UPS_TEMP 8 /* supports UPS temperature */ +#define UPS_LOADPCT 16 /* supports load percent */ -#define UPSSTATUS_NONE 0 -#define UPSSTATUS_OFF 1 -#define UPSSTATUS_OL 2 -#define UPSSTATUS_OB 4 -#define UPSSTATUS_LB 8 -#define UPSSTATUS_CAL 16 -#define UPSSTATUS_UNKOWN 32 +#define UPSSTATUS_NONE 0 +#define UPSSTATUS_OFF 1 +#define UPSSTATUS_OL 2 +#define UPSSTATUS_OB 4 +#define UPSSTATUS_LB 8 +#define UPSSTATUS_CAL 16 +#define UPSSTATUS_RB 32 /*Replace Battery */ +#define UPSSTATUS_UNKOWN 64 int server_port = PORT; -char *server_address = NULL; +char *server_address = "127.0.0.1"; char *ups_name = NULL; double warning_value = 0.0L; double critical_value = 0.0L; @@ -92,14 +97,13 @@ double ups_utility_voltage = 0.0L; double ups_battery_percent = 0.0L; double ups_load_percent = 0.0L; double ups_temperature = 0.0L; -char ups_status[MAX_INPUT_BUFFER] = "N/A"; +char *ups_status = "N/A"; int determine_status (void); int determine_supported_vars (void); int get_ups_variable (const char *, char *, int); int process_arguments (int, char **); -int call_getopt (int, char **); int validate_arguments (void); void print_help (void); void print_usage (void); @@ -108,7 +112,7 @@ int main (int argc, char **argv) { int result = STATE_OK; - char output_message[MAX_INPUT_BUFFER]; + char *message; char temp_buffer[MAX_INPUT_BUFFER]; double ups_utility_deviation = 0.0L; @@ -131,35 +135,39 @@ main (int argc, char **argv) if (determine_status () != OK) return STATE_CRITICAL; - ups_status[0] = 0; + asprintf (&ups_status, ""); result = STATE_OK; if (status & UPSSTATUS_OFF) { - strcpy (ups_status, "Off"); + asprintf (&ups_status, "Off"); result = STATE_CRITICAL; } else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) == (UPSSTATUS_OB | UPSSTATUS_LB)) { - strcpy (ups_status, "On Battery, Low Battery"); + asprintf (&ups_status, "On Battery, Low Battery"); result = STATE_CRITICAL; } else { if (status & UPSSTATUS_OL) { - strcat (ups_status, "Online"); + asprintf (&ups_status, "%s%s", ups_status, "Online"); } if (status & UPSSTATUS_OB) { - strcat (ups_status, "On Battery"); + asprintf (&ups_status, "%s%s", ups_status, "On Battery"); result = STATE_WARNING; } if (status & UPSSTATUS_LB) { - strcat (ups_status, ", Low Battery"); + asprintf (&ups_status, "%s%s", ups_status, ", Low Battery"); result = STATE_WARNING; } if (status & UPSSTATUS_CAL) { - strcat (ups_status, ", Calibrating"); + asprintf (&ups_status, "%s%s", ups_status, ", Calibrating"); + } + if (status & UPSSTATUS_RB) { + asprintf (&ups_status, "%s%s", ups_status, ", Replace Battery"); + result = STATE_WARNING; } if (status & UPSSTATUS_UNKOWN) { - strcat (ups_status, ", Unknown"); + asprintf (&ups_status, "%s%s", ups_status, ", Unknown"); } } } @@ -245,36 +253,27 @@ main (int argc, char **argv) alarm (0); - sprintf (output_message, "UPS %s - ", - (result == STATE_OK) ? "ok" : "problem"); + asprintf (&message, "UPS %s - ", (result == STATE_OK) ? "ok" : "problem"); - if (supported_options & UPS_STATUS) { - sprintf (temp_buffer, "Status=%s ", ups_status); - strcat (output_message, temp_buffer); - } - if (supported_options & UPS_UTILITY) { - sprintf (temp_buffer, "Utility=%3.1fV ", ups_utility_voltage); - strcat (output_message, temp_buffer); - } - if (supported_options & UPS_BATTPCT) { - sprintf (temp_buffer, "Batt=%3.1f%% ", ups_battery_percent); - strcat (output_message, temp_buffer); - } - if (supported_options & UPS_LOADPCT) { - sprintf (temp_buffer, "Load=%3.1f%% ", ups_load_percent); - strcat (output_message, temp_buffer); - } - if (supported_options & UPS_TEMP) { - sprintf (temp_buffer, "Temp=%3.1fF", ups_temperature); - strcat (output_message, temp_buffer); - } - if (supported_options == UPS_NONE) { - sprintf (temp_buffer, - "UPS does not appear to support any available options\n"); - strcat (output_message, temp_buffer); - } + if (supported_options & UPS_STATUS) + asprintf (&message, "%sStatus=%s ", message, ups_status); + + if (supported_options & UPS_UTILITY) + asprintf (&message, "%sUtility=%3.1fV ", message, ups_utility_voltage); + + if (supported_options & UPS_BATTPCT) + asprintf (&message, "%sBatt=%3.1f%% ", message, ups_battery_percent); + + if (supported_options & UPS_LOADPCT) + asprintf (&message, "%sLoad=%3.1f%% ", message, ups_load_percent); + + if (supported_options & UPS_TEMP) + asprintf (&message, "%sTemp=%3.1fF", message, ups_temperature); + + if (supported_options == UPS_NONE) + asprintf (&message, "UPS does not support any available options\n"); - printf ("%s\n", output_message); + printf ("%s\n", message); return result; } @@ -310,6 +309,8 @@ determine_status (void) status |= UPSSTATUS_LB; else if (!strcmp (ptr, "CAL")) status |= UPSSTATUS_CAL; + else if (!strcmp (ptr, "RB")) + status |= UPSSTATUS_RB; else status |= UPSSTATUS_UNKOWN; } @@ -433,50 +434,6 @@ process_arguments (int argc, char **argv) { int c; - if (argc < 2) - return ERROR; - - for (c = 1; c < argc; c++) { - if (strcmp ("-to", argv[c]) == 0) - strcpy (argv[c], "-t"); - else if (strcmp ("-wt", argv[c]) == 0) - strcpy (argv[c], "-w"); - else if (strcmp ("-ct", argv[c]) == 0) - strcpy (argv[c], "-c"); - } - - c = 0; - while ((c += (call_getopt (argc - c, &argv[c]))) < argc) { - - if (is_option (argv[c])) - continue; - - if (server_address == NULL) { - if (is_host (argv[c])) { - server_address = argv[c]; - } - else { - usage ("Invalid host name"); - } - } - } - - if (server_address == NULL) - server_address = strscpy (NULL, "127.0.0.1"); - - return validate_arguments (); -} - - - - - - -int -call_getopt (int argc, char **argv) -{ - int c, i = 0; - #ifdef HAVE_GETOPT_H int option_index = 0; static struct option long_options[] = { @@ -493,40 +450,39 @@ call_getopt (int argc, char **argv) }; #endif + if (argc < 2) + return ERROR; + + for (c = 1; c < argc; c++) { + if (strcmp ("-to", argv[c]) == 0) + strcpy (argv[c], "-t"); + else if (strcmp ("-wt", argv[c]) == 0) + strcpy (argv[c], "-w"); + else if (strcmp ("-ct", argv[c]) == 0) + strcpy (argv[c], "-c"); + } + while (1) { #ifdef HAVE_GETOPT_H c = - getopt_long (argc, argv, "+hVH:u:p:v:c:w:t:", long_options, + getopt_long (argc, argv, "hVH:u:p:v:c:w:t:", long_options, &option_index); #else - c = getopt (argc, argv, "+?hVH:u:p:v:c:w:t:"); + c = getopt (argc, argv, "hVH:u:p:v:c:w:t:"); #endif - i++; - - if (c == -1 || c == EOF || c == 1) + if (c == -1 || c == EOF) break; - switch (c) { - case 'H': - case 'u': - case 'p': - case 'v': - case 'c': - case 'w': - case 't': - i++; - } - switch (c) { case '?': /* help */ - usage ("Invalid argument\n"); + usage3 ("Unknown option", optopt); case 'H': /* hostname */ if (is_host (optarg)) { server_address = optarg; } else { - usage ("Invalid host name\n"); + usage2 ("Invalid host name", optarg); } break; case 'u': /* ups name */ @@ -537,7 +493,7 @@ call_getopt (int argc, char **argv) server_port = atoi (optarg); } else { - usage ("Server port must be a positive integer\n"); + usage2 ("Server port must be a positive integer", optarg); } break; case 'c': /* critical time threshold */ @@ -546,7 +502,7 @@ call_getopt (int argc, char **argv) check_critical_value = TRUE; } else { - usage ("Critical time must be a nonnegative integer\n"); + usage2 ("Critical time must be a nonnegative integer", optarg); } break; case 'w': /* warning time threshold */ @@ -555,7 +511,7 @@ call_getopt (int argc, char **argv) check_warning_value = TRUE; } else { - usage ("Warning time must be a nonnegative integer\n"); + usage2 ("Warning time must be a nonnegative integer", optarg); } break; case 'v': /* variable */ @@ -568,7 +524,7 @@ call_getopt (int argc, char **argv) else if (!strcmp (optarg, "LOADPCT")) check_variable = UPS_LOADPCT; else - usage ("Unrecognized UPS variable\n"); + usage2 ("Unrecognized UPS variable", optarg); break; case 't': /* timeout */ if (is_intnonneg (optarg)) { @@ -579,14 +535,23 @@ call_getopt (int argc, char **argv) } break; case 'V': /* version */ - print_revision (PROGNAME, "$Revision$"); + print_revision (progname, "$Revision$"); exit (STATE_OK); case 'h': /* help */ print_help (); exit (STATE_OK); } } - return i; + + + if (server_address == NULL && argc > optind) { + if (is_host (argv[optind])) + server_address = argv[optind++]; + else + usage ("Invalid host name"); + } + + return validate_arguments(); } @@ -606,7 +571,7 @@ validate_arguments (void) void print_help (void) { - print_revision (PROGNAME, "$Revision$"); + print_revision (progname, "$Revision$"); printf ("Copyright (c) 2000 Tom Shields/Karl DeBisschop\n\n" "This plugin tests the UPS service on the specified host.\n" @@ -646,5 +611,5 @@ print_usage (void) ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n" " [-t timeout] [-v]\n" " %s --help\n" - " %s --version\n", PROGNAME, PROGNAME, PROGNAME); + " %s --version\n", progname, progname, progname); }