forked from Project-N-E-K-O/N.E.K.O
-
Notifications
You must be signed in to change notification settings - Fork 0
194 lines (174 loc) · 8.24 KB
/
health-check.yml
File metadata and controls
194 lines (174 loc) · 8.24 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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, old) ──
- name: Check lanlan.app (old)
id: check_app
run: |
CURL_EXIT=0
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-mini-model","messages":[{"role":"user","content":"sends some useful information"}],"max_completion_tokens":5}' \
2>/dev/null) || CURL_EXIT=$?
BODY=$(cat /tmp/resp_app.json 2>/dev/null || echo "{}")
echo "http_code=$HTTP_CODE" >> "$GITHUB_OUTPUT"
echo "curl_exit=$CURL_EXIT" >> "$GITHUB_OUTPUT"
if [ "$HTTP_CODE" = "200" ]; then
if echo "$BODY" | jq -e '(.choices | type) == "array" and (.choices | length) > 0 and (.choices[0].message | type) == "object" and .choices[0].message.role == "assistant" and (.choices[0].message | has("content"))' >/dev/null 2>&1; then
echo "healthy=true" >> "$GITHUB_OUTPUT"
else
echo "healthy=false" >> "$GITHUB_OUTPUT"
fi
else
echo "healthy=false" >> "$GITHUB_OUTPUT"
fi
# ── 检查 www.lanlan.app (US, new) ──
- name: Check www.lanlan.app (new)
id: check_api_app
run: |
CURL_EXIT=0
HTTP_CODE=$(curl -s -o /tmp/resp_api_app.json -w "%{http_code}" \
--max-time 30 --connect-timeout 10 \
-X POST "https://www.lanlan.app/text/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer free-access" \
-d '{"model":"free-mini-model","messages":[{"role":"user","content":"sends some useful information"}],"max_completion_tokens":5}' \
2>/dev/null) || CURL_EXIT=$?
BODY=$(cat /tmp/resp_api_app.json 2>/dev/null || echo "{}")
echo "http_code=$HTTP_CODE" >> "$GITHUB_OUTPUT"
echo "curl_exit=$CURL_EXIT" >> "$GITHUB_OUTPUT"
if [ "$HTTP_CODE" = "200" ]; then
if echo "$BODY" | jq -e '(.choices | type) == "array" and (.choices | length) > 0 and (.choices[0].message | type) == "object" and .choices[0].message.role == "assistant" and (.choices[0].message | has("content"))' >/dev/null 2>&1; then
echo "healthy=true" >> "$GITHUB_OUTPUT"
else
echo "healthy=false" >> "$GITHUB_OUTPUT"
fi
else
echo "healthy=false" >> "$GITHUB_OUTPUT"
fi
# ── 检查 www.lanlan.tech (China, new) ──
- name: Check www.lanlan.tech (new)
id: check_api_tech
run: |
CURL_EXIT=0
HTTP_CODE=$(curl -s -o /tmp/resp_api_tech.json -w "%{http_code}" \
--max-time 30 --connect-timeout 10 \
-X POST "https://www.lanlan.tech/text/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer free-access" \
-d '{"model":"free-mini-model","messages":[{"role":"user","content":"sends some useful information"}],"max_completion_tokens":5}' \
2>/dev/null) || CURL_EXIT=$?
BODY=$(cat /tmp/resp_api_tech.json 2>/dev/null || echo "{}")
echo "http_code=$HTTP_CODE" >> "$GITHUB_OUTPUT"
echo "curl_exit=$CURL_EXIT" >> "$GITHUB_OUTPUT"
if [ "$HTTP_CODE" = "200" ]; then
if echo "$BODY" | jq -e '(.choices | type) == "array" and (.choices | length) > 0 and (.choices[0].message | type) == "object" and .choices[0].message.role == "assistant" and (.choices[0].message | has("content"))' >/dev/null 2>&1; 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 }}"
APP_CODE="${{ steps.check_app.outputs.http_code }}"
API_APP_HEALTHY="${{ steps.check_api_app.outputs.healthy }}"
API_TECH_HEALTHY="${{ steps.check_api_tech.outputs.healthy }}"
API_APP_CODE="${{ steps.check_api_app.outputs.http_code }}"
API_TECH_CODE="${{ steps.check_api_tech.outputs.http_code }}"
APP_EXIT="${{ steps.check_app.outputs.curl_exit }}"
API_APP_EXIT="${{ steps.check_api_app.outputs.curl_exit }}"
API_TECH_EXIT="${{ steps.check_api_tech.outputs.curl_exit }}"
# curl 退出码非零(拿不到响应、即 http_code=000)时附带退出码(28=超时, 7=拒连, 6=DNS, 35=TLS)以区分故障类型
fmt_code() {
if [ -n "$2" ] && [ "$2" != "0" ]; then
echo "$1, curl exit $2"
else
echo "$1"
fi
}
APP_CODE_DISP=$(fmt_code "$APP_CODE" "$APP_EXIT")
API_APP_CODE_DISP=$(fmt_code "$API_APP_CODE" "$API_APP_EXIT")
API_TECH_CODE_DISP=$(fmt_code "$API_TECH_CODE" "$API_TECH_EXIT")
ALL_HEALTHY="true"
ANY_DOWN="false"
for h in "$APP_HEALTHY" "$API_APP_HEALTHY" "$API_TECH_HEALTHY"; do
if [ "$h" = "false" ]; then
ALL_HEALTHY="false"
ANY_DOWN="true"
fi
done
if [ "$ALL_HEALTHY" = "true" ]; then
COLOR=3066993 # green
TITLE="All Servers Healthy"
elif [ "$APP_HEALTHY" = "false" ] && [ "$API_APP_HEALTHY" = "false" ] && [ "$API_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=":white_check_mark: OK (HTTP $APP_CODE_DISP)"
else
APP_STATUS=":x: DOWN (HTTP $APP_CODE_DISP)"
fi
if [ "$API_APP_HEALTHY" = "true" ]; then
API_APP_STATUS=":white_check_mark: OK (HTTP $API_APP_CODE_DISP)"
else
API_APP_STATUS=":x: DOWN (HTTP $API_APP_CODE_DISP)"
fi
if [ "$API_TECH_HEALTHY" = "true" ]; then
API_TECH_STATUS=":white_check_mark: OK (HTTP $API_TECH_CODE_DISP)"
else
API_TECH_STATUS=":x: DOWN (HTTP $API_TECH_CODE_DISP)"
fi
# 只在有故障时 @everyone
CONTENT=""
if [ "$ANY_DOWN" = "true" ]; then
CONTENT="@everyone Server alert!"
fi
PAYLOAD=$(jq -n \
--arg content "$CONTENT" \
--arg title "$TITLE" \
--argjson color "$COLOR" \
--arg app_status "$APP_STATUS" \
--arg api_app_status "$API_APP_STATUS" \
--arg api_tech_status "$API_TECH_STATUS" \
--arg timestamp "$TIMESTAMP" \
'{
content: (if $content == "" then null else $content end),
embeds: [{
title: $title,
color: $color,
fields: [
{name: "lanlan.app (US, old)", value: $app_status, inline: true},
{name: "\u200b", value: "\u200b", inline: false},
{name: "www.lanlan.app (US, new)", value: $api_app_status, inline: true},
{name: "www.lanlan.tech (China, new)", value: $api_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"