Ad Widget

Collapse

Thresholds Configuration Report

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • naikmanish
    Junior Member
    • Nov 2014
    • 1

    #1

    Thresholds Configuration Report

    Greetings to all,

    I am very new to Zabbix. I have installed it a couple of weeks back and am yet figuring it out.

    We have an internal IT Audit and the auditors are asking for a report which shows all the thresholds and limits set for each type of asset. I am not sure about the nomenclature so I am not sure if its called a trigger. What they essentially want is, what thresholds based on severity is the monitoring system configured for.

    For e.g.

    Description Warning High Disaster
    CPU Utilization 50 70 90
    Memory Utilization 50 60 80
    Network Interface 30 50 70

    and so on.

    Is there some report which gives the above details ? I have configured Zabbix on the Default Parameters and not changed anything, so in case there is some ready reckoner on the internet / PDF etc. it could also be helpful. Otherwise, I will have to painstakingly create an Excel sheet while going into each of the template and jot down the severity for each item

    Thank you all so much

    Manish Naik
  • dirckcopeland
    Member
    • Oct 2013
    • 50

    #2
    Thresholds Configuration Report

    Manish,
    I think these reports may be helpful to you. They are a couple of perl scripts written to create tab separated reports that are easy to load into a spreadsheet. The trigger script takes the exported data from Zabbix and formats it into a report of hosts/triggers/expressions/groups/IP/OS.

    The items report script takes the exported data from Zabbix and formats it into a report of:
    Template/Item/Key/Triggers/Status/Priority/Description/Interval/History/Trends

    instructions are included in the code.

    The first report is:
    zabbix-trigger-reports.pl
    Code:
    #
    # Script to parse the Zabbix xml file and list the hosts/templates/triggers/groups/os
    #
    # Name: zabbix-trigger-reports.pl
    # Author: Dirck Copeland
    # Creation Date: 10-31-2013
    #
    # The zbx_export_hosts.xml and zbx_export_templates.xml files are exported from the Zabbix
    # GUI as deacribed below.
    #
    # Usage: ./zabbix-trigger-reports.pl zbx_export_hosts.xml zbx_export_templates.xml > 
    #                               zbx-trigger-report-server-12-03-2014.csv
    #
    # Purpose and Description:
    # This script reads two XML files saved from Zabbix using the syntax above and 
    # generates a csv file (actually it's tab seperated because there are commas in 
    # the data). It merges data from both XML files. The first XML file contains the host, 
    # name, templates and groups and the second XML file contains the triggers associated 
    # with each host.
    #
    # 1) Download the hosts XML file from Zabbix by going to Configuration | Host 
    #    page.
    # 2) Clear the filter Name/DNS/IP/Port if any are set and in the Group
    #    pull down menu, select all. Select the Name box in the upper left 
    #    hand corner. This will select hosts on that page only. 
    # 3) Move to the next page and select the name box again. This needs to 
    #    be repeated for every page.
    # 4) Make sure the "Export selected" shows in the pull down menu in the
    #    lower left corner of the screen. Select the Go button in the lower left 
    #    and this will bring up an save file dialog. 
    # 5) Select Save and it will put it in the Downloads folder in a 
    #    file called zbx_export_hosts.xml.
    # 6) Download the templates XML file from Zabbix by going to Configuration | Templates 
    #    page.
    # 7) Select the Templates box in the upper left hand corner. This will select 
    #    the available templates. Delselect the templates that do not apply such as
    #    Template OS Windows and any other test templates that are not of concern.
    # 8) Make sure the "Export selected" shows in the pull down menu in the
    #    lower left corner of the screen. Select the Go button in the lower left 
    #    and this will bring up a save file dialog. 
    # 9) Select Save and this will put the file in the Downloads folder in a 
    #    file called zbx_export_templates.xml
    #
    #* This program is free software; you can redistribute it and/or modify
    #* it under the terms of the GNU General Public License as published by
    #* the Free Software Foundation; either version 2 of the License, or
    #* (at your option) any later version.
    #*
    #* This program is distributed in the hope that it will be useful,
    #* but WITHOUT ANY WARRANTY; without even the implied warranty of
    #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    #* GNU General Public License for more details.
    #*
    #* You should have received a copy of the GNU General Public License
    #* along with this program; if not, write to the Free Software
    #* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
    # flush disk buffer and not wait until it is full.
    $| = 1;
    
    # check for correct number of arguments and exit if not
    bag("Usage: $0 zbx_export_hosts.xml zbx_export_templates.xml") unless @ARGV == 2;
    
    #*
    #* Open xml files
    #*
    my ($file1) = $ARGV[0];
    my ($file2) = $ARGV[1];
    
    # check to make sure files are text
    -T $file1 or bag("$file1: binary");
    -T $file2 or bag("$file2: binary");
    
    open (F1, $file1) or bag("Couldn't open $file1: $!");
    open (F2, $file2) or bag("Couldn't open $file2: $!");
    
    $host_flag=0;
    $os_flag=0;
    $start_template_flag=0;
    $array_index=0;
    $first_record=0;
    my @trigger_array;
    #
    # subroutine to print header
    #
    print_header();
    #
    # Loop to search and parse the xml file
    #
    read_hosts_xml();
    #
    # Print the Header to STDOUT
    #
    sub print_header {
            printf "HOST\tNAME\t<PRIORITY><SEVERITY><TEMPLATE><TRIGGER><EXPRESSION>\tGROUPS\tIP\tOS\n";
    }
    
    #
    # Loop through zbx_export_hosts.xml and search for each host. As it
    # finds each host, it then gets each template name for that host and 
    # finds the template in zbx_export_templates.xml and stores each
    # trigger associated with that template/host in the @trigger_array.
    # The host is then printed out listing the templates/triggers for
    # the respective host.
    #
    
    sub read_hosts_xml {
    while (<F1>) {
            # Locate the lines with a <host> indicating that it's the beginning of a host entry
            if ( /^            <host>/ ) {
                    $host = $_;
                    $host =~ s/            <host>//;
                    $host =~ s/<\/host>//;
                    chomp($host);
                    printf "%s\t",$host;
                    # set host flag - a one time thing to eliminate the group names at the first of the file.
                    $host_flag=1;
            } # end of host
            # Get the Zabbix name that was assigned to that host
            if ( /^            <name>/ && $host_flag != 0 ) {
                    $name = $_;
                    $name =~ s/            <name>//;
                    $name =~ s/<\/name>//;
                    chomp($name);
                    # At one time this if statement was needed because the beginning of the host
                    # needed a tab and the rest were but with v11 fix, this is no longer needed
                    # this is only for the first NAME in the report. This was left in because
                    # further development is needed with certain hosts not printing out the triggers
                    # associated with the host and the $first_record flag may be needed then.
                    if ($first_record == 0) {
                            # insert tab between TEMPLATES/TRIGGERS and GROUPS on the first host
                            printf "%s\t",$name;
                            $first_record=1;
                    } else {
                            # otherwise insert just the host because the following tab is inserted 
                            # in the get_template_names subroutine
                            printf "%s\t",$name;
                    }
            }# end of name
            # Find the template name that is assigned to this host and get the list of templates
            # and store in $template. Then for each template get the list of trigger by calling
            # the sub get_template_names($template) and store in @trigger_array for later printing. 
            if ( /^            <templates>/ ) {
                    $start_template_flag=1;
                    while (<F1> ) {
                    last if /^            <\/templates>/;
                    if ( /^                    <name>/ ) {
                            $template=$_;
                            $template =~ s/                    <name>//;
                            $template =~ s/<\/name>//;
                            chomp($template);
                            @trigger_array=get_template_names($template);
                            push (@trigger_copy,@trigger_array);
                            printf "%s - ",$template; # this is the seperator between the template names
                    }
                    } # end of while
                    # tab between the templates and groups at the beginning of a template
                    printf "\t";
            }# end of templates
            if ( /^            <groups>/ ) {
                    while (<F1> ) {
                    last if /^            <\/groups>/;
                    if ( /^                    <name>/ ) {
                            $group=$_;
                            $group =~ s/                    <name>//;
                            $group =~ s/<\/name>//;
                            chomp($group);
                            printf "%s - ",$group;
                    }
                    } # end of while
                    # tab between the group and the IP address
                    printf "\t";
            }# end of groups
            if ( /^            <interfaces>/ ) {
                    while (<F1> ) {
                    last if /^            <\/interfaces>/;
                    if ( /^                    <ip>/ ) {
                            $ip=$_;
                            $ip =~ s/                    <ip>//;
                            $ip =~ s/<\/ip>//;
                            chomp($ip);
                            printf "%s\t",$ip;
                    }
                    } # end of while
            }# end of interfaces
            if ( /^            <inventory>/ ) {
                    while (<F1> ) {
                    last if /^            <\/inventory>/;
                    if ( /<os>/ || /<os\/>/) {
                            $os=$_;
                            $os =~ s/                <os>//;
                            $os =~ s/                <os\/>//;
                            $os =~ s/<\/os>//;
                            chomp($os);
                            printf "%s\n",$os;
                            $os_flag=0;
                    }
                    } # end of while
                            #If there is no inventory this needs to be repeated in the no inventory section
                            for($index = 1; $index <= $#trigger_copy; $index++) {
                            # print the two tabs before  printing the Templates and Triggers array associated with each host 
                                    printf "\t\t%s\t\t\t\t\n",$trigger_copy[$index];
                                    # 7/24/2014printf "\t%s\t\t\t\t\n",$trigger_copy[$index];
                            }
                            # Clear out the array for next time
                            @trigger_copy=();
            }# end of inventory/os
            # If a host does not have an os in the inventory, need to insert a new line to start the next host
            if ($os_flag == 0 && /<inventory\/>/){
                    printf "\n";
                            #If there is no inventory the array needs to be printed in this part
                            for($index = 1; $index <= $#trigger_copy; $index++) {
                            # print the two tabs before  printing the Templates and Triggers array associated with each host 
                                    printf "\t\t%s\t\t\t\t\n",$trigger_copy[$index];
                            }
                            # Clear out the array for next time
                            @trigger_copy=();
            }
            # when <triggers> is reached, the program can terminate
            if ( /^    <triggers>/ ) {
                    close(F1);
                    close(F2);
                    exit();
            }# end of trigger and end of parsing
    } # end of while
    } # end of sub to read read_hosts_xml
    #
    # Subroutine to get the template name from the XML file
    #
    sub get_template_names {
            my @template_array;
            my ($template) = @_;
            if ($os_flag == 1){
                    # insert the tab between the NAME Column and the Template/Triggers Column
                    printf "\t";
                    $os_flag=0;
            }
            while (<F2>) {
                    if ( /^            <expression>{$template/ ) {
                            chomp($_);
                            $trigger_expression=$_;
                            $trigger_expression =~ s/            <expression>{$template://;
                            $trigger_expression =~ s/<\/expression>//;
                            # get the next line after matching the template name. The XML file
                            # contains the trigger name immediatley following the template name.
                            my $trigger_name = <F2>;
                            chomp($trigger_name);
                            $trigger_name =~ s/            <name>//;
                            $trigger_name =~ s/<\/name>//;
                            # Get the priority by moving the file pointer ahead with <F2> until you reach priority
                            $url = <F2>;
                            $status = <F2>;
                            $priority = <F2>;
                            chomp($priority);
                            $priority =~ s/            <priority>//;
                            $priority =~ s/<\/priority>//;
                            $global_priority = eval { $priority };
                            $global_priority = $priority; 
                            if ( $priority == 1 ) { $priority_string="Information"; }
                            elsif ( $priority == 2 ) { $priority_string="Warning"; }
                            elsif ( $priority == 3 ) { $priority_string="Average"; }
                            elsif ( $priority == 4 ) { $priority_string="High"; }
                            else   { $priority_string="Disaster"; }
                            $template_constructed = $priority . " - " . $priority_string . " - " . $template . " - " . $trigger_name . " - " . $trigger_expression;
                            $array_index++;
                            push @template_array,$template_constructed;
                    }
            }# end of while reading the template XML
            # rewind the file pointer back to start for next time
            $array_index=0;
            seek(F2,0,0);
            return @template_array;
    }
    sub bag {
            my $msg = shift;
            $msg .= "\n";
            warn $msg;
            exit 2;
    }
    The second report is:
    zabbix-items-report.pl
    Code:
    #!/usr/bin/perl
    #
    # Script to parse the Zabbix xml file and list the templates/items/triggers
    #
    # Name: zabbix-items-report.pl
    # Author: Dirck Copeland
    # Creation Date: 2/24/2014
    # Version:0.8
    #
    # The zbx_export_templates.xml file is exported from the Zabbix
    # GUI as described below.
    #
    # Usage: ./zabbix-items-report.pl zbx_export_templates.xml > zbx_items_report_server-04-14-2014.csv
    #
    # Purpose and Description:
    # This script reads the XML file saved from Zabbix using the syntax above and 
    # generates a csv file (actually it's tab seperated because there are commas in 
    # the data). 
    # The file contains the item template, item, trigger.
    #
    # 1) Download the templates XML file from Zabbix by going to Configuration | Templates 
    #    page.
    # 2) Select the Templates box in the upper left hand corner. This will select 
    #    the available templates.
    # 3) Make sure the "Export selected" shows in the pull down menu in the
    #    lower left corner of the screen. Select the Go button in the lower left 
    #    and this will bring up an save file dialog. 
    # 4) Select Save and this will put the file in the Downloads folder in a 
    #    file called zbx_export_templates.xml
    #
    #* This program is free software; you can redistribute it and/or modify
    #* it under the terms of the GNU General Public License as published by
    #* the Free Software Foundation; either version 2 of the License, or
    #* (at your option) any later version.
    #*
    #* This program is distributed in the hope that it will be useful,
    #* but WITHOUT ANY WARRANTY; without even the implied warranty of
    #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    #* GNU General Public License for more details.
    #*
    #* You should have received a copy of the GNU General Public License
    #* along with this program; if not, write to the Free Software
    #* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
    # flush disk buffer and not wait until it is full.
    $| = 1;
    
    # check for correct number of arguments and exit if not
    bag("Usage: $0 zbx_export_templates.xml") unless @ARGV == 1;
    
    #*
    #* Open xml file
    #*
    my ($file1) = @ARGV[0];
    
    # check to make sure files are text
    -T $file1 or bag("$file1: binary");
    
    open (F1, $file1) or bag("Couldn't open $file1: $!");
    
    $template_flag=0;
    $item_flag=0;
    $trigger_flag="FALSE";
    
    #
    # subroutine to print header
    #
    print_header();
    #
    # Loop to search and parse the xml file
    #
    read_template_xml();
    #
    # Print the Header to STDOUT
    #
    sub print_header {
            printf "TEMPLATE\tITEM\tKEY\tTRIGGERS\tSTATUS\tPRIORITY\tDESCRIPTION\tINTERVAL\tHISTORY\tTRENDS\tKB-ARTICLE\tNOTIFY\n";
    }
    
    #
    # Loop to search and parse the input XML file
    #
    
    sub read_template_xml {
    while (<F1>) {
            # Locate the lines with a <template> indicating that it's the beginning of a template entry
            if ( /^            <template>/ ) {
                    # set template flag - 1 is start of template - 0 is end of template
                    $template_flag=1;
                    $item_flag=0;
                    $template_start = $_;
                    $template = $_;
                    $template =~ s/            <template>//;
                    $template =~ s/<\/template>//;
                    chomp($template);
                    printf "%s",$template;
            } # end of template
    
            if ( /^                <item>/ ) {
                    $item_flag=1;
                    my $item_name = <F1>;
                    $item_name =~ s/                    <name>//;
                    $item_name =~ s/<\/name>//;
                    chomp($item_name);
                    printf "\t%s\t",$item_name;
                    if ($template_flag == 1) {
                            $template_flag=0;
                    }else{
                            $template_flag=0;
                    }
                    # loop through each item and print the key, trigger, delay, host, and trends
                    LINE: while (<F1> ) {
                            if ( /<key>/ ) {
                                    $key_name=$_;
                                    $key_name=~s/                    <key>//;
                                    $key_name=~s/<\/key>//;
                                    chomp($key_name);
                                    printf "%s\t",$key_name;
                                    # need to add an extra [ if the key contains is because of the way the 
                                    # wiki page parses and displays the [] combination.
                                    # construct the trigger name so you can search the XML to find 
                                    # a trigger name associated with the key name. There may potentially
                                    # be two or more triggers associated with a single trigger.
                                    $trigger=$template.":".$key_name;
                                    # need to escape the backslash otherwise the trigger name does not
                                    # match correctly 
    
                                    # currently there is an issue with the OS Windows template
                                    # It's best to not include it in the export for now.
                                    if ( $trigger ne "Template OS Windows" ) {
                                            $trigger=~s/\[/\\[/;
                                            $trigger=~s/\]/\\]/;
                                    }
                                    # get the current position of the file pointer and
                                    # store it in $curpos so you can come back to it 
                                    # after you search for the trigger and get related 
                                    # information near the end of the XML file.
                                    $curpos = tell(F1);
                                    $trigger_count=0;
                                    while (<F1>) {
                                            #$_=~s/\\/BS_/;
                                            if ( /$trigger/ ) {
                                                    # OK, you've found a trigger, now print it
                                                    $trig_expression=$_;
                                                    $trig_expression=~s/            <expression>//;
                                                    $trig_expression=~s/<\/expression>//;
                                                    chomp($trig_expression);
                                                    ########## get name of trigger #################
                                                    my $template_name = <F1>;
                                                    chomp($template_name);
                                                    ########## get url of trigger #################
                                                    my $template_url = <F1>;
                                                    chomp($template_url);
                                                    ########## get status of trigger #################
                                                    my $template_status = <F1>;
                                                    chomp($template_status);
                                                    $template_status=~s/            <status>//;
                                                    $template_status=~s/<\/status>//;
                                                    if ( $template_status eq "0" ) {
                                                            $trig_stat="Enabled";
                                                    } else {
                                                            $trig_stat="Disabled";
                                                    }
                                                    ########## get priority of trigger ################
                                                    my $template_priority = <F1>;
                                                    chomp($template_priority);
                                                    $template_priority=~s/            <priority>//;
                                                    $template_priority=~s/<\/priority>//;
                                                    if ( $template_priority eq "0" ) {
                                                            $trig_prio="Not Classified";
                                                    } 
                                                    if ( $template_priority eq "1" ) {
                                                            $trig_prio="Information";
                                                    } 
                                                    if ( $template_priority eq "2" ) {
                                                            $trig_prio="Warning";
                                                    } 
                                                    if ( $template_priority eq "3" ) {
                                                            $trig_prio="Average";
                                                    } 
                                                    if ( $template_priority eq "4" ) {
                                                            $trig_prio="High";
                                                    } 
                                                    if ( $template_priority eq "5" ) {
                                                            $trig_prio="Disaster";
                                                    } 
                                                    ########## get description of trigger #################
                                                    my $template_desc = <F1>;
                                                    chomp($template_desc);
                                                    $template_desc=~s/            <description>//;
                                                    $template_desc=~s/<\/description>//;
                                                    $template_desc=~s/<description\/>//;
                                                    ### if this is the first trigger for the item, print it ###
                                                    if ( $trigger_count == 0 ) {
                                                            printf "%s\t",$trig_expression;
                                                            printf "%s\t",$trig_stat;
                                                            printf "%s\t",$trig_prio;
                                                            printf "%s\t",$template_desc;
                                                            $trigger_flag="TRUE";
                                                            $trigger_count++;
                                                    # otherwise print the second or more triggers for this item
                                                    # and inset the correct number of tabs to keep the formatting 
                                                    # correct
                                                    } else {
                                                            # need to add tab(s) here if adding columnss after
                                                            # the trigger expression - $trig_expression.
                                                            # This only applies to columns containing data from
                                                            # the XML file. Not columns simple being added to the
                                                            # header.
                                                            printf "\t\t\t\n\t\t\t"; 
                                                            printf "%s\t",$trig_expression;
                                                            printf "%s\t",$trig_stat;
                                                            printf "%s\t",$trig_prio;
                                                            printf "%s\t",$template_desc;
                                                    }
                                            } # end of trigger
                                    }
                                    # if adding columns after the trigger expression column, add tab(s) here
                                    # after the Status N/A
                                    if ( $trigger_flag ne 'TRUE') {
                                            printf "NO TRIGGER\tStatus N/A\t\t\t";
                                    }
                                    # return to the position you were prior to the search
                                    seek(F1, $curpos, 0);
                            }
                            if ( /<delay>/ ) {
                                    $delay_name=$_;
                                    $delay_name=~s/                    <delay>//;
                                    $delay_name=~s/<\/delay>//;
                                    chomp($delay_name);
                                    printf "%s\t",$delay_name;
                            }
                            if ( /<history>/ ) {
                                    $history_name=$_;
                                    $history_name=~s/                    <history>//;
                                    $history_name=~s/<\/history>//;
                                    chomp($history_name);
                                    printf "%s\t",$history_name;
                            }
                            if ( /<trends>/ ) {
                                    $trends_name=$_;
                                    $trends_name=~s/                    <trends>//;
                                    $trends_name=~s/<\/trends>//;
                                    chomp($trends_name);
                                    printf "%s\n",$trends_name;
                            }
                            last LINE if ( /                <\/item>/ );
                                    #printf "KEY:%s\n",$_;
                    $trigger_flag="FALSE";
                    } # end of ITEM while
            }# end of item_name
    } # end of while <F1>
    } # end of sub to read read_template_xml
    sub bag {
            my $msg = shift;
            $msg .= "\n";
            warn $msg;
            exit 2;
    }
    close(F1);
    Last edited by dirckcopeland; 10-12-2014, 20:25.

    Comment

    • TheCookieMonster
      Member
      • Jul 2017
      • 48

      #3
      Do you have an updated version of these?

      When trying to use zabbix-trigger-reports.pl I get:
      Unescaped left brace in regex is illegal here in regex; marked by <-- HERE in m/^ <expression>{ <-- HERE Template/
      at C:\Users\XXXXX\Downloads\zbx_report\zabbix-trigger-reports.pl line 239, <F2> line 1.

      When trying to use zabbix-items-report.pl I get:
      \C no longer supported in regex; marked by <-- HERE in m/Template:log\[&quot;C:\Log\ <-- HERE mylog.log&quot;,,UTF-8,
      10\]/ at C:\Users\XXXXX\Downloads\zbx_report\zabbix-items-report.pl line 142, <F1> line 5564.

      Originally posted by dirckcopeland
      Manish,
      I think these reports may be helpful to you. They are a couple of perl scripts written to create tab separated reports that are easy to load into a spreadsheet. The trigger script takes the exported data from Zabbix and formats it into a report of hosts/triggers/expressions/groups/IP/OS.

      The items report script takes the exported data from Zabbix and formats it into a report of:
      Template/Item/Key/Triggers/Status/Priority/Description/Interval/History/Trends

      instructions are included in the code.

      The first report is:
      zabbix-trigger-reports.pl
      Code:
      #
      # Script to parse the Zabbix xml file and list the hosts/templates/triggers/groups/os
      #
      # Name: zabbix-trigger-reports.pl
      # Author: Dirck Copeland
      # Creation Date: 10-31-2013
      #
      # The zbx_export_hosts.xml and zbx_export_templates.xml files are exported from the Zabbix
      # GUI as deacribed below.
      #
      # Usage: ./zabbix-trigger-reports.pl zbx_export_hosts.xml zbx_export_templates.xml > 
      #                               zbx-trigger-report-server-12-03-2014.csv
      #
      # Purpose and Description:
      # This script reads two XML files saved from Zabbix using the syntax above and 
      # generates a csv file (actually it's tab seperated because there are commas in 
      # the data). It merges data from both XML files. The first XML file contains the host, 
      # name, templates and groups and the second XML file contains the triggers associated 
      # with each host.
      #
      # 1) Download the hosts XML file from Zabbix by going to Configuration | Host 
      #    page.
      # 2) Clear the filter Name/DNS/IP/Port if any are set and in the Group
      #    pull down menu, select all. Select the Name box in the upper left 
      #    hand corner. This will select hosts on that page only. 
      # 3) Move to the next page and select the name box again. This needs to 
      #    be repeated for every page.
      # 4) Make sure the "Export selected" shows in the pull down menu in the
      #    lower left corner of the screen. Select the Go button in the lower left 
      #    and this will bring up an save file dialog. 
      # 5) Select Save and it will put it in the Downloads folder in a 
      #    file called zbx_export_hosts.xml.
      # 6) Download the templates XML file from Zabbix by going to Configuration | Templates 
      #    page.
      # 7) Select the Templates box in the upper left hand corner. This will select 
      #    the available templates. Delselect the templates that do not apply such as
      #    Template OS Windows and any other test templates that are not of concern.
      # 8) Make sure the "Export selected" shows in the pull down menu in the
      #    lower left corner of the screen. Select the Go button in the lower left 
      #    and this will bring up a save file dialog. 
      # 9) Select Save and this will put the file in the Downloads folder in a 
      #    file called zbx_export_templates.xml
      #
      #* This program is free software; you can redistribute it and/or modify
      #* it under the terms of the GNU General Public License as published by
      #* the Free Software Foundation; either version 2 of the License, or
      #* (at your option) any later version.
      #*
      #* This program is distributed in the hope that it will be useful,
      #* but WITHOUT ANY WARRANTY; without even the implied warranty of
      #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      #* GNU General Public License for more details.
      #*
      #* You should have received a copy of the GNU General Public License
      #* along with this program; if not, write to the Free Software
      #* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
      
      # flush disk buffer and not wait until it is full.
      $| = 1;
      
      # check for correct number of arguments and exit if not
      bag("Usage: $0 zbx_export_hosts.xml zbx_export_templates.xml") unless @ARGV == 2;
      
      #*
      #* Open xml files
      #*
      my ($file1) = $ARGV[0];
      my ($file2) = $ARGV[1];
      
      # check to make sure files are text
      -T $file1 or bag("$file1: binary");
      -T $file2 or bag("$file2: binary");
      
      open (F1, $file1) or bag("Couldn't open $file1: $!");
      open (F2, $file2) or bag("Couldn't open $file2: $!");
      
      $host_flag=0;
      $os_flag=0;
      $start_template_flag=0;
      $array_index=0;
      $first_record=0;
      my @trigger_array;
      #
      # subroutine to print header
      #
      print_header();
      #
      # Loop to search and parse the xml file
      #
      read_hosts_xml();
      #
      # Print the Header to STDOUT
      #
      sub print_header {
              printf "HOST\tNAME\t<PRIORITY><SEVERITY><TEMPLATE><TRIGGER><EXPRESSION>\tGROUPS\tIP\tOS\n";
      }
      
      #
      # Loop through zbx_export_hosts.xml and search for each host. As it
      # finds each host, it then gets each template name for that host and 
      # finds the template in zbx_export_templates.xml and stores each
      # trigger associated with that template/host in the @trigger_array.
      # The host is then printed out listing the templates/triggers for
      # the respective host.
      #
      
      sub read_hosts_xml {
      while (<F1>) {
              # Locate the lines with a <host> indicating that it's the beginning of a host entry
              if ( /^            <host>/ ) {
                      $host = $_;
                      $host =~ s/            <host>//;
                      $host =~ s/<\/host>//;
                      chomp($host);
                      printf "%s\t",$host;
                      # set host flag - a one time thing to eliminate the group names at the first of the file.
                      $host_flag=1;
              } # end of host
              # Get the Zabbix name that was assigned to that host
              if ( /^            <name>/ && $host_flag != 0 ) {
                      $name = $_;
                      $name =~ s/            <name>//;
                      $name =~ s/<\/name>//;
                      chomp($name);
                      # At one time this if statement was needed because the beginning of the host
                      # needed a tab and the rest were but with v11 fix, this is no longer needed
                      # this is only for the first NAME in the report. This was left in because
                      # further development is needed with certain hosts not printing out the triggers
                      # associated with the host and the $first_record flag may be needed then.
                      if ($first_record == 0) {
                              # insert tab between TEMPLATES/TRIGGERS and GROUPS on the first host
                              printf "%s\t",$name;
                              $first_record=1;
                      } else {
                              # otherwise insert just the host because the following tab is inserted 
                              # in the get_template_names subroutine
                              printf "%s\t",$name;
                      }
              }# end of name
              # Find the template name that is assigned to this host and get the list of templates
              # and store in $template. Then for each template get the list of trigger by calling
              # the sub get_template_names($template) and store in @trigger_array for later printing. 
              if ( /^            <templates>/ ) {
                      $start_template_flag=1;
                      while (<F1> ) {
                      last if /^            <\/templates>/;
                      if ( /^                    <name>/ ) {
                              $template=$_;
                              $template =~ s/                    <name>//;
                              $template =~ s/<\/name>//;
                              chomp($template);
                              @trigger_array=get_template_names($template);
                              push (@trigger_copy,@trigger_array);
                              printf "%s - ",$template; # this is the seperator between the template names
                      }
                      } # end of while
                      # tab between the templates and groups at the beginning of a template
                      printf "\t";
              }# end of templates
              if ( /^            <groups>/ ) {
                      while (<F1> ) {
                      last if /^            <\/groups>/;
                      if ( /^                    <name>/ ) {
                              $group=$_;
                              $group =~ s/                    <name>//;
                              $group =~ s/<\/name>//;
                              chomp($group);
                              printf "%s - ",$group;
                      }
                      } # end of while
                      # tab between the group and the IP address
                      printf "\t";
              }# end of groups
              if ( /^            <interfaces>/ ) {
                      while (<F1> ) {
                      last if /^            <\/interfaces>/;
                      if ( /^                    <ip>/ ) {
                              $ip=$_;
                              $ip =~ s/                    <ip>//;
                              $ip =~ s/<\/ip>//;
                              chomp($ip);
                              printf "%s\t",$ip;
                      }
                      } # end of while
              }# end of interfaces
              if ( /^            <inventory>/ ) {
                      while (<F1> ) {
                      last if /^            <\/inventory>/;
                      if ( /<os>/ || /<os\/>/) {
                              $os=$_;
                              $os =~ s/                <os>//;
                              $os =~ s/                <os\/>//;
                              $os =~ s/<\/os>//;
                              chomp($os);
                              printf "%s\n",$os;
                              $os_flag=0;
                      }
                      } # end of while
                              #If there is no inventory this needs to be repeated in the no inventory section
                              for($index = 1; $index <= $#trigger_copy; $index++) {
                              # print the two tabs before  printing the Templates and Triggers array associated with each host 
                                      printf "\t\t%s\t\t\t\t\n",$trigger_copy[$index];
                                      # 7/24/2014printf "\t%s\t\t\t\t\n",$trigger_copy[$index];
                              }
                              # Clear out the array for next time
                              @trigger_copy=();
              }# end of inventory/os
              # If a host does not have an os in the inventory, need to insert a new line to start the next host
              if ($os_flag == 0 && /<inventory\/>/){
                      printf "\n";
                              #If there is no inventory the array needs to be printed in this part
                              for($index = 1; $index <= $#trigger_copy; $index++) {
                              # print the two tabs before  printing the Templates and Triggers array associated with each host 
                                      printf "\t\t%s\t\t\t\t\n",$trigger_copy[$index];
                              }
                              # Clear out the array for next time
                              @trigger_copy=();
              }
              # when <triggers> is reached, the program can terminate
              if ( /^    <triggers>/ ) {
                      close(F1);
                      close(F2);
                      exit();
              }# end of trigger and end of parsing
      } # end of while
      } # end of sub to read read_hosts_xml
      #
      # Subroutine to get the template name from the XML file
      #
      sub get_template_names {
              my @template_array;
              my ($template) = @_;
              if ($os_flag == 1){
                      # insert the tab between the NAME Column and the Template/Triggers Column
                      printf "\t";
                      $os_flag=0;
              }
              while (<F2>) {
                      if ( /^            <expression>{$template/ ) {
                              chomp($_);
                              $trigger_expression=$_;
                              $trigger_expression =~ s/            <expression>{$template://;
                              $trigger_expression =~ s/<\/expression>//;
                              # get the next line after matching the template name. The XML file
                              # contains the trigger name immediatley following the template name.
                              my $trigger_name = <F2>;
                              chomp($trigger_name);
                              $trigger_name =~ s/            <name>//;
                              $trigger_name =~ s/<\/name>//;
                              # Get the priority by moving the file pointer ahead with <F2> until you reach priority
                              $url = <F2>;
                              $status = <F2>;
                              $priority = <F2>;
                              chomp($priority);
                              $priority =~ s/            <priority>//;
                              $priority =~ s/<\/priority>//;
                              $global_priority = eval { $priority };
                              $global_priority = $priority; 
                              if ( $priority == 1 ) { $priority_string="Information"; }
                              elsif ( $priority == 2 ) { $priority_string="Warning"; }
                              elsif ( $priority == 3 ) { $priority_string="Average"; }
                              elsif ( $priority == 4 ) { $priority_string="High"; }
                              else   { $priority_string="Disaster"; }
                              $template_constructed = $priority . " - " . $priority_string . " - " . $template . " - " . $trigger_name . " - " . $trigger_expression;
                              $array_index++;
                              push @template_array,$template_constructed;
                      }
              }# end of while reading the template XML
              # rewind the file pointer back to start for next time
              $array_index=0;
              seek(F2,0,0);
              return @template_array;
      }
      sub bag {
              my $msg = shift;
              $msg .= "\n";
              warn $msg;
              exit 2;
      }
      The second report is:
      zabbix-items-report.pl
      Code:
      #!/usr/bin/perl
      #
      # Script to parse the Zabbix xml file and list the templates/items/triggers
      #
      # Name: zabbix-items-report.pl
      # Author: Dirck Copeland
      # Creation Date: 2/24/2014
      # Version:0.8
      #
      # The zbx_export_templates.xml file is exported from the Zabbix
      # GUI as described below.
      #
      # Usage: ./zabbix-items-report.pl zbx_export_templates.xml > zbx_items_report_server-04-14-2014.csv
      #
      # Purpose and Description:
      # This script reads the XML file saved from Zabbix using the syntax above and 
      # generates a csv file (actually it's tab seperated because there are commas in 
      # the data). 
      # The file contains the item template, item, trigger.
      #
      # 1) Download the templates XML file from Zabbix by going to Configuration | Templates 
      #    page.
      # 2) Select the Templates box in the upper left hand corner. This will select 
      #    the available templates.
      # 3) Make sure the "Export selected" shows in the pull down menu in the
      #    lower left corner of the screen. Select the Go button in the lower left 
      #    and this will bring up an save file dialog. 
      # 4) Select Save and this will put the file in the Downloads folder in a 
      #    file called zbx_export_templates.xml
      #
      #* This program is free software; you can redistribute it and/or modify
      #* it under the terms of the GNU General Public License as published by
      #* the Free Software Foundation; either version 2 of the License, or
      #* (at your option) any later version.
      #*
      #* This program is distributed in the hope that it will be useful,
      #* but WITHOUT ANY WARRANTY; without even the implied warranty of
      #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      #* GNU General Public License for more details.
      #*
      #* You should have received a copy of the GNU General Public License
      #* along with this program; if not, write to the Free Software
      #* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
      
      # flush disk buffer and not wait until it is full.
      $| = 1;
      
      # check for correct number of arguments and exit if not
      bag("Usage: $0 zbx_export_templates.xml") unless @ARGV == 1;
      
      #*
      #* Open xml file
      #*
      my ($file1) = @ARGV[0];
      
      # check to make sure files are text
      -T $file1 or bag("$file1: binary");
      
      open (F1, $file1) or bag("Couldn't open $file1: $!");
      
      $template_flag=0;
      $item_flag=0;
      $trigger_flag="FALSE";
      
      #
      # subroutine to print header
      #
      print_header();
      #
      # Loop to search and parse the xml file
      #
      read_template_xml();
      #
      # Print the Header to STDOUT
      #
      sub print_header {
              printf "TEMPLATE\tITEM\tKEY\tTRIGGERS\tSTATUS\tPRIORITY\tDESCRIPTION\tINTERVAL\tHISTORY\tTRENDS\tKB-ARTICLE\tNOTIFY\n";
      }
      
      #
      # Loop to search and parse the input XML file
      #
      
      sub read_template_xml {
      while (<F1>) {
              # Locate the lines with a <template> indicating that it's the beginning of a template entry
              if ( /^            <template>/ ) {
                      # set template flag - 1 is start of template - 0 is end of template
                      $template_flag=1;
                      $item_flag=0;
                      $template_start = $_;
                      $template = $_;
                      $template =~ s/            <template>//;
                      $template =~ s/<\/template>//;
                      chomp($template);
                      printf "%s",$template;
              } # end of template
      
              if ( /^                <item>/ ) {
                      $item_flag=1;
                      my $item_name = <F1>;
                      $item_name =~ s/                    <name>//;
                      $item_name =~ s/<\/name>//;
                      chomp($item_name);
                      printf "\t%s\t",$item_name;
                      if ($template_flag == 1) {
                              $template_flag=0;
                      }else{
                              $template_flag=0;
                      }
                      # loop through each item and print the key, trigger, delay, host, and trends
                      LINE: while (<F1> ) {
                              if ( /<key>/ ) {
                                      $key_name=$_;
                                      $key_name=~s/                    <key>//;
                                      $key_name=~s/<\/key>//;
                                      chomp($key_name);
                                      printf "%s\t",$key_name;
                                      # need to add an extra [ if the key contains is because of the way the 
                                      # wiki page parses and displays the [] combination.
                                      # construct the trigger name so you can search the XML to find 
                                      # a trigger name associated with the key name. There may potentially
                                      # be two or more triggers associated with a single trigger.
                                      $trigger=$template.":".$key_name;
                                      # need to escape the backslash otherwise the trigger name does not
                                      # match correctly 
      
                                      # currently there is an issue with the OS Windows template
                                      # It's best to not include it in the export for now.
                                      if ( $trigger ne "Template OS Windows" ) {
                                              $trigger=~s/\[/\\[/;
                                              $trigger=~s/\]/\\]/;
                                      }
                                      # get the current position of the file pointer and
                                      # store it in $curpos so you can come back to it 
                                      # after you search for the trigger and get related 
                                      # information near the end of the XML file.
                                      $curpos = tell(F1);
                                      $trigger_count=0;
                                      while (<F1>) {
                                              #$_=~s/\\/BS_/;
                                              if ( /$trigger/ ) {
                                                      # OK, you've found a trigger, now print it
                                                      $trig_expression=$_;
                                                      $trig_expression=~s/            <expression>//;
                                                      $trig_expression=~s/<\/expression>//;
                                                      chomp($trig_expression);
                                                      ########## get name of trigger #################
                                                      my $template_name = <F1>;
                                                      chomp($template_name);
                                                      ########## get url of trigger #################
                                                      my $template_url = <F1>;
                                                      chomp($template_url);
                                                      ########## get status of trigger #################
                                                      my $template_status = <F1>;
                                                      chomp($template_status);
                                                      $template_status=~s/            <status>//;
                                                      $template_status=~s/<\/status>//;
                                                      if ( $template_status eq "0" ) {
                                                              $trig_stat="Enabled";
                                                      } else {
                                                              $trig_stat="Disabled";
                                                      }
                                                      ########## get priority of trigger ################
                                                      my $template_priority = <F1>;
                                                      chomp($template_priority);
                                                      $template_priority=~s/            <priority>//;
                                                      $template_priority=~s/<\/priority>//;
                                                      if ( $template_priority eq "0" ) {
                                                              $trig_prio="Not Classified";
                                                      } 
                                                      if ( $template_priority eq "1" ) {
                                                              $trig_prio="Information";
                                                      } 
                                                      if ( $template_priority eq "2" ) {
                                                              $trig_prio="Warning";
                                                      } 
                                                      if ( $template_priority eq "3" ) {
                                                              $trig_prio="Average";
                                                      } 
                                                      if ( $template_priority eq "4" ) {
                                                              $trig_prio="High";
                                                      } 
                                                      if ( $template_priority eq "5" ) {
                                                              $trig_prio="Disaster";
                                                      } 
                                                      ########## get description of trigger #################
                                                      my $template_desc = <F1>;
                                                      chomp($template_desc);
                                                      $template_desc=~s/            <description>//;
                                                      $template_desc=~s/<\/description>//;
                                                      $template_desc=~s/<description\/>//;
                                                      ### if this is the first trigger for the item, print it ###
                                                      if ( $trigger_count == 0 ) {
                                                              printf "%s\t",$trig_expression;
                                                              printf "%s\t",$trig_stat;
                                                              printf "%s\t",$trig_prio;
                                                              printf "%s\t",$template_desc;
                                                              $trigger_flag="TRUE";
                                                              $trigger_count++;
                                                      # otherwise print the second or more triggers for this item
                                                      # and inset the correct number of tabs to keep the formatting 
                                                      # correct
                                                      } else {
                                                              # need to add tab(s) here if adding columnss after
                                                              # the trigger expression - $trig_expression.
                                                              # This only applies to columns containing data from
                                                              # the XML file. Not columns simple being added to the
                                                              # header.
                                                              printf "\t\t\t\n\t\t\t"; 
                                                              printf "%s\t",$trig_expression;
                                                              printf "%s\t",$trig_stat;
                                                              printf "%s\t",$trig_prio;
                                                              printf "%s\t",$template_desc;
                                                      }
                                              } # end of trigger
                                      }
                                      # if adding columns after the trigger expression column, add tab(s) here
                                      # after the Status N/A
                                      if ( $trigger_flag ne 'TRUE') {
                                              printf "NO TRIGGER\tStatus N/A\t\t\t";
                                      }
                                      # return to the position you were prior to the search
                                      seek(F1, $curpos, 0);
                              }
                              if ( /<delay>/ ) {
                                      $delay_name=$_;
                                      $delay_name=~s/                    <delay>//;
                                      $delay_name=~s/<\/delay>//;
                                      chomp($delay_name);
                                      printf "%s\t",$delay_name;
                              }
                              if ( /<history>/ ) {
                                      $history_name=$_;
                                      $history_name=~s/                    <history>//;
                                      $history_name=~s/<\/history>//;
                                      chomp($history_name);
                                      printf "%s\t",$history_name;
                              }
                              if ( /<trends>/ ) {
                                      $trends_name=$_;
                                      $trends_name=~s/                    <trends>//;
                                      $trends_name=~s/<\/trends>//;
                                      chomp($trends_name);
                                      printf "%s\n",$trends_name;
                              }
                              last LINE if ( /                <\/item>/ );
                                      #printf "KEY:%s\n",$_;
                      $trigger_flag="FALSE";
                      } # end of ITEM while
              }# end of item_name
      } # end of while <F1>
      } # end of sub to read read_template_xml
      sub bag {
              my $msg = shift;
              $msg .= "\n";
              warn $msg;
              exit 2;
      }
      close(F1);
      Last edited by TheCookieMonster; 15-08-2017, 14:50. Reason: privacy

      Comment

      Working...