Skip to content

Alert when CDN cache-hit ratio drops

The CDN cache_status header (cf-cache-status or x-cache) suddenly returns MISS on more than 30% of requests — origin load + bandwidth bills both spike.

Recipe

bash
#!/usr/bin/env bash
# Sample cache_status header N times, alert if MISS rate > THRESHOLD.
URL=${1:?URL required}
N=${N:-20}
THRESHOLD_PERCENT=${THRESHOLD_PERCENT:-30}

MISS=0
for i in $(seq 1 "$N"); do
  STATUS=$(curl -sI "$URL?nocache=$RANDOM" | awk -F': ' '
    /^cf-cache-status:|^x-cache:/ { print toupper($2) }
  ' | tr -d '\r')
  case "$STATUS" in *MISS*|*EXPIRED*|*BYPASS*) MISS=$((MISS+1));; esac
done

PCT=$((MISS * 100 / N))
if [ "$PCT" -gt "$THRESHOLD_PERCENT" ]; then
  curl -X POST "$SLACK_WEBHOOK" \
    --data "{\"text\":\"CDN cache miss rate: $PCT% on $URL\"}"
fi

Same thing in Enterno.io

This is the "roll your own bot" approach. The Enterno HTTP monitor already stores response headers — Pioneer+ surfaces cache-hit ratio on the dashboard and alerts without a bash loop.

Set up HTTP monitor → ← All recipes

Related recipes