Skip to content

Instantly share code, notes, and snippets.

@jrh-spg
Last active May 30, 2019 16:40
Show Gist options
  • Save jrh-spg/2a8152cd65b8ac9606ec40b8f582d4d3 to your computer and use it in GitHub Desktop.
Save jrh-spg/2a8152cd65b8ac9606ec40b8f582d4d3 to your computer and use it in GitHub Desktop.
#!/bin/bash
options=':i:s:d:n:p:h:'
while getopts ${options} option
do
case ${option} in
i) interval=$OPTARG;;
s) site=$OPTARG;;
d) domain=$OPTARG;;
n) nameservers=$OPTARG;;
p) site_ip=$OPTARG;;
*) echo "Usage: uptimer.sh -s [site] -i [interval] -d [domain] -n [\"nameserver1 nameserver2\"] -s [site ip]"; exit;;
esac
done
function post_to_slack() {
SLACK_MESSAGE="\`\`\`$1\`\`\`"
SLACK_URL=https://hooks.slack.com/services/[API_KEY]
case "$2" in
INFO)
SLACK_ICON=':slack:'
;;
WARNING)
SLACK_ICON=':warning:'
;;
ERROR)
SLACK_ICON=':bangbang:'
;;
*)
SLACK_ICON=':slack:'
;;
esac
curl -X POST --data "payload={\"text\": \"${SLACK_ICON} ${SLACK_MESSAGE}\"}" ${SLACK_URL}
}
function check_http() {
result=$(curl -I --silent $site | grep HTTP | awk '{print $2}')
if [[ ${result} != 200 ]]; then
post_to_slack "Uh oh! We received a ${result} for ${site}!"
fi
}
function check_dns() {
for ns in ${nameservers[@]}; do
ns_result=$(dig +short ${domain} @${ns})
if [[ ${ns_result} != ${site_ip} ]]; then
echo "ns=${ns}"
echo "nsresult=${ns_result}"
post_to_slack "Uh oh! ${domain} is not resolving to ${site_ip} on ${ns}!"
fi
done
}
while [[ x==0 ]]; do
if [[ -z ${interval} ]]; then
echo "Interval not specified with -i."; exit 1
fi
if [[ ! -z ${site} ]]; then
check_http
fi
if [[ ${#nameservers[@]} != 0 ]]; then
check_dns
fi
sleep $interval
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment