Code

still trying to fix #1094326
[nagiosplug.git] / plugins / check_snmp.c
index dcb6e68aff2024a5d5439e94ec27834729db98bf..ef411e15fe59208e521b66fbdb7a723cbaff06a2 100644 (file)
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
+ $Id$
 ******************************************************************************/
 
 const char *progname = "check_snmp";
 const char *revision = "$Revision$";
-const char *copyright = "1999-2003";
+const char *copyright = "1999-2004";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 #include "common.h"
@@ -84,7 +86,7 @@ int errcode, excode;
 #endif
 
 char *server_address = NULL;
-char *community = DEFAULT_COMMUNITY;
+char *community = NULL;
 char *authpriv = NULL;
 char *proto = NULL;
 char *seclevel = NULL;
@@ -92,17 +94,17 @@ char *secname = NULL;
 char *authproto = NULL;
 char *authpasswd = NULL;
 char *privpasswd = NULL;
-char *oid = "";
-char *label = "SNMP";
-char *units = "";
-char *port = DEFAULT_PORT;
+char *oid;
+char *label;
+char *units;
+char *port;
 char string_value[MAX_INPUT_BUFFER] = "";
 char **labels = NULL;
 char **unitv = NULL;
-int nlabels = 0;
-int labels_size = 8;
-int nunits = 0;
-int unitv_size = 8;
+size_t nlabels = 0;
+size_t labels_size = 8;
+size_t nunits = 0;
+size_t unitv_size = 8;
 int verbose = FALSE;
 unsigned long lower_warn_lim[MAX_OIDS];
 unsigned long upper_warn_lim[MAX_OIDS];
@@ -111,16 +113,13 @@ unsigned long upper_crit_lim[MAX_OIDS];
 unsigned long response_value[MAX_OIDS];
 int check_warning_value = FALSE;
 int check_critical_value = FALSE;
-int eval_method[MAX_OIDS];
-char *delimiter = DEFAULT_DELIMITER;
-char *output_delim = DEFAULT_OUTPUT_DELIMITER;
-char *miblist = DEFAULT_MIBLIST;
+unsigned long eval_method[MAX_OIDS];
+char *delimiter;
+char *output_delim;
+char *miblist;
 
 
 
-
-
-\f
 int
 main (int argc, char **argv)
 {
@@ -131,20 +130,34 @@ main (int argc, char **argv)
        char input_buffer[MAX_INPUT_BUFFER];
        char *command_line = NULL;
        char *response = NULL;
-       char *outbuff = "";
-       char *output = "";
+       char *outbuff;
+       char *output;
        char *ptr = NULL;
        char *p2 = NULL;
        char *show = NULL;
 
+       setlocale (LC_ALL, "");
+       bindtextdomain (PACKAGE, LOCALEDIR);
+       textdomain (PACKAGE);
+
        labels = malloc (labels_size);
        unitv = malloc (unitv_size);
        for (i = 0; i < MAX_OIDS; i++)
                eval_method[i] = CHECK_UNDEF;
        i = 0;
 
+       oid = strdup ("");
+       label = strdup ("SNMP");
+       units = strdup ("");
+       port = strdup (DEFAULT_PORT);
+       outbuff = strdup ("");
+       output = strdup ("");
+       delimiter = strdup (DEFAULT_DELIMITER);
+       output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
+       miblist = strdup (DEFAULT_MIBLIST);
+
        if (process_arguments (argc, argv) == ERROR)
-               usage (_("Incorrect arguments supplied\n"));
+               usage4 (_("Could not parse arguments"));
 
        /* create the command line to execute */
        asprintf (&command_line, "%s -t 1 -r %d -m %s -v %s %s %s:%s %s",
@@ -201,7 +214,7 @@ main (int argc, char **argv)
                                ptr = strpbrk (ptr, "\n");
                        }
                        if (ptr && strstr (ptr, delimiter) == NULL) {
-                               response = strscat (response, ptr);
+                               asprintf (&response, "%s%s", response, ptr);
                                ptr = NULL;
                        }
                }
@@ -238,7 +251,7 @@ main (int argc, char **argv)
                    eval_method[i] & WARN_NE) {
                        p2 = strpbrk (p2, "0123456789");
                        if (p2 == NULL) 
-                               die (STATE_UNKNOWN,"No valid data returned");
+                               die (STATE_UNKNOWN,_("No valid data returned"));
                        response_value[i] = strtoul (p2, NULL, 10);
                        iresult = check_num (i);
                        asprintf (&show, "%lu", response_value[i]);
@@ -246,7 +259,7 @@ main (int argc, char **argv)
 
                /* Process this block for string matching */
                else if (eval_method[i] & CRIT_STRING) {
-                       if (strcmp (response, string_value))
+                       if (strcmp (show, string_value))
                                iresult = STATE_CRITICAL;
                        else
                                iresult = STATE_OK;
@@ -268,7 +281,7 @@ main (int argc, char **argv)
                                iresult = STATE_CRITICAL;
                        }
 #else
-                       printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
+                       printf (_("Call for regex which was not a compiled option"));
                        exit (STATE_UNKNOWN);
 #endif
                }
@@ -287,7 +300,7 @@ main (int argc, char **argv)
                result = max_state (result, iresult);
 
                /* Prepend a label for this OID if there is one */
-               if (nlabels > 1 && i < nlabels && labels[i] != NULL)
+               if (nlabels > (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
                        asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
                                  (i == 0) ? " " : output_delim,
                                  labels[i], mark (iresult), show, mark (iresult));
@@ -296,7 +309,7 @@ main (int argc, char **argv)
                                  mark (iresult), show, mark (iresult));
 
                /* Append a unit string for this OID if there is one */
-               if (nunits > 0 && i < nunits && unitv[i] != NULL)
+               if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
                        asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
 
                i++;
@@ -305,7 +318,7 @@ main (int argc, char **argv)
 
        if (found == 0)
                die (STATE_UNKNOWN,
-                    _("%s problem - No data recieved from host\nCMD: %s\n"),
+                    _("%s problem - No data received from host\nCMD: %s\n"),
                     label,
                     command_line);
 
@@ -330,9 +343,6 @@ main (int argc, char **argv)
 
 
 
-
-
-\f
 /* process command-line arguments */
 int
 process_arguments (int argc, char **argv)
@@ -341,8 +351,8 @@ process_arguments (int argc, char **argv)
        int c = 1;
        int j = 0, jj = 0, ii = 0;
 
-       int option_index = 0;
-       static struct option long_options[] = {
+       int option = 0;
+       static struct option longopts[] = {
                STD_LONG_OPTS,
                {"community", required_argument, 0, 'C'},
                {"oid", required_argument, 0, 'o'},
@@ -381,14 +391,14 @@ process_arguments (int argc, char **argv)
 
        while (1) {
                c = getopt_long (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:m:P:L:U:a:A:X:",
-                                                                        long_options, &option_index);
+                                                                        longopts, &option);
 
                if (c == -1 || c == EOF)
                        break;
 
                switch (c) {
                case '?':       /* usage */
-                       usage3 ("Unknown argument", optopt);
+                       usage2 (_("Unknown argument"), optarg);
                case 'h':       /* help */
                        print_help ();
                        exit (STATE_OK); 
@@ -401,48 +411,46 @@ process_arguments (int argc, char **argv)
 
        /* Connection info */
                case 'C':                                                                       /* group or community */
-                       community = strscpy (community, optarg);
+                       community = optarg;
                        break;
                case 'H':                                                                       /* Host or server */
-                       server_address = strscpy (server_address, optarg);
+                       server_address = optarg;
                        break;
                case 'p':       /* TCP port number */
-                       port = strscpy(port, optarg);
+                       port = optarg;
                        break;
                case 'm':      /* List of MIBS  */
-                       miblist = strscpy(miblist, optarg);
+                       miblist = optarg;
                        break;
                case 'P':     /* SNMP protocol version */
-                       proto = strscpy(proto, optarg);
+                       proto = optarg;
                        break;
                case 'L':     /* security level */
-                       seclevel = strscpy(seclevel,optarg);
+                       seclevel = optarg;
                        break;
                case 'U':     /* security username */
-                       secname = strscpy(secname, optarg);
+                       secname = optarg;
                        break;
                case 'a':     /* auth protocol */
-                       asprintf (&authproto, optarg);
+                       authproto = optarg;
                        break;
                case 'A':     /* auth passwd */
-                       authpasswd = strscpy(authpasswd, optarg);
+                       authpasswd = optarg;
                        break;
                case 'X':     /* priv passwd */
-                       privpasswd = strscpy(privpasswd, optarg);
+                       privpasswd = optarg;
                        break;
                case 't':       /* timeout period */
                        if (!is_integer (optarg))
-                               usage2 (_("Timeout Interval must be an integer"), optarg);
-                       timeout_interval = atoi (optarg);
+                               usage2 (_("Timeout interval must be a positive integer"), optarg);
+                       else
+                               timeout_interval = atoi (optarg);
                        break;
 
        /* Test parameters */
                case 'c':                                                                       /* critical time threshold */
-                       if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
-                               printf (_("Invalid critical threshold: %s\n"), optarg);
-                               print_usage ();
-                               exit (STATE_UNKNOWN);
-                       }
+                       if (strspn (optarg, "0123456789:,") < strlen (optarg))
+                               usage2 (_("Invalid critical threshold: %s\n"), optarg);
                        for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
                                if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
                                        eval_method[jj] |= CRIT_LT;
@@ -452,11 +460,8 @@ process_arguments (int argc, char **argv)
                        }
                        break;
                case 'w':                                                                       /* warning time threshold */
-                       if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
-                               printf (_("Invalid warning threshold: %s\n"), optarg);
-                               print_usage ();
-                               exit (STATE_UNKNOWN);
-                       }
+                       if (strspn (optarg, "0123456789:,") < strlen (optarg))
+                               usage2 (_("Invalid warning threshold: %s\n"), optarg);
                        for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
                                if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
                                        eval_method[ii] |= WARN_LT;
@@ -506,7 +511,7 @@ process_arguments (int argc, char **argv)
                        eval_method[jj++] = CRIT_REGEX;
                        ii++;
 #else
-                       printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
+                       printf (_("call for regex which was not a compiled option"));
                        exit (STATE_UNKNOWN);
 #endif
                        break;
@@ -525,21 +530,19 @@ process_arguments (int argc, char **argv)
                                labels_size += 8;
                                labels = realloc (labels, labels_size);
                                if (labels == NULL)
-                                       die (STATE_UNKNOWN,
-                                                                                _("Could not realloc() labels[%d]"), nlabels);
+                                       die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), nlabels);
                        }
                        labels[nlabels - 1] = optarg;
                        ptr = thisarg (optarg);
+                       labels[nlabels - 1] = ptr;
                        if (strstr (ptr, "'") == ptr)
                                labels[nlabels - 1] = ptr + 1;
-                       else
-                               labels[nlabels - 1] = ptr;
                        while (ptr && (ptr = nextarg (ptr))) {
                                if (nlabels >= labels_size) {
                                        labels_size += 8;
                                        labels = realloc (labels, labels_size);
                                        if (labels == NULL)
-                                               die (STATE_UNKNOWN, _("Could not realloc() labels\n"));
+                                               die (STATE_UNKNOWN, _("Could not reallocate labels\n"));
                                }
                                labels++;
                                ptr = thisarg (ptr);
@@ -556,15 +559,13 @@ process_arguments (int argc, char **argv)
                                unitv_size += 8;
                                unitv = realloc (unitv, unitv_size);
                                if (unitv == NULL)
-                                       die (STATE_UNKNOWN,
-                                                                                _("Could not realloc() units [%d]\n"), nunits);
+                                       die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), nunits);
                        }
                        unitv[nunits - 1] = optarg;
                        ptr = thisarg (optarg);
+                       unitv[nunits - 1] = ptr;
                        if (strstr (ptr, "'") == ptr)
                                unitv[nunits - 1] = ptr + 1;
-                       else
-                               unitv[nunits - 1] = ptr;
                        while (ptr && (ptr = nextarg (ptr))) {
                                if (nunits >= unitv_size) {
                                        unitv_size += 8;
@@ -585,14 +586,15 @@ process_arguments (int argc, char **argv)
        }
 
        if (server_address == NULL)
-               asprintf (&server_address, argv[optind]);
+               server_address = argv[optind];
+
+       if (community == NULL)
+               community = strdup (DEFAULT_COMMUNITY);
 
        return validate_arguments ();
 }
 
 
-
-
 /******************************************************************************
 
 @@-
@@ -613,6 +615,8 @@ first character cannot be a number, however.</para>
 -@@
 ******************************************************************************/
 
+
+
 int
 validate_arguments ()
 {
@@ -640,7 +644,7 @@ validate_arguments ()
                }
                else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
                        if ( secname == NULL || authpasswd == NULL) {
-                               printf (_("Missing secname (%s) or authpassword (%s) ! \n)"),secname, authpasswd );
+                               printf (_("Missing secname (%s) or authpassword (%s) ! \n"),secname, authpasswd );
                                print_usage ();
                                exit (STATE_UNKNOWN);
                        }
@@ -648,32 +652,23 @@ validate_arguments ()
                }
                else if ( strcmp(seclevel, "authPriv") == 0 ) {
                        if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
-                               printf (("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
+                               printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
                                print_usage ();
                                exit (STATE_UNKNOWN);
                        }
                        asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
                }
                
-                                                                       
        }
        else {
-               printf (_("Invalid SNMP version: %s\n"), proto);
-               print_usage ();
-               exit (STATE_UNKNOWN);                           
+               usage2 (_("Invalid SNMP version"), proto);
        }
                        
-       
-       
-
        return OK;
 }
 
 
 
-
-
-\f
 char *
 clarify_message (char *msg)
 {
@@ -711,7 +706,6 @@ clarify_message (char *msg)
 
 
 
-
 int
 check_num (int i)
 {
@@ -735,7 +729,7 @@ check_num (int i)
        }
 
        if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
-                       lower_warn_lim[i] > upper_warn_lim[i]) {
+                       lower_crit_lim[i] > upper_crit_lim[i]) {
                if (response_value[i] <= lower_crit_lim[i] &&
                                response_value[i] >= upper_crit_lim[i]) {
                        result = STATE_CRITICAL;
@@ -756,7 +750,6 @@ check_num (int i)
 
 
 
-
 int
 lu_getll (unsigned long *ll, char *str)
 {
@@ -772,7 +765,6 @@ lu_getll (unsigned long *ll, char *str)
 
 
 
-
 int
 lu_getul (unsigned long *ul, char *str)
 {
@@ -788,7 +780,6 @@ lu_getul (unsigned long *ul, char *str)
 
 
 
-
 /* trim leading whitespace
         if there is a leading quote, make sure it balances */
 
@@ -798,14 +789,13 @@ thisarg (char *str)
        str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
        if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
                if (strlen (str) == 1 || !strstr (str + 1, "'"))
-                       die (STATE_UNKNOWN, "Unbalanced quotes\n");
+                       die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
        }
        return str;
 }
 
 
 
-
 /* if there's a leading quote, advance to the trailing quote
         set the trailing quote to '\x0'
         if the string continues, advance beyond the comma */
@@ -814,23 +804,21 @@ char *
 nextarg (char *str)
 {
        if (strstr (str, "'") == str) {
+               str[0] = 0;
                if (strlen (str) > 1) {
                        str = strstr (str + 1, "'");
-                       str[0] = 0;
                        return (++str);
                }
                else {
-                       str[0] = 0;
                        return NULL;
                }
        }
        if (strstr (str, ",") == str) {
+               str[0] = 0;
                if (strlen (str) > 1) {
-                       str[0] = 0;
                        return (++str);
                }
                else {
-                       str[0] = 0;
                        return NULL;
                }
        }
@@ -843,15 +831,12 @@ nextarg (char *str)
 
 
 
-
-
-\f
 void
 print_help (void)
 {
        print_revision (progname, revision);
 
-       printf (_(COPYRIGHT), copyright, email);
+       printf (COPYRIGHT, copyright, email);
 
        printf (_("\
 Check status of remote machines and obtain sustem information via SNMP\n\n"));
@@ -952,15 +937,16 @@ Check status of remote machines and obtain sustem information via SNMP\n\n"));
        printf (_(UT_SUPPORT));
 }
 
+
+
 void
 print_usage (void)
 {
-       printf (_("\
+       printf ("\
 Usage: %s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
-  [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
-  [-l label] [-u units] [-p port-number] [-d delimiter]\n\
-  [-D output-delimiter] [-m miblist] [-P snmp version]\n\
-  [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
-  [-X privpasswd]\n"), progname);
-       printf (_(UT_HLP_VRS), progname, progname);
+                  [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
+                  [-l label] [-u units] [-p port-number] [-d delimiter]\n\
+                  [-D output-delimiter] [-m miblist] [-P snmp version]\n\
+                  [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
+                  [-X privpasswd]\n", progname);
 }