[mrtg] Re: bgp mointoring with mrtg
Bill Wichers
billw at waveform.net
Thu Jun 24 20:55:56 MEST 2004
Here is my script for all who asked. IP and AS info has been changed to
protect the innocent of course, but there everything should be easy to
follow and get working. You need net::telnet for perl to be able to use
this, which is a very handy module allowing you to telnet with simple code
from perl. Better programmers maybe could shrink my script down, but it
works for me so I'm happy with it :-)
If you find it useful let me know, and you can modify it as you see fit
just leave some credit to me somewhere... If anyone enhances it I would
appreciate if you send me what you come up with.
-Bill
=====
The script is below (with the MRTG config file stuff I use to run it). You
will need to put in your router's IP and the password you use (it only
needs regular shell, not 'enable'). The peers are peer1 and peer2, you can
see where they come from in the example output from the cisco. You're
probably going to need to fiddle with the following lines:
$peer1_bgp = @lines[$lineslength-5];
$peer2_bgp = @lines[$lineslength-4];
and change the -5 and -4 numbers. Those two lines are pulling the relevant
lines out from all the stuff returned by the 'show ip bgp sum' command.
Just so you have a reference, here are the last few lines of the output of
that command from the router this script watches:
Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down
State/PfxRcd
IP_1 4 AS_1 584530 55509 32669247 0 0 6d11h 121488 <= PEER1
IP_2 4 AS_2 8389193 12206804 32669252 0 0 6d12h 117477 <= PEER2
IP_3 4 AS_3 3197 244973 32669252 0 0 2d00h 2
IP_4 4 AS_3 199 123292 0 0 0 2d00h Active
sfld1core1>
The script reads UP from the bottom and LEFT from right, to get the 121488
number and send it to MRTG as the first variable, and the 117477 as the
second variable. If you only have two peers you would need to change the
numbers to -3 and -2 so that the script will look at the correct lines.
The script uses mod::telnet to telnet in, then waits for the login prompt,
sends a password, waits for a command prompt, and then runs the 'show ip
bgp sum' command and puts the output into an array before closing the
telnet
connection. The script will timeout if it takes more than 10 seconds to do
all that.
The last two lines of the script give MRTG the last few things it wants to
see, which are an uptime ("All Eternity" :-), and a router name, which in
this case is "Southfield 7507", which is the particular router on our
network that this script watches.
---- snip 8< ----
#!/usr/bin/perl
#
# MRTG BGP prefix graphing script for Cisco Routers
# Written by Bill Wichers, billw at waveform.net, in April of 2003
# Modify as you see fit, just leave some credit to me somewhere,
# and send any useful additions you may add to me.
# No guarantees, YMMV, I just hope this is useful to you.
#
# This script will run `show ip bgp sum` on a Cisco router
# and report the BGP prefixes heard from two peers to MRTG for
# graphing.
#
# Replace ROUTER_PASSWORD with a password that has
# access to the router and can run the necassary command.
# Replace ROUTER_IP_ADDRESS with address of your router.
# Replace ROUTER_NAME with the name of the router as seen on
# the IOS command prompt.
use Net::Telnet ();
$t = new Net::Telnet (Timeout => 10,
Prompt => '/[\>\#]$/'
);
$t->open("ROUTER_IP_ADDRESS");
$t->waitfor(String => 'Password:');
$t->print("ROUTER_PASSWORD");
$t->waitfor(String => 'ROUTER_NAME>');
@lines = $t->cmd("show ip bgp sum");
# Get number of lines returned
$lineslength=@lines;
# Pull out the lines pertaining to our two interesting peers
# and put the lines into strings
$peer1_bgp = @lines[$lineslength-5];
$peer2_bgp = @lines[$lineslength-4];
# Split strings at spaces, so each value in each string becomes
# a separate value in a new array
@peer1_routes = split(/\s/,$peer1_bgp);
@peer2_routes = split(/\s/,$peer2_bgp);
# Get length of new strings
$peer1_length=@peer1_routes;
$peer2_length=@peer2_routes;
# BGP prefix number is last value in each array, which is one position
# before the end-of-line character
print @peer1_routes[$peer1_length-1];
print "\n";
print @peer2_routes[$peer2_length-1];
print "\n";
# Keep MRTG happy with an uptime value and system name
print "All Eternity\n";
print "ROUTER_NAME\n";
---- snip 8< ----
MRTG config file info: (note that I have the graph scaled to be rather large)
---- snip 8< ----
# Graph BGP prefixes for peer 1 and peer 2
Target[bgpprefixes]: `/usr/local/mrtg/bgp-prefixes.pl`
MaxBytes[bgpprefixes]: 150000
Options[bgpprefixes]: gauge, nopercent, growright
Title[bgpprefixes]: BGP Route Prefixes
PageTop[bgpprefixes]: <h1>BGP Route Prefixes</h1>
YLegend[bgpprefixes]: BGP Route Prefixes
Ysize[bgpprefixes]: 300
Ytics[bgpprefixes]: 15
ShortLegend[bgpprefixes]: Routes
kMG[bgpprefixes]:
LegendI[bgpprefixes]: peer 1 Prefixes
LegendO[bgpprefixes]: peer 2 Prefixes
Legend1[bgpprefixes]:
Legend2[bgpprefixes]:
---- snip 8< ----
> Unsubscribe mailto:mrtg-request at list.ee.ethz.ch?subject=unsubscribe
> Archive http://www.ee.ethz.ch/~slist/mrtg
> FAQ http://faq.mrtg.org Homepage http://www.mrtg.org
> WebAdmin http://www.ee.ethz.ch/~slist/lsg2.cgi
>
>
*****************************
Waveform Technology
UNIX Systems Administrator
--
Unsubscribe mailto:mrtg-request at list.ee.ethz.ch?subject=unsubscribe
Archive http://www.ee.ethz.ch/~slist/mrtg
FAQ http://faq.mrtg.org Homepage http://www.mrtg.org
WebAdmin http://www.ee.ethz.ch/~slist/lsg2.cgi
More information about the mrtg
mailing list