interface 2.49 KB
Newer Older
Alexander Gall's avatar
Alexander Gall committed
1
#!/usr/bin/perl
Alexander Gall's avatar
Alexander Gall committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
###
### Copyright (C) 2016 Alexander Gall <gall@switch.ch>
###
### 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 3 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, see <http://www.gnu.org/licenses/>.

Alexander Gall's avatar
Alexander Gall committed
18 19 20 21 22
use strict;
use warnings;
use Getopt::Long;
use Snabb::SNMP::Agent qw(%persistent_ifIndex %compound_scalar_handlers $sysUpTime);
my %opt = ( 'check-interval' => 5,
23
            'shmem-dir' => '/var/lib/snabb/shmem', );
Alexander Gall's avatar
Alexander Gall committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

sub usage() {
  print <<"EOF";
usage: $0 --ifindex=<file>
EOF
  exit(1);
}

sub ifTable_indexer($$$) {
  ## Force persistent ifIndex based on ifDescr
  my ($oid, $table_oid, $segment) = @_;
  my $ifDescr_obj = $segment->{objs}{ifDescr};
  defined $ifDescr_obj or die
    "ifTable IID indexer: no ifDescr present for $segment->{name}";
  tie my $ifDescr, 'Snabb::SNMP::Tie::OCTETSTR', $segment,
    'ifDescr';
  my $ifIndex = $persistent_ifIndex{$ifDescr};
  defined $ifIndex or die
    "ifTable IID indexer: unknown interface $ifDescr";
  return($oid.".".$ifIndex);
}

my %subtrees =
  ( interfaces =>
    { tables =>
      { scalars =>
	{ handlers =>
	  { ifNumber =>
	    { compound_handler => sub {
		return scalar(keys(%persistent_ifIndex));
	      }
	    },
	  },
	},
	ifTable => { indexer => \&ifTable_indexer },
      },
    },

    ifMIB =>
    { tables =>
      { scalars =>
	{ handlers =>
	  { ifTableLastChange =>
	    { compound_handler => sub {
		return $sysUpTime;
	      }
	    },
	  },
	},
	ifXTable => { indexer => \&ifTable_indexer },
      },
    },
  );

GetOptions(\%opt, "check-interval=i",
79
           "ifindex=s", "shmem-dir=s") or usage();
Alexander Gall's avatar
Alexander Gall committed
80
defined $opt{ifindex} or usage();
81
Snabb::SNMP::Agent::start({ name => "interface",
Alexander Gall's avatar
Alexander Gall committed
82 83 84 85 86 87 88 89 90 91 92 93 94
                            subtrees => \%subtrees,
			    check_interval => $opt{'check-interval'},
			    if_index => $opt{ifindex},
                            mibs_dirs => '',
                            mibs => '',
                            shmem_dir => $opt{'shmem-dir'},
			  });

## Not reached

## Local Variables:
## mode: CPerl
## End: