Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ Usage: notify-alertmanager.sh [OPTIONS]

Options:
-t TYPE Object type: host | service
-T NOTIFICATION_TYPE Icinga notification type: PROBLEM | RECOVERY | ACKNOWLEDGEMENT | FLAPPINGSTART | FLAPPINGSTOP | DOWNTIMESTART | DOWNTIMEEND
-T NOTIFICATION_TYPE Icinga notification type:
PROBLEM | RECOVERY | ACKNOWLEDGEMENT |
FLAPPINGSTART | FLAPPINGSTOP | DOWNTIMESTART | DOWNTIMEEND
-H HOST_NAME Icinga host name
-u ALERTMANAGER_URL Alertmanager base URL (default: http://localhost:9093)
-U USER:PASSWORD Basic authentication credentials
-C CERT_FILE Client certificate file (PEM)
-K KEY_FILE Client private key file (PEM)
-S CA_FILE CA certificate file (PEM)
-c COMMENT Notification comment
-a AUTHOR Notification author
-i ICINGA_URL Icinga URL for the object (used in annotations)
-l LABELS Extra labels as comma-separated key=value pairs
-s STATE Object state: UP | DOWN | UNREACHABLE | OK | WARNING | CRITICAL | UNKNOWN
-s STATE Object state:
UP | DOWN | UNREACHABLE |
OK | WARNING | CRITICAL | UNKNOWN
-v Verbose output
-h Show this help

Expand All @@ -45,6 +53,8 @@ Service options:
-N SERVICE_DISPLAY_NAME Service display name (optional)
```

The options `ALERTMANAGER_URL` and `ALERTMANAGER_BASIC_AUTH` can be set via environment variables.

Examples:

```bash
Expand Down
35 changes: 27 additions & 8 deletions notify-alertmanager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set -euo pipefail

ALERTMANAGER_URL="${ALERTMANAGER_URL:-http://localhost:9093}"
ALERTMANAGER_BASIC_AUTH=${ALERTMANAGER_BASIC_AUTH:-}
TIMEOUT=10
NOTIFICATION_TYPE=""
OBJECT_TYPE=""
Expand All @@ -14,6 +15,9 @@ SERVICE_NAME=""
SERVICE_DISPLAY_NAME=""
NOTIFICATION_COMMENT=""
NOTIFICATION_AUTHOR=""
CA_CERT=""
CLIENT_CERT=""
CLIENT_KEY=""
ICINGA_URL=""
EXTRA_LABELS=""
VERBOSE=0
Expand All @@ -26,7 +30,11 @@ Options:
-t TYPE Object type: host | service
-T NOTIFICATION_TYPE Icinga notification type: PROBLEM | RECOVERY | ACKNOWLEDGEMENT | FLAPPINGSTART | FLAPPINGSTOP | DOWNTIMESTART | DOWNTIMEEND
-H HOST_NAME Icinga host name
-u ALERTMANAGER_URL Alertmanager base URL (default: $ALERTMANAGER_URL)
-u ALERTMANAGER_URL Alertmanager base URL (env: ALERTMANAGER_URL)
-U USER:PASSWORD Basic authentication credentials (env: ALERTMANAGER_BASIC_AUTH)
-C CERT_FILE Client certificate file (PEM)
-K KEY_FILE Client private key file (PEM)
-S CA_FILE CA certificate file (PEM)
-c COMMENT Notification comment
-a AUTHOR Notification author
-i ICINGA_URL Icinga URL for the object (used in annotations)
Expand All @@ -47,7 +55,7 @@ EOF
}

# Parsing arguments
while getopts ":t:T:H:d:A:n:N:s:u:c:a:i:l:vh" opt; do
while getopts ":t:T:H:d:A:n:N:s:u:c:a:i:l:vhU:C:K:S:" opt; do
case $opt in
t) OBJECT_TYPE="$OPTARG" ;;
T) NOTIFICATION_TYPE="$OPTARG" ;;
Expand All @@ -58,6 +66,10 @@ while getopts ":t:T:H:d:A:n:N:s:u:c:a:i:l:vh" opt; do
N) SERVICE_DISPLAY_NAME="$OPTARG" ;;
s) STATE="$OPTARG" ;;
u) ALERTMANAGER_URL="$OPTARG" ;;
U) ALERTMANAGER_BASIC_AUTH="$OPTARG" ;;
C) CLIENT_CERT="$OPTARG" ;;
K) CLIENT_KEY="$OPTARG" ;;
S) CA_CERT="$OPTARG" ;;
c) NOTIFICATION_COMMENT="$OPTARG" ;;
a) NOTIFICATION_AUTHOR="$OPTARG" ;;
i) ICINGA_URL="$OPTARG" ;;
Expand Down Expand Up @@ -87,9 +99,9 @@ fi

# Determine alert state and severity
if [[ "$OBJECT_TYPE" == "service" ]]; then
ALERTNAME="IcingaService"
ALERTNAME="IcingaNotificationForService"
else
ALERTNAME="IcingaHost"
ALERTNAME="IcingaNotificationForHost"
fi

# Simple JSON string escaping (handles quotes, backslashes, newlines)
Expand Down Expand Up @@ -208,19 +220,26 @@ if [[ "$VERBOSE" -eq 1 ]]; then
fi

# Sending the notification
HTTP_RESPONSE=$(curl \
CURL_CMD=(curl \
--silent \
--show-error \
--max-time "$TIMEOUT" \
--connect-timeout 5 \
--write-out "\n%{http_code}" \
--request POST \
--header "Content-Type: application/json" \
--data "$PAYLOAD" \
"$ENDPOINT" 2>&1) || {
--data "$PAYLOAD"
)

[[ -n "$ALERTMANAGER_BASIC_AUTH" ]] && CURL_CMD+=(--user "$ALERTMANAGER_BASIC_AUTH")
[[ -n "$CLIENT_CERT" ]] && CURL_CMD+=(--cert "$CLIENT_CERT")
[[ -n "$CLIENT_KEY" ]] && CURL_CMD+=(--key "$CLIENT_KEY")
[[ -n "$CA_CERT" ]] && CURL_CMD+=(--cacert "$CA_CERT")

HTTP_RESPONSE=$("${CURL_CMD[@]}" "$ENDPOINT" 2>&1) || {
echo "ERROR: Alertmanager unreachable at $ENDPOINT" >&2
exit 2
}
}

HTTP_BODY=$(echo "$HTTP_RESPONSE" | sed '$d')
HTTP_CODE=$(echo "$HTTP_RESPONSE" | tail -n1)
Expand Down