dashboard/alert.sh

55 lines
1.4 KiB
Bash
Executable File

#!/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
# Build jq args
JQARGS=(--arg msg "$MESSAGE")
HTML=""
if [ -n "$HTML_FILE" ] && [ -f "$HTML_FILE" ]; then
HTML=$(cat "$HTML_FILE")
fi
JQARGS+=(--arg html "$HTML")
IMAGE=""
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
IMAGE="data:${MIME};base64,$(base64 "$IMAGE_FILE" | tr -d '\n')"
fi
JQARGS+=(--arg image "$IMAGE")
PAYLOAD=$(jq -n "${JQARGS[@]}" '{"message":$msg,"html":$html,"image":$image}')
curl -sf -X POST "http://localhost:${PORT}/api/alert" \
-H "Content-Type: application/json" \
-d "$PAYLOAD" \
&& echo "✓ Alerte déclenchée : $MESSAGE" \
|| echo "✗ Serveur non disponible sur le port ${PORT}"