2024-03-10 09:57:47 +00:00
|
|
|
|
package monitor
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/songquanpeng/one-api/common"
|
|
|
|
|
"github.com/songquanpeng/one-api/common/config"
|
|
|
|
|
"github.com/songquanpeng/one-api/common/logger"
|
2024-03-10 11:16:06 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/common/message"
|
2024-03-10 09:57:47 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/model"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func notifyRootUser(subject string, content string) {
|
2024-03-10 11:16:06 +00:00
|
|
|
|
if config.MessagePusherAddress != "" {
|
|
|
|
|
err := message.SendMessage(subject, content, content)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.SysError(fmt.Sprintf("failed to send message: %s", err.Error()))
|
|
|
|
|
} else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-10 09:57:47 +00:00
|
|
|
|
if config.RootUserEmail == "" {
|
|
|
|
|
config.RootUserEmail = model.GetRootUserEmail()
|
|
|
|
|
}
|
2024-03-10 11:16:06 +00:00
|
|
|
|
err := message.SendEmail(subject, config.RootUserEmail, content)
|
2024-03-10 09:57:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
logger.SysError(fmt.Sprintf("failed to send email: %s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DisableChannel disable & notify
|
|
|
|
|
func DisableChannel(channelId int, channelName string, reason string) {
|
|
|
|
|
model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled)
|
|
|
|
|
logger.SysLog(fmt.Sprintf("channel #%d has been disabled: %s", channelId, reason))
|
2024-03-24 15:01:03 +00:00
|
|
|
|
subject := fmt.Sprintf("渠道「%s」(#%d)已被禁用", channelName, channelId)
|
|
|
|
|
content := fmt.Sprintf("渠道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
|
2024-03-10 09:57:47 +00:00
|
|
|
|
notifyRootUser(subject, content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func MetricDisableChannel(channelId int, successRate float64) {
|
|
|
|
|
model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled)
|
|
|
|
|
logger.SysLog(fmt.Sprintf("channel #%d has been disabled due to low success rate: %.2f", channelId, successRate*100))
|
2024-03-24 15:01:03 +00:00
|
|
|
|
subject := fmt.Sprintf("渠道 #%d 已被禁用", channelId)
|
|
|
|
|
content := fmt.Sprintf("该渠道(#%d)在最近 %d 次调用中成功率为 %.2f%%,低于阈值 %.2f%%,因此被系统自动禁用。",
|
|
|
|
|
channelId, config.MetricQueueSize, successRate*100, config.MetricSuccessRateThreshold*100)
|
2024-03-10 09:57:47 +00:00
|
|
|
|
notifyRootUser(subject, content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EnableChannel enable & notify
|
|
|
|
|
func EnableChannel(channelId int, channelName string) {
|
|
|
|
|
model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled)
|
|
|
|
|
logger.SysLog(fmt.Sprintf("channel #%d has been enabled", channelId))
|
2024-03-24 15:01:03 +00:00
|
|
|
|
subject := fmt.Sprintf("渠道「%s」(#%d)已被启用", channelName, channelId)
|
|
|
|
|
content := fmt.Sprintf("渠道「%s」(#%d)已被启用", channelName, channelId)
|
2024-03-10 09:57:47 +00:00
|
|
|
|
notifyRootUser(subject, content)
|
|
|
|
|
}
|