Skip to content

Commit cca1863

Browse files
authored
feat: Adjust alarm settings (#9616)
1 parent 033f08a commit cca1863

File tree

14 files changed

+174
-112
lines changed

14 files changed

+174
-112
lines changed

agent/app/dto/alert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,13 @@ type AlertSendTimeRange struct {
307307
}
308308

309309
type AlertCommonConfig struct {
310-
AlertDailyNum uint `json:"alertDailyNum"`
311310
IsOffline string `json:"isOffline"`
312311
AlertSendTimeRange AlertSendTimeRange `json:"alertSendTimeRange"`
313312
}
314313

315314
type AlertSmsConfig struct {
316-
Phone string `json:"phone"`
315+
Phone string `json:"phone"`
316+
AlertDailyNum uint `json:"alertDailyNum"`
317317
}
318318

319319
type AlertEmailConfig struct {

agent/app/model/alert.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type AlertTask struct {
1818
Type string `gorm:"type:varchar(64);not null" json:"type"`
1919
Quota string `gorm:"type:varchar(64)" json:"quota"`
2020
QuotaType string `gorm:"type:varchar(64)" json:"quotaType"`
21+
Method string `gorm:"type:varchar(64);not null;default:'sms" json:"method"`
2122
}
2223

2324
type AlertLog struct {

agent/app/repo/alert.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type IAlertRepo interface {
4444
GetAlertTask(opts ...DBOption) (model.AlertTask, error)
4545
LoadTaskCount(alertType string, project string) (uint, uint, error)
4646
GetTaskLog(alertType string, alertId uint) (time.Time, error)
47-
GetLicensePushCount() (uint, error)
47+
GetLicensePushCount(method string) (uint, error)
4848

4949
GetConfig(opts ...DBOption) (model.AlertConfig, error)
5050
AlertConfigList(opts ...DBOption) ([]model.AlertConfig, error)
@@ -267,14 +267,14 @@ func getAlertDB(opts ...DBOption) (*gorm.DB, error) {
267267
return db, nil
268268
}
269269

270-
func (a *AlertRepo) GetLicensePushCount() (uint, error) {
270+
func (a *AlertRepo) GetLicensePushCount(method string) (uint, error) {
271271
var (
272272
todayCount int64
273273
)
274274
now := time.Now()
275275
todayMidnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
276276
tomorrowMidnight := todayMidnight.Add(24 * time.Hour)
277-
err := global.AlertDB.Model(&model.AlertTask{}).Where("created_at > ? AND created_at < ?", todayMidnight, tomorrowMidnight).Count(&todayCount).Error
277+
err := global.AlertDB.Model(&model.AlertTask{}).Where("created_at > ? AND created_at < ? AND method = ?", todayMidnight, tomorrowMidnight, method).Count(&todayCount).Error
278278
return uint(todayCount), err
279279
}
280280

agent/app/service/alert_helper.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ func (m *AlertTaskHelper) InitTask(alertType string) {
8787

8888
func resourceTask(resourceAlert []dto.AlertDTO) {
8989
for _, alert := range resourceAlert {
90-
if !alertUtil.CheckTaskFrequency() {
91-
return
92-
}
9390
if !alertUtil.CheckSendTimeRange(alert.Type) {
9491
continue
9592
}
@@ -109,9 +106,6 @@ func resourceTask(resourceAlert []dto.AlertDTO) {
109106

110107
func baseTask(baseAlert []dto.AlertDTO) {
111108
for _, alert := range baseAlert {
112-
if !alertUtil.CheckTaskFrequency() {
113-
return
114-
}
115109
if !alertUtil.CheckSendTimeRange(alert.Type) {
116110
continue
117111
}
@@ -245,20 +239,23 @@ func loadSSLInfo(alert dto.AlertDTO) {
245239
params = createAlertBaseParams(strconv.Itoa(len(primaryDomain)), strconv.Itoa(daysDifference))
246240
switch m {
247241
case constant.SMS:
242+
if !alertUtil.CheckTaskFrequency(constant.SMS) {
243+
continue
244+
}
248245
_ = xpack.CreateSMSAlertLog(alert, create, primaryDomain, params, constant.SMS)
246+
alertUtil.CreateNewAlertTask(alert.Project, alert.Type, projectJSON, constant.SMS)
249247
case constant.Email:
250248
alertDetail := alertUtil.ProcessAlertDetail(alert, primaryDomain, params, constant.Email)
251249
alertRule := alertUtil.ProcessAlertRule(alert)
252250
create.AlertRule = alertRule
253251
create.AlertDetail = alertDetail
254252
transport := xpack.LoadRequestTransport()
255253
_ = alertUtil.CreateEmailAlertLog(create, alert, params, transport)
254+
alertUtil.CreateNewAlertTask(alert.Project, alert.Type, projectJSON, constant.Email)
256255
default:
257256
}
258257
}
259258
}
260-
261-
alertUtil.CreateNewAlertTask(alert.Project, alert.Type, projectJSON)
262259
global.LOG.Info("SSL alert push successful")
263260
}
264261
}
@@ -305,19 +302,23 @@ func loadWebsiteInfo(alert dto.AlertDTO) {
305302
params = createAlertBaseParams(strconv.Itoa(len(websites)), strconv.Itoa(daysDifference))
306303
switch m {
307304
case constant.SMS:
305+
if !alertUtil.CheckTaskFrequency(constant.SMS) {
306+
continue
307+
}
308308
_ = xpack.CreateSMSAlertLog(alert, create, primaryDomain, params, constant.SMS)
309+
alertUtil.CreateNewAlertTask(alert.Project, alert.Type, projectJSON, constant.SMS)
309310
case constant.Email:
310311
alertDetail := alertUtil.ProcessAlertDetail(alert, primaryDomain, params, constant.Email)
311312
alertRule := alertUtil.ProcessAlertRule(alert)
312313
create.AlertDetail = alertDetail
313314
create.AlertRule = alertRule
314315
transport := xpack.LoadRequestTransport()
315316
_ = alertUtil.CreateEmailAlertLog(create, alert, params, transport)
317+
alertUtil.CreateNewAlertTask(alert.Project, alert.Type, projectJSON, constant.Email)
316318
default:
317319
}
318320
}
319321
}
320-
alertUtil.CreateNewAlertTask(alert.Project, alert.Type, projectJSON)
321322
global.LOG.Info("website expiration alert push successful")
322323
}
323324
}
@@ -358,18 +359,22 @@ func loadPanelPwd(alert dto.AlertDTO) {
358359
m = strings.TrimSpace(m)
359360
switch m {
360361
case constant.SMS:
362+
if !alertUtil.CheckTaskFrequency(constant.SMS) {
363+
continue
364+
}
361365
_ = xpack.CreateSMSAlertLog(alert, create, strconv.Itoa(daysDifference), params, constant.SMS)
366+
alertUtil.CreateNewAlertTask(expirationTime.Value, alert.Type, expirationTime.Value, constant.SMS)
362367
case constant.Email:
363368
alertDetail := alertUtil.ProcessAlertDetail(alert, strconv.Itoa(daysDifference), params, constant.Email)
364369
alertRule := alertUtil.ProcessAlertRule(alert)
365370
create.AlertRule = alertRule
366371
create.AlertDetail = alertDetail
367372
transport := xpack.LoadRequestTransport()
368373
_ = alertUtil.CreateEmailAlertLog(create, alert, params, transport)
374+
alertUtil.CreateNewAlertTask(expirationTime.Value, alert.Type, expirationTime.Value, constant.Email)
369375
default:
370376
}
371377
}
372-
alertUtil.CreateNewAlertTask(expirationTime.Value, alert.Type, expirationTime.Value)
373378
global.LOG.Info("panel password expiration alert push successful")
374379
}
375380
}
@@ -411,18 +416,22 @@ func loadPanelUpdate(alert dto.AlertDTO) {
411416
m = strings.TrimSpace(m)
412417
switch m {
413418
case constant.SMS:
419+
if !alertUtil.CheckTaskFrequency(constant.SMS) {
420+
continue
421+
}
414422
_ = xpack.CreateSMSAlertLog(alert, create, version, params, constant.SMS)
423+
alertUtil.CreateNewAlertTask(version, alert.Type, version, constant.SMS)
415424
case constant.Email:
416425
alertDetail := alertUtil.ProcessAlertDetail(alert, version, params, constant.Email)
417426
alertRule := alertUtil.ProcessAlertRule(alert)
418427
create.AlertRule = alertRule
419428
create.AlertDetail = alertDetail
420429
transport := xpack.LoadRequestTransport()
421430
_ = alertUtil.CreateEmailAlertLog(create, alert, params, transport)
431+
alertUtil.CreateNewAlertTask(version, alert.Type, version, constant.Email)
422432
default:
423433
}
424434
}
425-
alertUtil.CreateNewAlertTask(version, alert.Type, version)
426435
global.LOG.Info("panel update alert push successful")
427436
}
428437

@@ -586,18 +595,22 @@ func createAndLogAlert(alert dto.AlertDTO, avgUsage float64, todayCount uint) {
586595
m = strings.TrimSpace(m)
587596
switch m {
588597
case constant.SMS:
598+
if !alertUtil.CheckTaskFrequency(constant.SMS) {
599+
continue
600+
}
589601
_ = xpack.CreateSMSAlertLog(alert, create, avgUsagePercent, params, constant.SMS)
602+
alertUtil.CreateNewAlertTask(avgUsagePercent, alert.Type, strconv.Itoa(int(alert.Cycle)), constant.SMS)
590603
case constant.Email:
591604
alertDetail := alertUtil.ProcessAlertDetail(alert, avgUsagePercent, params, constant.Email)
592605
alertRule := alertUtil.ProcessAlertRule(alert)
593606
create.AlertRule = alertRule
594607
create.AlertDetail = alertDetail
595608
transport := xpack.LoadRequestTransport()
596609
_ = alertUtil.CreateEmailAlertLog(create, alert, params, transport)
610+
alertUtil.CreateNewAlertTask(avgUsagePercent, alert.Type, strconv.Itoa(int(alert.Cycle)), constant.Email)
597611
default:
598612
}
599613
}
600-
alertUtil.CreateNewAlertTask(avgUsagePercent, alert.Type, strconv.Itoa(int(alert.Cycle)))
601614
}
602615

603616
func getModule(alertType string) string {
@@ -649,25 +662,20 @@ func processAllDisks(alert dto.AlertDTO, todayCount uint) error {
649662
flag = true
650663
}
651664
}
652-
653665
if flag {
654-
alertUtil.CreateNewAlertTask(strconv.Itoa(int(alert.Cycle)), alert.Type, alert.Project)
655666
global.LOG.Info("all disk alert push successful")
656667
}
657668
return nil
658669
}
659670

660671
func processSingleDisk(alert dto.AlertDTO, todayCount uint) error {
661-
662672
success, err := checkAndCreateDiskAlert(alert, alert.Project, todayCount)
663673
if err != nil {
664674
return err
665675
}
666676
if success {
667-
alertUtil.CreateNewAlertTask(strconv.Itoa(int(alert.Cycle)), alert.Type, alert.Project)
668677
global.LOG.Info("disk alert push successful")
669678
}
670-
671679
return nil
672680
}
673681

@@ -700,14 +708,19 @@ func checkAndCreateDiskAlert(alert dto.AlertDTO, path string, todayCount uint) (
700708
m = strings.TrimSpace(m)
701709
switch m {
702710
case constant.SMS:
711+
if !alertUtil.CheckTaskFrequency(constant.SMS) {
712+
continue
713+
}
703714
_ = xpack.CreateSMSAlertLog(alert, create, path, params, constant.SMS)
715+
alertUtil.CreateNewAlertTask(strconv.Itoa(int(alert.Cycle)), alert.Type, alert.Project, constant.SMS)
704716
case constant.Email:
705717
alertDetail := alertUtil.ProcessAlertDetail(alert, path, params, constant.Email)
706718
alertRule := alertUtil.ProcessAlertRule(alert)
707719
create.AlertRule = alertRule
708720
create.AlertDetail = alertDetail
709721
transport := xpack.LoadRequestTransport()
710722
_ = alertUtil.CreateEmailAlertLog(create, alert, params, transport)
723+
alertUtil.CreateNewAlertTask(strconv.Itoa(int(alert.Cycle)), alert.Type, alert.Project, constant.Email)
711724
default:
712725
}
713726
}

agent/init/migration/migrate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func InitAgentDB() {
3232
migrations.AddTableAlert,
3333
migrations.InitAlertConfig,
3434
migrations.AddMethodToAlertLog,
35+
migrations.AddMethodToAlertTask,
3536
})
3637
if err := m.Migrate(); err != nil {
3738
global.LOG.Error(err)

agent/init/migration/migrations/init.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,13 @@ var InitAlertConfig = &gormigrate.Migration{
376376
Type: "sms",
377377
Title: "xpack.alert.smsConfig",
378378
Status: "Enable",
379-
Config: "{}",
379+
Config: `{"alertDailyNum":50}`,
380380
},
381381
{
382382
Type: "common",
383383
Title: "xpack.alert.commonConfig",
384384
Status: "Enable",
385-
Config: `{"alertDailyNum":50,"isOffline":"Disable","alertSendTimeRange":{"noticeAlert":{"sendTimeRange":"08:00:00 - 23:59:59","type":["ssl","siteEndTime","panelPwdEndTime","panelUpdate"]},"resourceAlert":{"sendTimeRange":"00:00:00 - 23:59:59","type":["clams","cronJob","cpu","memory","load","disk"]}}}`,
385+
Config: `{"isOffline":"Disable","alertSendTimeRange":{"noticeAlert":{"sendTimeRange":"08:00:00 - 23:59:59","type":["ssl","siteEndTime","panelPwdEndTime","panelUpdate"]},"resourceAlert":{"sendTimeRange":"00:00:00 - 23:59:59","type":["clams","cronJob","cpu","memory","load","disk"]}}}`,
386386
},
387387
}
388388
for _, r := range records {
@@ -406,3 +406,16 @@ var AddMethodToAlertLog = &gormigrate.Migration{
406406
return nil
407407
},
408408
}
409+
410+
var AddMethodToAlertTask = &gormigrate.Migration{
411+
ID: "20250723-add-method-to-alert_task",
412+
Migrate: func(tx *gorm.DB) error {
413+
if err := global.AlertDB.AutoMigrate(&model.AlertTask{}); err != nil {
414+
return err
415+
}
416+
if err := global.AlertDB.Model(&model.AlertTask{}).Where("method IS NULL OR method = ''").Update("method", "sms").Error; err != nil {
417+
return err
418+
}
419+
return nil
420+
},
421+
}

agent/utils/alert/alert.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,13 @@ func SaveAlertLog(create dto.AlertLogCreate, alertLog *model.AlertLog) error {
100100
return nil
101101
}
102102

103-
func CreateNewAlertTask(quota, alertType, quotaType string) {
103+
func CreateNewAlertTask(quota, alertType, quotaType, method string) {
104104
alertRepo := repo.NewIAlertRepo()
105105
taskBase := model.AlertTask{
106106
Type: alertType,
107107
Quota: quota,
108108
QuotaType: quotaType,
109+
Method: method,
109110
}
110111
err := alertRepo.CreateAlertTask(&taskBase)
111112
if err != nil {
@@ -190,20 +191,20 @@ func CreateAlertParams(param string) []dto.Param {
190191

191192
var checkTaskMutex sync.Mutex
192193

193-
func CheckTaskFrequency() bool {
194+
func CheckTaskFrequency(method string) bool {
194195
alertRepo := repo.NewIAlertRepo()
195-
config, err := alertRepo.GetConfig(alertRepo.WithByType(constant.CommonConfig))
196+
config, err := alertRepo.GetConfig(alertRepo.WithByType(constant.SMSConfig))
196197
if err != nil {
197198
return false
198199
}
199-
var cfg dto.AlertCommonConfig
200+
var cfg dto.AlertSmsConfig
200201
err = json.Unmarshal([]byte(config.Config), &cfg)
201202
if err != nil {
202203
return false
203204
}
204205
limitCount := cfg.AlertDailyNum
205206
checkTaskMutex.Lock()
206-
todayCount, err := repo.NewIAlertRepo().GetLicensePushCount()
207+
todayCount, err := alertRepo.GetLicensePushCount(method)
207208
defer checkTaskMutex.Unlock()
208209
if err != nil {
209210
global.LOG.Errorf("error getting license push count info, err: %v", err)

agent/utils/alert_push/alert_push.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ import (
1313
)
1414

1515
func PushAlert(pushAlert dto.PushAlert) error {
16-
if !alertUtil.CheckTaskFrequency() {
17-
return nil
18-
}
19-
2016
if !alertUtil.CheckSendTimeRange(alertUtil.GetCronJobType(pushAlert.AlertType)) {
2117
return nil
2218
}
@@ -45,18 +41,21 @@ func PushAlert(pushAlert dto.PushAlert) error {
4541
m = strings.TrimSpace(m)
4642
switch m {
4743
case constant.SMS:
44+
if !alertUtil.CheckTaskFrequency(constant.SMS) {
45+
continue
46+
}
4847
_ = xpack.CreateTaskScanSMSAlertLog(alert, create, pushAlert, constant.SMS)
48+
alertUtil.CreateNewAlertTask(strconv.Itoa(int(pushAlert.EntryID)), alertUtil.GetCronJobType(alert.Type), strconv.Itoa(int(pushAlert.EntryID)), constant.SMS)
4949
case constant.Email:
5050
transport := xpack.LoadRequestTransport()
5151
err := alertUtil.CreateTaskScanEmailAlertLog(alert, create, pushAlert, constant.Email, transport)
5252
if err != nil {
5353
return err
5454
}
55+
alertUtil.CreateNewAlertTask(strconv.Itoa(int(pushAlert.EntryID)), alertUtil.GetCronJobType(alert.Type), strconv.Itoa(int(pushAlert.EntryID)), constant.Email)
5556
default:
5657
}
5758
}
58-
// 处理告警任务
59-
alertUtil.CreateNewAlertTask(strconv.Itoa(int(pushAlert.EntryID)), alertUtil.GetCronJobType(alert.Type), strconv.Itoa(int(pushAlert.EntryID)))
6059
global.LOG.Infof("%s alert push successful", alert.Type)
6160
return nil
6261
}

frontend/src/api/interface/setting.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,10 @@ export namespace Setting {
253253
force: boolean;
254254
withDockerRestart: boolean;
255255
}
256+
257+
export interface SmsInfo {
258+
licenseName: string;
259+
smsUsed: number;
260+
smsTotal: number;
261+
}
256262
}

frontend/src/api/modules/setting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const listAllNodes = () => {
4949
};
5050

5151
export const getLicenseSmsInfo = () => {
52-
return http.get<Setting.LicenseStatus>(`/core/licenses/sms/info`);
52+
return http.get<Setting.SmsInfo>(`/core/licenses/sms/info`);
5353
};
5454

5555
// agent

0 commit comments

Comments
 (0)