Ad Widget

Collapse

SNMP Builder for Zabbix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elvar
    Senior Member
    • Feb 2008
    • 226

    #76
    Originally posted by elvar
    This fixes my problem with being able to load the HP mibs but I'm not able to actually pull any data with any of the HP mibs. I also tried loading up Fortinet mibs for Fortigate firewalls and I'm not able to get any data using those either. When you tried the HP mibs, did you actually test them on any HP devices by chance? For example, if I load up the HP-MEMPROC-MIB I get nothing returned for anything HP specific. Not sure if I'm doing something wrong but I've tried every possible option under the HP specific sections.

    Thanks for your continued work on this addon, I appreciate it!

    Interestingly enough I was able to get the Fortigate mibs to return data after I changed the community string from '40G^te' to 'public'. Is there issues with those characters from the first community string? If so, is there a way for you to modify your code to support those characters?


    Regards,

    Comment

    • giapnguyen
      Member
      • Aug 2009
      • 33

      #77
      Originally posted by elvar
      Interestingly enough I was able to get the Fortigate mibs to return data after I changed the community string from '40G^te' to 'public'. Is there issues with those characters from the first community string? If so, is there a way for you to modify your code to support those characters?


      Regards,
      escape parameters before build shell command will help.

      Comment

      • jhgrc
        Member
        • Jun 2009
        • 52

        #78
        I added a little debugging to snmp_builder.php and also added -LE 2 (do not log message below certain priority level")

        I inserted debug to all snmpcommands (snmpget & snmptranslate)
        Code:
        file_put_contents('/tmp/snmpbuilder.log', "<!-- $cmd -->\n", FILE_APPEND);
        and from log lines I can now see the commands. I think the problem causing command is

        <!-- snmpget -LE 2 -v 2c -c public -M /var/www/zabbix/snmp_builder/mibs:/usr/share/snmp/mibs -m ALL 127.0.0.1 cgspLinkState.""TSF-INSTANCE"."TO-TSS-ITP".0" -->

        It returns when ran on command line without "-LE 2"
        Code:
        cgspLinkState.TSF-INSTANCE.TO-TSS-ITP.0: Unknown Object Identifier (Index out of range: TSF-INSTANCE (cgspInstNetwork))
        Well there is such an item on SNMP tree as it was displayed by SNMP_BUILDER on oid view/tree.

        If I may suggest for SNMP_BUILDER it would be much easier to have table indexes (oidtree etc) in numerical OID-format. Those will not lie and have conversion problems. For user value text you can convert them to human readable, but maintain all internally with numeric. Also add Zabbix items with numbers.

        As there might be missmatch on mib-files, and with mib files you need extra cpu for extra conversion.

        Comment

        • giapnguyen
          Member
          • Aug 2009
          • 33

          #79
          Originally posted by jhgrc
          I added a little debugging to snmp_builder.php and also added -LE 2 (do not log message below certain priority level")

          I inserted debug to all snmpcommands (snmpget & snmptranslate)
          Code:
          file_put_contents('/tmp/snmpbuilder.log', "<!-- $cmd -->\n", FILE_APPEND);
          and from log lines I can now see the commands. I think the problem causing command is

          <!-- snmpget -LE 2 -v 2c -c public -M /var/www/zabbix/snmp_builder/mibs:/usr/share/snmp/mibs -m ALL 127.0.0.1 cgspLinkState.""TSF-INSTANCE"."TO-TSS-ITP".0" -->

          It returns when ran on command line without "-LE 2"
          Code:
          cgspLinkState.TSF-INSTANCE.TO-TSS-ITP.0: Unknown Object Identifier (Index out of range: TSF-INSTANCE (cgspInstNetwork))
          Well there is such an item on SNMP tree as it was displayed by SNMP_BUILDER on oid view/tree.

          If I may suggest for SNMP_BUILDER it would be much easier to have table indexes (oidtree etc) in numerical OID-format. Those will not lie and have conversion problems. For user value text you can convert them to human readable, but maintain all internally with numeric. Also add Zabbix items with numbers.

          As there might be missmatch on mib-files, and with mib files you need extra cpu for extra conversion.
          I really understand the problem, index value with double quote will broke shell command. Could you try to escape the command to fix it (in sell command)?

          But main problem is snmptable (from NET-SNMP) may have issue that get weird index values and it out of my scope. I'm considering write my own snmptable.

          Comment

          • jhgrc
            Member
            • Jun 2009
            • 52

            #80
            If net-snmp snmptable is a 'failure', could following code help from

            Return all objects including their respective object ID within the specified one


            Code:
            <?php
                $a = snmptable("10.1.1.1", "public", ".1.3.6.1.2.1.4.21") or die("error");
                print_r($a);
                $a = snmptable("10.1.1.1", "public", ".1.3.6.1.2.1.2.2") or die("error");
                print_r($a);
            
            function snmptable($host, $community, $oid) {
                // TODO: get original state and restore at bottom
                snmp_set_oid_numeric_print(TRUE);
                snmp_set_quick_print(TRUE);
                snmp_set_enum_print(TRUE);
            
                $retval = array();
                $raw = snmprealwalk($host, $community, $oid) or die("snmptable: unable to walk OID $oid");
            
                $prefix_length = 0;
            
                foreach ($raw as $key => $value) {
                    if ($prefix_length == 0) {
                        // don't just use $oid's length since it may be non-numeric
                        $prefix_elements = count(explode('.',$oid));
                        $tmp = '.' . strtok($key, '.');
                        while ($prefix_elements > 1) {
                            $tmp .= '.' . strtok('.');
                            $prefix_elements--;
                        }
                        $tmp .= '.';
                        $prefix_length = strlen($tmp);
                    }
                    $key = substr($key, $prefix_length);
                    $index = explode('.', $key, 2);
                    isset($retval[$index[1]]) or $retval[$index[1]] = array();
                    isset($firstrow) or $firstrow = $index[1];
                    $retval[$index[1]][$index[0]] = $value;
                }
            
                // check for holes in the table and fill them in
                foreach ($retval[$firstrow] as $key => $tmp) {
                    foreach($retval as $check => $tmp2) {
                        if (! isset($retval[$check][$key])) {
                            $retval[$check][$key] = '';
                        }
                    }
                }
            
                return($retval);
            }
            ?>
            Above code should use snmp v2 function (snmp2_real_walk) - I think it should he achieved with a simple fix..

            Comment

            • jhgrc
              Member
              • Jun 2009
              • 52

              #81
              Originally posted by giapnguyen
              I really understand the problem, index value with double quote will broke shell command. Could you try to escape the command to fix it (in sell command)?
              Hooray, with quoting it works..

              snmpget -On -v 2c -c public -M /var/www/zabbix/snmp_builder/mibs:/usr/share/snmp/mibs -m ALL 127.0.0.1 cgspLinkState.\"TSF-INSTANCE\".\"TO-TSS-ITP\".0
              .1.3.6.1.4.1.9.9.336.1.6.1.1.3.12.84.83.70.45.73.7 8.83.84.65.78.67.69.10.84.79.45.84.83.83.45.73.84. 80.0 = INTEGER: available(1)

              But note, the original command had double double quotes, with those above fails.

              Original index was: ""TSF-IN ... ITP".0"
              Last edited by jhgrc; 24-02-2010, 08:56.

              Comment

              • jhgrc
                Member
                • Jun 2009
                • 52

                #82
                Here is a quick fix for my case. But does it break something else?

                Code:
                // idx is number or string thank danrog
                if (preg_match('/^[0-9]+$/', $idx)) {
                $cmd = "snmpget -LE 2 -v 2c -c $community -M ".MIBS_ALL_PATH." -m ALL $server_ip $oid.$idx";
                    } else {
                #$cmd = "snmpget -LE 2 -v 2c -c $community -M ".MIBS_ALL_PATH." -m ALL $server_ip $oid.\"".$idx."\"";
                $cmd = "snmpget -LE 2 -v 2c -c $community -M ".MIBS_ALL_PATH." -m ALL $server_ip $oid.$idx";
                    }
                $cmd = preg_replace('/"/','\\\\"',$cmd);
                file_put_contents('/tmp/snmpbuilder.log', "<!-- $cmd -->\n", FILE_APPEND);
                echo "<!-- $cmd -->\n";
                $results = exec($cmd);
                - I removed the double quotes in original non-numeric-oid snmpget
                - Replaced all found double quotes with escaped ones
                - Some debugging (file & generated html-code)

                Comment

                • giapnguyen
                  Member
                  • Aug 2009
                  • 33

                  #83
                  Originally posted by jhgrc
                  Here is a quick fix for my case. But does it break something else?

                  Code:
                  // idx is number or string thank danrog
                  if (preg_match('/^[0-9]+$/', $idx)) {
                  $cmd = "snmpget -LE 2 -v 2c -c $community -M ".MIBS_ALL_PATH." -m ALL $server_ip $oid.$idx";
                      } else {
                  #$cmd = "snmpget -LE 2 -v 2c -c $community -M ".MIBS_ALL_PATH." -m ALL $server_ip $oid.\"".$idx."\"";
                  $cmd = "snmpget -LE 2 -v 2c -c $community -M ".MIBS_ALL_PATH." -m ALL $server_ip $oid.$idx";
                      }
                  $cmd = preg_replace('/"/','\\\\"',$cmd);
                  file_put_contents('/tmp/snmpbuilder.log', "<!-- $cmd -->\n", FILE_APPEND);
                  echo "<!-- $cmd -->\n";
                  $results = exec($cmd);
                  - I removed the double quotes in original non-numeric-oid snmpget
                  - Replaced all found double quotes with escaped ones
                  - Some debugging (file & generated html-code)
                  That's great, but snmptable definitely have an issue to give correct snmp values. I look forward your code to integrate new version. In fact, snmptable is a series of snmpwalk, I think.

                  Comment

                  • richlv
                    Senior Member
                    Zabbix Certified Trainer
                    Zabbix Certified SpecialistZabbix Certified Professional
                    • Oct 2005
                    • 3112

                    #84
                    Originally posted by giapnguyen
                    That's great, but snmptable definitely have an issue to give correct snmp values. I look forward your code to integrate new version. In fact, snmptable is a series of snmpwalk, I think.
                    snmptable uses snmp getnext or getbulk requests, at least according to manpage (so essentially it uses the same mechanism as snmpwalk).

                    if you have problems with snmptable, i'd suggest taking them to net-snmp developers and working with fixing problems upstream instead of reinventing the wheel
                    Zabbix 3.0 Network Monitoring book

                    Comment

                    • giapnguyen
                      Member
                      • Aug 2009
                      • 33

                      #85
                      Originally posted by richlv
                      snmptable uses snmp getnext or getbulk requests, at least according to manpage (so essentially it uses the same mechanism as snmpwalk).

                      if you have problems with snmptable, i'd suggest taking them to net-snmp developers and working with fixing problems upstream instead of reinventing the wheel
                      That's reason, i'm considering report net-snmp issue or write new one. Indexes is one of issue, snmptable try to convert all dot-type indexes into string but in some cases, it give mean-less strings (non printable characters).

                      Comment

                      • richlv
                        Senior Member
                        Zabbix Certified Trainer
                        Zabbix Certified SpecialistZabbix Certified Professional
                        • Oct 2005
                        • 3112

                        #86
                        Originally posted by giapnguyen
                        That's reason, i'm considering report net-snmp issue or write new one. Indexes is one of issue, snmptable try to convert all dot-type indexes into string but in some cases, it give mean-less strings (non printable characters).
                        sorry for being ignorant here - i haven't followed whole thread - but doesn't "-On" help any ?
                        Zabbix 3.0 Network Monitoring book

                        Comment

                        • danrog
                          Senior Member
                          • Sep 2009
                          • 164

                          #87
                          Originally posted by jhgrc
                          Here is a quick fix for my case. But does it break something else?

                          Code:
                          // idx is number or string thank danrog
                          if (preg_match('/^[0-9]+$/', $idx)) {
                          $cmd = "snmpget -LE 2 -v 2c -c $community -M ".MIBS_ALL_PATH." -m ALL $server_ip $oid.$idx";
                              } else {
                          #$cmd = "snmpget -LE 2 -v 2c -c $community -M ".MIBS_ALL_PATH." -m ALL $server_ip $oid.\"".$idx."\"";
                          $cmd = "snmpget -LE 2 -v 2c -c $community -M ".MIBS_ALL_PATH." -m ALL $server_ip $oid.$idx";
                              }
                          $cmd = preg_replace('/"/','\\\\"',$cmd);
                          file_put_contents('/tmp/snmpbuilder.log', "<!-- $cmd -->\n", FILE_APPEND);
                          echo "<!-- $cmd -->\n";
                          $results = exec($cmd);
                          - I removed the double quotes in original non-numeric-oid snmpget
                          - Replaced all found double quotes with escaped ones
                          - Some debugging (file & generated html-code)
                          I ran into this too and if I remember correctly, this wasn't enough to fix it completely (at least for me). I didn't post any of my other attempts to fix this as nothing has worked thus far. snmptranslate is translating the string based OID just fine, however, I still cannot add it to the template (\ and " issues I think). Also, I was using the php function addslashes($var) in another part of snmp_builder due to my other problems I just didn't add that to my original post.

                          Comment

                          • jhgrc
                            Member
                            • Jun 2009
                            • 52

                            #88
                            Well I finally got energy to fix the rest of snmp_builder as I needed to add some devices to monitoring. The fix was quite similar to previous fix, escape " characters for command line

                            So you need similar fix to "get_oid_from_name", I attached the full method. But you can easily just add the $name=preg_replace...

                            Code:
                            function get_oid_from_name($name)
                            {
                            $name = preg_replace('/"/','\\\\"',$name);
                            file_put_contents('/tmp/snmpbuilder.log', "<!-- snmptranslate -LE 1 -M -m ALL -On $name -->\n", FILE_APPEND);
                            echo "<!-- snmptranslate -LE 1 -M -m ALL -On $name -->\n";
                            $oid = exec("snmptranslate -LE 1 -M ".MIBS_ALL_PATH." -m ALL -On $name");
                            
                            if (preg_match('/[0123456789\.]+/', $oid))
                            return $oid;
                            else
                            return null;
                            }
                            Now I am able to save keys to Zabbix items. And snmp_builder.php works at least in my case. I have not used it to add router interfaces, but I guess those should work as I've just added escape characters to shell commands

                            " => \\" (but PHP requires " => \\\\")

                            Comment

                            • Peteris
                              Member
                              • Feb 2010
                              • 89

                              #89
                              Has anybody got a clue why i get this error:

                              Cannot find module (SNMPv2-SMI): At line 8 in /usr/share/snmp/mibs/SYMANTEC-EMAIL-SECURITY.mib

                              Comment

                              • jhgrc
                                Member
                                • Jun 2009
                                • 52

                                #90
                                Originally posted by Peteris
                                Has anybody got a clue why i get this error:

                                Cannot find module (SNMPv2-SMI): At line 8 in /usr/share/snmp/mibs/SYMANTEC-EMAIL-SECURITY.mib
                                Sure. Symantec MIB depends on SNMPv2-SMI MIB. And SNMPv2-SMI mib is not found on path. Search it from the net, and save it somewhere in SNMP MIB PATH. For example the ones you have stored in snmp_builder.php.

                                Example paths are:
                                ../snmp_builder/mibs/
                                /usr/share/snmp/mibs/

                                Comment

                                Working...