Blog IndexPosts by TagHome

Cisco Switch Graphing

Posted <2017-01-23 Mon 22:02> by Aaron S. Jackson.

Not sure how well this will work yet but I have written a script to generate a graph for each switch port on Cisco switches. It uses expect and gnuplot. You can see it in action here.

Here is the script:

#!/usr/local/bin/bash
pushd /home/aaron/logging/

expect -f getstats.exp > /dev/null

t=$(date +%s)

grep FastEthernet summary |
    awk '{ print NR,$7/1024/1024,$9/1024/1024 }' |
    xargs -L1 sh -c 'echo '$t',$1,$2 >> int$0'

for f in int*; do
    int=$(cat $f)
    echo "$int" | tail -n288 > $f # cleanup

    gnuplot <<EOF > /var/www/htdocs/graphs.aaronsplace.co.uk/lana/$f.svg
set terminal svg
set datafile sep ','
set title "Interface $f TX/RX (lana.nat.rhwyd.co.uk)"
set xlabel "Time"
set timefmt "%s"
set format x "%d/%m/%Y %H:%M"
set xdata time
set xtics 1800 rotate
set ylabel "Mbps"
set grid
plot '$f' using 1:2 title 'RX' with lines, \
     '$f' using 1:3 title 'TX' with lines
EOF

done

popd

It needs the following expect script, but you'll need to update your host name and password.

set timeout 5
set hostname "hostname"
set password "password"

spawn telnet $hostname

expect "Password:"
send "$password\r"
expect "*>" {
    send "terminal length 0\r"
    expect "*>*"

    send "enable\r"
    expect "*Password:"
    send "$password\r"
    expect "*#" {
        send "show interface summary\r"
        log_file -noappend "summary"
        expect "*#"
    }

    send "exit\n"
    expect *

}

<2017-03-12 Sun> I have made a few changes to the script:

#!/usr/local/bin/bash
pushd /home/aaron/logging/

plotsize=600,420
base=/var/www/htdocs/graphs.aaronsplace.co.uk/lana

expect -f getstats.exp > /dev/null

t=$(date +%s)

grep '^*' summary |
    sed 's/FastEthernet/fa/' | tr '/' '_' |
    awk '{ dest="int_" tolower($2) }
         { print '$t'"," $7/1024/1024 "," $9/1024/1024 >> dest }'

rm past24h_*
rm past7d_*

files=int*
for f in $files; do
    cat $f | awk -F, -v t="$t" '$1 > (t-(60*60*24))' > past24h_$f

    sum=$(tail  past24h_$f  | awk -F, '{ sum += $2+$3 } END { print sum }')
    if [ "$sum" == '0' ]; then rm past24h_$f; fi
done

rm $base/*.gif

cat <<EOF > $base/index.html
<!DOCTYPE html>
<html>
  <head>
    <title>lana.nat.rhwyd.co.uk</title>
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
  </head>
  <body>
    <h1>lana.nat.rhwyd.co.uk switch</h1>
    <ul>
      <li>fa0/1 connects to router</li>
      <li>fa0/2 connects to wireless access point</li>
    </ul>
    <p>
      Script
      available <a href="http://www.aaronsplace.co.uk/blog/2017-01-23-cisco-graphing.html">here</a>. The
      graphs are using a logarithmic scale for the y axis to prevent
      spikes from hiding the lower amounts of traffic (which I usually
      find more interesting).
    </p>
<pre>
EOF

grep uptime ver >> $base/index.html
grep processor ver >> $base/index.html
grep interface ver >> $base/index.html

cat <<EOF >> $base/index.html
</pre>
    <hr />
      <img src="rx.gif" />
      <img src="tx.gif" />
      <hr />
EOF


for f in past24h_*; do
    name=$(echo $f | sed 's/past24h_int_//' | tr '_' '/')
    gnuplot <<EOF
set terminal gif small size $plotsize
set output '$base/$f.gif'
set datafile sep ','
set title "$name 24 Hour TX/RX (lana.nat.rhwyd.co.uk)"
set xlabel "Time"
set timefmt "%s"
set format x "%H:%M"
set xdata time
set xtics 7200
set ylabel "Mbps"
set grid
set logscale y
plot '$f' using 1:2 title 'RX' with lines, \
     '$f' using 1:3 title 'TX' with lines
EOF

    cat <<EOF >> $base/index.html
      <img src="$f.gif" />
EOF
done

rx="
set terminal gif small size $plotsize
set output '$base/rx.gif'
set datafile sep ','
set title '24 Hour Combined RX (lana.nat.rhwyd.co.uk)'
set xlabel 'Time'
set timefmt '%s'
set format x '%H:%M '
set xdata time
set xtics 7600
set ylabel 'Mbps'
set grid
set logscale y
set key rmargin center vertical
plot "

tx="
set terminal gif small size $plotsize
set output '$base/tx.gif'
set datafile sep ','
set title '24 Hour Combined TX (lana.nat.rhwyd.co.uk)'
set xlabel 'Time'
set timefmt '%s'
set format x '%H:%M '
set xdata time
set xtics 7600
set ylabel 'Mbps'
set grid
set logscale y
set key rmargin center vertical
plot "

for f in past24h_*; do
    name=$(echo $f | sed 's/past24h_int_//' | tr '_' '/')
    rx="$rx '$f' using 1:2 with lines title '$name',"
    tx="$tx '$f' using 1:3 with lines title '$name',"
done

echo "$rx" | gnuplot
echo "$tx" | gnuplot

cat <<EOF >> $base/index.html
  </body>
</html>
EOF

popd

Related posts:

Tags: networking

Blog IndexPosts by TagHome

Copyright 2007-2017 Aaron S. Jackson (modified: Sun 23 Jul 14:30:46 BST 2017)