Last active
February 14, 2019 05:23
-
-
Save siutin/d07c69ded476fac5d6e7e30a44c016db to your computer and use it in GitHub Desktop.
Uptime Robot Ubuntu notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# usage: API_KEY=XXXX MONITOR_NAME=GOOGLE uptimerobot.sh | |
# | |
keep=$HOME/.uptimerobot-status | |
if [ -z "$MONITOR_NAME" ]; then | |
(>&2 echo "ERROR: MONITOR_NAME is required") | |
exit 1 | |
fi | |
if [ -z "$API_KEY" ]; then | |
(>&2 echo "ERROR: API_KEY is required") | |
exit 1 | |
fi | |
result=$(curl -ss -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "Cache-Control: no-cache" -d "api_key=$API_KEY&format=json&logs=1" "https://api.uptimerobot.com/v2/getMonitors") | |
if [[ '"ok"' != $(echo $result | jq '.stat') ]]; then | |
(>&2 echo "ERROR: request failed") | |
exit 1 | |
fi | |
item=$(echo $result | jq '.monitors[] | select(.friendly_name == "'$MONITOR_NAME'") | .logs | max_by(.datetime) | {datetime: .datetime, reason_detail: .reason.detail}') | |
datetime=$(date -d @$(echo $item | jq '.datetime')) | |
reason=$(echo $item | jq '.reason_detail') | |
echo "monitor: $MONITOR_NAME" | |
echo "datetime: $datetime" | |
echo "status: $reason" | |
if [[ ! -f $keep ]] || [[ $reason != $(cat $keep | head -n 1) ]]; then | |
echo "is changed" | |
notify-send "Monitor $MONITOR_NAME Status" "$(echo -e "Reason: $reason\r\nLast Update At $datetime")" | |
echo -e "$reason\n$datetime\n$(date)" > $keep | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment