Linux Training

Linux training for private, public & voluntary sector.

0793 572 8612

City LinUX sample scripts - chkup


#!/bin/bash
# $Id: chkup,v 1.121 2017/01/19 14:16:13 fulford Exp fulford $
# $Source: /src/merlin/usr/local/etc/RCS/chkup,v $
# $Revision: 1.121 $
# Author C W Fulford.
# Copyright 2014 (c) C W Fulford.
# Licensed for public use under the LGPL, .
# For assistance contact fulford@fulford.net 0709 229 5385
########################################################################
cmd=`basename $0`
syntax="$cmd [-c config] [-d(ebug)] [-l] [-r <email_rcpts>] [-v(erbose)] clientid"
while [ $# -gt 0 ] ;do
	case $1 in 
		-c) config=$2;shift 2;;	
		-d) debug=:;set -x;shift;;
		-l) log=:;shift;;
		-r) _rcpts=$2;shift 2;;
		-v) verbose=:;shift;;
		 *) clientid=$1;shift;;
	esac
done


if [ -z "$clientid" ];then
	echo $syntax >&2;
	exit 1
fi

config=${config:-"/usr/local/etc/chkup.cf"}

if [ ! -f $config ] ;then
		echo "$cmd: $config not found">&2
fi
lock=/tmp/$cmd.$clientid.lock
tcfg=/tmp/$cmd.t$$
tmp=/var/tmp/$cmd$$

[ -f $lock ] && {
	echo "$cmd: failed - $lock already set" >&2
	exit 1
}



if [ -f $config ];then
	if grep -e "^${clientid}:" $config >/dev/null ;then
		sed -ne '/^'$clientid':/,/^$/{
			/[\t ][^#]/p
		}' <$config >$tcfg
		. $tcfg
	else
		[ "$verbose" ] &&{
	      	 	echo "$cmd: can't find client id \"$clientid\" in $config" >&2
			echo "$cmd: using $host as host name.">&2
		}
		host=$clientid
	fi
fi

_end () {
	[ -f $tmp ] && rm  $tmp 
	[ -f $tcfg ] && rm $tcfg
	[ -f $lock ] && rm $lock
	exit $rval
}
trap  "_end" 15

if ! getent hosts $host>/dev/null;then 
	echo "$cmd: can't resolve $host" >&2
	rval=1
	_end 
fi

[ -n "$_rcpts" -a -n "$rcpts" ] && $rcpts="$rcpts,$_rcpts"
rcpts=${rcpts:-`id -nu`}

_alert () { case $1 in
		down) mailx -s "ALERT - $host port $ports down" $rcpts <$tmp ;;
		fail) mailx -s "ALERT - $host port $ports still down, giving up" $rcpts <$tmp ;;
		up) mailx -s "RECOVERED - $host port $ports" $rcpts <$tmp ;;
	     esac
	     [ "$log" ] && logger -t "$cmd:" "$host $ports $1"
	}



_up () { if [ $# -eq 1 ];then
		state=`awk -v p=$1  <$tmp '{split($1,a,"/");if(a[1] == p)print $2}'`
		if [ "$state" = /open/ ];then
			return $1 
		fi
	 else
		state=`awk <$tmp '{if(/Host is up/){
						print "up"
				   	}else{
						print "down"
					}
				  }'`
		if [ $state ~ "up" ];then
			return 0
		else
			return 1
		fi
	fi
}
if [ $ports ];then
	opt="-p$ports"
fi
nmap -Pn $opt $host > $tmp
c=0
max=10
for p in `echo $ports|sed -e 's/,/ /g'`;do
	while ! _up $p && [ $c -lt $max ];do
		echo "$host port $p down" >&2
		[ ! -f $lock ] && _alert down
		touch $lock 
		sleep $((c++ * 60))
		nmap -Pn -$opt $host > $tmp
	done
	[ $verbose ] && {
		if [ $p ];then
			echo "$cmd: port $p available on $host." >&2
		else
			echo "$cmd: $host UP." >&2
		fi
	}
done

if [ -f $lock -a $c -eq $max ] ;then
	#lock is only created if host was down
	[ $verbose ] && echo "$cmd: $host down for 10 minutes! Giving up" >&2
	_alert fail
	rm $lock
	rval=1	
elif [ -f $lock ];then
	[ $verbose ] && echo "$cmd: RECOVERED port $p on $host." >&2
	_alert up
	rval=0
fi
_end

######################################################################
# This program is free software: you can redistribute it and or      #
# modify it under the terms of the Lesser GNU General Public License #
# as published by the Free Software Foundation, either version 3 of  #
# the License, or (at your option) any later version.                #
#                                                                    #
# This program is distributed in the hope that it will be useful,    #
# but WITHOUT ANY WARRANTY; without even the implied warranty of     #    
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
# GNU General Public License for more details.                       #
#                                                                    # 
# A copy of the Lesser GNU General Public License and the GNU        #
# General Public License is available at                             #
# <http://www.gnu.org/licenses/>.                                    #
######################################################################
######################################################################
# This program is free software: you can redistribute it and or      #
# modify it under the terms of the Lesser GNU General Public License #
# as published by the Free Software Foundation, either version 3 of  #
# the License, or (at your option) any later version.                #
#                                                                    #
# This program is distributed in the hope that it will be useful,    #
# but WITHOUT ANY WARRANTY; without even the implied warranty of     #    
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
# GNU General Public License for more details.                       #
#                                                                    # 
# A copy of the Lesser GNU General Public License and the GNU        #
# General Public License is available at                             #
# <http://www.gnu.org/licenses/>.                                    #
######################################################################

The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.