Code

Fix translations when extra-opts aren't enabled
[nagiosplug.git] / plugins / check_ups.c
index e974b7534576799b3e1c7045b84359ba35843ca9..ce57ef3d00f8aafc415397c21183b12a1f7c8c04 100644 (file)
@@ -1,38 +1,41 @@
-/******************************************************************************
-
- check_ups
-
- Program: Network UPS Tools plugin for Nagios
- License: GPL
- Copyright (c) 2000 Tom Shields
-               2004 Alain Richard <alain.richard@equation.fr>
-               2004 Arnaud Quette <arnaud.quette@mgeups.com>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or (at
- your option) any later version.
-
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
- $Id$
-******************************************************************************/
+/*****************************************************************************
+* 
+* Nagios check_ups plugin
+* 
+* License: GPL
+* Copyright (c) 2000 Tom Shields
+*               2004 Alain Richard <alain.richard@equation.fr>
+*               2004 Arnaud Quette <arnaud.quette@mgeups.com>
+* Copyright (c) 2002-2007 Nagios Plugins Development Team
+* 
+* Description:
+* 
+* This file contains Network UPS Tools plugin for Nagios
+* 
+* This plugin tests the UPS service on the specified host. Network UPS Tools
+* from www.networkupstools.org must be running for this plugin to work.
+* 
+* 
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+* 
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+* 
+* You should have received a copy of the GNU General Public License
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+* 
+* 
+*****************************************************************************/
 
 const char *progname = "check_ups";
-const char *revision = "$Revision$";
-const char *copyright = "2000-2002";
+const char *copyright = "2000-2007";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
-#include <locale.h>
 #include "common.h"
 #include "netutils.h"
 #include "utils.h"
@@ -96,9 +99,10 @@ void print_usage (void);
 int
 main (int argc, char **argv)
 {
-       int result = STATE_OK;
+       int result = STATE_UNKNOWN;
        char *message;
        char *data;
+       char *tunits;
        char temp_buffer[MAX_INPUT_BUFFER];
        double ups_utility_deviation = 0.0;
        int res;
@@ -111,7 +115,10 @@ main (int argc, char **argv)
        data = strdup ("");
        message = strdup ("");
 
-       if (process_arguments (argc, argv) != TRUE)
+       /* Parse extra opts if any */
+       argv=np_extra_opts (&argc, argv, progname);
+
+       if (process_arguments (argc, argv) == ERROR)
                usage4 (_("Could not parse arguments"));
 
        /* initialize alarm signal handling */
@@ -134,48 +141,48 @@ main (int argc, char **argv)
                }
                else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) ==
                                                 (UPSSTATUS_OB | UPSSTATUS_LB)) {
-                       asprintf (&ups_status, "On Battery, Low Battery");
+                       asprintf (&ups_status, _("On Battery, Low Battery"));
                        result = STATE_CRITICAL;
                }
                else {
                        if (status & UPSSTATUS_OL) {
-                               asprintf (&ups_status, "%s%s", ups_status, "Online");
+                               asprintf (&ups_status, "%s%s", ups_status, _("Online"));
                        }
                        if (status & UPSSTATUS_OB) {
-                               asprintf (&ups_status, "%s%s", ups_status, "On Battery");
+                               asprintf (&ups_status, "%s%s", ups_status, _("On Battery"));
                                result = STATE_WARNING;
                        }
                        if (status & UPSSTATUS_LB) {
-                               asprintf (&ups_status, "%s%s", ups_status, ", Low Battery");
+                               asprintf (&ups_status, "%s%s", ups_status, _(", Low Battery"));
                                result = STATE_WARNING;
                        }
                        if (status & UPSSTATUS_CAL) {
-                               asprintf (&ups_status, "%s%s", ups_status, ", Calibrating");
+                               asprintf (&ups_status, "%s%s", ups_status, _(", Calibrating"));
                        }
                        if (status & UPSSTATUS_RB) {
-                               asprintf (&ups_status, "%s%s", ups_status, ", Replace Battery");
+                               asprintf (&ups_status, "%s%s", ups_status, _(", Replace Battery"));
                                result = STATE_WARNING;
                        }
                        if (status & UPSSTATUS_BYPASS) {
-                               asprintf (&ups_status, "%s%s", ups_status, ", On Bypass");
+                               asprintf (&ups_status, "%s%s", ups_status, _(", On Bypass"));
                        }
                        if (status & UPSSTATUS_OVER) {
-                               asprintf (&ups_status, "%s%s", ups_status, ", Overload");
+                               asprintf (&ups_status, "%s%s", ups_status, _(", Overload"));
                        }
                        if (status & UPSSTATUS_TRIM) {
-                               asprintf (&ups_status, "%s%s", ups_status, ", Trimming");
+                               asprintf (&ups_status, "%s%s", ups_status, _(", Trimming"));
                        }
                        if (status & UPSSTATUS_BOOST) {
-                               asprintf (&ups_status, "%s%s", ups_status, ", Boosting");
+                               asprintf (&ups_status, "%s%s", ups_status, _(", Boosting"));
                        }
                        if (status & UPSSTATUS_CHRG) {
-                               asprintf (&ups_status, "%s%s", ups_status, ", Charging");
+                               asprintf (&ups_status, "%s%s", ups_status, _(", Charging"));
                        }
                        if (status & UPSSTATUS_DISCHRG) {
-                               asprintf (&ups_status, "%s%s", ups_status, ", Discharging");
+                               asprintf (&ups_status, "%s%s", ups_status, _(", Discharging"));
                        }
                        if (status & UPSSTATUS_UNKOWN) {
-                               asprintf (&ups_status, "%s%s", ups_status, ", Unknown");
+                               asprintf (&ups_status, "%s%s", ups_status, _(", Unknown"));
                        }
                }
                asprintf (&message, "%sStatus=%s ", message, ups_status);
@@ -282,10 +289,12 @@ main (int argc, char **argv)
        else {
                supported_options |= UPS_TEMP;
                if (temp_output_c) {
+                 tunits="degC";
                  ups_temperature = atof (temp_buffer);
                  asprintf (&message, "%sTemp=%3.1fC", message, ups_temperature);
                }
                else {
+                 tunits="degF";
                  ups_temperature = (atof (temp_buffer) * 1.8) + 32;
                  asprintf (&message, "%sTemp=%3.1fF", message, ups_temperature);
                }
@@ -298,13 +307,13 @@ main (int argc, char **argv)
                                result = max_state (result, STATE_WARNING);
                        }
                        asprintf (&data, "%s %s", data,
-                                 perfdata ("temp", (long)ups_temperature, "degF",
+                                 perfdata ("temp", (long)ups_temperature, tunits,
                                            check_warn, (long)(1000*warning_value),
                                            check_crit, (long)(1000*critical_value),
                                            TRUE, 0, FALSE, 0));
                } else {
                        asprintf (&data, "%s %s", data,
-                                 perfdata ("temp", (long)ups_temperature, "degF",
+                                 perfdata ("temp", (long)ups_temperature, tunits,
                                            FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
                }
        }
@@ -312,7 +321,7 @@ main (int argc, char **argv)
        /* if the UPS does not support any options we are looking for, report an error */
        if (supported_options == UPS_NONE) {
                result = STATE_CRITICAL;
-               asprintf (&message, "UPS does not support any available options\n");
+               asprintf (&message, _("UPS does not support any available options\n"));
        }
 
        /* reset timeout */
@@ -336,7 +345,7 @@ determine_status (void)
        res=get_ups_variable ("ups.status", recv_buffer, sizeof (recv_buffer));
        if (res == NOSUCHVAR) return OK;
        if (res != STATE_OK) {
-               printf ("Invalid response received from host\n");
+               printf ("%s\n", _("Invalid response received from host"));
                return ERROR;
        }
 
@@ -385,48 +394,52 @@ get_ups_variable (const char *varname, char *buf, size_t buflen)
        char temp_buffer[MAX_INPUT_BUFFER];
        char send_buffer[MAX_INPUT_BUFFER];
        char *ptr;
+       char *logout = "OK Goodbye\n";
+       int logout_len = strlen(logout);
        int len;
 
        *buf=0;
-       
+
        /* create the command string to send to the UPS daemon */
-       sprintf (send_buffer, "GET VAR %s %s\n", ups_name, varname);
+       /* Add LOGOUT to avoid read failure logs */
+       sprintf (send_buffer, "GET VAR %s %s\nLOGOUT\n", ups_name, varname);
 
        /* send the command to the daemon and get a response back */
        if (process_tcp_request
                        (server_address, server_port, send_buffer, temp_buffer,
                         sizeof (temp_buffer)) != STATE_OK) {
-               printf ("Invalid response received from host\n");
+               printf ("%s\n", _("Invalid response received from host"));
                return ERROR;
        }
 
        ptr = temp_buffer;
        len = strlen(ptr);
+       if (len > logout_len && strcmp (ptr + len - logout_len, logout) == 0) len -= logout_len;
        if (len > 0 && ptr[len-1] == '\n') ptr[len-1]=0;
        if (strcmp (ptr, "ERR UNKNOWN-UPS") == 0) {
-               printf ("CRITICAL - no such ups '%s' on that host\n", ups_name);
+               printf (_("CRITICAL - no such UPS '%s' on that host\n"), ups_name);
                return ERROR;
        }
 
        if (strcmp (ptr, "ERR VAR-NOT-SUPPORTED") == 0) {
-               //printf ("Error: Variable '%s' is not supported\n", varname);
+               /*printf ("Error: Variable '%s' is not supported\n", varname);*/
                return NOSUCHVAR;
        }
 
        if (strcmp (ptr, "ERR DATA-STALE") == 0) {
-               printf ("CRITICAL - UPS data is stale\n");
+               printf ("%s\n", _("CRITICAL - UPS data is stale"));
                return ERROR;
        }
 
        if (strncmp (ptr, "ERR", 3) == 0) {
-               printf ("Unknown error: %s\n", ptr);
+               printf (_("Unknown error: %s\n"), ptr);
                return ERROR;
        }
 
        ptr = temp_buffer + strlen (varname) + strlen (ups_name) + 6;
        len = strlen(ptr);
        if (len < 2 || ptr[0] != '"' || ptr[len-1] != '"') {
-               printf ("Error: unable to parse variable\n");
+               printf ("%s\n", _("Error: unable to parse variable"));
                return ERROR;
        }
        strncpy (buf, ptr+1, len - 2);
@@ -436,7 +449,7 @@ get_ups_variable (const char *varname, char *buf, size_t buflen)
 }
 
 
-/* Command line: CHECK_UPS -H <host_address> -u ups [-p port] [-v variable] 
+/* Command line: CHECK_UPS -H <host_address> -u ups [-p port] [-v variable]
                           [-wv warn_value] [-cv crit_value] [-to to_sec] */
 
 
@@ -482,9 +495,7 @@ process_arguments (int argc, char **argv)
 
                switch (c) {
                case '?':                                                                       /* help */
-                       printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
-                       print_usage ();
-                       exit (STATE_UNKNOWN);
+                       usage5 ();
                case 'H':                                                                       /* hostname */
                        if (is_host (optarg)) {
                                server_address = optarg;
@@ -493,7 +504,7 @@ process_arguments (int argc, char **argv)
                                usage2 (_("Invalid hostname/address"), optarg);
                        }
                        break;
-               case 'T': /* FIXME: to be improved (ie "-T C" for Celsius or "-T F" for Farenheit) */ 
+               case 'T': /* FIXME: to be improved (ie "-T C" for Celsius or "-T F" for Farenheit) */
                        temp_output_c = 1;
                        break;
                case 'u':                                                                       /* ups name */
@@ -504,7 +515,7 @@ process_arguments (int argc, char **argv)
                                server_port = atoi (optarg);
                        }
                        else {
-                               usage2 ("Port must be a positive integer", optarg);
+                               usage2 (_("Port must be a positive integer"), optarg);
                        }
                        break;
                case 'c':                                                                       /* critical time threshold */
@@ -513,7 +524,7 @@ process_arguments (int argc, char **argv)
                                check_crit = TRUE;
                        }
                        else {
-                               usage2 ("Critical time must be a positive integer", optarg);
+                               usage2 (_("Critical time must be a positive integer"), optarg);
                        }
                        break;
                case 'w':                                                                       /* warning time threshold */
@@ -522,7 +533,7 @@ process_arguments (int argc, char **argv)
                                check_warn = TRUE;
                        }
                        else {
-                               usage2 ("Warning time must be a positive integer", optarg);
+                               usage2 (_("Warning time must be a positive integer"), optarg);
                        }
                        break;
                case 'v':                                                                       /* variable */
@@ -535,18 +546,18 @@ process_arguments (int argc, char **argv)
                        else if (!strcmp (optarg, "LOADPCT"))
                                check_variable = UPS_LOADPCT;
                        else
-                               usage2 ("Unrecognized UPS variable", optarg);
+                               usage2 (_("Unrecognized UPS variable"), optarg);
                        break;
                case 't':                                                                       /* timeout */
                        if (is_intnonneg (optarg)) {
                                socket_timeout = atoi (optarg);
                        }
                        else {
-                               usage ("Time interval must be a positive integer\n");
+                               usage4 (_("Timeout interval must be a positive integer"));
                        }
                        break;
                case 'V':                                                                       /* version */
-                       print_revision (progname, revision);
+                       print_revision (progname, NP_VERSION);
                        exit (STATE_OK);
                case 'h':                                                                       /* help */
                        print_help ();
@@ -569,86 +580,87 @@ process_arguments (int argc, char **argv)
 }
 
 
-
 int
 validate_arguments (void)
 {
        if (! ups_name) {
-               printf ("Error : no ups indicated\n");
+               printf ("%s\n", _("Error : no UPS indicated"));
                return ERROR;
        }
        return OK;
 }
 
 
-
 void
 print_help (void)
 {
        char *myport;
        asprintf (&myport, "%d", PORT);
 
-       print_revision (progname, revision);
+       print_revision (progname, NP_VERSION);
 
-       printf ("Copyright (c) 2000 Tom Shields");
+       printf ("Copyright (c) 2000 Tom Shields\n");
        printf ("Copyright (c) 2004 Alain Richard <alain.richard@equation.fr>\n");
        printf ("Copyright (c) 2004 Arnaud Quette <arnaud.quette@mgeups.com>\n");
-
        printf (COPYRIGHT, copyright, email);
 
-       printf (_("This plugin tests the UPS service on the specified host.\n\
-Network UPS Tools from www.networkupstools.org must be running for this\n\
-plugin to work.\n\n"));
+       printf ("%s\n", _("This plugin tests the UPS service on the specified host. Network UPS Tools"));
+  printf ("%s\n", _("from www.networkupstools.org must be running for this plugin to work."));
 
-       print_usage ();
+  printf ("\n\n");
 
-       printf (_(UT_HELP_VRSN));
-
-       printf (_(UT_HOST_PORT), 'p', myport);
-
-       printf (_("\
- -u, --ups=STRING\n\
-    Name of UPS\n"));
-
-       printf (_("\
- -T, --temperature\n\
-    Output of temperatures in Celsius\n"));
-
-       printf (_(UT_WARN_CRIT));
-
-       printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
-
-       printf (_(UT_VERBOSE));
-
-       printf (_("\
-This plugin attempts to determine the status of a UPS (Uninterruptible Power\n\
-Supply) on a local or remote host. If the UPS is online or calibrating, the\n\
-plugin will return an OK state. If the battery is on it will return a WARNING\n\
-state.  If the UPS is off or has a low battery the plugin will return a CRITICAL\n\
-state.\n\n"));
-
-       printf (_("\
-You may also specify a variable to check [such as temperature, utility voltage,\n\
-battery load, etc.]  as well as warning and critical thresholds for the value of\n\
-that variable.  If the remote host has multiple UPS that are being monitored you\n\
-will have to use the [ups] option to specify which UPS to check.\n\n"));
-
-       printf (_("Notes:\n\n\
-This plugin requires that the UPSD daemon distributed with Russel Kroll's\n\
-Smart UPS Tools be installed on the remote host.  If you do not have the\n\
-package installed on your system, you can download it from\n\
-http://www.networkupstools.org\n\n"));
+       print_usage ();
 
-       printf (_(UT_SUPPORT));
+       printf (UT_HELP_VRSN);
+       printf (UT_EXTRA_OPTS);
+
+       printf (UT_HOST_PORT, 'p', myport);
+
+       printf (" %s\n", "-u, --ups=STRING");
+  printf ("    %s\n", _("Name of UPS"));
+  printf (" %s\n", "-T, --temperature");
+  printf ("    %s\n", _("Output of temperatures in Celsius"));
+  printf (" %s\n", "-v, --variable=STRING");
+  printf ("    %s %s\n", _("Valid values for STRING are"), "LINE, TEMP, BATTPCT or LOADPCT");
+
+       printf (UT_WARN_CRIT);
+
+       printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
+
+/* TODO: -v clashing with -v/-variable. Commenting out help text since verbose
+         is unused up to now */
+/*     printf (UT_VERBOSE); */
+
+  printf ("\n");
+       printf ("%s\n", _("This plugin attempts to determine the status of a UPS (Uninterruptible Power"));
+  printf ("%s\n", _("Supply) on a local or remote host. If the UPS is online or calibrating, the"));
+  printf ("%s\n", _("plugin will return an OK state. If the battery is on it will return a WARNING"));
+  printf ("%s\n", _("state. If the UPS is off or has a low battery the plugin will return a CRITICAL"));
+  printf ("%s\n", _("state."));
+
+  printf ("\n");
+  printf ("%s\n", _("Notes:"));
+  printf (" %s\n", _("You may also specify a variable to check (such as temperature, utility voltage,"));
+  printf (" %s\n", _("battery load, etc.) as well as warning and critical thresholds for the value"));
+  printf (" %s\n", _("of that variable.  If the remote host has multiple UPS that are being monitored"));
+  printf (" %s\n", _("you will have to use the --ups option to specify which UPS to check."));
+  printf ("\n");
+  printf (" %s\n", _("This plugin requires that the UPSD daemon distributed with Russell Kroll's"));
+  printf (" %s\n", _("Network UPS Tools be installed on the remote host. If you do not have the"));
+  printf (" %s\n", _("package installed on your system, you can download it from"));
+  printf (" %s\n", _("http://www.networkupstools.org"));
+#ifdef NP_EXTRA_OPTS
+  printf ("\n");
+  printf (UT_EXTRA_OPTS_NOTES);
+#endif
+
+       printf (UT_SUPPORT);
 }
 
 
-
 void
 print_usage (void)
 {
-       printf (_("\
-Usage: %s -H host -u ups [-p port] [-v variable]\n\
-  [-wv warn_value] [-cv crit_value] [-to to_sec] [-T]\n"), progname);
-       printf (_(UT_HLP_VRS), progname, progname);
+  printf (_("Usage:"));
+       printf ("%s -H host -u ups [-p port] [-v variable] [-w warn_value] [-c crit_value] [-to to_sec] [-T]\n", progname);
 }