Skip to content

Commit ae53471

Browse files
committed
feat: 添加Uptime Kuma监控功能
1 parent 3e7b1cc commit ae53471

File tree

6 files changed

+113
-5
lines changed

6 files changed

+113
-5
lines changed

Diff for: README-zh.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Channel Monitor 是一个用于监控OneAPI/NewAPI渠道的工具,它直接读
1616
- [x] 支持间隔时间配置
1717
- [x] 支持多种数据库类型(MySQL、SQLite、PostgreSQL、SQL Server)
1818
- [x] 并发测试
19+
- [x] 支持Uptime Kuma, 在测试时Push URL来可视化模型可用性
1920

2021

2122
## 安装
@@ -87,7 +88,17 @@ docker-compose up -d
8788
"db_type": "mysql",
8889
"db_dsn": "YOUR_DB_DSN",
8990
"base_url": "http://localhost:3000",
90-
"system_token": "YOUR_SYSTEM_TOKEN"
91+
"system_token": "YOUR_SYSTEM_TOKEN",
92+
"uptime-kuma": {
93+
"status": "disabled",
94+
"model_url": {
95+
"gpt-3.5-turbo": "https://demo.kuma.pet/api/push/A12n43563?status=up&msg=OK&ping=",
96+
"gpt-4o": "https://demo.kuma.pet/api/push/ArJd2BOUJN?status=up&msg=OK&ping="
97+
},
98+
"channel_url": {
99+
"5": "https://demo.kuma.pet/api/push/ArJd2BOUJN?status=up&msg=OK&ping="
100+
}
101+
}
91102
}
92103
```
93104

@@ -102,6 +113,7 @@ docker-compose up -d
102113
- db_dsn: 数据库DSN字符串,不同数据库类型的DSN格式不同,示例如下
103114
- base_url: OneAPI/NewAPI/OneHub的基础URL,如果使用host模式,可以直接使用http://localhost:3000,目前只有OneHub需要填写
104115
- system_token: 系统Token,目前只有OneHub需要填写
116+
- uptime-kuma: Uptime Kuma的配置,status为`enabled``disabled`,model_url和channel_url为模型和渠道的可用性Push URL
105117

106118
### MySQL
107119

Diff for: README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Channel Monitor is a tool designed for monitoring OneAPI/NewAPI channels. It dir
1616
- [x] Support configurable intervals
1717
- [x] Support multiple database types, including MySQL, SQLite, PostgreSQL, and SQL Server
1818
- [x] Concurrent testing
19+
- [x] Support Uptime Kuma, push URL during testing to visualize model availability
1920

2021
## Installation
2122

@@ -86,7 +87,17 @@ The configuration file is `config.json` located in the same directory, with the
8687
"db_type": "mysql",
8788
"db_dsn": "YOUR_DB_DSN",
8889
"base_url": "http://localhost:3000",
89-
"system_token": ""
90+
"system_token": "YOUR_SYSTEM_TOKEN",
91+
"uptime-kuma": {
92+
"status": "disabled",
93+
"model_url": {
94+
"gpt-3.5-turbo": "https://demo.kuma.pet/api/push/A12n43563?status=up&msg=OK&ping=",
95+
"gpt-4o": "https://demo.kuma.pet/api/push/ArJd2BOUJN?status=up&msg=OK&ping="
96+
},
97+
"channel_url": {
98+
"5": "https://demo.kuma.pet/api/push/ArJd2BOUJN?status=up&msg=OK&ping="
99+
}
100+
}
90101
}
91102
```
92103

@@ -101,6 +112,7 @@ Configuration explanation:
101112
- db_dsn: Database DSN string, the format varies by database type. Examples below
102113
- base_url: The base URL for OneAPI/NewAPI/OneHub. If using host mode, you can directly use http://localhost:3000. Currently, only OneHub requires this field.
103114
- system_token: System token, currently only required for OneHub.
115+
- uptime-kuma: Configuration for Uptime Kuma. The status can be `enabled` or `disabled`. The model_url and channel_url are the availability Push URLs for models and channels.
104116

105117
### MySQL
106118

Diff for: VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.3.2
1+
v0.4.0

Diff for: config.go

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ type Config struct {
1717
DbDsn string `json:"db_dsn"`
1818
BaseURL string `json:"base_url"`
1919
SystemToken string `json:"system_token"`
20+
UptimeKuma struct {
21+
Status string `json:"status"`
22+
ModelURL map[string]string `json:"model_url"`
23+
ChannelURL map[string]string `json:"channel_url"`
24+
} `json:"uptime-kuma"`
2025
}
2126

2227
func loadConfig() (*Config, error) {
@@ -42,5 +47,9 @@ func loadConfig() (*Config, error) {
4247
config.BaseURL = config.BaseURL[:len(config.BaseURL)-1]
4348
}
4449

50+
if config.UptimeKuma.Status == "" {
51+
config.UptimeKuma.Status = "disabled"
52+
}
53+
4554
return &config, nil
4655
}

Diff for: config_example.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,15 @@
88
"db_type": "YOUR_DB_TYPE",
99
"db_dsn": "YOUR_DB_DSN",
1010
"base_url": "http://localhost:3000",
11-
"system_token": "YOUR_SYSTEM_TOKEN"
11+
"system_token": "YOUR_SYSTEM_TOKEN",
12+
"uptime-kuma": {
13+
"status": "disabled",
14+
"model_url": {
15+
"gpt-3.5-turbo": "https://demo.kuma.pet/api/push/A12n43563?status=up&msg=OK&ping=",
16+
"gpt-4o": "https://demo.kuma.pet/api/push/ArJd2BOUJN?status=up&msg=OK&ping="
17+
},
18+
"channel_url": {
19+
"5": "https://demo.kuma.pet/api/push/ArJd2BOUJN?status=up&msg=OK&ping="
20+
}
21+
}
1222
}

Diff for: main.go

+66-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func fetchChannels() ([]Channel, error) {
5252
c.BaseURL = "https://api.openai.com"
5353
}
5454
}
55-
// 检查是否在排��列表中
55+
// 检查是否在排除列表中
5656
if contains(config.ExcludeChannel, c.ID) {
5757
log.Printf("渠道 %s(ID:%d) 在排除列表中,跳过\n", c.Name, c.ID)
5858
continue
@@ -185,6 +185,13 @@ func testModels(channel Channel, wg *sync.WaitGroup, mu *sync.Mutex) {
185185
availableModels = append(availableModels, model)
186186
modelMu.Unlock()
187187
log.Printf("\033[32m渠道 %s(ID:%d) 的模型 %s 测试成功\033[0m\n", channel.Name, channel.ID, model)
188+
// 推送UptimeKuma
189+
if err := pushModelUptime(model); err != nil {
190+
log.Printf("\033[31m推送UptimeKuma失败:%v\033[0m\n", err)
191+
}
192+
if err := pushChannelUptime(channel.ID); err != nil {
193+
log.Printf("\033[31m推送UptimeKuma失败:%v\033[0m\n", err)
194+
}
188195
} else {
189196
log.Printf("\033[31m渠道 %s(ID:%d) 的模型 %s 测试失败,状态码:%d,响应:%s\033[0m\n", channel.Name, channel.ID, model, resp.StatusCode, string(body))
190197
}
@@ -343,6 +350,64 @@ func updateModels(channelID int, models []string) error {
343350
return nil
344351
}
345352

353+
func pushModelUptime(modelName string) error {
354+
if config.UptimeKuma.Status != "enabled" {
355+
return nil
356+
}
357+
358+
pushURL, ok := config.UptimeKuma.ModelURL[modelName]
359+
if !ok {
360+
return fmt.Errorf("找不到模型 %s 的推送地址", modelName)
361+
}
362+
363+
req, err := http.NewRequest("GET", pushURL, nil)
364+
if err != nil {
365+
return fmt.Errorf("创建请求失败:%v", err)
366+
}
367+
368+
client := &http.Client{}
369+
resp, err := client.Do(req)
370+
if err != nil {
371+
return fmt.Errorf("推送失败:%v", err)
372+
}
373+
defer resp.Body.Close()
374+
375+
if resp.StatusCode != http.StatusOK {
376+
return fmt.Errorf("推送失败,状态码:%d", resp.StatusCode)
377+
}
378+
379+
return nil
380+
}
381+
382+
func pushChannelUptime(channelID int) error {
383+
if config.UptimeKuma.Status != "enabled" {
384+
return nil
385+
}
386+
387+
pushURL, ok := config.UptimeKuma.ChannelURL[fmt.Sprintf("%d", channelID)]
388+
if !ok {
389+
return fmt.Errorf("找不到渠道 %d 的推送地址", channelID)
390+
}
391+
392+
req, err := http.NewRequest("GET", pushURL, nil)
393+
if err != nil {
394+
return fmt.Errorf("创建请求失败:%v", err)
395+
}
396+
397+
client := &http.Client{}
398+
resp, err := client.Do(req)
399+
if err != nil {
400+
return fmt.Errorf("推送失败:%v", err)
401+
}
402+
defer resp.Body.Close()
403+
404+
if resp.StatusCode != http.StatusOK {
405+
return fmt.Errorf("推送失败,状态码:%d", resp.StatusCode)
406+
}
407+
408+
return nil
409+
}
410+
346411
func main() {
347412
var err error
348413
config, err = loadConfig()

0 commit comments

Comments
 (0)