0

I use "Template SMNP Interfaces" to monitor switches.

It gives me keys like: ifOutOctets[16]

I would like to have an item that covers all ports:

MaxOutOctets = max(ifOutOctets[*])

that I can use in graphs.

I have read https://www.zabbix.com/documentation/2.2/manual/config/items/itemtypes/calculated but I seem to not be able to get the syntax right.

2 Answers 2

0

Unfortunately, what you seek is currently not possible out of the box, but there is a feature request for that: ZBXNEXT-1829. Requests ZBXNEXT-1420 and ZBXNEXT-1521 are also related.

2
  • Great, so others need this, too. But what is the workaround? Assume that I have ifOutOctets[1]..ifOutOctets[48] autodiscovered by SNMP. Can I then by hand make an MaxOutOctets-item?
    – Ole Tange
    Sep 23, 2014 at 8:15
  • With two items you would take the maximum like so: "(last(A)>last(B))*last(A)+(last(B)>=last(A))*last(B)". Unfortunately, it does not seem like this idea is easily extendable to 48 items.
    – asaveljevs
    Sep 23, 2014 at 12:09
1

The workaround (others might find that helpful):

#!/usr/bin/perl                                                                                                                                                                                                                        

# This program reads off traffic from a switch by SNMP.                                                                                                                                                                                
# It compares the values with last run and returns the value of the port with the biggest change                                                                                                                                       
#                                                                                                                                                                                                                                      
# Usage:                                                                                                                                                                                                                               
#    snmp_max_io 192.168.1.1                                                                                                                                                                                                           
#                                                                                                                                                                                                                                      
# (C) 2014-09-29 Ole Tange                                                                                                                                                                                                             
# License: GPLv3                                                                                                                                                                                                                       

my $switch = shift;

my ($last,$this) = update_cache_file($switch);
my %delta = delta($last,$this);
my $max = max(values %delta);
print "$max\n";
`logger $0 $switch returned $max`;

sub delta {
    my ($last,$this) = @_;
    # IF-MIB::ifHCOutOctets.45 = Counter64: 262606806485                                                                                                                                                                               
    my %last = map { split /: /,$_ } grep /Counter/, @$last;
    my %this = map { split /: /,$_ } grep /Counter/, @$this;
    my %delta = map { $_ => undef_as_zero($this{$_}) - undef_as_zero($last{$_}) }
    keys %last, keys %this;
    return %delta;
}

sub undef_as_zero {
    my $a = shift;
    return $a ? $a : 0;
}

sub my_dump {
    # Returns:                                                                                                                                                                                                                         
    #   ascii expression of object if Data::Dump(er) is installed                                                                                                                                                                      
    #   error code otherwise                                                                                                                                                                                                           
    my @dump_this = (@_);
    eval "use Data::Dump qw(dump);";
    if ($@) {
        # Data::Dump not installed                                                                                                                                                                                                     
        eval "use Data::Dumper;";
        if ($@) {
            my $err =  "Neither Data::Dump nor Data::Dumper is installed\n".
                "Not dumping output\n";
            print $Global::original_stderr $err;
            return $err;
        } else {
            return Dumper(@dump_this);
        }
    } else {
    # Create a dummy Data::Dump:dump as Hans Schou sometimes has                                                                                                                                                                       
    # it undefined                                                                                                                                                                                                                     
    eval "sub Data::Dump:dump {}";
        eval "use Data::Dump qw(dump);";
        return (Data::Dump::dump(@dump_this));
    }
}

sub update_cache_file {
    # Input:                                                                                                                                                                                                                           
    #   $switch = DNS name or IP address of switch                                                                                                                                                                                     
    my $switch = shift;
    my $tmpdir = "/tmp/switch-$ENV{USER}";
    my $tmpfile = "$tmpdir/snmp_last_$switch";
    mkdir $tmpdir;
    chmod 0700, $tmpdir || die;
    my @last = `cat $tmpfile 2>/dev/null`;
    my @this = snmpget_io($switch);
    open(OUT,">",$tmpfile) || die;
    print OUT @this;
    close OUT;
    if(not @last) {
    @last = @this;
    }
    return (\@last,\@this);
}

sub snmpget_io {
    # Input:                                                                                                                                                                                                                           
    #   $switch = DNS name or IP address of switch                                                                                                                                                                                     
    my $switch = shift;
    # Requires snmp-mibs-downloader installed                                                                                                                                                                                          
    return qx{ bash -c 'snmpget -v 2c -c public $switch IF-MIB::ifHCInOctets.{1..60}; snmpget -v 2c -c public $switch IF-MIB::ifHCOutOctets.{1..60};' }
}

sub max {
    # Returns:                                                                                                                                                                                                                         
    #   Maximum value of array                                                                                                                                                                                                         
    my $max;
    for (@_) {
        # Skip undefs                                                                                                                                                                                                                  
        defined $_ or next;
        defined $max or do { $max = $_; next; }; # Set $_ to the first non-undef                                                                                                                                                       
        $max = ($max > $_) ? $max : $_;
    }
    return $max;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .