[Netpass-devel] NetPass/bin npsubagent.pl,1.5,1.6
Brought to you by:
jeffmurphy
From: Matt <mt...@us...> - 2005-06-08 15:40:11
|
Update of /cvsroot/netpass/NetPass/bin In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7312 Modified Files: npsubagent.pl Log Message: Index: npsubagent.pl =================================================================== RCS file: /cvsroot/netpass/NetPass/bin/npsubagent.pl,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- npsubagent.pl 3 Jun 2005 15:16:53 -0000 1.5 +++ npsubagent.pl 8 Jun 2005 15:40:00 -0000 1.6 @@ -1,20 +1,81 @@ #!/usr/bin/perl -w +=head1 NAME + + npsubagent.pl + +=head1 SYNOPSIS + + npsubagent.pl <-o basoid> <-c brctl_cmd> <-m npvnat_macs> <-n npvnat_nummacs> <-t timeout> <-r refreshrate> <-b dev> traphost + -o baseoid snmp base oid + -m npvnat_macs the npvnat macs file in the /proc fs + -n npvnat_nummacs the npvnat nummacs file in the proc fs + -t timeout the amount of time to wait before sending a linkdown + trap for a mac disappearing from out mac table + -r refreshrate how often we will refresh our mac table + -b dev the name of the bridge device (default br0) + -h this message + + +=head1 DESCRIPTION + +This script is an snmp interface to ebtables with the npvnat module. + +=head1 AUTHOR + +Matt Bell <mt...@bu...> + +=head1 LICENSE + + (c) 2004 University at Buffalo. + Available under the "Artistic License" + http://www.gnu.org/licenses/license-list.html#ArtisticLicense + +=head1 REVISION + +=cut + use strict; +use Getopt::Std; +use Pod::Usage; use NetSNMP::agent (':all'); use NetSNMP::OID (':all'); use NetSNMP::ASN (':all'); use FileHandle; -use SNMP; -my $BASEOID = ".1.3.6.1.4.1.8072.9999.9999.7375"; -my $BRCTLCMD = "/usr/local/sbin/brctl showmacs br0 |"; +my %opts; +getopts('o:c:m:n:t:r:h', \%opts); + +my $TRAPHOST = shift; +pod2usage(2) if (!defined $TRAPHOST); +pod2usage(2) if exists $opts{'h'}; + +my $BASEOID = (exists $opts{'o'}) ? $opts{'o'} : ".1.3.6.1.4.1.8072.9999.9999.7375"; +my $REFRESHRATE = (exists $opts{'r'}) ? $opts{'r'} : 5; +my $TIMEOUT = (exists $opts{'t'}) ? $opts{'t'} : 3600; + my $PROCEBTMAC = "/proc/ebtables/npvnat/macs"; +if (exists $opts{'m'} && $opts{'m'} && -e $opts{'m'}) { + $PROCEBTMAC = $opts{'m'}; +} my $PROCEBTNMAC = "/proc/ebtables/npvnat/nummacs"; -my $REFRESHRATE = 5; # refresh rate -my $TIMEOUT = 3600; # 1hr -my $TRAPHOST = "npw2-d.cit.buffalo.edu"; -my $TRAPHOSTCOM = "50ohm"; +if (exists $opts{'n'} && $opts{'n'} && -e $opts{'n'}) { + $PROCEBTNMAC = $opts{'n'}; +} +my $BRCTLCMD = "/usr/local/sbin/brctl"; +if (exists $opts{'c'} && $opts{'c'} && -e $opts{'c'}) { + $BRCTLCMD = $opts{'c'}; +} +my $BRDEV = "br0"; +if (exists $opts{'b'} && $opts{'b'} && -e $opts{'b'}) { + $BRDEV = $opts{'b'}; +} + +die "ERROR, $PROCEBTMAC doesn't exist!" if (!-e $PROCEBTMAC); +die "ERROR, $PROCEBTNMAC doesn't exist!" if (!-e $PROCEBTNMAC); +die "ERROR, $BRCTLCMD doesn't exist!" if (!-e $BRCTLCMD); + +$BRCTLCMD .= " showmacs $BRDEV"; my $fh = new FileHandle(); my $mactable = {}; @@ -58,6 +119,7 @@ delete $mactable->{$m}; # send linkdown trap here... + sendTrap($mactable->{$m}{port}, 'down', $TRAPHOST); } foreach my $m (keys %$mactb) { @@ -72,7 +134,7 @@ $mactable->{$m}{lastseen} = $time; # send linkup trap here... - sendTrap($mactable->{$m}{port}, 'up', $TRAPHOST, $TRAPHOSTCOM); + sendTrap($mactable->{$m}{port}, 'up', $TRAPHOST); } $ltime += $REFRESHRATE; @@ -83,13 +145,12 @@ exit 0; sub sendTrap { - - my($port, $traptype, $traphost, $traphostcom) = @_; + my($port, $traptype, $traphost) = @_; my $enterpriseoid; my $generic; my $portoidbase = $BASEOID.'1'; -print "sending linkup trap\n"; + use Net::SNMP qw(:ALL); # # enterprise oids @@ -105,24 +166,29 @@ $generic = 2; } - my $snmp = new SNMP::Session( - DestHost => $traphost, - RemotePort => 162, - ); + my ($session, $error) = Net::SNMP->session( + -hostname => $traphost, + -port => SNMP_TRAP_PORT, + -community => 'public' + ); - if (!defined($snmp)) { + if (!defined($session)) { warn "Unable to connect to $traphost"; return -1; } -print "about to send trap\n"; - $snmp->trap ( - enterprise => $enterpriseoid, - agent => $traphost, - generic => $generic, - specific => 0, - [[$portoidbase, $port, 1]] - ); -print "trap sent\n"; + + my $res = $session->trap ( + -enterprise => $enterpriseoid, + -agentaddr => $traphost, + -generictrap => $generic, + -specifictrap => 0, + -varbindlist => [$portoidbase.'.'.$port, INTEGER, 1] + ); + + if (!defined $res) { + warn "Unable to send trap ".$session->error(); + return -1; + } return 1; } @@ -164,7 +230,7 @@ my $fh = new FileHandle; my %mtable; - $fh->open($BRCTLCMD) || return -1; + $fh->open("$BRCTLCMD |") || return -1; while (my $l = $fh->getline) { if ($l =~ /(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})\s+no/) { my $m = lc($1); |