-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealth_check.sh
More file actions
executable file
·37 lines (29 loc) · 1.13 KB
/
health_check.sh
File metadata and controls
executable file
·37 lines (29 loc) · 1.13 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
#!/bin/sh
# Guard
set -euo pipefail
URL="${URL:-http://localhost/api/v1/healthcheck}" # 필요시 환경변수 URL로 오버라이드
WEBHOOK_URL="${WEBHOOK_URL:-}" # 디스코드 웹훅 URL (필수)
TIMEOUT="${TIMEOUT:-3}" # 타임아웃 초
HOSTNAME="$(hostname)"
TIMESTAMP="$(date +'%Y-%m-%d %H:%M:%S %z')"
if [[ -z "${WEBHOOK_URL}" ]]; then
echo "[ERROR] WEBHOOK_URL not set" >&2
exit 2
fi
# --- 헬스체크 ---
echo "connection failed" > "./hc_body.$$"
HTTP_CODE="$(curl -sS -o ./hc_body.$$ -w '%{http_code}' --connect-timeout "${TIMEOUT}" -m "${TIMEOUT}" "${URL}" || echo 000)"
if [[ "${HTTP_CODE}" != "200" ]]; then
BODY="$(head -c 500 ./hc_body.$$ | sed 's/"/\\"/g')" # 메시지에 넣기 위해 따옴표 이스케이프
CONTENT=":rotating_light: Healthcheck FAILED
- host: ${HOSTNAME}
- url: ${URL}
- http_code: ${HTTP_CODE}
- time: ${TIMESTAMP}
- body: ${BODY}"
# 디스코드 전송 (간단히 content만)
curl -sS -H 'Content-Type: application/json' \
-d "$(jq -n --arg content "$CONTENT" '{content: $content}')" \
"${WEBHOOK_URL}" >/dev/null || true
fi
rm -f ./hc_body.$$