From b610a87d8d41b71cc01b4de9703bfca54ab2e494 Mon Sep 17 00:00:00 2001 From: Mark Eaton Date: Sat, 24 Jan 2026 21:27:42 -0500 Subject: [PATCH] switch to gitea --- notify/notify-gitea.sh | 89 ++++++++++++++++++++++++++++++++++++++++++ notify/notify-teams.sh | 68 -------------------------------- 2 files changed, 89 insertions(+), 68 deletions(-) create mode 100755 notify/notify-gitea.sh delete mode 100755 notify/notify-teams.sh diff --git a/notify/notify-gitea.sh b/notify/notify-gitea.sh new file mode 100755 index 0000000..03df77d --- /dev/null +++ b/notify/notify-gitea.sh @@ -0,0 +1,89 @@ +#!/bin/bash +# notify-gitea.sh - Create Gitea issues for systemd service failures +# +# Usage: notify-gitea.sh +# status: "success" or "failure" +# Only creates an issue on failure; success is logged but ignored. +# +# Configuration: +# Set these environment variables or create /etc/notify-gitea.conf: +# GITEA_URL - Base URL of your Gitea instance (e.g., https://gitea.example.com) +# GITEA_TOKEN - API token with repo write access +# GITEA_OWNER - Repository owner (user or org) +# GITEA_REPO - Repository name + +set -euo pipefail + +SERVICE_NAME="${1:-unknown}" +STATUS="${2:-unknown}" +HOSTNAME=$(hostname) +TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S %Z') + +# Load config from file if not set +if [[ -f /etc/notify-gitea.conf ]]; then + source /etc/notify-gitea.conf +fi + +# Validate required variables +for var in GITEA_URL GITEA_TOKEN GITEA_OWNER GITEA_REPO; do + if [[ -z "${!var:-}" ]]; then + echo "Error: $var not set" >&2 + exit 1 + fi +done + +# Only create issues on failure +if [[ "$STATUS" == "success" ]]; then + echo "Service ${SERVICE_NAME} succeeded - no issue created" + exit 0 +fi + +# Get recent logs for context (last 20 lines) +RECENT_LOGS=$(journalctl -u "$SERVICE_NAME" -n 20 --no-pager 2>/dev/null || echo "No logs available") + +# Build issue title and body +TITLE="[${HOSTNAME}] Service failure: ${SERVICE_NAME}" + +BODY="## Service Failure Report + +**Service:** \`${SERVICE_NAME}\` +**Host:** \`${HOSTNAME}\` +**Time:** ${TIMESTAMP} +**Status:** ${STATUS} + +## Recent Logs + +\`\`\` +${RECENT_LOGS} +\`\`\` +" + +# Create JSON payload using jq for proper escaping +PAYLOAD=$(jq -n \ + --arg title "$TITLE" \ + --arg body "$BODY" \ + '{title: $title, body: $body}') + +# Create the issue via Gitea API +API_URL="${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/${GITEA_REPO}/issues" + +RESPONSE=$(curl -s -w "\n%{http_code}" \ + -X POST \ + -H "Content-Type: application/json" \ + -H "Authorization: token ${GITEA_TOKEN}" \ + -d "$PAYLOAD" \ + "$API_URL") + +HTTP_CODE=$(echo "$RESPONSE" | tail -1) +BODY_RESPONSE=$(echo "$RESPONSE" | sed '$d') + +if [[ "$HTTP_CODE" -ge 200 && "$HTTP_CODE" -lt 300 ]]; then + ISSUE_NUMBER=$(echo "$BODY_RESPONSE" | jq -r '.number // "unknown"') + ISSUE_URL=$(echo "$BODY_RESPONSE" | jq -r '.html_url // "unknown"') + echo "Issue #${ISSUE_NUMBER} created for ${SERVICE_NAME} failure" + echo "URL: ${ISSUE_URL}" +else + echo "Error creating issue: HTTP ${HTTP_CODE}" >&2 + echo "$BODY_RESPONSE" >&2 + exit 1 +fi diff --git a/notify/notify-teams.sh b/notify/notify-teams.sh deleted file mode 100755 index fc2aae6..0000000 --- a/notify/notify-teams.sh +++ /dev/null @@ -1,68 +0,0 @@ -#!/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})"