Hello,<br><br>Thought I'd share a small script I run from cron each day - it's a quick and dirty way of listing interfaces on your switches that are bouncing a lot .. something that's often missed in the busy day of a network administrator.<br>
<br>It works by reading through ironview's syslog file, so expects ironview's slightly ugly syslog format (and also copes with its strange behaviour of starting a uniquely named file each day.) You can hack it to match for syslog-ng systems.<br>
<br>You will need: perl.<br><br>Apologies if gmail screws up the formatting! And run at your own risk etc - no responsibility taken if it eats your network.<br><br><span style="font-family: courier new,monospace;">#!/usr/bin/perl -w</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># bouncing-interfaces.pl, Nick Morrison, 11/5/2009</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># Simple script to look for a high number (where "high" is defined below) of</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># interface state changes, and report on them.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># Good for alerting the network team to misconfigured server NICs or switch ports.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">use strict;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">use POSIX qw (strftime);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">use Getopt::Std;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">our ( $opt_h, $opt_t, $opt_d, $opt_l );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">getopts ('hdl:s:t:');</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">####################################################</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># Configurable settings</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">####################################################</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;">my $today = strftime "%m%d%y", localtime;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">my $logfile = "/ironview/logs/syslog$today.log";</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">my $switch;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># bounce_threshhold tells the script how many bounces it's worth alerting for</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">my $bounce_threshhold = 15;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">####################################################</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># End of configurable settings</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">####################################################</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;">$logfile = $opt_l if defined $opt_l;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">$bounce_threshhold = $opt_t if defined $opt_t;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">if (defined $opt_h) {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print <<EOM</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Looks for repetitious interface up/down messages in the logfile</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">specified with -l (or today's ironview syslog, by default) and</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">prints its findings.</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Options:</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> -h this help text</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> -l filename specify a log file to look through</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> -t n set the number-of-bounces threshhold to report on</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><span style="font-family: courier new,monospace;"> -s switch look only for the named switch (not implemented! use grep.)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">EOM</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ;exit;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;"># Open our file for reading</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">open FILE, "<$logfile" or die $!;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># We need a hash to store the bounce counts in</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">my %boing;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># Read each line of the logfile</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">while (<FILE>) {</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # Look for lines that look like an interface state change</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if (/Sender:(\S+).+Message:System: Interface ethernet (\S+), state (\S+)/) {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> $boing{$1}{$2}{$3}++;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # $1 should be the hostname</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # $2 should be the interface number</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # $3 should be the state (up or down)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># Produce some output</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">foreach my $switch (sort keys %boing) {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> foreach my $int (sort keys %{$boing{$switch}}) {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if (defined ($boing{$switch}{$int}{"down"}) ) { # this might not be defined,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # if an interface came UP but didn't go down</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if ( $boing{$switch}{$int}{"down"} > $bounce_threshhold ) {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print "$switch ethernet $int has bounced " . $boing{$switch}{$int}{"down"} . " times today.\n";</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">}</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># Be polite and explicitly close the log file</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">close FILE;</span><br>
<br><br>Cheers!<br>Nick<br>-- <br>Nick Morrison <<a href="mailto:nick@nick.on.net">nick@nick.on.net</a>><br><br>