add uptimerobot monitor subcommand to app.sh

This commit is contained in:
2026-03-26 13:13:41 -04:00
parent 43fc8c2e7b
commit 697de3e199
3 changed files with 49 additions and 2 deletions
+46 -1
View File
@@ -28,7 +28,8 @@ usage() {
echo " garage Create Garage media bucket and grant access"
echo " build Trigger initial build workflow"
echo " verify Check if site is live"
echo " all Run all steps (dns, repo, files, cert, garage, commit, build, verify)"
echo " monitor Create UptimeRobot HTTPS monitor"
echo " all Run all steps (dns, repo, files, cert, garage, commit, build, verify, monitor)"
echo ""
echo "Requires: bws, jq, dig"
exit 1
@@ -467,6 +468,48 @@ cmd_verify() {
echo " - The container is running on the server: docker ps"
}
cmd_monitor() {
require_bws
echo "==> Fetching UptimeRobot API key..."
UPTIMEROBOT_API_KEY=$(bws_get "hantim-uptimerobot-api-key")
UPTIMEROBOT_API="https://api.uptimerobot.com/v3"
UPTIMEROBOT_AUTH="Authorization: Bearer $UPTIMEROBOT_API_KEY"
MONITOR_URL="https://www.$APP"
echo "==> Checking for existing UptimeRobot monitor for $MONITOR_URL..."
EXISTING=$(curl -s -G "$UPTIMEROBOT_API/monitors" \
-H "$UPTIMEROBOT_AUTH" \
--data-urlencode "search=$APP")
MATCH=$(echo "$EXISTING" | jq -r --arg url "$MONITOR_URL" \
'.monitors[]? | select(.url == $url) | .id')
if [ -n "$MATCH" ]; then
echo " Monitor already exists (id: $MATCH), skipping."
return
fi
echo "==> Fetching alert contacts..."
ALERT_CONTACTS=$(curl -s "$UPTIMEROBOT_API/user/alert-contacts" \
-H "$UPTIMEROBOT_AUTH" | jq '[.[] | {alertContactId: .id, threshold: 5, recurrence: 30}]')
echo "==> Creating UptimeRobot HTTPS monitor for $MONITOR_URL..."
RESP=$(curl -s -X POST "$UPTIMEROBOT_API/monitors" \
-H "$UPTIMEROBOT_AUTH" \
-H "Content-Type: application/json" \
-d "$(jq -n --arg name "$APP" --arg url "$MONITOR_URL" --argjson contacts "$ALERT_CONTACTS" \
'{friendlyName: $name, url: $url, type: 1, interval: 300, timeout: 30, assignedAlertContacts: $contacts, tagNames: ["hantim"]}')")
MONITOR_ID=$(echo "$RESP" | jq -r '.id // empty')
if [ -n "$MONITOR_ID" ]; then
echo " Monitor created (id: $MONITOR_ID)."
else
ERROR_MSG=$(echo "$RESP" | jq -r 'if .message then (.message | if type == "array" then join("; ") else . end) else .error // "unknown error" end')
echo " Warning: Failed to create monitor: $ERROR_MSG"
fi
}
cmd_all() {
cmd_dns
cmd_repo
@@ -486,6 +529,7 @@ cmd_all() {
cmd_garage
cmd_build
cmd_verify
cmd_monitor
}
# --- Dispatch ---
@@ -498,6 +542,7 @@ case "$COMMAND" in
garage) cmd_garage ;;
build) cmd_build ;;
verify) cmd_verify ;;
monitor) cmd_monitor ;;
all) cmd_all ;;
*)
echo "Unknown command: $COMMAND"