Skip to content

Instantly share code, notes, and snippets.

@aleks-mariusz
Last active August 29, 2015 14:00
Show Gist options
  • Save aleks-mariusz/1399f31e122cbe88b809 to your computer and use it in GitHub Desktop.
Save aleks-mariusz/1399f31e122cbe88b809 to your computer and use it in GitHub Desktop.
Mar 10 2004 - There's a decent amount one can accomplish with basic shell scripting, though you reach your limits eventually with what you can do with sed/awk and so some perl code sneaks into here.. which is a network management utility i wrote while at an internet service provider for creating index pages from a set of mrtg-monitored devices
#!/usr/local/bin/bash
if [ X"$(whoami)" != "Xroot" ]
then
echo "This program now must be run as root.."
exit
fi
INTERFACES='cs1 cs2 cs3 cs4 cs5 cs7 cs8 cs9 cs10 cs11 cs12 cs13 cs14 cs15 cs20 cs20b cs21 cs22 cs23 cs24 cs25 cs30 cs30b cs40b csmarigold core01 core02 core04 core05 core06'
WALKDIR="/home/cricket/walk"
WORKDIR="/home/cricket/mrtg"
COMMUNITY="nyinet"
CONF="$COMMUNITY.cfg"
LOCKFILE="$WALKDIR/.lock"
if [ -f $LOCKFILE ]
then
echo "in the middle rewalking, cannot rewalk again until other walk finishes"
exit
fi
touch $LOCKFILE
echo "rewalking the following interfaces: $INTERFACES"
for interface in $INTERFACES ; do
interface_ip=$(host -t a $interface.nyi.net 2>/dev/null|awk '{print $4}')
if [ $(grep -c $interface /etc/hosts) -eq 1 ]; then
interface_ip=$(awk '/'"$interface"'/ {print $1}' /etc/hosts)
fi
if [ "X$interface_ip" != "X" ]; then
echo -n "now walking $interface ($interface_ip)... "
if [ -d $WALKDIR.old ]
then
cp $WALKDIR/$interface.raw $WALKDIR.old
fi
/usr/local/bin/cfgmaker ${COMMUNITY}@${interface_ip} \
2> $WALKDIR/$interface.status 1> $WALKDIR/$interface.raw
mkdir -p $WORKDIR/devices/$interface_ip
echo -e "\tnow creating $interface.html..."
perl -p -i -e '
s@# WorkDir: /home/http/mrtg@WorkDir: '"$WORKDIR"'@' \
$WALKDIR/$interface.raw
if [ -d $WALKDIR.old ]
then
cp $WALKDIR/$interface $WALKDIR.old
fi
cat $WALKDIR/$interface.raw|awk '$1 !~ /^#/ && $1 !~ /^$/ {print}'|perl -e '
while(<>) {
chomp $_;
if (/^Target\[(([^]]+)_(\d+))\]: .+$/) {
$target = $1; $source = $2;
$number = sprintf("%03d",$3);
$source = "devices/$source/$number";
print; print "\n";
print "Directory[$target]: $source\n";
} else {
print; print "\n";
}
}' > $WALKDIR/$interface
/usr/local/bin/indexmaker --columns=1 --section=descr \
$WALKDIR/$interface > $WORKDIR/devices/$interface.html
perl -p -i -e '
s#HREF="devices/#HREF="#;
s#SRC="devices/[^/]+/.../../devices/#SRC="#' \
$WORKDIR/devices/$interface.html
fi
done
mv $WALKDIR/$CONF $WALKDIR/$CONF.old
>$WALKDIR/$CONF
echo -n "now building $CONF using "
for interface in $INTERFACES ; do
if [ -f $WALKDIR/$interface ]; then
numLines=$(wc -l $WALKDIR/$interface|awk '{print $1}')
tail -$[numLines-1] $WALKDIR/$interface >> $WALKDIR/$CONF
echo -n "$interface "
fi
done
if [ -f $WORKDIR/$CONF ]; then
mv $WORKDIR/$CONF $WORKDIR/old/$CONF.$(date '+%y%m%d%H%M')
fi
cat $WORKDIR/mrtg.options $WALKDIR/$CONF > $WORKDIR/$CONF
if [ -x mrtg/threshold/thresholdUpdate.pl ]; then
mrtg/threshold/thresholdUpdate.pl $WORKDIR/$CONF
chown -R www mrtg/threshold/conf
fi
echo "... done."
chown -R cricket mrtg/devices
CHANGES="$(diff -u $WALKDIR/$CONF.old $WALKDIR/$CONF|egrep '^(\+|-)(SetEnv|Title)')"
if [ "X$CHANGES" != "X" ]
then
echo
echo "=== CHANGES DETECTED =="
echo "Here's a list of changes made since the last walk:"
echo "$CHANGES"
echo "===== lines beginning with + are additions, lines beginning with - are deletions.. ======"
echo
fi
echo "Restarting MRTG.. (PLEASE IGNORE ANY WARNINGS ABOUT A BAIL OUT, THIS IS NORMAL)"
/usr/local/etc/rc.d/mrtg.sh stop 2>/dev/null
sleep 1
/usr/local/etc/rc.d/mrtg.sh start
rm -f $LOCKFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment