Code

cleaning help and usage
[nagiosplug.git] / plugins / check_snmp.c
index 5d65113f4046e7fa5c25ab4b73b690f00b566de6..9ff3439e74783bed1d366c780e86514e70684997 100644 (file)
@@ -75,8 +75,7 @@ char *nextarg (char *str);
 void print_usage (void);
 void print_help (void);
 
-#ifdef HAVE_REGEX_H
-#include <regex.h>
+#include "regex.h"
 char regex_expect[MAX_INPUT_BUFFER] = "";
 regex_t preg;
 regmatch_t pmatch[10];
@@ -86,7 +85,6 @@ char perfstr[MAX_INPUT_BUFFER];
 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
 int eflags = 0;
 int errcode, excode;
-#endif
 
 char *server_address = NULL;
 char *community = NULL;
@@ -109,6 +107,7 @@ size_t labels_size = 8;
 size_t nunits = 0;
 size_t unitv_size = 8;
 int verbose = FALSE;
+int usesnmpgetnext = FALSE;
 unsigned long lower_warn_lim[MAX_OIDS];
 unsigned long upper_warn_lim[MAX_OIDS];
 unsigned long lower_crit_lim[MAX_OIDS];
@@ -120,7 +119,9 @@ int retries = 0;
 unsigned long eval_method[MAX_OIDS];
 char *delimiter;
 char *output_delim;
-char *miblist;
+char *miblist = NULL;
+int needmibs = FALSE;
+
 
 int
 main (int argc, char **argv)
@@ -138,6 +139,7 @@ main (int argc, char **argv)
        char *p2 = NULL;
        char *show = NULL;
        char type[8];
+       char *str[MAX_INPUT_BUFFER];
 
        setlocale (LC_ALL, "");
        bindtextdomain (PACKAGE, LOCALEDIR);
@@ -157,7 +159,7 @@ main (int argc, char **argv)
        output = strdup ("");
        delimiter = strdup (" = ");
        output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
-       miblist = strdup (DEFAULT_MIBLIST);
+       /* miblist = strdup (DEFAULT_MIBLIST); */
        timeout_interval = DEFAULT_TIMEOUT;
        retries = DEFAULT_RETRIES;
 
@@ -165,11 +167,20 @@ main (int argc, char **argv)
                usage4 (_("Could not parse arguments"));
 
        /* create the command line to execute */
-       asprintf (&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
+               if(usesnmpgetnext == TRUE) {
+               asprintf(&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
+                                   PATH_TO_SNMPGETNEXT, timeout_interval, retries, miblist, proto,
+                                               authpriv, server_address, port, oid);
+       }else{
+
+               asprintf (&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
                  PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto,
                  authpriv, server_address, port, oid);
+       }
+       
        if (verbose)
                printf ("%s\n", command_line);
+       
 
        /* run the command */
        child_process = spopen (command_line);
@@ -279,7 +290,6 @@ main (int argc, char **argv)
 
                /* Process this block for regex matching */
                else if (eval_method[i] & CRIT_REGEX) {
-#ifdef HAVE_REGEX_H
                        excode = regexec (&preg, response, 10, pmatch, eflags);
                        if (excode == 0) {
                                iresult = STATE_OK;
@@ -292,10 +302,6 @@ main (int argc, char **argv)
                        else {
                                iresult = STATE_CRITICAL;
                        }
-#else
-                       printf (_("Call for regex which was not a compiled option"));
-                       exit (STATE_UNKNOWN);
-#endif
                }
 
                /* Process this block for existence-nonexistence checks */
@@ -326,7 +332,6 @@ main (int argc, char **argv)
 
                i++;
 
-               char *str[MAX_INPUT_BUFFER];
                asprintf(str, "=%s%s;;;; ", show, type ? type : "");
                strcat(perfstr, *str);
 
@@ -391,6 +396,7 @@ process_arguments (int argc, char **argv)
                {"authproto", required_argument, 0, 'a'},
                {"authpasswd", required_argument, 0, 'A'},
                {"privpasswd", required_argument, 0, 'X'},
+               {"next", no_argument, 0, 'n'},
                {0, 0, 0, 0}
        };
 
@@ -408,7 +414,7 @@ process_arguments (int argc, char **argv)
        }
 
        while (1) {
-               c = getopt_long (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:L:U:a:A:X:",
+               c = getopt_long (argc, argv, "nhvVt:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:L:U:a:A:X:",
                                                                         longopts, &option);
 
                if (c == -1 || c == EOF)
@@ -440,6 +446,9 @@ process_arguments (int argc, char **argv)
                case 'm':      /* List of MIBS  */
                        miblist = optarg;
                        break;
+               case 'n':     /* usesnmpgetnext */
+                       usesnmpgetnext = TRUE;
+                       break;
                case 'P':     /* SNMP protocol version */
                        proto = optarg;
                        break;
@@ -496,6 +505,15 @@ process_arguments (int argc, char **argv)
                                retries = atoi(optarg);
                        break;
                case 'o':                                                                       /* object identifier */
+                       if ( strspn( optarg, "0123456789." ) != strlen( optarg ) ) {
+                                       /*
+                                        * we have something other than digits and periods, so we
+                                        * have a mib variable, rather than just an SNMP OID, so
+                                        * we have to actually read the mib files
+                                        */
+                                       needmibs = TRUE;
+                       }
+
                        for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
                                ptr[0] = ' '; /* relpace comma with space */
                        for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
@@ -517,11 +535,8 @@ process_arguments (int argc, char **argv)
                        ii++;
                        break;
                case 'R':                                                                       /* regex */
-#ifdef HAVE_REGEX_H
                        cflags = REG_ICASE;
-#endif
                case 'r':                                                                       /* regex */
-#ifdef HAVE_REGEX_H
                        cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
                        strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
                        regex_expect[sizeof (regex_expect) - 1] = 0;
@@ -533,10 +548,6 @@ process_arguments (int argc, char **argv)
                        }
                        eval_method[jj++] = CRIT_REGEX;
                        ii++;
-#else
-                       printf (_("call for regex which was not a compiled option"));
-                       exit (STATE_UNKNOWN);
-#endif
                        break;
 
        /* Format */
@@ -553,7 +564,7 @@ process_arguments (int argc, char **argv)
                                labels_size += 8;
                                labels = realloc (labels, labels_size);
                                if (labels == NULL)
-                                       die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), nlabels);
+                                       die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), (int)nlabels);
                        }
                        labels[nlabels - 1] = optarg;
                        ptr = thisarg (optarg);
@@ -582,7 +593,7 @@ process_arguments (int argc, char **argv)
                                unitv_size += 8;
                                unitv = realloc (unitv, unitv_size);
                                if (unitv == NULL)
-                                       die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), nunits);
+                                       die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), (int)nunits);
                        }
                        unitv[nunits - 1] = optarg;
                        ptr = thisarg (optarg);
@@ -613,6 +624,8 @@ process_arguments (int argc, char **argv)
 
        if (community == NULL)
                community = strdup (DEFAULT_COMMUNITY);
+       
+
 
        return validate_arguments ();
 }
@@ -626,13 +639,11 @@ process_arguments (int argc, char **argv)
 
 <para>&PROTO_validate_arguments;</para>
 
-<para>Given a database name, this function returns TRUE if the string
-is a valid PostgreSQL database name, and returns false if it is
-not.</para>
+<para>Checks to see if the default miblist needs to be loaded. Also verifies 
+the authentication and authorization combinations based on protocol version 
+selected.</para>
 
-<para>Valid PostgreSQL database names are less than &NAMEDATALEN;
-characters long and consist of letters, numbers, and underscores. The
-first character cannot be a number, however.</para>
+<para></para>
 
 </sect3>
 -@@
@@ -643,6 +654,15 @@ first character cannot be a number, however.</para>
 int
 validate_arguments ()
 {
+       /* check whether to load locally installed MIBS (CPU/disk intensive) */
+       if (miblist == NULL) {
+               if ( needmibs  == TRUE ) {
+                       miblist = strdup (DEFAULT_MIBLIST);
+               }else{
+                       miblist = "''";                 /* don't read any mib files for numeric oids */
+               }
+       }
+
 
        /* Need better checks to verify seclevel and authproto choices */
        
@@ -875,6 +895,8 @@ Check status of remote machines and obtain sustem information via SNMP\n\n"));
 
        /* SNMP and Authentication Protocol */
        printf (_("\
+ -n, --next\n\
+    Use SNMP GETNEXT instead of SNMP GET\n\
  -P, --protocol=[1|2c|3]\n\
     SNMP protocol version\n\
  -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
@@ -897,9 +919,11 @@ Check status of remote machines and obtain sustem information via SNMP\n\n"));
        /* OID Stuff */
        printf (_("\
  -o, --oid=OID(s)\n\
-    Object identifier(s) whose value you wish to query\n\
+    Object identifier(s) or SNMP variables whose value you wish to query\n\
  -m, --miblist=STRING\n\
-    List of MIBS to be loaded (default = ALL)\n -d, --delimiter=STRING\n\
+    List of MIBS to be loaded (default = none if using numeric oids or 'ALL'\n\
+               for symbolic oids.)\n\
+ -d, --delimiter=STRING\n\
     Delimiter to use when parsing returned data. Default is \"%s\"\n\
     Any data on the right hand side of the delimiter is considered\n\
     to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);