#!/bin/bash # Usage: ./alert.sh "MESSAGE" [-i image_file] [-f html_file] set -euo pipefail PORT="${DASHBOARD_PORT:-3000}" MESSAGE="" HTML_FILE="" IMAGE_FILE="" while [[ $# -gt 0 ]]; do case "$1" in -i|--image) IMAGE_FILE="$2"; shift 2 ;; -f|--html) HTML_FILE="$2"; shift 2 ;; -h|--help) echo "Usage: $0 [\"MESSAGE\"] [-i image_file] [-f html_file]" >&2; exit 0 ;; -*) echo "Option inconnue : $1" >&2; exit 1 ;; *) MESSAGE="$1"; shift ;; esac done if [ -z "$MESSAGE" ] && [ -z "$HTML_FILE" ] && [ -z "$IMAGE_FILE" ]; then echo "Usage: $0 [\"MESSAGE\"] [-i image_file] [-f html_file]" >&2 exit 1 fi TMPDIR_PAYLOAD=$(mktemp -d) trap 'rm -rf "$TMPDIR_PAYLOAD"' EXIT # HTML HTML_TMP="$TMPDIR_PAYLOAD/html.txt" if [ -n "$HTML_FILE" ] && [ -f "$HTML_FILE" ]; then cp "$HTML_FILE" "$HTML_TMP" else printf '' > "$HTML_TMP" fi # Image : encodée en base64 dans un fichier temporaire (évite "Argument list too long") IMAGE_TMP="$TMPDIR_PAYLOAD/image.txt" if [ -n "$IMAGE_FILE" ] && [ -f "$IMAGE_FILE" ]; then case "${IMAGE_FILE##*.}" in jpg|jpeg) MIME="image/jpeg" ;; png) MIME="image/png" ;; gif) MIME="image/gif" ;; webp) MIME="image/webp" ;; *) MIME="image/png" ;; esac printf 'data:%s;base64,' "$MIME" > "$IMAGE_TMP" base64 -i "$IMAGE_FILE" | tr -d '\n' >> "$IMAGE_TMP" else printf '' > "$IMAGE_TMP" fi PAYLOAD_TMP="$TMPDIR_PAYLOAD/payload.json" jq -n \ --arg msg "$MESSAGE" \ --rawfile html "$HTML_TMP" \ --rawfile image "$IMAGE_TMP" \ '{"message":$msg,"html":$html,"image":$image}' > "$PAYLOAD_TMP" curl -sf -X POST "http://localhost:${PORT}/api/alert" \ -H "Content-Type: application/json" \ -d "@$PAYLOAD_TMP" \ && echo "✓ Alerte déclenchée : $MESSAGE" \ || echo "✗ Serveur non disponible sur le port ${PORT}"