Code

Attempt at fixing check_snmp multiline output:
authorThomas Guyot-Sionnest <dermoth@aei.ca>
Thu, 17 Sep 2009 04:49:56 +0000 (00:49 -0400)
committerThomas Guyot-Sionnest <dermoth@aei.ca>
Thu, 22 Apr 2010 02:04:28 +0000 (22:04 -0400)
This patch tries to detect and output nicely multi-line strings. This
method is broken by design; only a count of double-quotes and escapes
could work in every situation.

plugins/check_snmp.c
plugins/tests/check_snmp_agent.pl

index f04ca85fa2e9fb76f93b8550495c680909b55c50..19f78d03987c9d1a911dc2193cc9091117fcac62 100644 (file)
@@ -117,7 +117,7 @@ int needmibs = FALSE;
 int
 main (int argc, char **argv)
 {
 int
 main (int argc, char **argv)
 {
-       int i, len;
+       int i, len, line;
        int iresult = STATE_UNKNOWN;
        int result = STATE_UNKNOWN;
        int return_code = 0;
        int iresult = STATE_UNKNOWN;
        int result = STATE_UNKNOWN;
        int return_code = 0;
@@ -126,6 +126,7 @@ main (int argc, char **argv)
        char *cl_hidden_auth = NULL;
        char *oidname = NULL;
        char *response = NULL;
        char *cl_hidden_auth = NULL;
        char *oidname = NULL;
        char *response = NULL;
+       char *mult_resp = NULL;
        char *outbuff;
        char *ptr = NULL;
        char *show = NULL;
        char *outbuff;
        char *ptr = NULL;
        char *show = NULL;
@@ -249,10 +250,10 @@ main (int argc, char **argv)
                }
        }
 
                }
        }
 
-       for (i = 0; i < chld_out.lines; i++) {
+       for (line=0, i=0; line < chld_out.lines; line++, i++) {
                const char *conv = "%.0f";
 
                const char *conv = "%.0f";
 
-               ptr = chld_out.line[i];
+               ptr = chld_out.line[line];
                oidname = strpcpy (oidname, ptr, delimiter);
                response = strstr (ptr, delimiter);
                if (response == NULL)
                oidname = strpcpy (oidname, ptr, delimiter);
                response = strstr (ptr, delimiter);
                if (response == NULL)
@@ -283,6 +284,18 @@ main (int argc, char **argv)
                else if (strstr (response, "STRING: ")) {
                        show = strstr (response, "STRING: ") + 8;
                        conv = "%.10g";
                else if (strstr (response, "STRING: ")) {
                        show = strstr (response, "STRING: ") + 8;
                        conv = "%.10g";
+                       /* Get the rest of the string on multi-line strings */
+                       if (show[0] == '"' && (response[strlen(response)-1] != '\"' || response[strlen(response)-2] != '\\')) {
+                               /* Strip out unmatched double-quote */
+                               if (show[0] == '"') show++;
+                               if (!mult_resp) mult_resp = strdup("");
+                               asprintf (&mult_resp, "%s%s:\n%s\n", mult_resp, oids[i], strstr (response, "STRING: ") + 8);
+                               for (line++; line < chld_out.lines; line++) {
+                                       asprintf (&mult_resp, "%s%s\n", mult_resp, chld_out.line[line]);
+                                       if (mult_resp[strlen(mult_resp)-2] == '"' && response[strlen(response)-2] != '\\') break;
+                               }
+                       }
+
                }
                else if (strstr (response, "Timeticks: "))
                        show = strstr (response, "Timeticks: ");
                }
                else if (strstr (response, "Timeticks: "))
                        show = strstr (response, "Timeticks: ");
@@ -367,6 +380,7 @@ main (int argc, char **argv)
        }
 
        printf ("%s %s -%s %s \n", label, state_text (result), outbuff, perfstr);
        }
 
        printf ("%s %s -%s %s \n", label, state_text (result), outbuff, perfstr);
+       if (mult_resp) printf ("%s", mult_resp);
 
        return result;
 }
 
        return result;
 }
index d1ff6d00ca40d9754d462f5d401fbd4a7ec869d3..302ba6e7aa5fd585f42956084a43db8d7a46ab95 100644 (file)
@@ -18,15 +18,24 @@ if (!$agent) {
 my $baseoid = '.1.3.6.1.4.1.8072.3.2.67';
 # Next are arrays of indexes (Type, initial value and increments)
 # Undef miltipliers are randomized
 my $baseoid = '.1.3.6.1.4.1.8072.3.2.67';
 # Next are arrays of indexes (Type, initial value and increments)
 # Undef miltipliers are randomized
-my $multiline = "Cisco Internetwork Operating System Software
-IOS (tm) Catalyst 4000 L3 Switch Software (cat4000-I9K91S-M), Version
+my $multiline = 'Cisco Internetwork Operating System Software
+IOS (tm) Catalyst 4000 "L3" Switch Software (cat4000-I9K91S-M), Version
 12.2(20)EWA, RELEASE SOFTWARE (fc1)
 Technical Support: http://www.cisco.com/techsupport
 Copyright (c) 1986-2004 by cisco Systems, Inc.
 12.2(20)EWA, RELEASE SOFTWARE (fc1)
 Technical Support: http://www.cisco.com/techsupport
 Copyright (c) 1986-2004 by cisco Systems, Inc.
-";
-my @fields = (ASN_OCTET_STR, ASN_UNSIGNED, ASN_UNSIGNED, ASN_COUNTER, ASN_COUNTER64, ASN_UNSIGNED);
-my @values = ($multiline, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32)));
-my @incrts = (undef, 1000, -500, 1000, 100000, undef);
+';
+my $multilin2 = "Kisco Outernetwork Oserating Gystem Totware
+Copyleft (c) 2400-2689 by kisco Systrems, Inc.";
+my $multilin3 = 'This should not confuse check_snmp "parser"
+into thinking there is no 2nd line';
+my $multilin4 = 'It\'s getting even harder if the line
+ends with with this: C:\\';
+my $multilin5 = 'And now have fun with with this: "C:\\"
+because we\'re not done yet!';
+
+my @fields = (ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_UNSIGNED, ASN_UNSIGNED, ASN_COUNTER, ASN_COUNTER64, ASN_UNSIGNED);
+my @values = ($multiline, $multilin2, $multilin3, $multilin4, $multilin5, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32)));
+my @incrts = (undef, undef, undef, undef, undef, 1000, -500, 1000, 100000, undef);
 
 # Number of elements in our OID
 my $oidelts;
 
 # Number of elements in our OID
 my $oidelts;