#!/bin/bash

# Author : Asfihani (asfik@cakraweb.com)
# Description : Skrip sederhana untuk memonitor host dengan memparser hasil
#               output dari fping dan ditampilkan dalam html.
# Required : fping, akses cron
# Catatan : idea, icon, and design shamelessly stolen dari http://netmon.itb.ac.id

## Konfigurasi
# Multiple files are OK separated by whitespace, eg FILES="hosts.cfg servers.cfg"
FILES="hosts.cfg"

# Keterangan untuk judul :-)
KETERANGAN="Intranet"

# Direktori dimana script ini disimpan (buat terlebih dahulu)
BASEDIR=/etc/netmon

# Binari file fping
FPING=/usr/local/sbin/fping

# File HTML dimana hasil akan disimpan
OUTPUT=/usr/local/httpd/htdocs/netmon.html

# Icon "up" dan "down" bisa didapatkan dari http://hujan.cakraweb.co.id/down.gif dan
# http://hujan.cakraweb.co.id/up.gif atau anda bisa membuat sendiri :-)
# Letakkan pada satu direktori dengan file $OUTPUT
DOWN="<img src=down.gif>"
UP="<img src=up.gif>" 

# Direktori temporari
TMPDIR=/tmp

## Fungsi-fungsi
header() {
cat > $OUTPUT << HABIS
<html><head><title>Host status at $KETERANGAN</title>
<meta http-equiv="refresh" content="360"></head>
<body><h3><font face=arial>Host status at $KETERANGAN</h3>
<table border="1" width=500 bgcolor="#FBFCBE">
<font face=arial size=2>Last pool : $(date)<br><br>
<tr><td><font face=arial size=2><b>Host</td><td><font face=arial size=2><b>IP</td>
<td><font face=arial size=2><b>Status</b></td><td><font face=arial size=2><b>Reason</b></td></tr>
HABIS
}

pemisah() {
cat >> $OUTPUT << HABIS
<tr><td width=20% valign=middle bgcolor=blue></td><td width=25% valign=middle bgcolor=blue></td>
<td width=5% valign=middle bgcolor=blue></td><td width=50% valign=middle bgcolor=blue></tr>
HABIS
}

myres() {
cat >> $OUTPUT << HABIS
<tr><td width=20% height=10><font size=1 face=Arial color=red><b>$HOST</b></font></td>
<td width=25% height=10><font size=1 face=Arial color=red><b>$myIP</b></font></td>
<td width=5% valign=middle align=center bgcolor=#FFFFFF>$DOWN</td>
<td width=50% valign=middle><font size=1 face=Arial>$myRES</td></tr>
HABIS
}

alive() {
cat >> $OUTPUT << HABIS
<tr><td width=20% height=10><font size=1 face=Arial>$HOST</font></td>
<td width=25% height=10><font size=1 face=Arial>$myIP</font></td>
<td width=5% valign=middle align=center bgcolor=#FFFFFF>$UP</td>
<td width=50% valign=middle><font size=1 face=Arial>-</td></tr>
HABIS
}

down() {
cat >> $OUTPUT << HABIS
<tr><td width=20% height=10><font size=1 face=Arial color=red><b>$HOST</b></font>
</td><td width=25% height=10><font size=1 face=Arial color=red><b>$myIP</b></font>
</td><td width=5% valign=middle align=center bgcolor=#FFFFFF>$DOWN</td>
<td width=50% valign=middle><font size=1 face=Arial>?</td></tr>
HABIS
}

# Don't edit anything below, sampeyan wis diperingatno (*kidding*) :-)
# Semuanya dimulai dari sini

header
cd $BASEDIR

for myFILES in $FILES
	do 
	pemisah
	for HOST in $(awk -F: '{print $1}' $myFILES | grep -v ^#)
		do
		mySTAT=$(grep ^$HOST $myFILES | awk -F: '{print $3}')
		myIP=$(grep ^$HOST $myFILES | awk -F: '{print $2}')
			if [ "$mySTAT" == "no" ] ; then
				myRES=$(grep ^$HOST $myFILES | awk -F: '{print $4}')
				myres
			else
				$FPING $myIP > $TMPDIR/$myIP.tmp
				STATUS=$(awk '{print $3}' $TMPDIR/$myIP.tmp)
				if [ "$STATUS" == "alive" ] ; then
					alive
				else
					down
				fi
			fi
	done
done
