Pejman Moghadam / Slackware

Slackware 12.2 - MRTG ping monitoring

Public domain


/usr/local/mrtg-2/etc/mrtg-pinger.sh

#!/bin/bash

CFGDIR="/usr/local/mrtg-2/etc"
TMP="/tmp/ping.$$"

# Check IP parameter
if [ "$1" == "" ];then
  echo "Usage: $0 <ip>"
  exit
fi

# Infinite ping
while :;do
  ping -c 300 -q $1 > "${TMP}"
  TIMESTAMP=$(date +%s)
  echo -n "${TIMESTAMP} " >> "${CFGDIR}/mrtg-ping-$1.loss"
  grep 'packet loss' "${TMP}" >> "${CFGDIR}/mrtg-ping-$1.loss"
  echo -n "${TIMESTAMP} " >> "${CFGDIR}/mrtg-ping-$1.rtt"
  grep 'rtt' "${TMP}" >> "${CFGDIR}/mrtg-ping-$1.rtt"
  rm "${TMP}"
  sleep 1
done

/usr/local/mrtg-2/etc/mrtg-ping-avg.sh

#!/bin/bash

CFGDIR="/usr/local/mrtg-2/etc"
HOST=$(hostname)
UPTIME=$(cat /proc/uptime | sed -e 's,\..*,,')
H=$(( $UPTIME / 3600 ))
M=$(( ($UPTIME - $H * 3600) / 60  ))

# Check IP parameter
if [ "$1" == "" ];then
  echo "Usage: $0 <ip>"
  exit
fi

AVG=$(tail -1 "${CFGDIR}/mrtg-ping-${1}.rtt" | awk -F '/' '{print$5}')

echo $AVG
echo $AVG
echo $H:$M
echo $HOST

/usr/local/mrtg-2/etc/mrtg-ping-loss.sh

#!/bin/bash

CFGDIR="/usr/local/mrtg-2/etc"
HOST=$(hostname)
UPTIME=$(cat /proc/uptime | sed -e 's,\..*,,')
H=$(( $UPTIME / 3600 ))
M=$(( ($UPTIME - $H * 3600) / 60  ))

# Check IP parameter
if [ "$1" == "" ];then
  echo "Usage: $0 <ip>"
  exit
fi

LOSS=$(tail -1 "${CFGDIR}/mrtg-ping-${1}.loss" | sed -e 's,% packet loss.*,,' -e 's,.* ,,')

echo $LOSS
echo $LOSS
echo $H:$M
echo $HOST

/usr/local/mrtg-2/etc/mrtg-ping-cron.sh

#!/bin/bash
# crontab -e
#   */5 * * * * /usr/local/mrtg-2/etc/mrtg-ping-cron.sh 8.8.8.8 4.2.2.4 yahoo.com &> /dev/null

# Variables
MAX="5000"
WORKDIR="/var/www/htdocs/ping"
CFGDIR="/usr/local/mrtg-2/etc"
CFGFILE="${CFGDIR}/mrtg-ping.cfg"
PATH="/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/mrtg-2/bin/"

# Check for ip parameter
if [ "$#" == "0" ];then
  echo "Usage: $0 <ip> ..."
  exit 1
fi

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

# Create a fresh config file
echo "WorkDir: ${WORKDIR}" > "${CFGFILE}"
echo  >> "${CFGFILE}"

for I in $(seq 1 $#); do

  # RTT

  echo "Target[${!I}.rtt]: \`${CFGDIR}/mrtg-ping-avg.sh ${!I}\`"  >> ${CFGFILE}
  echo "Options[${!I}.rtt]: unknaszero,nopercent,gauge"  >> ${CFGFILE}
  echo "Title[${!I}.rtt]: RTT to ${!I}"  >> ${CFGFILE}
  echo "PageTop[${!I}.rtt]: <H1>Round Trip Time to ${!I} </H1>"  >> ${CFGFILE}
  echo "MaxBytes[${!I}.rtt]: $MAX"  >> ${CFGFILE}
  echo "Colours[${!I}.rtt]: YELLOW#F9C000,RED#F90000,DARKGREEN#006600,VIOLET#FF00FF"  >> ${CFGFILE}
  echo "YLegend[${!I}.rtt]: RTT (ms)"  >> ${CFGFILE}
  echo "LegendI[${!I}.rtt]:     "  >> ${CFGFILE}
  echo "LegendO[${!I}.rtt]: Avg RTT (ms)"  >> ${CFGFILE}
  echo "XSize[${!I}.rtt]: 300"  >> ${CFGFILE}
  echo "YSize[${!I}.rtt]: 200"  >> ${CFGFILE}
  echo "Legend1[${!I}.rtt]:     "  >> ${CFGFILE}
  echo "Legend2[${!I}.rtt]: Round Trip Time in millisecond"  >> ${CFGFILE}
  echo "ShortLegend[${!I}.rtt]:     "  >> ${CFGFILE}
  echo "kMG[${!I}.rtt]: ,,"  >> ${CFGFILE}
  echo  >> ${CFGFILE}

  # LOSS

  echo "Target[${!I}.loss]: \`${CFGDIR}/mrtg-ping-loss.sh ${!I}\`"  >> ${CFGFILE}
  echo "Options[${!I}.loss]: unknaszero,nopercent,gauge,integer"  >> ${CFGFILE}
  echo "Title[${!I}.loss]: Pkt loss to ${!I}"  >> ${CFGFILE}
  echo "PageTop[${!I}.loss]: <H1>Packet Loss to ${!I} </H1>"  >> ${CFGFILE}
  echo "MaxBytes[${!I}.loss]: 100"  >> ${CFGFILE}
  echo "Colours[${!I}.loss]: YELLOW#F9C000,RED#F90000,DARKGREEN#006600,VIOLET#FF00FF"  >> ${CFGFILE}
  echo "YLegend[${!I}.loss]: Loss %"  >> ${CFGFILE}
  echo "LegendI[${!I}.loss]:     "  >> ${CFGFILE}
  echo "LegendO[${!I}.loss]: Pkt Loss %"  >> ${CFGFILE}
  echo "XSize[${!I}.loss]: 300"  >> ${CFGFILE}
  echo "YSize[${!I}.loss]: 200"  >> ${CFGFILE}
  echo "Legend1[${!I}.loss]:     "  >> ${CFGFILE}
  echo "Legend2[${!I}.loss]: Packt Loss percentage"  >> ${CFGFILE}
  echo "ShortLegend[${!I}.loss]:  %   "  >> ${CFGFILE}
  #echo "Unscaled[${!I}.loss]: ymwd"  >> ${CFGFILE}
  #echo "kMG[${!I}.loss]: ,,"  >> ${CFGFILE}
  echo  >> ${CFGFILE}

done

# Create index file
indexmaker "${CFGFILE}" > "${WORKDIR}/index.html"

# Change title of index file
HOST=$(hostname)
sed -i -e "s,MRTG Index Page,${HOST} - Ping Monitoring," ${WORKDIR}/index.html

# Lunch
mrtg "${CFGFILE}" &> /dev/null

Commands

chmod +x /usr/local/mrtg-2/etc/mrtg-pinger.sh
chmod +x /usr/local/mrtg-2/etc/mrtg-ping-avg.sh
chmod +x /usr/local/mrtg-2/etc/mrtg-ping-loss.sh
chmod +x /usr/local/mrtg-2/etc/mrtg-ping-cron.sh

/etc/rc.d/rc.local

# MRTG Pinger
/usr/local/mrtg-2/etc/mrtg-pinger.sh 8.8.8.8   &> /dev/null &
/usr/local/mrtg-2/etc/mrtg-pinger.sh 4.2.2.4   &> /dev/null &
/usr/local/mrtg-2/etc/mrtg-pinger.sh yahoo.com &> /dev/null &

crontab -e

# MRTG
*/5 * * * * /usr/local/mrtg-2/etc/mrtg-ping-cron.sh 8.8.8.8 4.2.2.4 yahoo.com &> /dev/null

BY: Pejman Moghadam
TAG: mrtg, ping
DATE: 2010-10-02 15:41:12


Pejman Moghadam / Slackware [ TXT ]