Sunday, November 11, 2012

MRTG Start Up Script for Ubuntu

MRTG as a Start Up Service in Ubuntu

I am monitoring a lot of routers and switches of my organization using MRTG ( Multi Router Traffic Grapher ) . It is one of the best tools to give you an overview of bandwidth utilizations, CPU/Memory Utilizations etc. Here is a small bash script to make all these MRTG process act as a service which is automatically start up at reboot.

Details of MRTG

I am using MRTG with RRDTool using routers.cgi script. My MRTG configurations arrangements are like
  • Location1 Folder
    • router.cfg
    • switch.cfg
    • servers.cfg
  • Location2 Folder
    • router.cfg
    • switch.cfg
    • servers.cfg
  • Location3 Folder
    • SubLocation1 Folder
      • router.cfg
      • switch.cfg
      • servers.cfg
    • SubLocation2 Folder
      • router.cfg
      • switch.cfg
      • servers.cfg
    • router.cfg

mrtg script


#! /bin/bash
#
#  Written by Irfan Naseef  <irfannaseefp at gmail dot com>.
#  Modified for Debian GNU/Linux
#  Modified for MyOrganization

### BEGIN INIT INFO
# Provides:          mrtg
# Required-Start:    $local_fs $remote_fs $syslog $named $network $time
# Required-Stop:     $local_fs $remote_fs $syslog $named $network
# Should-Start:      
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: MRTG Daemon
# Description:       MRTG or Multi Router Traffic grapher.
### END INIT INFO



if ! [ -x "/lib/lsb/init-functions" ]; then
  . /lib/lsb/init-functions
else
  echo "E: /lib/lsb/init-functions not found, lsb-base (>= 3.0-6) needed"
  exit 1
fi

DAEMON=/usr/bin/mrtg
CONFDIR=/home/iocl/mrtg/cfg
VARLOCK=/var/lock/mrtg

[ -x "$DAEMON" ] || exit 0

startfunc(){

 locdir=$(dirname $1)
 bname=$(basename $1 .cfg)
 pid_file=$locdir/$bname.pid
 cfg_file=$1
 [ -e $pid_file ] && rm -f $pid_file
 if [ -e $cfg_file ] 
 then  
  $DAEMON $cfg_file
  status=$?
  if [ $status -eq 0 ]
  then
   echo "Demonizing $bname.cfg"
  else
   echo "Error in starting $1"
  fi
 else
  echo "Error : No configuration exist for $1"
 fi
}

stopfunc(){
 bname=$(basename $1 .pid)
 pid=$(cat $1)
 if [ $pid -eq $pid 2> /dev/null ]
 then
  kill $pid
  echo "Killing $bname with pid $pid"
 else
  echo "unknow file $bname: Not a pid file"
 fi 
}

mrtg_start(){

items=`ls $1 | grep -v '.pid\|.sh\|.bat'`

for i in ${items[@]}
do

 if [ -f $1/$i ]
 then
  startfunc $1/$i
 elif [ -d $1/$i ]
 then
  mrtg_start "$1/$i"
 fi

done

}

mrtg_stop(){

items=`ls $1 | grep -v '.cfg\|.sh\|.bat'`

for i in ${items[@]}
do

 if [ -f $1/$i ]
 then
  stopfunc $1/$i
 elif [ -d $1/$i ]
 then
  mrtg_stop "$1/$i"
 fi

done
}

# Carry out specific functions when asked to by the system
case "$1" in
  start)
 echo  "Starting MRTG"
 if [ ! -d $VARLOCK ]
 then
  mkdir $VARLOCK
 fi

 mrtg_start $CONFDIR
    ;;
  stop)
     echo "Stopping MRTG"
     mrtg_stop $CONFDIR
    ;;
  restart|reload)
 echo "Restarting MRTG"
 mrtg_stop $CONFDIR
 mrtg_start $CONFDIR
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
esac

exit 0

Save this script into /etc/init.d . Now use update-rc.d command for adding this script to various run levels. Add this to default runlevel using this command .
  sudo update-rc.d mrtg defaults
Now from the next reboot onwards MRTG service will be automatically started.
Also you can manage MRTG using service command. For stopping all the mrtg processes
sudo service mrtg stop
For starting all the MRTG processes
sudo service mrtg start
For restarting all the MRTG processes
sudo service mrtg restart

NOTE: This is a System-V model init script. Now Ubuntu started supporting an event-based init script-Upstart also. More details on Upstart you can check out here http://upstart.ubuntu.com/cookbook/

No comments:

Post a Comment