From 9a070c81befaa543382e63bd74752b65aa2d7b93 Mon Sep 17 00:00:00 2001 From: Mark Eaton Date: Sun, 11 Jan 2026 00:00:20 -0500 Subject: [PATCH] add notify service --- notify/notify-teams-failure@.service | 8 ++++ notify/notify-teams-success@.service | 8 ++++ notify/notify-teams.conf.example | 3 ++ notify/notify-teams.sh | 68 ++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 notify/notify-teams-failure@.service create mode 100644 notify/notify-teams-success@.service create mode 100644 notify/notify-teams.conf.example create mode 100755 notify/notify-teams.sh diff --git a/notify/notify-teams-failure@.service b/notify/notify-teams-failure@.service new file mode 100644 index 0000000..74ce838 --- /dev/null +++ b/notify/notify-teams-failure@.service @@ -0,0 +1,8 @@ +[Unit] +Description=Send Teams failure notification for %i + +[Service] +Type=oneshot +ExecStart=/usr/local/bin/notify-teams.sh %i failure +# Load webhook URL from environment file +EnvironmentFile=-/etc/notify-teams.conf diff --git a/notify/notify-teams-success@.service b/notify/notify-teams-success@.service new file mode 100644 index 0000000..2e278be --- /dev/null +++ b/notify/notify-teams-success@.service @@ -0,0 +1,8 @@ +[Unit] +Description=Send Teams success notification for %i + +[Service] +Type=oneshot +ExecStart=/usr/local/bin/notify-teams.sh %i success +# Load webhook URL from environment file +EnvironmentFile=-/etc/notify-teams.conf diff --git a/notify/notify-teams.conf.example b/notify/notify-teams.conf.example new file mode 100644 index 0000000..54efb3d --- /dev/null +++ b/notify/notify-teams.conf.example @@ -0,0 +1,3 @@ +# /etc/notify-teams.conf +# Teams incoming webhook URL +TEAMS_WEBHOOK_URL="https://outlook.office.com/webhook/your-webhook-url-here" diff --git a/notify/notify-teams.sh b/notify/notify-teams.sh new file mode 100755 index 0000000..fc2aae6 --- /dev/null +++ b/notify/notify-teams.sh @@ -0,0 +1,68 @@ +#!/bin/bash +# notify-teams.sh - Send Teams notifications for systemd service events +# +# Usage: notify-teams.sh +# status: "success" or "failure" +# +# Configuration: +# Set TEAMS_WEBHOOK_URL environment variable or create /etc/notify-teams.conf + +set -euo pipefail + +SERVICE_NAME="${1:-unknown}" +STATUS="${2:-unknown}" +HOSTNAME=$(hostname) +TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S %Z') + +# Load webhook URL from config file if not set +if [[ -z "${TEAMS_WEBHOOK_URL:-}" ]] && [[ -f /etc/notify-teams.conf ]]; then + source /etc/notify-teams.conf +fi + +if [[ -z "${TEAMS_WEBHOOK_URL:-}" ]]; then + echo "Error: TEAMS_WEBHOOK_URL not set" >&2 + exit 1 +fi + +# Set color and title based on status +if [[ "$STATUS" == "success" ]]; then + THEME_COLOR="00FF00" + TITLE="Service Completed Successfully" + EMOJI="✓" +else + THEME_COLOR="FF0000" + TITLE="Service Failed" + EMOJI="✗" +fi + +# Get recent logs for context (last 15 lines) +RECENT_LOGS=$(journalctl -u "$SERVICE_NAME" -n 15 --no-pager 2>/dev/null | tail -15 || echo "No logs available") + +# Escape special characters for JSON +escape_json() { + printf '%s' "$1" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))' +} + +ESCAPED_LOGS=$(escape_json "$RECENT_LOGS") + +# Build and send the payload +curl -s -H "Content-Type: application/json" -d "{ + \"@type\": \"MessageCard\", + \"@context\": \"https://schema.org/extensions\", + \"themeColor\": \"${THEME_COLOR}\", + \"title\": \"${EMOJI} ${TITLE}\", + \"summary\": \"${SERVICE_NAME} ${STATUS} on ${HOSTNAME}\", + \"sections\": [{ + \"facts\": [ + {\"name\": \"Service\", \"value\": \"${SERVICE_NAME}\"}, + {\"name\": \"Status\", \"value\": \"${STATUS}\"}, + {\"name\": \"Host\", \"value\": \"${HOSTNAME}\"}, + {\"name\": \"Time\", \"value\": \"${TIMESTAMP}\"} + ] + }, { + \"title\": \"Recent Logs\", + \"text\": ${ESCAPED_LOGS} + }] +}" "$TEAMS_WEBHOOK_URL" + +echo "Notification sent for ${SERVICE_NAME} (${STATUS})"