Ad Widget

Collapse

Tool for bulkloading a template with snmpwalk data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Steven
    Junior Member
    • Mar 2005
    • 2

    #1

    Tool for bulkloading a template with snmpwalk data

    I just started testing out ZABBIX yesterday (1.1a6) and had a need to bulk load large amounts of snmpwalk data into ZABBIX templates using vendor specific MIBs. Manually adding hundreds of items for a template through the UI could take a while and I didn't notice a ZABBIX provided tool that did this. So, I wrote a simple loader. It works in my test environment for the equipment I'm using, but I suspect that it may need changes for other MIBs and environments. Please read the code for more details and use at your own risk as this hasn't been tested very much yet
    Also, I haven't bothered to write the snmptranslate in perl so '-m all' is quite expensive each time snmptranslate loads. It's best to just -m the MIBs you care about.

    Thanks,
    Steven

    Code:
    #!/usr/bin/perl
    ####################################
    #
    #       zload_snmpwalk
    #       Author: Steven Dossett
    #       Email: sd at panath.com
    #
    ####################################
    # Disclaimer:
    # This script has only been used with ZABBIX 1.1alpha6. It isn't thoroughly
    # tested. It isn't very efficient. It may eat your database or cause other
    # unintentional harm. Be careful and use at your own risk.
    # Please share bug fixes and improvements that you make...
    #
    # See usage instructions by running: zload_snmpwalk -h
    #
    # Description:
    # Creating new ZABBIX host templates can be time consuming. This script
    # automates some of the process by loading snmpwalk data into a ZABBIX
    # template. It is very important to use a MIB so that keys and descriptions
    # for the data are usable. MIBs can be specified on the command line or
    # snmpwalk may find them via your MIBS environment variable. The template
    # items will need modification from the ZABBIX UI after bulk loading. The
    # script doesn't attempt to manage interesting things like 'units' or 'custom
    # multipliers'. The script also has a limited notion of what qualifies as
    # 'Character' data and defaults to 'Numeric' for just about everything.
    # Update the script to properly identify more types.
    #
    # An example session might work like the following:
    #
    # 1  -  Create a new host template in the Zabbix UI. Make note of the ID for the
    #       new template.
    # 2  -  Use an additional -T option to test the examples in 2a and 2b.
    # 2a -  Build an initial set of items for the template. In the example below
    #       the template id 10015 is used:
    #       zload_snmpwalk -m SOMEVENDOR-MASTER-MIB secretstring dbuser dbpass 10015 somehost .iso.org.dod.internet.private
    # 2b -  Alternatively, the template can be created from saved snmpwalk data:
    #       zload_snmpwalk -m SOMEVENDOR-MASTER-MIB secretstring dbuser dbpass 10015 somefile
    #       For the option above, the data must be in 'snmpwalk -Of' format.
    # 3  -  Next, the template should be tweaked and tested from the Zabbix UI before
    #       applying to hosts.
    #
    use Getopt::Std;
    use DBI;
    
    use strict;
    use vars qw($PROG $VERSION $community $host $mib $usagetxt %OPTS
            $dbhost $dbuser $dbpass $zabbix_db $zabbix_id $translate $dbh
            $zabbix_port $zabbix_delay $zabbix_history $zabbix_trends
            $TEST);
    
    $VERSION = "1.0";
    $PROG = "zload_snmpwalk";
    
    $usagetxt = qq {
    USAGE:
     $PROG [OPTIONS] <community> <dbuser> <dbpasswd> <zabbix_id> <agent> <oid>
     $PROG [OPTIONS] <community> <dbuser> <dbpasswd> <zabbix_id> <file>
    
    Version: $VERSION
    snmpwalk the target <agent> from the starting <oid> and then store the results
    in the ZABBIX template <zabbix_id>. Alternatively, a <file> containing the
    output of a previous snmpwalk can be used. The data is expected in the format
    generated by 'smpwalk -Of' and should be generated using the appropriate MIB(s)
    so that meaningful descriptions can be created for ZABBIX. Without using a
    MIB, the attempt will likely fail as the descriptions used for ZABBIX keys will
    be duplicated.
    Be sure to test your data with the -T option first!
    
    BASIC ENVIRONMENT REQUIREMENTS:
    Zabbix 1.1alpha6        - Not tested with any other versions.
    perl                    - Tested with 5.8.0
    Mysql & perl DBI        - Might work with postgres after minor changes.
    net-snmp                - Tested with net-snmp 5.0.8
    
    PRIMARY ARGUMENTS:
    <community>     SNMP community string
    <dbuser>        DB user that can update ZABBIX tables
    <dbpasswd>      DB password for the user
    <zabbix_id>     Zabbix template id for a template created from the UI
    
    DATA SOURCES:
    <agent>         Target SNMP host/agent
    <oid>           Beginning oid - example: .iso.org.dod.internet.private
            OR
    <file>          File containing snmpwalk data in '-Of' format to be loaded
    
    OPTIONS:
            -T              Test Mode. No DB updates - just view the new records.
            -v 1|2c         SNMP version
            -m MIB[:...]    load given list of MIBs (ALL loads everything)
            -p port         SNMP port, default is 161
    
      Database or ZABBIX data related options:
            -r delay        SNMP polling delay, default is 60
            -h history      How long to keep polling history, default is 7
            -t trends       How long to keep polling trends, default is 365
            -s server       Database server, default is localhost
            -d database   Database, default is zabbix
    };
    
    sub connect_db {
            my $db = "DBI:mysql:$zabbix_db:$dbhost";
            $dbh = DBI->connect($db, $dbuser, $dbpass) ||
                    die "Can't connect to DB: $dbh->errstr\n";
            $dbh->{AutoCommit} = 0;
    }
    
    sub get_zabbix_description
    {
    
            my $raw_oid = shift(@_);
            my $pos;
    
            # Step back 2 positions for a description string and key
            $pos = rindex($raw_oid,'.');
            $pos = rindex($raw_oid,'.',$pos - 1);
    
            return substr($raw_oid, $pos + 1);
    }
    
    sub get_zabbix_oid
    {
            my $raw_oid = shift(@_);
            my $zabbix_oid = `$translate $raw_oid`;
            chomp($zabbix_oid);
            return $zabbix_oid;
    }
    
    sub get_zabbix_value_type
    {
            my $snmp_type = shift(@_);
            my $zabbix_value_type;
    
            # Zabbix types
            # 0 = Numeric
            # 1 = Character
            # Add additional character types as needed. More are needed..
            SWITCH:
            {
                    if ($snmp_type eq "STRING")
                            { $zabbix_value_type = 1;  last SWITCH; }
                    $zabbix_value_type = 0;
            }
    
            return $zabbix_value_type;
    }
    
    sub usage
    {
            print $usagetxt;
            exit 0;
    }
    
    MAIN:
    $| = 1; # no print delay
    
    my $input;
    my $sql;
    my $tcounter = 0;
    
    if (!getopts('d:h:m:p:r:s:t:u:v:T', \%OPTS))
    {
            usage();
    }
    
    
    if (@ARGV == 5 || @ARGV == 6)
    {
            $TEST = exists($OPTS{T}) ? "1" : "0";
    
            $community = $ARGV[0];
            $dbuser = $ARGV[1];
            $dbpass = $ARGV[2];
            $zabbix_id = $ARGV[3];
    
            $dbhost = exists($OPTS{s}) ? ($OPTS{s}) : "localhost";
            $zabbix_db = exists($OPTS{d}) ? ($OPTS{d}) : "zabbix";
    
            $zabbix_port = exists($OPTS{p}) ? ($OPTS{p}) : "161";
            $zabbix_delay = exists($OPTS{r}) ? ($OPTS{r}) : "60";
            $zabbix_history = exists($OPTS{h}) ? ($OPTS{h}) : "7";
            $zabbix_trends = exists($OPTS{t}) ? ($OPTS{t}) : "365";
    
            $translate = "snmptranslate -Ofn ";
            $translate .= exists($OPTS{m}) ? ("-m $OPTS{m} ") : "";
    
            # Input from file by default, might change below
            $input = "<$ARGV[4]";
    }
    else
    {
            usage();
    };
    
    # Data from live snmpwalk
    if (@ARGV == 6)
    {
            $host = $ARGV[4];
    
            $input = "snmpwalk -c $community ";
            $input .= exists($OPTS{m}) ? ("-m $OPTS{m} ") : "";
            $input .= exists($OPTS{v}) ? ("-v $OPTS{v} ") : "";
            # Host
            $input .= "-Of $host";
            $input .= exists($OPTS{p}) ? (":$OPTS{p} ") : ":161 ";
            # OID
            $input .= "$ARGV[5]|";
    }
    
    connect_db();
    open(SWALK,$input) or die "Can't open $input\n";
    
    print "Processing Data from $input ";
    
    $sql = $dbh->prepare("INSERT INTO items (type, snmp_community, snmp_oid, snmp_port, hostid, description, key_, delay, history, trends, value_type) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
    
    while(my $line = <SWALK>)
    {
            my $zabbix_value_type;
            my $zabbix_description;
            my $zabbix_oid;
    
            next if ($line !~ /(.iso.org.dod.*) = (.*):/ );
            #$1 = oid string to translate
            #$2 = integer, string or other snmp type
            #More snmp "character" types should be added
    
            $zabbix_description = get_zabbix_description($1);
            $zabbix_oid = get_zabbix_oid($1);
            $zabbix_value_type = get_zabbix_value_type($2);
    
    
            if ($TEST)
            {
                    if (($tcounter++ % 10) == 0)
                    {
                            print "\ntype\tsnmp_community\tsnmp_oid\tsnmp_port\t",
                                    "hostid\tdescription\tkey_\tdelay\t",
                                    "history\ttrends\tvalue_type\n";
                    }
                    print "4\t$community\t$zabbix_oid\t$zabbix_port\t",
                            "$zabbix_id\t$zabbix_description\t",
                            "$zabbix_description\t$zabbix_delay\t",
                            "$zabbix_history\t$zabbix_trends\t",
                            "$zabbix_value_type\n";
            }
            else
            {
                    print "." if (($tcounter++ % 10) == 0 );
                    $sql->execute(4, $community, $zabbix_oid, $zabbix_port,
                    $zabbix_id, $zabbix_description, $zabbix_description,
                    $zabbix_delay, $zabbix_history, $zabbix_trends,
                    $zabbix_value_type) || die "Insert Failure: $sql->strerror\n";
            }
    }
    close(SWALK);
    $dbh->commit();
    $dbh->disconnect();
    print "Finished\n";
    exit 0;
    Last edited by Steven; 05-03-2005, 22:42.
  • grabber
    Member
    • Mar 2005
    • 85

    #2
    version 1.1

    Hi,

    I am trying this with version 1.1 but it is failing with a duplicate entry fault.

    /usr/bin/perl zload_snmpwalk.pl -v 2c -m all <community> <dbuser> <dbpassword> 10007 <ipaddress> .iso.org.dod.internet.private.enterprises.3375.2
    Processing Data from snmpwalk -c public -m all -v 2c -Of <ipaddress>:161 .iso.org.dod.internet.private.enterprises.3375.2| ...DBD::mysql::st execute failed: Duplicate entry '10007-1.0' for key 2 at zload_snmpwalk.pl line 252, <SWALK> line 30.
    Insert Failure: DBI::st=HASH(0x796f20)->strerror

    any Ideas, or alternatives???

    thanks

    Ian


    ***EDIT***

    Ok it works

    Last edited by grabber; 08-06-2006, 17:14.

    Comment

    • vfly4
      Junior Member
      • Jul 2006
      • 1

      #3
      How did you get around this problem?

      Comment

      • qix
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • Oct 2006
        • 423

        #4
        Has anybody looked at modifying this script for use with the XML import function of Zabbix 1.4 ?
        With kind regards,

        Raymond

        Comment

        • Niels
          Senior Member
          • May 2007
          • 239

          #5
          This kind of functionality is essential for Zabbix in the long run. I hope the developers are looking to include something equivalent in 1.6.

          Comment

          • qix
            Senior Member
            Zabbix Certified SpecialistZabbix Certified Professional
            • Oct 2006
            • 423

            #6
            I agree, it is vital for us to ease the making of templates for new (SNMP) devices.
            With kind regards,

            Raymond

            Comment

            • topkoa
              Member
              • Oct 2007
              • 47

              #7
              compatability?

              Does anyone know if this is compatible with v1.4.2 ?

              Thanks!

              Comment

              • topkoa
                Member
                • Oct 2007
                • 47

                #8
                Another question...

                Are there any plans to add simililar functionality like this to the Web UI?

                It would be really useful to have a page that simply asks you for the host, community string, and a new/existing template name, then does a snmpwalk.

                Something called "Create Items based upon snmpwalk data from host"..

                Just a thaught...

                Comment

                • ploeger
                  Junior Member
                  • Jun 2006
                  • 24

                  #9
                  Hi!

                  I've modified the script to generate a xml-importfile to be imported using the Web UI. It's in the Wiki:

                  http://www.zabbix.com/wiki/doku.php?...zload_snmpwalk

                  Have fun.

                  Regards,
                  Dennis

                  Comment

                  • ploeger
                    Junior Member
                    • Jun 2006
                    • 24

                    #10
                    I've updated the script to correctly produce v1 and v2c-items and the ability for the user to change the way the scripts behaves when generating item keys and descriptions.

                    Also added one wonderful new feature: A version number.

                    Comment

                    • qix
                      Senior Member
                      Zabbix Certified SpecialistZabbix Certified Professional
                      • Oct 2006
                      • 423

                      #11
                      Hi Ploeger,

                      I finally came around to testing this new script.
                      Thank you very much!!

                      I found the following in the output that doesn't seem correct to me:

                      1. At the end of the generated XML file it says "Finished" In my opinion, this is just a cosmetic problem.

                      2. The items are not enclosed by <ITEMS> and </ITEMS> tags like this:

                      Code:
                      <items>
                      	<item type="0" key="proc.num[zabbix_server]" value_type="3">
                      	 	<description>Number of running processes zabbix_server</description>
                      		<delay>300</delay>
                      		<history>90</history>
                      		<trends>365</trends>
                      		<formula>1</formula>
                      		<snmp_community>public</snmp_community>
                      		<snmp_oid>interfaces.ifTable.ifEntry.ifInOctets.1</snmp_oid>
                      		<snmp_port>161</snmp_port>
                      	</item>
                      </items>
                      3. The start of the file looks like this:

                      Code:
                      <zabbix_export version="1.0" date="08.01.08" time="16:27">
                      	<hosts>
                      		<host name="Template_SNMP">
                      			<status>3</status>
                      Processing Data from <walk.txt 			<item type="4" key="sysDescr.0" value_type="1">
                      				<description>sysDescr.0</description>
                      				<delay>60</delay>
                      				<history>30</history>
                      				<trends>365</trends>
                      				<snmp_community>xxxxxxxx</snmp_community>
                      				<snmp_oid>.1.3.6.1.2.1.1.1.0</snmp_oid>
                      				<snmp_port>161</snmp_port>
                      			</item>
                      ....
                      The "Processing Data from <walk.txt" doesn't seem to belong there.

                      After a little adjusting, the import goes just fine
                      Again, thank you!
                      With kind regards,

                      Raymond

                      Comment

                      • ploeger
                        Junior Member
                        • Jun 2006
                        • 24

                        #12
                        Hi Ramond!

                        BTW: My name's Dennis ;-)

                        Did you try (the new) V1.0 from the wiki page or the previous version (without a version number). I've added a debug-switch to dis- or enable the debugging output (like "Processing data from...").

                        Please try out V1.0 again.

                        Thanks.

                        Regards,
                        Dennis

                        Comment

                        • qix
                          Senior Member
                          Zabbix Certified SpecialistZabbix Certified Professional
                          • Oct 2006
                          • 423

                          #13
                          Hi Dennis,

                          I copied the script from the wiki just yesterday, so I was using version 1.0.
                          Would you like me to rerun the script with debugging enabled?
                          With kind regards,

                          Raymond

                          Comment

                          • ploeger
                            Junior Member
                            • Jun 2006
                            • 24

                            #14
                            Hi!

                            Hmm.. The Output you mentioned should only be displayed, when the debug mode is enabled, so when you've used it without enabling debug mode, the output shouldn't be there.

                            Could you please just retry it and check, if the version you've used really is V1 ?

                            Thanks.

                            Kind Regards,
                            Dennis

                            Comment

                            • qix
                              Senior Member
                              Zabbix Certified SpecialistZabbix Certified Professional
                              • Oct 2006
                              • 423

                              #15
                              Hi Dennis,

                              Output from "./zload_snmpwalk --help":

                              Code:
                              ./zload_snmpwalk.pl version 1.0 calling Getopt::Std::getopts (version 1.05 [paranoid]),
                              running under Perl version 5.8.8.
                              
                              Usage: zload_snmpwalk.pl [-OPTIONS [-MORE_OPTIONS]] [--] [PROGRAM_ARG1 ...]
                              
                              The following single-character options are accepted:
                                      With arguments: -d -h -m -p -r -s -t -u -v -u -P -r
                                      Boolean (without arguments): -T -x
                              
                              Options may be merged together.  -- stops processing of options.
                              Space is not required between options and their arguments.
                                [Now continuing due to backward compatibility and excessive paranoia.
                                 See ``perldoc Getopt::Std'' about $Getopt::Std::STANDARD_HELP_VERSION.]
                              
                              USAGE:
                               zload_snmpwalk [OPTIONS] <community> <zabbix_id> <agent> <oid>
                               zload_snmpwalk [OPTIONS] <community> <zabbix_id> <file>
                              
                              Version: 1.0
                              snmpwalk the target <agent> from the starting <oid> and then store the results
                              in the ZABBIX template <zabbix_id>. Alternatively, a <file> containing the
                              output of a previous snmpwalk can be used. The data is expected in the format
                              generated by 'smpwalk -Of' and should be generated using the appropriate MIB(s)
                              so that meaningful descriptions can be created for ZABBIX. Without using a
                              MIB, the attempt will likely fail as the descriptions used for ZABBIX keys will
                              be duplicated.
                              Be sure to test your data with the -T option first!
                              For Zabbix > 1.4 a XML-output suitable for importing over the WEB frontend is supported (-x).
                              To use it, specify the hosts name rather than the hosts id as <zabbix_id>.
                              
                              BASIC ENVIRONMENT REQUIREMENTS:
                              Zabbix 1.1alpha6        - Not tested with any other versions.
                              perl                    - Tested with 5.8.0
                              Mysql & perl DBI        - Might work with postgres after minor changes.
                              net-snmp                - Tested with net-snmp 5.0.8
                              
                              PRIMARY ARGUMENTS:
                              <community>     SNMP community string
                              <dbuser>        DB user that can update ZABBIX tables
                              <dbpasswd>      DB password for the user
                              <zabbix_id>     Zabbix template id for a template created from the UI
                              
                              DATA SOURCES:
                              <agent>         Target SNMP host/agent
                              <oid>           Beginning oid - example: .iso.org.dod.internet.private
                                      OR
                              <file>          File containing snmpwalk data in '-Of' format to be loaded
                              
                              OPTIONS:
                                      -T              Test Mode. No DB updates - just view the new records.
                                      -v 1|2c|3       SNMP version
                                      -m MIB[:...]    load given list of MIBs (ALL loads everything)
                                      -p port         SNMP port, default is 161
                                      -x              Output XML suitable for import rather than doing SQL.
                                      -d              Output debugging information
                              
                                Database or ZABBIX data related options:
                                      -r delay        SNMP polling delay, default is 60
                                      -h history      How long to keep polling history, default is 7
                                      -t trends       How long to keep polling trends, default is 365
                                      -s server       Database server, default is localhost
                                      -r num          Use num indexes from the right as the description (default: 2)
                                      -d database   Database, default is zabbix
                                      -u user         Database-user
                                      -P password     Database-password
                              So its version 1, however, I figured out what the problem is.
                              There are options that can be given to the program that are not unique.
                              the -r and -d options have a dual purpose:

                              Code:
                              -d              Output debugging information
                              -d database   Database, default is zabbix
                              
                              -r delay        SNMP polling delay, default is 60
                              -r num          Use num indexes from the right as the description (default: 2)

                              When I ran the program, I tried to set the default delay time with -r, this resulted in to much indexes being used.
                              So I asummed that -d would set the delay, but off course it just turned on debugging.

                              You might want to fix this (maybe use capital letters?) in a 1.1 version
                              With kind regards,

                              Raymond

                              Comment

                              Working...