summaryrefslogtreecommitdiffstats
path: root/plugins/check_snmp.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_snmp.c')
-rw-r--r--plugins/check_snmp.c211
1 files changed, 106 insertions, 105 deletions
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c
index 56a586a..90a0402 100644
--- a/plugins/check_snmp.c
+++ b/plugins/check_snmp.c
@@ -55,8 +55,6 @@ const char *email = "devel@monitoring-plugins.org";
55#define CRIT_STRING 2 55#define CRIT_STRING 2
56#define CRIT_REGEX 4 56#define CRIT_REGEX 4
57#define WARN_PRESENT 8 57#define WARN_PRESENT 8
58#define WARN_STRING 16
59#define WARN_REGEX 32
60 58
61#define OID_COUNT_STEP 8 59#define OID_COUNT_STEP 8
62 60
@@ -86,82 +84,82 @@ const char *email = "devel@monitoring-plugins.org";
86 84
87 85
88 86
89int process_arguments (int, char **); 87static int process_arguments (int, char **);
90int validate_arguments (void); 88static int validate_arguments (void);
91char *thisarg (char *str); 89static char *thisarg (char *str);
92char *nextarg (char *str); 90static char *nextarg (char *str);
93void print_usage (void); 91void print_usage (void);
94void print_help (void); 92static void print_help (void);
95char *multiply (char *str); 93static char *multiply (char *str);
96 94
97#include "regex.h" 95#include "regex.h"
98char regex_expect[MAX_INPUT_BUFFER] = ""; 96static char regex_expect[MAX_INPUT_BUFFER] = "";
99regex_t preg; 97static regex_t preg;
100regmatch_t pmatch[10]; 98static regmatch_t pmatch[10];
101char errbuf[MAX_INPUT_BUFFER] = ""; 99static char errbuf[MAX_INPUT_BUFFER] = "";
102char perfstr[MAX_INPUT_BUFFER] = "| "; 100static char perfstr[MAX_INPUT_BUFFER] = "| ";
103int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; 101static int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
104int eflags = 0; 102static int eflags = 0;
105int errcode, excode; 103static int errcode, excode;
106 104
107char *server_address = NULL; 105static char *server_address = NULL;
108char *community = NULL; 106static char *community = NULL;
109char **contextargs = NULL; 107static char **contextargs = NULL;
110char *context = NULL; 108static char *context = NULL;
111char **authpriv = NULL; 109static char **authpriv = NULL;
112char *proto = NULL; 110static char *proto = NULL;
113char *seclevel = NULL; 111static char *seclevel = NULL;
114char *secname = NULL; 112static char *secname = NULL;
115char *authproto = NULL; 113static char *authproto = NULL;
116char *privproto = NULL; 114static char *privproto = NULL;
117char *authpasswd = NULL; 115static char *authpasswd = NULL;
118char *privpasswd = NULL; 116static char *privpasswd = NULL;
119int nulloid = STATE_UNKNOWN; 117static int nulloid = STATE_UNKNOWN;
120char **oids = NULL; 118static char **oids = NULL;
121size_t oids_size = 0; 119static size_t oids_size = 0;
122char *label; 120static char *label;
123char *units; 121static char *units;
124char *port; 122static char *port;
125char *snmpcmd; 123static char *snmpcmd;
126char string_value[MAX_INPUT_BUFFER] = ""; 124static char string_value[MAX_INPUT_BUFFER] = "";
127int invert_search=0; 125static int invert_search=0;
128char **labels = NULL; 126static char **labels = NULL;
129char **unitv = NULL; 127static char **unitv = NULL;
130size_t nlabels = 0; 128static size_t nlabels = 0;
131size_t labels_size = OID_COUNT_STEP; 129static size_t labels_size = OID_COUNT_STEP;
132size_t nunits = 0; 130static size_t nunits = 0;
133size_t unitv_size = OID_COUNT_STEP; 131static size_t unitv_size = OID_COUNT_STEP;
134int numoids = 0; 132static size_t numoids = 0;
135int numauthpriv = 0; 133static int numauthpriv = 0;
136int numcontext = 0; 134static int numcontext = 0;
137int verbose = 0; 135static int verbose = 0;
138int usesnmpgetnext = FALSE; 136static bool usesnmpgetnext = false;
139char *warning_thresholds = NULL; 137static char *warning_thresholds = NULL;
140char *critical_thresholds = NULL; 138static char *critical_thresholds = NULL;
141thresholds **thlds; 139static thresholds **thlds;
142size_t thlds_size = OID_COUNT_STEP; 140static size_t thlds_size = OID_COUNT_STEP;
143double *response_value; 141static double *response_value;
144size_t response_size = OID_COUNT_STEP; 142static size_t response_size = OID_COUNT_STEP;
145int retries = 0; 143static int retries = 0;
146int *eval_method; 144static int *eval_method;
147size_t eval_size = OID_COUNT_STEP; 145static size_t eval_size = OID_COUNT_STEP;
148char *delimiter; 146static char *delimiter;
149char *output_delim; 147static char *output_delim;
150char *miblist = NULL; 148static char *miblist = NULL;
151int needmibs = FALSE; 149static bool needmibs = false;
152int calculate_rate = 0; 150static int calculate_rate = 0;
153double offset = 0.0; 151static double offset = 0.0;
154int rate_multiplier = 1; 152static int rate_multiplier = 1;
155state_data *previous_state; 153static state_data *previous_state;
156double *previous_value; 154static double *previous_value;
157size_t previous_size = OID_COUNT_STEP; 155static size_t previous_size = OID_COUNT_STEP;
158int perf_labels = 1; 156static int perf_labels = 1;
159char* ip_version = ""; 157static char* ip_version = "";
160double multiplier = 1.0; 158static double multiplier = 1.0;
161char *fmtstr = ""; 159static char *fmtstr = "";
162bool fmtstr_set = false; 160static bool fmtstr_set = false;
163char buffer[DEFAULT_BUFFER_SIZE]; 161static char buffer[DEFAULT_BUFFER_SIZE];
164bool ignore_mib_parsing_errors = false; 162static bool ignore_mib_parsing_errors = false;
165 163
166static char *fix_snmp_range(char *th) 164static char *fix_snmp_range(char *th)
167{ 165{
@@ -187,7 +185,8 @@ static char *fix_snmp_range(char *th)
187int 185int
188main (int argc, char **argv) 186main (int argc, char **argv)
189{ 187{
190 int i, len, line, total_oids; 188 int len, total_oids;
189 size_t line;
191 unsigned int bk_count = 0, dq_count = 0; 190 unsigned int bk_count = 0, dq_count = 0;
192 int iresult = STATE_UNKNOWN; 191 int iresult = STATE_UNKNOWN;
193 int result = STATE_UNKNOWN; 192 int result = STATE_UNKNOWN;
@@ -253,14 +252,16 @@ main (int argc, char **argv)
253 if(calculate_rate) { 252 if(calculate_rate) {
254 if (!strcmp(label, "SNMP")) 253 if (!strcmp(label, "SNMP"))
255 label = strdup("SNMP RATE"); 254 label = strdup("SNMP RATE");
256 i=0; 255
256 size_t i = 0;
257
257 previous_state = np_state_read(); 258 previous_state = np_state_read();
258 if(previous_state!=NULL) { 259 if(previous_state!=NULL) {
259 /* Split colon separated values */ 260 /* Split colon separated values */
260 previous_string = strdup((char *) previous_state->data); 261 previous_string = strdup((char *) previous_state->data);
261 while((ap = strsep(&previous_string, ":")) != NULL) { 262 while((ap = strsep(&previous_string, ":")) != NULL) {
262 if(verbose>2) 263 if(verbose>2)
263 printf("State for %d=%s\n", i, ap); 264 printf("State for %zd=%s\n", i, ap);
264 while (i >= previous_size) { 265 while (i >= previous_size) {
265 previous_size += OID_COUNT_STEP; 266 previous_size += OID_COUNT_STEP;
266 previous_value = realloc(previous_value, previous_size * sizeof(*previous_value)); 267 previous_value = realloc(previous_value, previous_size * sizeof(*previous_value));
@@ -273,7 +274,7 @@ main (int argc, char **argv)
273 /* Populate the thresholds */ 274 /* Populate the thresholds */
274 th_warn=warning_thresholds; 275 th_warn=warning_thresholds;
275 th_crit=critical_thresholds; 276 th_crit=critical_thresholds;
276 for (i=0; i<numoids; i++) { 277 for (size_t i = 0; i < numoids; i++) {
277 char *w = th_warn ? strndup(th_warn, strcspn(th_warn, ",")) : NULL; 278 char *w = th_warn ? strndup(th_warn, strcspn(th_warn, ",")) : NULL;
278 char *c = th_crit ? strndup(th_crit, strcspn(th_crit, ",")) : NULL; 279 char *c = th_crit ? strndup(th_crit, strcspn(th_crit, ",")) : NULL;
279 /* translate "2:1" to "@1:2" for backwards compatibility */ 280 /* translate "2:1" to "@1:2" for backwards compatibility */
@@ -302,7 +303,7 @@ main (int argc, char **argv)
302 } 303 }
303 304
304 /* Create the command array to execute */ 305 /* Create the command array to execute */
305 if(usesnmpgetnext == TRUE) { 306 if(usesnmpgetnext) {
306 snmpcmd = strdup (PATH_TO_SNMPGETNEXT); 307 snmpcmd = strdup (PATH_TO_SNMPGETNEXT);
307 }else{ 308 }else{
308 snmpcmd = strdup (PATH_TO_SNMPGET); 309 snmpcmd = strdup (PATH_TO_SNMPGET);
@@ -333,11 +334,11 @@ main (int argc, char **argv)
333 } 334 }
334 335
335 336
336 for (i = 0; i < numcontext; i++) { 337 for (int i = 0; i < numcontext; i++) {
337 command_line[index++] = contextargs[i]; 338 command_line[index++] = contextargs[i];
338 } 339 }
339 340
340 for (i = 0; i < numauthpriv; i++) { 341 for (int i = 0; i < numauthpriv; i++) {
341 command_line[index++] = authpriv[i]; 342 command_line[index++] = authpriv[i];
342 } 343 }
343 344
@@ -348,7 +349,7 @@ main (int argc, char **argv)
348 server_address, 349 server_address,
349 port); 350 port);
350 351
351 for (i = 0; i < numoids; i++) { 352 for (size_t i = 0; i < numoids; i++) {
352 command_line[index++] = oids[i]; 353 command_line[index++] = oids[i];
353 xasprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]); 354 xasprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]);
354 } 355 }
@@ -382,7 +383,7 @@ main (int argc, char **argv)
382 if (external_error) { 383 if (external_error) {
383 if (chld_err.lines > 0) { 384 if (chld_err.lines > 0) {
384 printf (_("External command error: %s\n"), chld_err.line[0]); 385 printf (_("External command error: %s\n"), chld_err.line[0]);
385 for (i = 1; i < chld_err.lines; i++) { 386 for (size_t i = 1; i < chld_err.lines; i++) {
386 printf ("%s\n", chld_err.line[i]); 387 printf ("%s\n", chld_err.line[i]);
387 } 388 }
388 } else { 389 } else {
@@ -392,12 +393,14 @@ main (int argc, char **argv)
392 } 393 }
393 394
394 if (verbose) { 395 if (verbose) {
395 for (i = 0; i < chld_out.lines; i++) { 396 for (size_t i = 0; i < chld_out.lines; i++) {
396 printf ("%s\n", chld_out.line[i]); 397 printf ("%s\n", chld_out.line[i]);
397 } 398 }
398 } 399 }
399 400
400 for (line=0, i=0; line < chld_out.lines && i < numoids ; line++, i++) { 401 line = 0;
402 total_oids = 0;
403 for (size_t i = 0; line < chld_out.lines && i < numoids ; line++, i++, total_oids++) {
401 if(calculate_rate) 404 if(calculate_rate)
402 conv = "%.10g"; 405 conv = "%.10g";
403 else 406 else
@@ -410,7 +413,7 @@ main (int argc, char **argv)
410 break; 413 break;
411 414
412 if (verbose > 2) { 415 if (verbose > 2) {
413 printf("Processing oid %i (line %i)\n oidname: %s\n response: %s\n", i+1, line+1, oidname, response); 416 printf("Processing oid %zi (line %zi)\n oidname: %s\n response: %s\n", i+1, line+1, oidname, response);
414 } 417 }
415 418
416 /* Clean up type array - Sol10 does not necessarily zero it out */ 419 /* Clean up type array - Sol10 does not necessarily zero it out */
@@ -634,7 +637,6 @@ main (int argc, char **argv)
634 strncat(perfstr, " ", sizeof(perfstr)-strlen(perfstr)-1); 637 strncat(perfstr, " ", sizeof(perfstr)-strlen(perfstr)-1);
635 } 638 }
636 } 639 }
637 total_oids=i;
638 640
639 /* Save state data, as all data collected now */ 641 /* Save state data, as all data collected now */
640 if(calculate_rate) { 642 if(calculate_rate) {
@@ -644,7 +646,7 @@ main (int argc, char **argv)
644 die(STATE_UNKNOWN, _("Cannot malloc")); 646 die(STATE_UNKNOWN, _("Cannot malloc"));
645 647
646 current_length=0; 648 current_length=0;
647 for(i=0; i<total_oids; i++) { 649 for(int i = 0; i < total_oids; i++) {
648 xasprintf(&temp_string,"%.0f",response_value[i]); 650 xasprintf(&temp_string,"%.0f",response_value[i]);
649 if(temp_string==NULL) 651 if(temp_string==NULL)
650 die(STATE_UNKNOWN,_("Cannot asprintf()")); 652 die(STATE_UNKNOWN,_("Cannot asprintf()"));
@@ -687,7 +689,7 @@ process_arguments (int argc, char **argv)
687{ 689{
688 char *ptr; 690 char *ptr;
689 int c = 1; 691 int c = 1;
690 int j = 0, jj = 0, ii = 0; 692 size_t j = 0, jj = 0;
691 693
692 int option = 0; 694 int option = 0;
693 static struct option longopts[] = { 695 static struct option longopts[] = {
@@ -777,7 +779,7 @@ process_arguments (int argc, char **argv)
777 miblist = optarg; 779 miblist = optarg;
778 break; 780 break;
779 case 'n': /* usesnmpgetnext */ 781 case 'n': /* usesnmpgetnext */
780 usesnmpgetnext = TRUE; 782 usesnmpgetnext = true;
781 break; 783 break;
782 case 'P': /* SNMP protocol version */ 784 case 'P': /* SNMP protocol version */
783 proto = optarg; 785 proto = optarg;
@@ -831,7 +833,7 @@ process_arguments (int argc, char **argv)
831 * so we have a mib variable, rather than just an SNMP OID, 833 * so we have a mib variable, rather than just an SNMP OID,
832 * so we have to actually read the mib files 834 * so we have to actually read the mib files
833 */ 835 */
834 needmibs = TRUE; 836 needmibs = true;
835 } 837 }
836 for (ptr = strtok(optarg, ", "); ptr != NULL; ptr = strtok(NULL, ", "), j++) { 838 for (ptr = strtok(optarg, ", "); ptr != NULL; ptr = strtok(NULL, ", "), j++) {
837 while (j >= oids_size) { 839 while (j >= oids_size) {
@@ -843,7 +845,6 @@ process_arguments (int argc, char **argv)
843 numoids = j; 845 numoids = j;
844 if (c == 'E' || c == 'e') { 846 if (c == 'E' || c == 'e') {
845 jj++; 847 jj++;
846 ii++;
847 while (j+1 >= eval_size) { 848 while (j+1 >= eval_size) {
848 eval_size += OID_COUNT_STEP; 849 eval_size += OID_COUNT_STEP;
849 eval_method = realloc(eval_method, eval_size * sizeof(*eval_method)); 850 eval_method = realloc(eval_method, eval_size * sizeof(*eval_method));
@@ -870,7 +871,6 @@ process_arguments (int argc, char **argv)
870 memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8); 871 memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8);
871 } 872 }
872 eval_method[jj++] = CRIT_STRING; 873 eval_method[jj++] = CRIT_STRING;
873 ii++;
874 break; 874 break;
875 case 'R': /* regex */ 875 case 'R': /* regex */
876 cflags = REG_ICASE; 876 cflags = REG_ICASE;
@@ -891,7 +891,6 @@ process_arguments (int argc, char **argv)
891 memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8); 891 memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8);
892 } 892 }
893 eval_method[jj++] = CRIT_REGEX; 893 eval_method[jj++] = CRIT_REGEX;
894 ii++;
895 break; 894 break;
896 895
897 /* Format */ 896 /* Format */
@@ -1029,12 +1028,12 @@ selected.</para>
1029 1028
1030 1029
1031 1030
1032int 1031static int
1033validate_arguments () 1032validate_arguments ()
1034{ 1033{
1035 /* check whether to load locally installed MIBS (CPU/disk intensive) */ 1034 /* check whether to load locally installed MIBS (CPU/disk intensive) */
1036 if (miblist == NULL) { 1035 if (miblist == NULL) {
1037 if ( needmibs == TRUE ) { 1036 if (needmibs) {
1038 miblist = strdup (DEFAULT_MIBLIST); 1037 miblist = strdup (DEFAULT_MIBLIST);
1039 }else{ 1038 }else{
1040 miblist = ""; /* don't read any mib files for numeric oids */ 1039 miblist = ""; /* don't read any mib files for numeric oids */
@@ -1138,7 +1137,7 @@ validate_arguments ()
1138/* trim leading whitespace 1137/* trim leading whitespace
1139 if there is a leading quote, make sure it balances */ 1138 if there is a leading quote, make sure it balances */
1140 1139
1141char * 1140static char *
1142thisarg (char *str) 1141thisarg (char *str)
1143{ 1142{
1144 str += strspn (str, " \t\r\n"); /* trim any leading whitespace */ 1143 str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
@@ -1155,7 +1154,7 @@ thisarg (char *str)
1155 set the trailing quote to '\x0' 1154 set the trailing quote to '\x0'
1156 if the string continues, advance beyond the comma */ 1155 if the string continues, advance beyond the comma */
1157 1156
1158char * 1157static char *
1159nextarg (char *str) 1158nextarg (char *str)
1160{ 1159{
1161 if (str[0] == '\'') { 1160 if (str[0] == '\'') {
@@ -1187,7 +1186,7 @@ nextarg (char *str)
1187 1186
1188 1187
1189/* multiply result (values 0 < n < 1 work as divider) */ 1188/* multiply result (values 0 < n < 1 work as divider) */
1190char * 1189static char *
1191multiply (char *str) 1190multiply (char *str)
1192{ 1191{
1193 char *endptr; 1192 char *endptr;
@@ -1224,7 +1223,7 @@ multiply (char *str)
1224} 1223}
1225 1224
1226 1225
1227void 1226static void
1228print_help (void) 1227print_help (void)
1229{ 1228{
1230 print_revision (progname, NP_VERSION); 1229 print_revision (progname, NP_VERSION);
@@ -1252,10 +1251,12 @@ print_help (void)
1252 printf (" %s\n", _("SNMPv3 context")); 1251 printf (" %s\n", _("SNMPv3 context"));
1253 printf (" %s\n", "-L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]"); 1252 printf (" %s\n", "-L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]");
1254 printf (" %s\n", _("SNMPv3 securityLevel")); 1253 printf (" %s\n", _("SNMPv3 securityLevel"));
1255 printf (" %s\n", "-a, --authproto=[MD5|SHA]"); 1254 printf (" %s\n", "-a, --authproto=AUTHENTICATION_PROTOCOL");
1256 printf (" %s\n", _("SNMPv3 auth proto")); 1255 printf (" %s\n", _("SNMPv3 authentication protocol (default MD5), available options depend on the specific version of the net-snmp tools"));
1257 printf (" %s\n", "-x, --privproto=[DES|AES]"); 1256 printf (" %s\n", _("if < 5.8 SHA (1) and MD5 should be available, if >= 5.8 additionally SHA-224, SHA-256, SHA-384 and SHA-512"));
1258 printf (" %s\n", _("SNMPv3 priv proto (default DES)")); 1257 printf (" %s\n", "-x, --privproto=PRIVACY_PROTOCOL");
1258 printf (" %s\n", _("SNMPv3 privacy protocol (default DES), available options depend on the specific version of the net-snmp tools"));
1259 printf (" %s\n", _("if < 5.8 DES and AES should be available, if >= 5.8 additionally AES-192 and AES-256"));
1259 1260
1260 /* Authentication Tokens*/ 1261 /* Authentication Tokens*/
1261 printf (" %s\n", "-C, --community=STRING"); 1262 printf (" %s\n", "-C, --community=STRING");