aboutsummaryrefslogtreecommitdiffstats
path: root/kubernetes/namespaces/monitoring/prometheus/reloader-script.yaml
blob: 6aae9b30e7d157456c837fd29e92ab3c23c1100d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-reloader-script
  namespace: monitoring
data:
  hook.sh: |-
    #!/bin/sh

    set -exo pipefail

    # Endpoint to call to reload Prometheus
    RELOAD_URL="http://localhost:9090/-/reload"
    # Icon for the webhook
    PROMETHEUS_ICON_URL="https://static-00.iconduck.com/assets.00/prometheus-icon-511x512-1vmxbcxr.png"

    echo "Detected change in mounted configmaps, reloading Prometheus..."

    # Make a temporary store to keep any errors
    RESPONSE_STORE="$(mktemp)"

    # Attempt the reload, writing the response to the tempfile and the reload HTTP
    # code to the variable
    RELOAD_RESULT="$(curl -o "$RESPONSE_STORE" -X POST $RELOAD_URL -s -w "%{http_code}")"

    # Parse and filter the response body into a JSON string
    RESPONSE_CONTENT="$(cat "$RESPONSE_STORE")"
    FILTERED_BODY="$(echo "$RESPONSE_CONTENT" | jq -Rsa)"

    # Send a notification based on pass/failure
    if [ $RELOAD_RESULT -eq 200 ]; then
      BODY='{"username": "Prometheus Reloader", "embeds": [{ "title": "Prometheus Config Reload Succeeded", "description": "No errors.", "color": 6663286 } ], "avatar_url": "'"$PROMETHEUS_ICON_URL"'" }'
    else
      BODY='{"username": "Prometheus Reloader", "embeds": [{ "title": "Prometheus Config Reload Failed", "description": '"$FILTERED_BODY"', "color": 12799052 } ], "avatar_url": "'"$PROMETHEUS_ICON_URL"'" }'
    fi;

    # Send the webhook
    curl -X POST -H "Content-Type: application/json" "$RELOADER_DISCORD_HOOK" -d "$BODY"