Register
It is currently Sat Nov 01, 2014 6:39 am

mohacu.sh: MOnitor Hosts And Calculate Uptime


All times are UTC - 6 hours


Post new topic Reply to topic  [ 1 post ] 
Author Message
 PostPosted: Tue Mar 11, 2008 8:43 am   

Joined: Sun Mar 09, 2008 7:35 pm
Posts: 4
Hi there,

I learned a lot from reading these forums. Time to give something back. :)

Here's MOHACU, my host monitor:

Screenshot:
Image

Code:
#!/bin/bash
##
## Name: mohacu.sh
## MOHACU: MOnitor Hosts And Calculate Uptime
## Version: 1.0.3
## Date: 28/02/2008
## Author: Maarten Trimbos
##
## Description:
##  This script checks with 5 second interval if user
##  configured hosts are up and calculates uptime in %.
##
## Requirements:
##  ping bc mv sleep
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
#######################################################################


###USER CONFIG STARTS HERE###

# Enter hosts to monitor.
# Format is IP\<TAB><TAB># COMMENT
# Yes, it's dirty but it works.
#
HOSTS=( \
192.168.1.1\      # LAN Gateway
192.168.1.3\      # LAN Client
82.133.85.65\      # jolt.co.uk
62.69.184.230\      # nu.nl
66.94.234.13\      # yahoo.com
)

# Where to store log.
#
LOGPATH=~/Desktop

###USER CONFIG ENDS HERE###


# Check for required non-bash commands.
#
CMDS=( ping bc mv sleep )
for CMD in ${CMDS[@]:0}; do
  if [[ `which $CMD 2>/dev/null` = "" ]] ; then
   echo "$CMD not found. Exiting."
  exit
  fi
done

# Some vars.
#
NUM=0
RUNS=1
LOOP=1
LOG=$LOGPATH/mohacu.log

# Check if logfile exists and make a backup if it does.
#
if [ -e $LOG ]; then
mv $LOG $LOG.bak
fi

# Declare a variable for each host to store amount of downs in
# and set these variables to 0.
#
for IP in ${HOSTS[@]:0}; do
let "NUM += 1"
declare "DOWNVAR$NUM"=0
done

# Check if host is up or down.
#
PINGER (){
PING=`ping -c1 -w1 $IP|grep rtt`
test "$PING" != "" && STATEUP || STATEDOWN
}

# Stuff to do when host is up.
#
STATEUP (){
STATE=" UP "
COLOR="\E[1;32m"  ## Green
UP_CALC
}

# Stuff to do when host is down.
#
STATEDOWN (){
STATE=DOWN
COLOR="\E[1;31m" ## Red
let "DOWNVAR$NUM += 1"
UP_CALC
}

# Calculate uptime in %.
# Also adjust output based on value. Purely cosmetic.
#
UP_CALC () {
UP=`echo "scale=2; 100-(100*$((DOWNVAR$NUM))/$RUNS)"|bc`
# Cosmetic stuff starts here.
if [[ $UP = 100.00 ]]; then
   UPECHO="$UP%"
  elif [[ $UP > 9 ]]; then
   UPECHO=" $UP%"
  elif [[ $UP = 0 ]]; then
   UPECHO="  0.00%"
  else UPECHO=" $UP%"
fi
}

# Console output.
#
OUTHEADER () {
echo -e " STATE \tIP \t\t UPTIME"
echo --------------------------------
}
OUTPUT () {
echo -ne "$COLOR $STATE \t" && tput sgr0
echo -e "$IP \t $UPECHO"
}

# Logfile output.
#
OUTHEADERLOG () {
echo -e "IP \t\t  UPTIME"
echo -------------------------
}
OUTPUTLOG () {
echo -e "$IP \t  $UPECHO"
}

# Script shows how many times it has run.
# This function adds 1 to that value after each cycle.
#
CYCLERUNS (){
let "RUNS += 1"
}

# Main loop starts here.
#
until [ $LOOP = 0 ]; do
clear
NUM=0
OUTHEADER
OUTHEADERLOG > $LOG
for IP in ${HOSTS[@]:0}; do
  let "NUM += 1"
  PINGER
  OUTPUT
  OUTPUTLOG >> $LOG
done
echo "Runs: $RUNS"
echo "Runs: $RUNS" >> $LOG
CYCLERUNS
sleep 5
done


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP