[rancid] sorting output from custom commands...

Jon Peatfield J.S.Peatfield at damtp.cam.ac.uk
Wed Sep 29 00:45:45 UTC 2010


On Tue, 28 Sep 2010, john heasley wrote:

> ProcessHistory(tag, command, command_arg, data)
>
> data is added the buffer, by command with command_arg (if specified),
> unless tag or command has changed since the last call, in which case
> the buffer would be flushed before calling command.
>
> a good example, where $aclsort = "ipsort" (the function)
>
>        # order access-lists
>        /^access-list\s+(\d\d?)\s+(\S+)\s+(\S+)/ &&
>            ProcessHistory("ACL $1 $2","$aclsort","$3","$_") && next;


So since the output is multi-line per section/device I guess I still need 
to collect together the lines for each device-id so we can pass that in 
the string that the sort function uses...

I'm including a few some bits of output from a switch:

fl10-v2# show cdp nei det

  CDP neighbors information

   Port : 1
   Device ID : SEP001FCA369E19
   Address Type : IP
   Address      : 172.18.18.179
   Platform     : SIP11.8-5-3SCisco IP Phone 7911
   Capability   :
   Device Port  : Port 1
   Version      : SIP11.8-5-3SCisco IP Phone 7911

------------------------------------------------------------------------------

   Port : 1
   Device ID : 01 ac 12 12 b3
   Address Type : IP
   Address      : 172.18.18.179
   Platform     : Cisco IP Phone 7911G,V5, SIP11.8-5-3S
   Capability   : Switch
   Device Port  : 001FCA369E19:P1
   Version      : Cisco IP Phone 7911G,V5, SIP11.8-5-3S

------------------------------------------------------------------------------

   Port : 2
   Device ID : SEP001FCA369FE8
   Address Type : IP
   Address      : 172.18.18.181
   Platform     : SIP11.8-5-3SCisco IP Phone 7911
   Capability   :
   Device Port  : Port 1
   Version      : SIP11.8-5-3SCisco IP Phone 7911

------------------------------------------------------------------------------

   Port : 2
   Device ID : 01 ac 12 12 b5
   Address Type : IP
   Address      : 172.18.18.181
   Platform     : Cisco IP Phone 7911G,V5, SIP11.8-5-3S
   Capability   : Switch
   Device Port  : 001FCA369FE8:P1
   Version      : Cisco IP Phone 7911G,V5, SIP11.8-5-3S
...


For each block of lines corresponding to a device I need to call 
ProcessHistory with the device-id as something to sort on...

Or in code something like:

     my $currport=' ';
     my $deviceid='';
     my $cdpdblock='';
     while (<INPUT>) {

... usual-tests and skip junk...

 	if (/^\s*Port\s*:\s*(.*)/) {
 	    my $newport=$1;
 	    # send any previous device-block
 	    ProcessHistory("COMMENTS$currport","keysort","IO $deviceid",$cdpdblock);
 	    $currport=$newport;
 	    $cdpdblock='';
 	    $deviceid='';
 	} elsif (/^\s*Device ID\s*:\s*(.*)/) {
 	    $deviceid=$1;
 	}
 	# Accumulate all the lines into the current block...
 	print STDERR " Adding to cdp block (port=$currport dev=$deviceid): $_\n";
 	$cdpdblock.=";CDP: $_\n";
     }

     # And deal with the last block...
     $cdpdblock.=";\n";
     ProcessHistory("COMMENTS$currport","keysort","IO $deviceid",$cdpdblock);
...

which seems to work - at least for my simple tests.

I may have it use "keyrsort" (like keysort but backwards) simply because 
that seems to be closer to the unsorted output I get from more of our 
switches, or take the bigger hit once.

Many thanks.

-- 
/--------------------------------------------------------------------\
| "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/ |
\--------------------------------------------------------------------/


More information about the Rancid-discuss mailing list