#!/bin/bash set -uo pipefail textfile_dir={{ node_exporter_textfile_dir }} file=${textfile_dir}/uptimerobot.prom tmp=${file}.$$ mkdir -p ${textfile_dir} response=$(curl -sS -m 25 -X POST \ "https://api.uptimerobot.com/v2/getMonitors?api_key={{ uptimerobot_api_key }}&format=json&custom_uptime_ratios=1-7-30" \ 2>/dev/null) {% raw %} printf '%s' "${response}" | python3 -c ' import json import sys print("# HELP uptimerobot_api_ok Whether the uptimerobot api answered successfully.") print("# TYPE uptimerobot_api_ok gauge") print("# HELP uptimerobot_monitor_up Whether an external monitor reports the target as up.") print("# TYPE uptimerobot_monitor_up gauge") print("# HELP uptimerobot_monitor_enabled Whether an external monitor is active rather than paused.") print("# TYPE uptimerobot_monitor_enabled gauge") print("# HELP uptimerobot_monitor_status Raw uptimerobot status, 0 paused 1 unchecked 2 up 8 seems down 9 down.") print("# TYPE uptimerobot_monitor_status gauge") print("# HELP uptimerobot_uptime_ratio Uptime percentage over the given window.") print("# TYPE uptimerobot_uptime_ratio gauge") try: payload = json.load(sys.stdin) except ValueError: print("uptimerobot_api_ok 0") sys.exit(0) if payload.get("stat") != "ok": print("uptimerobot_api_ok 0") sys.exit(0) print("uptimerobot_api_ok 1") def escape(value): return value.replace("\\", "\\\\").replace("\"", "\\\"") for monitor in payload.get("monitors", []): name = escape(str(monitor.get("friendly_name", "unknown"))) status = int(monitor.get("status", 1)) labels = "monitor=\"%s\"" % name print("uptimerobot_monitor_status{%s} %d" % (labels, status)) print("uptimerobot_monitor_enabled{%s} %d" % (labels, 0 if status == 0 else 1)) print("uptimerobot_monitor_up{%s} %d" % (labels, 1 if status == 2 else 0)) ratios = str(monitor.get("custom_uptime_ratio", "")).split("-") for window, ratio in zip(("1d", "7d", "30d"), ratios): try: print("uptimerobot_uptime_ratio{%s,window=\"%s\"} %s" % (labels, window, float(ratio))) except ValueError: pass ' > "${tmp}" {% endraw %} if [ ! -s "${tmp}" ] then rm -f "${tmp}" exit 1 fi chmod 0644 "${tmp}" mv "${tmp}" "${file}"