Skip to content

Instantly share code, notes, and snippets.

@soheilpro soheilpro/ping.sh
Created Jul 29, 2015

Embed
What would you like to do?
Simple web site monitoring script.
#!/usr/bin/env bash
DATE=$(TZ=Asia/Tehran date +"%Y-%m-%d %H:%M")
TIMESTAMP=$(TZ=Asia/Tehran date +"%Y%m%d%H%M")
# URLs
URLS=(
"google|https://www.google.com"
"wikipedia|https://www.wikipedia.org"
)
URLS_LEN=${#URLS[@]}
# Ping urls
for i in $(seq 0 $(($URLS_LEN - 1))); do
URL=$(echo ${URLS[i]} | sed 's/.*|//')
curl -s -o /dev/null -w "%{http_code}" -i --insecure $URL > ${TIMESTAMP}-${i}.res &
done
# Wait for completion
while true; do
DONE="1"
for i in $(seq 0 $(($URLS_LEN - 1))); do
RESPONSE=$(cat ${TIMESTAMP}-${i}.res)
if [[ "$RESPONSE" == "" ]]; then
DONE="0";
break
fi
done
if [[ "$DONE" == "1" ]]; then
break
fi;
sleep 0
done
# Output
OK="1"
RESULT=""
for i in $(seq 0 $(($URLS_LEN - 1))); do
RESPONSE=$(cat ${TIMESTAMP}-${i}.res)
TITLE=$(echo ${URLS[i]} | sed 's/|.*//')
RESULT="$RESULT $TITLE=$RESPONSE"
if [[ "$RESPONSE" != "200" ]]; then
OK="0";
fi
done
if [[ "$OK" = "1" ]]; then
echo $DATE "OK"
else
echo $DATE $RESULT
fi
# Clean up
for i in $(seq 0 $(($URLS_LEN - 1))); do
rm ${TIMESTAMP}-${i}.res
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.