Pejman Moghadam / Scripts

MRTG Reconfigure script

Public domain


Version 2.0

#!/bin/bash
PATH="/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin"

##### localhost #####
CFG="/usr/local/mrtg-2/etc/mrtg.cfg"
WRK="/var/www/htdocs/mrtg"
SNMP="public@127.0.0.1"

mkdir -p "${WRK}"
mkdir -p $(dirname "${CFG}")

/usr/local/mrtg-2/bin/cfgmaker  \
  --zero-speed=100000000 \
  --if-filter="\$default && \$if_type!=23" \
  --ifref=name --ifdesc=name    \
  --global "Options[_]: bits"   \
  --global "kilo[_]: 1024"      \
  --global "MaxBytes[_]: 125000" \
  --global "WithPeak[_]: ymw" \
  --global "WorkDir: ${WRK}" \
  --output=${CFG}   \
  ${SNMP}

/usr/local/mrtg-2/bin/indexmaker ${CFG} > ${WRK}/index.html
/bin/sed -i -e 's,</td><td>,,' ${WRK}/index.html
/usr/local/mrtg-2/bin/mrtg ${CFG}

Version 1.0

#!/bin/bash
# MRTG re-configuration and re-run script
# By : Pejman Moghadam
# crontab usage : */5 * * * * /path/to/mrtg-reconfigure > /dev/null 2>&1

# Configuration
MAXBPS="4000000"
COMMUNITY="public"
WORKDIR="/var/www/htdocs/mrtg"
MRTG="/usr/local/mrtg-2/bin/mrtg"
CFGDIR="/usr/local/mrtg-2/etc/"
CFGMAKER="/usr/local/mrtg-2/bin/cfgmaker"
INDEXMAKER="/usr/local/mrtg-2/bin/indexmaker"

# Create directories if not exist
/bin/mkdir -p ${WORKDIR}
/bin/mkdir -p ${CFGDIR}

# Create config file
/bin/echo "WorkDir: ${WORKDIR}" > ${CFGDIR}/mrtg.cfg
/bin/echo "Options[_]: bits" >> ${CFGDIR}/mrtg.cfg
${CFGMAKER} ${COMMUNITY}@127.0.0.1 | /bin/egrep -v '^#|^ *$' >> ${CFGDIR}/mrtg.cfg

# Change MaxBytes
let "MAX=${MAXBPS}/8"
for OLDMAX in $(/bin/grep "MaxBytes" ${CFGDIR}/mrtg.cfg|/bin/awk '{print$2}'); do
  /bin/sed -i -e "s,${OLDMAX},${MAX}," ${CFGDIR}/mrtg.cfg
done

# Change filenames in config file from ip_num to ip_ifname
# Gather ethernet interfaces list
IFLST=$(/sbin/ifconfig | /bin/grep "Link encap:Ethernet" | /bin/awk '{print $1}')
# For each interface
for INTF in ${IFLST}; do
  # Find ip address of interface
  IP=$(/sbin/ifconfig ${INTF} | /bin/grep "inet addr:" | /bin/sed -e 's,.*inet addr:,,' | /bin/awk '{print $1}')
  # Find old filename in config file
  OLDFILE=$(/bin/grep "\"${IP}\"" ${CFGDIR}/mrtg.cfg | /bin/sed -e 's,.*\[,,' -e 's,\].*,,')
  # Separate first part of old filename
  START=$(/bin/echo ${OLDFILE} | /bin/sed -e 's,_.*,,')
  # Separate last part of old filename
  END=$(/bin/echo ${OLDFILE} | /bin/sed -e 's,.*_,,')
  # Make new filename
  NEWFILE="${START}_${INTF}"
  # Remove alias interfaces from NEWFILE name
  NEWFILE=$(/bin/echo ${NEWFILE} | /bin/sed -e 's,:.*,,')

  # Debug info
  #echo INTF=${INTF}
  #echo IP=${IP}
  #echo OLDFILE=${OLDFILE}
  #echo START=${START}
  #echo END=${END}
  #echo NEWFILE=${NEWFILE}
  #echo

  # Change old filename to new
  if [ "${OLDFILE}" != "" ]; then
    /bin/sed -i -e "s,${OLDFILE},${NEWFILE}," ${CFGDIR}/mrtg.cfg
    /bin/sed -i -e "s,Traffic Analysis for ${END} --,Traffic Analysis for ${INTF} --," ${CFGDIR}/mrtg.cfg
  fi
done

# Create index file
${INDEXMAKER} ${CFGDIR}/mrtg.cfg > ${WORKDIR}/index.html

# Change index file to display everything in a column
/bin/sed -i -e 's,</td><td><DIV><B>,<DIV><B>,' ${WORKDIR}/index.html

# Lunch
${MRTG} ${CFGDIR}/mrtg.cfg > /dev/null 2>&1

BY: Pejman Moghadam
TAG: mrtg, bash, bash-script
DATE: 2009-05-18 14:18:44


Pejman Moghadam / Scripts [ TXT ]