Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions agent/app/service/alert_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,28 @@ func sendAlerts(alert dto.AlertDTO, alertType, quota, quotaType string, params [
continue
}
alertUtil.CreateNewAlertTask(quota, alertType, quotaType, m)
case constant.Bark:
todayCount, isValid := canSendAlertToday(alertType, quotaType, alert.SendCount, m)
if !isValid {
continue
}
var create = dto.AlertLogCreate{
Type: alertUtil.GetCronJobType(alert.Type),
AlertId: alert.ID,
Count: todayCount + 1,
}
alertInfo := alert
alertInfo.Type = alertType
create.AlertRule = alertUtil.ProcessAlertRule(alert)
create.AlertDetail = alertUtil.ProcessAlertDetail(alertInfo, quotaType, params, m)
transport := xpack.LoadRequestTransport()
agentInfo, _ := xpack.GetAgentInfo()
alertErr := alertUtil.CreateBarkAlertLog(create, alertInfo, params, transport, agentInfo)
if alertErr != nil {
global.LOG.Infof("%s alert %s push failed, err: %v", alertType, m, alertErr.Error())
continue
}
alertUtil.CreateNewAlertTask(quota, alertType, quotaType, m)
default:
}
}
Expand Down
53 changes: 53 additions & 0 deletions agent/app/service/alert_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func (s *AlertSender) Send(quota string, params []dto.Param) {
s.sendSMS(quota, params)
case constant.Email:
s.sendEmail(quota, params)
case constant.Bark:
s.sendBark(quota, params)
case constant.WeCom, constant.DingTalk, constant.FeiShu:
s.sendWebhook(quota, params, method)
}
Expand All @@ -45,6 +47,8 @@ func (s *AlertSender) ResourceSend(quota string, params []dto.Param) {
s.sendResourceSMS(quota, params)
case constant.Email:
s.sendResourceEmail(quota, params)
case constant.Bark:
s.sendResourceBark(quota, params)
case constant.WeCom, constant.DingTalk, constant.FeiShu:
s.sendResourceWebhook(quota, params, method)
}
Expand Down Expand Up @@ -101,6 +105,31 @@ func (s *AlertSender) sendEmail(quota string, params []dto.Param) {
alertUtil.CreateNewAlertTask(quota, s.alert.Type, s.quotaType, constant.Email)
}

func (s *AlertSender) sendBark(quota string, params []dto.Param) {
totalCount, isValid := s.canSendAlert(constant.Bark)
if !isValid {
return
}

create := dto.AlertLogCreate{
Status: constant.AlertSuccess,
Count: totalCount + 1,
AlertId: s.alert.ID,
Type: s.alert.Type,
AlertRule: alertUtil.ProcessAlertRule(s.alert),
AlertDetail: alertUtil.ProcessAlertDetail(s.alert, quota, params, constant.Bark),
}

transport := xpack.LoadRequestTransport()
agentInfo, _ := xpack.GetAgentInfo()
err := alertUtil.CreateBarkAlertLog(create, s.alert, params, transport, agentInfo)
if err != nil {
global.LOG.Errorf("%s alert bark push failed: %v", s.alert.Type, err)
return
}
alertUtil.CreateNewAlertTask(quota, s.alert.Type, s.quotaType, constant.Bark)
}

func (s *AlertSender) sendWebhook(quota string, params []dto.Param, method string) {
totalCount, isValid := s.canSendAlert(method)
if !isValid {
Expand Down Expand Up @@ -171,6 +200,30 @@ func (s *AlertSender) sendResourceEmail(quota string, params []dto.Param) {
alertUtil.CreateNewAlertTask(quota, s.alert.Type, s.quotaType, constant.Email)
}

func (s *AlertSender) sendResourceBark(quota string, params []dto.Param) {
todayCount, isValid := s.canResourceSendAlert(constant.Bark)
if !isValid {
return
}

create := dto.AlertLogCreate{
Status: constant.AlertSuccess,
Count: todayCount + 1,
AlertId: s.alert.ID,
Type: s.alert.Type,
AlertRule: alertUtil.ProcessAlertRule(s.alert),
AlertDetail: alertUtil.ProcessAlertDetail(s.alert, quota, params, constant.Bark),
}

transport := xpack.LoadRequestTransport()
agentInfo, _ := xpack.GetAgentInfo()
if err := alertUtil.CreateBarkAlertLog(create, s.alert, params, transport, agentInfo); err != nil {
global.LOG.Errorf("failed to send Bark alert: %v", err)
return
}
alertUtil.CreateNewAlertTask(quota, s.alert.Type, s.quotaType, constant.Bark)
}

func (s *AlertSender) sendResourceWebhook(quota string, params []dto.Param, method string) {
todayCount, isValid := s.canResourceSendAlert(method)
if !isValid {
Expand Down
1 change: 1 addition & 0 deletions agent/constant/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ const (
DingTalk = "dingTalk"
FeiShu = "feiShu"
Custom = "custom"
Bark = "bark"
)
35 changes: 35 additions & 0 deletions agent/utils/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/i18n"
"github.com/1Panel-dev/1Panel/agent/utils/bark"
"github.com/1Panel-dev/1Panel/agent/utils/email"
"github.com/1Panel-dev/1Panel/agent/utils/psutil"
"github.com/1Panel-dev/1Panel/agent/utils/re"
Expand Down Expand Up @@ -103,6 +104,40 @@ func CreateEmailAlertLog(create dto.AlertLogCreate, alert dto.AlertDTO, params [
}
}

func CreateBarkAlertLog(create dto.AlertLogCreate, alert dto.AlertDTO, params []dto.Param, transport *http.Transport, agentInfo *dto.AgentInfo) error {
var alertLog model.AlertLog
alertRepo := repo.NewIAlertRepo()

create.Method = constant.Bark
barkConfig, err := alertRepo.GetConfig(alertRepo.WithByType(constant.Bark))
if err != nil {
return err
}
var barkInfo dto.AlertWebhookConfig
err = json.Unmarshal([]byte(barkConfig.Config), &barkInfo)
if err != nil {
return err
}
if barkInfo.Url == "" {
create.Message = "bark config url is required"
create.Status = constant.AlertError
return SaveAlertLog(create, &alertLog)
}

content := GetSendContent(alert.Type, params, agentInfo)
if content == "" {
content = i18n.GetMsgWithMap("CommonAlert", map[string]interface{}{"msg": alert.Title})
}

if err = bark.SendMessage(barkInfo.Url, i18n.GetMsgByKey("PanelAlertTitle"), content, transport); err != nil {
create.Message = err.Error()
create.Status = constant.AlertError
return SaveAlertLog(create, &alertLog)
}
create.Status = constant.AlertSuccess
return SaveAlertLog(create, &alertLog)
}

func SaveAlertLog(create dto.AlertLogCreate, alertLog *model.AlertLog) error {
alertRepo := repo.NewIAlertRepo()
if err := copier.Copy(&alertLog, &create); err != nil {
Expand Down
23 changes: 23 additions & 0 deletions agent/utils/alert_push/alert_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ func PushAlert(pushAlert dto.PushAlert) error {
continue
}
alertUtil.CreateNewAlertTask(strconv.Itoa(int(pushAlert.EntryID)), alertUtil.GetCronJobType(alert.Type), strconv.Itoa(int(pushAlert.EntryID)), constant.Email)
case constant.Bark:
todayCount, _, err := alertRepo.LoadTaskCount(alertUtil.GetCronJobType(alert.Type), strconv.Itoa(int(pushAlert.EntryID)), constant.Bark)
if err != nil || alert.SendCount <= todayCount {
continue
}
var create = dto.AlertLogCreate{
Type: alertUtil.GetCronJobType(alert.Type),
AlertId: alert.ID,
Count: todayCount + 1,
}
transport := xpack.LoadRequestTransport()
agentInfo, _ := xpack.GetAgentInfo()
params := alertUtil.CreateAlertParams(alertUtil.GetCronJobTypeName(pushAlert.Param))
alertDetail := alertUtil.ProcessAlertDetail(alert, pushAlert.TaskName, params, constant.Bark)
alertRule := alertUtil.ProcessAlertRule(alert)
create.AlertRule = alertRule
create.AlertDetail = alertDetail
err = alertUtil.CreateBarkAlertLog(create, alert, params, transport, agentInfo)
if err != nil {
global.LOG.Errorf("%s alert bark push failed: %v", alert.Type, err)
continue
}
alertUtil.CreateNewAlertTask(strconv.Itoa(int(pushAlert.EntryID)), alertUtil.GetCronJobType(alert.Type), strconv.Itoa(int(pushAlert.EntryID)), constant.Bark)
case constant.WeCom, constant.DingTalk, constant.FeiShu:
todayCount, _, err := alertRepo.LoadTaskCount(alertUtil.GetCronJobType(alert.Type), strconv.Itoa(int(pushAlert.EntryID)), m)
if err != nil || alert.SendCount <= todayCount {
Expand Down
46 changes: 46 additions & 0 deletions agent/utils/bark/bark.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package bark

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)

type BarkMessage struct {
Title string `json:"title"`
Body string `json:"body"`
}

func SendMessage(url string, title string, body string, transport *http.Transport) error {
msg := BarkMessage{
Title: title,
Body: body,
}
data, err := json.Marshal(msg)
if err != nil {
return err
}

client := &http.Client{
Transport: transport,
}

req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")

resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bark push failed with status code %d", resp.StatusCode)
}

return nil
}
2 changes: 2 additions & 0 deletions frontend/src/lang/modules/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4076,6 +4076,7 @@ const message = {
feiShu: 'FeiShu',
mail: 'Email',
weCom: 'WeCom',
bark: 'Bark',
sendCountRulesHelper: 'Total alerts sent before expiry (once daily)',
panelUpdateRulesHelper: 'Total alerts sent for new panel version (once daily)',
oneDaySendCountRulesHelper: 'Maximum alerts sent per day',
Expand Down Expand Up @@ -4245,6 +4246,7 @@ const message = {
userNameHelper: 'Username is empty, the sender address will be used by default',
alertConfigHelper: 'Configure alert notification channels to receive panel message push',
weComConfigHelper: 'WeCom alert notification configuration',
barkConfigHelper: 'Bark alert notification configuration',
wechatConfigHelper: 'WeChat Official Account alert notification configuration',
dingTalkConfigHelper: 'DingTalk alert notification configuration',
feiShuConfigHelper: 'Feishu alert notification configuration',
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lang/modules/es-es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4088,6 +4088,7 @@ const message = {
feiShu: 'FeiShu',
mail: 'Correo',
weCom: 'WeCom',
bark: 'Bark',
sendCountRulesHelper: 'Número total de alertas enviadas antes de expirar (una vez al día)',
panelUpdateRulesHelper: 'Alertas totales enviadas por nueva versión del panel (una vez al día)',
oneDaySendCountRulesHelper: 'Número máximo de alertas diarias',
Expand Down Expand Up @@ -4256,6 +4257,7 @@ const message = {
wechatConfigHelper: 'Configuración de notificación de alerta de Cuenta Oficial WeChat',
dingTalkConfigHelper: 'Configuración de notificación de alerta DingTalk',
feiShuConfigHelper: 'Configuración de notificación de alerta Feishu',
barkConfigHelper: 'Configuración de notificación de alerta Bark',
webhookName: 'Nombre del bot',
webhookUrl: 'URL de Webhook',
alertConfigProHelper:
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lang/modules/ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4081,6 +4081,7 @@ const message = {
feiShu: 'FeiShu',
mail: 'メール',
weCom: 'WeCom',
bark: 'Bark',
sendCountRulesHelper: '期限前に送信されるアラートの合計(1日1回)',
panelUpdateRulesHelper: '新しいパネルバージョンに関するアラートの合計(1日1回)',
oneDaySendCountRulesHelper: '1日に送信できる最大アラート回数',
Expand Down Expand Up @@ -4254,6 +4255,7 @@ const message = {
wechatConfigHelper: 'WeChat公式アカウントアラート通知設定',
dingTalkConfigHelper: 'DingTalkアラート通知設定',
feiShuConfigHelper: 'Feishuアラート通知設定',
barkConfigHelper: 'Barkアラート通知設定',
webhookName: 'ボット名',
webhookUrl: 'Webhook URL',
alertConfigProHelper:
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lang/modules/ko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3990,6 +3990,7 @@ const message = {
feiShu: '페이슈',
mail: '이메일',
weCom: 'WeCom',
bark: 'Bark',
sendCountRulesHelper: '만료 전 발송된 총 알림 수 (하루 1회)',
panelUpdateRulesHelper: '새 패널 버전에 대한 총 알림 수 (하루 1회)',
oneDaySendCountRulesHelper: '하루 최대 발송 가능한 알림 수',
Expand Down Expand Up @@ -4158,6 +4159,7 @@ const message = {
wechatConfigHelper: 'WeChat 공식 계정 알림 구성',
dingTalkConfigHelper: 'DingTalk 알림 구성',
feiShuConfigHelper: 'Feishu 알림 구성',
barkConfigHelper: 'Bark 알림 구성',
webhookName: '봇 이름',
webhookUrl: 'Webhook URL',
alertConfigProHelper:
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lang/modules/ms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4135,6 +4135,7 @@ const message = {
feiShu: 'FeiShu',
mail: 'E-mel',
weCom: 'WeCom',
bark: 'Bark',
sendCountRulesHelper: 'Jumlah amaran dihantar sebelum tamat tempoh (sekali sehari)',
panelUpdateRulesHelper: 'Jumlah amaran dihantar untuk versi panel baharu (sekali sehari)',
oneDaySendCountRulesHelper: 'Maksimum amaran dihantar setiap hari',
Expand Down Expand Up @@ -4312,6 +4313,7 @@ const message = {
wechatConfigHelper: 'Konfigurasi pemberitahuan amaran Akaun Rasmi WeChat',
dingTalkConfigHelper: 'Konfigurasi pemberitahuan amaran DingTalk',
feiShuConfigHelper: 'Konfigurasi pemberitahuan amaran Feishu',
barkConfigHelper: 'Konfigurasi pemberitahuan amaran Bark',
webhookName: 'Nama bot',
webhookUrl: 'URL Webhook',
alertConfigProHelper: 'Edisi Profesional turut menyokong amaran WeCom, DingTalk, Feishu dan SMS.',
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lang/modules/pt-br.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4285,6 +4285,7 @@ const message = {
feiShu: 'FeiShu',
mail: 'E-mail',
weCom: 'WeCom',
bark: 'Bark',
sendCountRulesHelper: 'Alertas totais enviados antes da expiração (uma vez por dia)',
panelUpdateRulesHelper: 'Alertas totais enviados para nova versão do painel (uma vez por dia)',
oneDaySendCountRulesHelper: 'Número máximo de alertas enviados por dia',
Expand Down Expand Up @@ -4461,6 +4462,7 @@ const message = {
wechatConfigHelper: 'Configuração de notificação de alerta da Conta Oficial WeChat',
dingTalkConfigHelper: 'Configuração de notificação de alerta DingTalk',
feiShuConfigHelper: 'Configuração de notificação de alerta Feishu',
barkConfigHelper: 'Configuração de notificação de alerta Bark',
webhookName: 'Nome do bot',
webhookUrl: 'URL do Webhook',
alertConfigProHelper:
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lang/modules/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4137,6 +4137,7 @@ const message = {
feiShu: 'FeiShu',
mail: 'Электронная Почта',
weCom: 'WeCom',
bark: 'Bark',
sendCountRulesHelper: 'Общее количество уведомлений до истечения срока действия (раз в день)',
panelUpdateRulesHelper: 'Общее количество уведомлений для новой версии панели (раз в день)',
oneDaySendCountRulesHelper: 'Максимальное количество уведомлений в день',
Expand Down Expand Up @@ -4317,6 +4318,7 @@ const message = {
wechatConfigHelper: 'Конфигурация уведомлений официального аккаунта WeChat',
dingTalkConfigHelper: 'Конфигурация уведомлений DingTalk',
feiShuConfigHelper: 'Конфигурация уведомлений Feishu',
barkConfigHelper: 'Конфигурация уведомлений Bark',
webhookName: 'Имя бота',
webhookUrl: 'URL Webhook',
alertConfigProHelper:
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lang/modules/tr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4127,6 +4127,7 @@ const message = {
feiShu: 'FeiShu',
mail: 'E-posta',
weCom: 'WeCom',
bark: 'Bark',
sendCountRulesHelper: 'Sona ermeden önce gönderilen toplam uyarılar (günde bir kez)',
panelUpdateRulesHelper:
'Yeni panel sürümü algılandığında bir kez uyarı gönder (işlenmezse ertesi gün tekrar gönderilir)',
Expand Down Expand Up @@ -4305,6 +4306,7 @@ const message = {
wechatConfigHelper: 'WeChat Resmi Hesap uyarı bildirim yapılandırması',
dingTalkConfigHelper: 'DingTalk uyarı bildirim yapılandırması',
feiShuConfigHelper: 'Feishu uyarı bildirim yapılandırması',
barkConfigHelper: 'Bark uyarı bildirim yapılandırması',
webhookName: 'Bot adı',
webhookUrl: 'Webhook URL',
alertConfigProHelper:
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lang/modules/zh-Hant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3750,6 +3750,7 @@ const message = {
feiShu: '飛書通知',
mail: '信箱通知',
weCom: '企業微信',
bark: 'Bark',
sendCountRulesHelper: '到期前發送告警的總數(每日僅發送一次)',
panelUpdateRulesHelper: '新版本發送告警總數(每日僅發送一次)',
oneDaySendCountRulesHelper: '每日發送告警的總數',
Expand Down Expand Up @@ -3915,6 +3916,7 @@ const message = {
wechatConfigHelper: '微信公眾號告警通知設定',
dingTalkConfigHelper: '釘釘告警通知設定',
feiShuConfigHelper: '飛書告警通知設定',
barkConfigHelper: 'Bark 告警通知設定',
webhookName: '機器人名稱',
webhookUrl: 'Webhook 位址',
alertConfigProHelper: '專業版額外支援企業微信、釘釘、飛書及簡訊告警。',
Expand Down
Loading
Loading