-
Notifications
You must be signed in to change notification settings - Fork 211
133 lines (120 loc) · 5.01 KB
/
Copy pathhealth-check.yml
File metadata and controls
133 lines (120 loc) · 5.01 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
name: Server Health Check
on:
schedule:
# 每 5 分钟运行一次
- cron: "*/5 * * * *"
workflow_dispatch: # 允许手动触发
jobs:
health-check:
if: github.repository == 'Project-N-E-K-O/N.E.K.O' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
# ── 检查 lanlan.app (US) ──
- name: Check lanlan.app
id: check_app
run: |
HTTP_CODE=$(curl -s -o /tmp/resp_app.json -w "%{http_code}" \
--max-time 30 --connect-timeout 10 \
-X POST "https://lanlan.app/text/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer free-access" \
-d '{"model":"free-model","messages":[{"role":"user","content":"sends some useful information"}],"max_completion_tokens":5}' \
2>/dev/null || echo "000")
BODY=$(cat /tmp/resp_app.json 2>/dev/null || echo "{}")
echo "http_code=$HTTP_CODE" >> "$GITHUB_OUTPUT"
if [ "$HTTP_CODE" = "200" ]; then
HAS_CHOICES=$(echo "$BODY" | jq -r '.choices[0].message.content // empty' 2>/dev/null)
if [ -n "$HAS_CHOICES" ]; then
echo "healthy=true" >> "$GITHUB_OUTPUT"
else
echo "healthy=false" >> "$GITHUB_OUTPUT"
fi
else
echo "healthy=false" >> "$GITHUB_OUTPUT"
fi
# ── 检查 lanlan.tech (China) ──
- name: Check lanlan.tech
id: check_tech
run: |
HTTP_CODE=$(curl -s -o /tmp/resp_tech.json -w "%{http_code}" \
--max-time 30 --connect-timeout 10 \
-X POST "https://lanlan.tech/text/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer free-access" \
-d '{"model":"free-model","messages":[{"role":"user","content":"sends some useful information"}],"max_completion_tokens":5}' \
2>/dev/null || echo "000")
BODY=$(cat /tmp/resp_tech.json 2>/dev/null || echo "{}")
echo "http_code=$HTTP_CODE" >> "$GITHUB_OUTPUT"
if [ "$HTTP_CODE" = "200" ]; then
HAS_CHOICES=$(echo "$BODY" | jq -r '.choices[0].message.content // empty' 2>/dev/null)
if [ -n "$HAS_CHOICES" ]; then
echo "healthy=true" >> "$GITHUB_OUTPUT"
else
echo "healthy=false" >> "$GITHUB_OUTPUT"
fi
else
echo "healthy=false" >> "$GITHUB_OUTPUT"
fi
# ── 推送结果到 Discord(仅当 secret 存在时) ──
- name: Send Discord notification
env:
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_HEALTH_WEBHOOK_URL }}
if: env.DISCORD_WEBHOOK_URL != ''
run: |
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
APP_HEALTHY="${{ steps.check_app.outputs.healthy }}"
TECH_HEALTHY="${{ steps.check_tech.outputs.healthy }}"
APP_CODE="${{ steps.check_app.outputs.http_code }}"
TECH_CODE="${{ steps.check_tech.outputs.http_code }}"
if [ "$APP_HEALTHY" = "true" ] && [ "$TECH_HEALTHY" = "true" ]; then
COLOR=3066993 # green
TITLE="All Servers Healthy"
elif [ "$APP_HEALTHY" = "false" ] && [ "$TECH_HEALTHY" = "false" ]; then
COLOR=15158332 # red
TITLE="All Servers Down"
else
COLOR=15105570 # orange
TITLE="Partial Outage"
fi
if [ "$APP_HEALTHY" = "true" ]; then
APP_STATUS="OK (HTTP $APP_CODE)"
APP_EMOJI=":white_check_mark:"
else
APP_STATUS="DOWN (HTTP $APP_CODE)"
APP_EMOJI=":x:"
fi
if [ "$TECH_HEALTHY" = "true" ]; then
TECH_STATUS="OK (HTTP $TECH_CODE)"
TECH_EMOJI=":white_check_mark:"
else
TECH_STATUS="DOWN (HTTP $TECH_CODE)"
TECH_EMOJI=":x:"
fi
# 只在有故障时 @everyone
CONTENT=""
if [ "$APP_HEALTHY" = "false" ] || [ "$TECH_HEALTHY" = "false" ]; then
CONTENT="@everyone Server alert!"
fi
PAYLOAD=$(jq -n \
--arg content "$CONTENT" \
--arg title "$TITLE" \
--argjson color "$COLOR" \
--arg app_status "$APP_EMOJI $APP_STATUS" \
--arg tech_status "$TECH_EMOJI $TECH_STATUS" \
--arg timestamp "$TIMESTAMP" \
'{
content: (if $content == "" then null else $content end),
embeds: [{
title: $title,
color: $color,
fields: [
{name: "lanlan.app (US)", value: $app_status, inline: true},
{name: "lanlan.tech (China)", value: $tech_status, inline: true}
],
footer: {text: "N.E.K.O Health Monitor"},
timestamp: $timestamp
}]
}')
curl -s -X POST "$DISCORD_WEBHOOK_URL" \
-H "Content-Type: application/json" \
-d "$PAYLOAD"