Skip to content

Instantly share code, notes, and snippets.

@tteggel
Created September 2, 2012 18:33
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 tteggel/3602759 to your computer and use it in GitHub Desktop.
Save tteggel/3602759 to your computer and use it in GitHub Desktop.
Cyclops

Cyclops - a one-dimensional HTTP monitor script

#!/bin/bash
##
# Cyclops. A one dimensional monitor script.
#
# Verbose output on stdout. Only changes of state on stderr.
#
# Suggested usage: $ ./cyclops.sh 2> cdn.log
# ...or: $ nohup ./cyclops.sh > /dev/null 2> cdn.log &
#
# Don't forget to chmod +x me first.
##
nslookup_server() {
(nslookup 2> /dev/null | tail -n2 | cut -d" " -f2) <<EOF
server $1
$2
EOF
}
edge_test() {
curl -w %{http_code} -s -o /dev/null -H Host:$VCDN_HOST http://$1$ASSET_PATH
}
red() {
echo -ne "\e[00;31m$1\e[00m"
}
green() {
echo -ne "\e[00;32m$1\e[00m"
}
echo_response() {
if [ $1 -ne 200 ]
then
echo -ne "["
red $1
echo -ne "]"
else
echo -ne "["
green $1
echo -ne "]"
fi
}
check_results() {
LAST_CHANGE="${LAST_CHANGES[$1]}"
LAST_RESULT="${LAST_RESULTS[$1]}"
CHANGED_AT=$(date --date=@$LAST_CHANGE)
if [ $2 -eq $LAST_RESULT ]
then
echo " since $CHANGED_AT."
else
LAST_CHANGES[$1]=$(date -u +%s)
echo " new result at $CHANGED_AT."
echo -ne "$3 changed from " 1>&2 && echo_response $LAST_RESULT 1>&2
echo -ne " to " 1>&2 && echo_response $2 1>&2 && echo -e " at $(date)." 1>&2
LAST_RESULTS[$1]=$2
fi
}
run_checks() {
echo "Starting test run at $(date)"
echo -ne "Origin\t($ORIGIN_HOST)\t"
ORIGIN_RESULT=$(curl -w %{http_code} -s -o /dev/null $ORIGIN_URL$ASSET_PATH)
echo_response $ORIGIN_RESULT
check_results 0 $ORIGIN_RESULT "Origin ($ORIGIN_HOST)"
echo -ne "Edge\tLocal DNS\t($DHCP_DNS_IP)\t"
DHCP_RESULT=$(edge_test $DHCP_DNS_IP)
echo_response $DHCP_RESULT
check_results 1 $DHCP_RESULT "Edge Local DNS ($DHCP_DNS_IP)"
echo -ne "Edge\tOpen DNS\t($OPEN_DNS_IP)\t"
OPEN_DNS_RESULT=$(edge_test $OPEN_DNS_IP)
echo_response $OPEN_DNS_RESULT
check_results 2 $OPEN_DNS_RESULT "Edge Open DNS ($OPEN_DNS_IP)"
echo -ne "Edge\tGoogle DNS\t($GOOGLE_DNS_IP)\t"
GOOGLE_DNS_RESULT=$(edge_test $GOOGLE_DNS_IP)
echo_response $GOOGLE_DNS_RESULT
check_results 3 $GOOGLE_DNS_RESULT "Edge Google DNS ($GOOGLE_DNS_IP)"
echo "Finished test run at $(date)"
echo
}
lookup_dns() {
DHCP_DNS_IP=$(nslookup_server "" $VCDN_HOST)
OPEN_DNS_IP=$(nslookup_server $OPEN_DNS_SERVER $VCDN_HOST)
GOOGLE_DNS_IP=$(nslookup_server $GOOGLE_DNS_SERVER $VCDN_HOST)
}
init() {
for i in {0..3}
do
LAST_RESULTS[$i]=0
LAST_CHANGES[$i]=$(date -u +%s)
done
}
# URL config
ORIGIN_HOST="rest-src-sl-01.mh.nokia.com"
ORIGIN_URL="http://$ORIGIN_HOST/p/d/music_asset"
VCDN_HOST="musicassets.vcdn.nokia.com"
VCDN_URL="http://$VCDN_HOST"
ASSET_PATH="/midgard/1.0.209/us/js/lib/modernizr/modernizr.custom.77273.js"
# Some different DNS services
GOOGLE_DNS_SERVER="8.8.4.4"
OPEN_DNS_SERVER="208.67.222.222"
# Run
init
while [ 1 ]
do
lookup_dns
run_checks
sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment