Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[nagiosplug.git] / plugins / check_ups.c
index 110ea0a2bc2bf58530975d534dae962fc970d317..30c9357acbc14521e4d55c31e7460afde84cb824 100644 (file)
@@ -1,42 +1,39 @@
-/******************************************************************************
-*
+/*****************************************************************************
+* 
 * 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-2006 nagios-plugins team
-*
-* Last Modified: $Date$
-*
+* Copyright (c) 2002-2007 Nagios Plugins Development Team
+* 
 * Description:
-*
+* 
 * This file contains Network UPS Tools plugin for Nagios
-*
-* License Information:
-*
-* This program is free software; you can redistribute it and/or modify
+* 
+* 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 2 of the License, or
+* 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, write to the Free Software
-* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-*
-*  $Id$
-*
-******************************************************************************/
+* 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-2006";
+const char *copyright = "2000-2007";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 #include "common.h"
@@ -118,6 +115,9 @@ main (int argc, char **argv)
        data = strdup ("");
        message = strdup ("");
 
+       /* Parse extra opts if any */
+       argv=np_extra_opts (&argc, argv, progname);
+
        if (process_arguments (argc, argv) == ERROR)
                usage4 (_("Could not parse arguments"));
 
@@ -394,12 +394,15 @@ 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
@@ -411,9 +414,10 @@ get_ups_variable (const char *varname, char *buf, size_t buflen)
 
        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;
        }
 
@@ -445,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] */
 
 
@@ -491,7 +495,7 @@ process_arguments (int argc, char **argv)
 
                switch (c) {
                case '?':                                                                       /* help */
-                       usage2 (_("Unknown argument"), optarg);
+                       usage5 ();
                case 'H':                                                                       /* hostname */
                        if (is_host (optarg)) {
                                server_address = optarg;
@@ -500,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 */
@@ -553,7 +557,7 @@ process_arguments (int argc, char **argv)
                        }
                        break;
                case 'V':                                                                       /* version */
-                       print_revision (progname, revision);
+                       print_revision (progname, NP_VERSION);
                        exit (STATE_OK);
                case 'h':                                                                       /* help */
                        print_help ();
@@ -580,7 +584,7 @@ int
 validate_arguments (void)
 {
        if (! ups_name) {
-               printf ("%s\n", _("Error : no ups indicated"));
+               printf ("%s\n", _("Error : no UPS indicated"));
                return ERROR;
        }
        return OK;
@@ -593,60 +597,66 @@ 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 ("%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 thisplugin to work."));
+       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."));
 
   printf ("\n\n");
 
        print_usage ();
 
-       printf (_(UT_HELP_VRSN));
+       printf (UT_HELP_VRSN);
+       printf (UT_EXTRA_OPTS);
 
-       printf (_(UT_HOST_PORT), 'p', myport);
+       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_WARN_CRIT);
 
-       printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
+       printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
 
-       printf (_(UT_VERBOSE));
+/* 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", _("Notes:"));
-  
        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\n", _("state."));
-
-       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 of"));
-  printf ("%s\n", _("that variable.  If the remote host has multiple UPS that are being monitored you"));
-  printf ("%s\n", _("will have to use the [ups] option to specify which UPS to check."));
+  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 ("%s\n", _("This plugin requires that the UPSD daemon distributed with Russel Kroll's"));
-  printf ("%s\n", _("Smart 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 http://www.networkupstools.org"));
+  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"));
 
-       printf (_(UT_SUPPORT));
+       printf (UT_SUPPORT);
 }
 
 
 void
 print_usage (void)
 {
-  printf (_("Usage:"));
+  printf ("%s\n", _("Usage:"));
        printf ("%s -H host -u ups [-p port] [-v variable] [-w warn_value] [-c crit_value] [-to to_sec] [-T]\n", progname);
 }