#!/bin/bash
#
# $id: makemrtg.sh build 2005/06/18 by makoecke@lansys.ch
#
# SYNOPSIS:
#       Build snmp multi router traffic grapher to html indexed pages
#       The excellent MRTG package is required from http://people.ee.ethz.ch/~oetiker/webtools/mrtg/
# params:
#       docroot, mrtg work directory
#       columns, how many columns the index build (mostly 1 or 2)
#
#       option1, snmp community of router switch or host
#       option2, the host ip
#       option3, mrtg_cfg_file without (.cfg) extension
#
# Exp. use: ./makemrtg.sh community host_ip host_name
#
# changes here if any needed

docroot=/home/www/root.a-enterprise.ch/web/html/mrtg/router
mibstxt=/usr/share/snmp/mibs/UCD-SNMP-MIB.txt
cfgroot=/etc/mrtg
machine=`hostname`
operate=`uname -o -p -r`
columns=1

# lets go to that
if [ "$1" = $1 2> /dev/null ] ; then
cfgmaker --global "WorkDir: $docroot" --global "Background[_]: #D7D8EE" \
--global "XSize[_]: 480" --global "Options[_]: growright, bits" \
--global "LoadMIBs: $mibstxt" \
--ifdesc=descr \
--ifref=ip $1@$2 > $cfgroot/$3.cfg

# make html index pages
indexmaker --columns=$columns --section=descr \
--pagetop="<h1>MRTG on $machine $operate</h1>" --bodyopt="BGCOLOR=\"#D7D8EE\"" \
--title="Traffic Analyse von $2 $3" --enumerate $cfgroot/$3.cfg --output=$docroot/$3.html

# generate the graphs
mrtg $cfgroot/$3.cfg > /dev/null
mrtg $cfgroot/$3.cfg > /dev/null
sleep 2; mrtg $cfgroot/$3.cfg > /dev/null
mrtg $cfgroot/$3.cfg > /dev/null

# append mrtg task in to a executable file which are controled by crontab
echo "mrtg $cfgroot/$3.cfg" >> $cfgroot/mrtgrun.sh
echo "biuld stylesheet.."
sleep 2

# make a nice style face
sed -e '9i\
 \
<style type="text/css"> \
<!-- \
body { background-color: #D7D8EE; color: #000000; \
font-family: Arial, Helvetica, sans-serif; font-size: 10pt; } \
table,tr,td { color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 10pt; } \
--> \
</style>' $docroot/$3.html > $docroot/$3xx.html
cp $docroot/$3xx.html $docroot/$3.html
rm -f $docroot/$3xx.html

sed -e '12i\
 \
<style type="text/css"> \
<!-- \
body { background-color: #D7D8EE; color: #000000; \
font-family: Arial, Helvetica, sans-serif; font-size: 12pt; } \
table,tr,td { color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 12pt; } \
--> \
</style>' $docroot/$2_$2.html > $docroot/$2_$2xx.html
cp $docroot/$2_$2xx.html $docroot/$2_$2.html
rm -f $docroot/$2_$2xx.html


# the final
echo ""
echo "build mrtg for community $1 host $3 target $2"
echo "making index for $3"
ls $docroot/$3.html
echo "makemrtg done"
 else
  echo ""
  echo "fail: no arguments"
  echo " use: ./makemrtg.sh community 192.168.1.1 router_name  (without .cfg)"
 fi
