-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocknotify.sh
More file actions
112 lines (94 loc) · 3.03 KB
/
docknotify.sh
File metadata and controls
112 lines (94 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
set -euo pipefail
# -----------------------------------------------------------------------------
# docknotify: send host notifications via SERVER_TOOLS notifier service
#
# Protocol:
# token<TAB>timeout<TAB>urgency<TAB>source<TAB>title<TAB>body\n
# token placeholder '-' is used if NOTIFY_TOKEN is empty
# -----------------------------------------------------------------------------
HOST="${NOTIFY_HOST:-SERVER_TOOLS}" # reachable on compose network
PORT="${NOTIFY_TCP_PORT:-9901}"
TOKEN="${NOTIFY_TOKEN:-}"
[[ -n "${TOKEN:-}" ]] || TOKEN='-'
SOURCE="${NOTIFY_SOURCE:-${HOSTNAME:-svc}}"
timeout="2500"
urgency="normal"
# Optional client-side caps (keep aligned with notifierd defaults)
TITLE_MAX="${NOTIFY_TITLE_MAX:-100}"
BODY_MAX="${NOTIFY_BODY_MAX:-300}"
[[ "${TITLE_MAX:-}" =~ ^[0-9]{1,4}$ ]] || TITLE_MAX=100
[[ "${BODY_MAX:-}" =~ ^[0-9]{1,5}$ ]] || BODY_MAX=300
# Default: best-effort (don't break app pipelines if tools is down)
# Set DOCKNOTIFY_STRICT=1 to fail hard on send error.
STRICT="${DOCKNOTIFY_STRICT:-0}"
usage() {
cat >&2 <<'EOF'
Usage:
docknotify [-H host] [-p port] [-t ms] [-u low|normal|critical] [-s source] <title> <body>
Env:
NOTIFY_HOST default: SERVER_TOOLS
NOTIFY_TCP_PORT default: 9901
NOTIFY_TOKEN optional (if empty, '-' placeholder is sent)
NOTIFY_SOURCE optional (default: HOSTNAME or 'svc')
NOTIFY_TITLE_MAX optional (default: 100)
NOTIFY_BODY_MAX optional (default: 300)
DOCKNOTIFY_STRICT optional (default: 0). if 1 -> exit non-zero on send failure
EOF
exit 2
}
while getopts ":H:p:t:u:s:" opt; do
case "$opt" in
H) HOST="$OPTARG" ;;
p) PORT="$OPTARG" ;;
t) timeout="$OPTARG" ;;
u) urgency="$OPTARG" ;;
s) SOURCE="$OPTARG" ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
[[ $# -ge 2 ]] || usage
title="$1"
body="$2"
[[ -n "${HOST:-}" ]] || {
echo "docknotify: host is empty" >&2
exit 2
}
[[ "$PORT" =~ ^[0-9]{1,5}$ ]] || {
echo "docknotify: invalid port: $PORT" >&2
exit 2
}
((PORT >= 1 && PORT <= 65535)) || {
echo "docknotify: port out of range: $PORT" >&2
exit 2
}
# validate (avoid breaking the server)
[[ "$timeout" =~ ^[0-9]{1,6}$ ]] || timeout="2500"
case "$urgency" in low | normal | critical) ;; *) urgency="normal" ;; esac
# sanitize to keep one-line protocol stable
SOURCE="${SOURCE//$'\n'/ }"
SOURCE="${SOURCE//$'\r'/ }"
SOURCE="${SOURCE//$'\t'/ }"
title="${title//$'\n'/ }"
title="${title//$'\r'/ }"
title="${title//$'\t'/ }"
body="${body//$'\n'/ }"
body="${body//$'\r'/ }"
body="${body//$'\t'/ }"
# apply caps client-side (optional but nice)
title="${title:0:${TITLE_MAX}}"
body="${body:0:${BODY_MAX}}"
command -v nc >/dev/null 2>&1 || {
echo "docknotify: nc not found" >&2
exit 127
}
# send (best-effort by default)
payload="$(printf '%s\t%s\t%s\t%s\t%s\t%s\n' "$TOKEN" "$timeout" "$urgency" "$SOURCE" "$title" "$body")"
if ! printf '%s' "$payload" | nc -w 1 "$HOST" "$PORT" >/dev/null 2>&1; then
if [[ "$STRICT" == "1" ]]; then
echo "docknotify: failed to send to $HOST:$PORT" >&2
exit 1
fi
exit 0
fi