[rancid] Logging CDP state Re: sorting output from custom commands...
Jon Peatfield
J.S.Peatfield at damtp.cam.ac.uk
Tue Mar 29 12:34:46 UTC 2011
On Wed, 29 Sep 2010, Jon Peatfield wrote:
...
> The current code I'm testing seems to give the answers I want though it does
> assume that all devices for port P will be consecutive in the output...
...
> I'll be happy to contribute what I end up with if anyone else wants this.
Some time has passed and I realized yesterday that I'd not actually sent
the version of the patches that we have been using.
So here they are, the first is very simple and is just for cisco boxes
where we simply have to remove the holdtime from the output, and the
second is for HP procurve and is slightly more complex since it does the
sorting or the device-ids shown on each port.
Note that this is running 'show cdp neighbors detail' so the output can be
quite long and you may well get lots of flapping if devices come/go so I'm
sure that this isn't for everyone!
Hopefully they might be of use to at least someone.
Although these patches say 2.3.4 in the filenames I've tested that they
apply cleanly to 2.3.6 and since yesterday that is what we are now using.
-- Jon
--
/--------------------------------------------------------------------\
| "Computers are different from telephones. Computers do not ring." |
| -- A. Tanenbaum, "Computer Networks", p. 32 |
---------------------------------------------------------------------|
| Jon Peatfield, _Computer_ Officer, DAMTP, University of Cambridge |
| Mail: jp107 at damtp.cam.ac.uk Web: http://www.damtp.cam.ac.uk/ |
\--------------------------------------------------------------------/
-------------- next part --------------
--- rancid-2.3.4/bin/rancid.in.ccdp 2010-07-08 02:48:20.000000000 +0100
+++ rancid-2.3.4/bin/rancid.in 2010-08-19 20:54:35.000000000 +0100
@@ -570,6 +570,34 @@
return(0);
}
+# A test routine for parsing the output of "show cdp neighbors detail"
+sub ShowCDPNeighborsDetail {
+ print STDERR " In ShowCDPNeighborsDetail: $_" if ($debug);
+
+ while (<INPUT>) {
+ tr/\015//d;
+ last if (/^$prompt/);
+ next if (/^(\s*|\s*$cmd\s*)$/);
+ return(1) if /^\s*\^\s*$/;
+ return(1) if /Line has invalid autocommand /;
+ return(1) if /(Invalid input detected|Type help or )/;
+ return(-1) if (/command authorization failed/i);
+ # the pager can not be disabled per-session on the PIX
+ if (/^(<-+ More -+>)/) {
+ my($len) = length($1);
+ s/^$1\s{$len}//;
+ }
+
+ # Skip Holdtime line, or the row of --- at the start of output
+ next if (/^(Holdtime\s|-------)/);
+
+ ProcessHistory("COMMENTS","keysort","IO","!CDP: $_");
+ }
+ ProcessHistory("COMMENTS","keysort","IO","!\n");
+ return(0);
+}
+
+
# This routine parses "show rsp chassis-info" for the rsp
# This will create arrays for hw info.
sub ShowRSP {
@@ -1883,6 +1911,7 @@
{'show bootvar' => 'ShowBoot'},
{'show variables boot' => 'ShowBoot'},
{'show flash' => 'ShowFlash'},
+ {'show cdp neighbors detail' => 'ShowCDPNeighborsDetail'},
{'dir /all nvram:' => 'DirSlotN'},
{'dir /all bootflash:' => 'DirSlotN'},
{'dir /all slot0:' => 'DirSlotN'},
-------------- next part --------------
--- rancid-2.3.4/bin/hrancid.in.pcdp 2010-06-23 00:17:30.000000000 +0100
+++ rancid-2.3.4/bin/hrancid.in 2011-03-28 18:09:35.000000000 +0100
@@ -283,6 +283,51 @@
return(0);
}
+# A simple routine for parsing the output of "show cdp neighbors detail"
+sub ShowCDPNeighborsDetail {
+ print STDERR " In ShowCDPNeighborsDetail: $_" if ($debug);
+
+ my $currport=' ';
+ my $deviceid='';
+ my $cdpdblock='';
+ my $cdpdblockl=0;
+
+ while (<INPUT>) {
+ tr/\015//d;
+ last if (/^$prompt/);
+ next if (/^(\s*|\s*$cmd\s*)$/);
+ return(-1) if (/command authorization failed/i);
+ return(1) if /^(Invalid|Ambiguous) input:/i;
+ # Skip the rows of --- or blank lines
+ next if (/^(-------|\s*$)/);
+ # Now strip off any whitespace at the end of the line!
+ s/\s+$//;
+ chomp();
+ if (/^\s*Port\s*:\s*(.*)/) {
+ my $newport=$1;
+ print STDERR " Found cdp port $1 (currport=$currport) sending $cdpdblockl lines\n";
+ # send any previous device-block
+ ProcessHistory("COMMENTS$currport","keysort","IO $deviceid",$cdpdblock);
+ $currport=$newport;
+ $cdpdblock='';
+ $cdpdblockl=0;
+ $deviceid='';
+ } elsif (/^\s*Device ID\s*:\s*(.*)/) {
+ $deviceid=$1;
+ }
+ # Accumulate all the lines into the current block...
+ $cdpdblockl++;
+ print STDERR " Adding to cdp block (port=$currport dev=$deviceid): $_\n";
+ $cdpdblock.=";CDP: $_\n";
+ }
+ # And deal with the last block...
+ print STDERR " Last cdp port $1 (currport=$currport) sending $cdpdblockl lines\n";
+ $cdpdblock.=";\n";
+ ProcessHistory("COMMENTS$currport","keysort","IO $deviceid",$cdpdblock);
+ return(0);
+}
+
+
# This routine processes a "write term"
sub WriteTerm {
print STDERR " In WriteTerm: $_" if ($debug);
@@ -472,6 +517,7 @@
{'show flash' => 'ShowFlash'},
{'show system-information' => 'ShowSystem'},
{'show system information' => 'ShowSystem'},
+ {'show cdp neighbors detail' => 'ShowCDPNeighborsDetail'},
{'show module' => 'ShowModule'},
{'show stack' => 'ShowStack'},
{'write term' => 'WriteTerm'}
More information about the Rancid-discuss
mailing list