MRTG网络流量监控自动化安装脚本

安装配置

#!/bin/bash
###cfgmaker.sh
_CFGMAKER_PROG="/usr/bin/cfgmaker"
_MRTG_SCRIPT="/var/www/html/mrtg/newmrtg.sh"
_GLOBAL="--global "workdir:/var/www/html/mrtg/new"
        --global "options[_]:growright,bits"
        --global "runasdaemon:yes"
        --global "interval:5"
        --global "language:chinese"
        --no-down
        --ifref=ip
        --subdirs=HOSTNAME"
_CFGS_DIR="/var/www/html/mrtg/cfg"
_ALL_CFGS="$_CFGS_DIR/all.cfg"
_DEV[0]="aa"
_COMMUNITY[0]="[email protected]"
_CFG[0]="$_CFGS_DIR/_AA.cfg"
_DEV[1]="bb"
_COMMUNITY[1]="[email protected]"
_CFG[1]="$_CFGS_DIR/_BB.cfg"
_DEV[2]="cc"
_COMMUNITY[2]="[email protected]"
_CFG[2]="$_CFGS_DIR/_CC.cfg"
_DEV[3]="dd"
_COMMUNITY[3]="[email protected]"
_CFG[3]="$_CFGS_DIR/_DD.cfg"
function _cfg_maker ()
{
_ROUTER_NAME=$1         ### Here $1 is the COMMUNITY in the route
_OUTPUT_CFG=$2          ### Here $2 is the OUTPUT to save the cfg-file
$_CFGMAKER_PROG $_GLOBAL $_ROUTER_NAME --output=$_OUTPUT_CFG
}
function _usage ()
{
echo "Usege: `basename $0` "DEV-NAME""
echo 'Here, you can choose the "DEV-NAME" as the following list:'
for ((i=0;i<${#_DEV[@]};i++)); do echo ${_DEV[$i]}; done
}
if [ "$#" -lt "1" ]
then
  _usage
  exit 1
else
  j=0
  while [ $# -gt 0 ]
  do
        m=0
        for ((i=0;i<${#_DEV[@]};i++))
        do
          case $1 in
                ${_DEV[$i]}) _cfg_maker ${_COMMUNITY[$i]} ${_CFG[$i]}
                m=1
                ;;
                *)
                ;;
          esac
        done
        if [ $m = 0 ]
        then
          _WRONG_DEV[$j]=$1
          echo "You input a invalid device listed below, please check it!!"
          echo ${_WRONG_DEV[$j]}
          j=`expr $j + 1`
        fi
  shift
  done
  if [ ${_WRONG_DEV} ]
  then
        echo "The following DEVICE(S) you input is not valid, please check them !!"
        for ((k=0;k<${#_WRONG_DEV[@]};k++))
        do
          echo ${_WRONG_DEV[$k]}
        done
  fi
fi
echo '###This is the combination configuration file for all the _CFG files !!' > $_ALL_CFGS
for ((i=0;i<${#_CFG[@]};i++))
do
  cat ${_CFG[$i]} >> $_ALL_CFGS
done
$_MRTG_SCRIPT restart



放到cron里面的

#!/bin/bash
#### newmrtg.sh
_MRTG_PROG="/usr/bin/mrtg"
_PID_FILE="/var/run/newmrtg.pid"
_LOG_FILE="/var/www/html/mrtg/log/all.log"
_CFG_FILE="/var/www/html/mrtg/cfg/all.cfg"
start() {
                if [ -f $_PID_FILE ] ; then
                        echo "MRTG process is existing!!!"
                        echo "Please restart the MRTG!!!!"
                        echo "Exiting Now!!!"
                        exit 0
                fi
        echo "Starting MRTG now!!!!"
        $_MRTG_PROG --pid-file=$_PID_FILE --logging $_LOG_FILE $_CFG_FILE
}
stop()  {
        if [ -f $_PID_FILE ] ; then
        echo "Stopping MRTG!!! "
        kill -9 `cat $_PID_FILE`
        rm -f "$_PID_FILE"
        fi
}
case "$1" in
   start)
        start
        ;;
   stop)
        stop
        ;;
   restart)
        stop
        start
        ;;
   *)
        echo $"Usage:{start|stop|restart}"
        ;;
esac
exit 0



你可能感兴趣的:(职场,休闲,mrtg完美教程)