Monitoring cloudflared for fun and profit

This is a very hacky script for monitoring cloudflared, it requires curl and jq, which I think you should have on there anyway

You need the following line in the config.yml file, which sets cloudflared to expose the metrics/monitoring endpoint, or --metrics localhost:40355 on the command line:

metrics: localhost:40355

And… here’s the script. TL;DR, it checks the /ready endpoint, which returns {"status":200,"readyConnections":4} on good, or a 503 on not-working.

#!/bin/bash

SYSTEMD_TOPIC="cloudflared_monitoring"
METRICS_PORT="40355"
CLOUDFLARED_STATUS="$( curl --silent --max-time 5 "http://localhost:${METRICS_PORT}/ready" | jq -r '.status' )"
if [ "$CLOUDFLARED_STATUS" -ne 200 ]; then	
    echo "cloudflared status: ${CLOUDFLARED_STATUS} restarting." | /usr/bin/systemd-cat -t "${SYSTEMD_TOPIC}"
    /bin/systemctl restart cloudflared 2>&1 | sudo /usr/bin/systemd-cat -t "${SYSTEMD_TOPIC}"
else
    echo "cloudflared status: ${CLOUDFLARED_STATUS} OK" | /usr/bin/systemd-cat -t "${SYSTEMD_TOPIC}"
fi

References



#cloudflared #argo #scripting