Skip to content

Instantly share code, notes, and snippets.

@daqing
Created December 5, 2008 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daqing/32302 to your computer and use it in GitHub Desktop.
Save daqing/32302 to your computer and use it in GitHub Desktop.
#! /bin/bash
#
# File: check_status.sh
# Author: Kinch Zhang <kinch.zhang@gmail.com> with help from ChaoQian Xu <chaoranxu@gmail.com>
# Usage: $ check_status.sh [HOSTS ...]
#
function check()
{
ip=`ping $1 | head -n 1| awk '{print $3}'`
code=`curl -I $1 2>/dev/null| head -n 1| awk '{print $2}'`
if [ -z "$code" ];then
echo "[Error] couldn't connect to host $1."
continue;
fi
if [ "$code" == 200 ];then
echo "$1 $ip is OK."
else
echo "[Error] $1 $ip is DOWN (response code: $code)."
fi
}
# display help
if [ "$1" == "--help" ];then
echo "usage: check_status.sh [HOSTS ...]"
echo
echo -n "NOTE: if you invoke check_status.sh without any parameter,"
echo " it will read configuration from '~/server-list.txt'"
exit;
fi
if [ -z "$1" ];then
if [ -f "$HOME/server-list.txt" ];then
# read config from file
hosts=`cat ~/server-list.txt`
else
echo "[Error] file not exists: '~/server-list.txt'"
hosts=''
fi
else
hosts="$@"
fi
if [ -n "$hosts" ];then
for host in $hosts
do
check $host
done
else
echo "No hosts to check."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment