Skip to content

Instantly share code, notes, and snippets.

@derek-baker
Created June 24, 2018 18:00
Show Gist options
  • Save derek-baker/1f9e82c4d30b23f5bacef83f77c4d7dd to your computer and use it in GitHub Desktop.
Save derek-baker/1f9e82c4d30b23f5bacef83f77c4d7dd to your computer and use it in GitHub Desktop.
#!/bin/bash
## Script makes http requests to specified endpoints and build an error message in response
## to non-200 return codes, then emails that message if it has a length greater than 0.
alert_recipient="<AlertEmail>"
urls='https://www.someurl.com/ https://www.someurl.com/test'
error_msg=""
## Make GETs
for url in $urls
do
return_code=$(curl -s -o /dev/null -w "%{http_code}" "$url");
if [ "$return_code" != "200" ]
then
error_msg+="\\nThere is a problem at $url: $return_code"
echo "Test Failed For: $url"
#else
# echo "Test Succeeded"
fi
done
main_url="https://someotherotherurlforpost.com/rp5217"
form_response=$(curl -X POST -s -o /dev/null -w "%{http_code}" "$main_url");
## Make POSTs
if [ "$return_code" != "200" ]
then
error_msg+="\nThere is a problem at $main_url: $return_code"
echo "Test Failed For: $url"
#else
# echo "Test Succeeded"
fi
## Send email if necessary
error_msg_len=${#error_msg}
if [[ $error_msg_len -gt 0 ]]
then
# echo -e "$error_msg" | mail -s "SDG Alert" $alert_recipient
echo "error_msg_len is: $error_msg_len"
python3 send_email.py "$error_msg"
fi
# Was trying to ensure cronjob was running
rm /home/<user>/test.txt
touch /home/<user>/test.txt
echo $(date) >> /home/<user>/test.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment