From 3db8a61d1686d6e36626ff3903832b87121bcceb Mon Sep 17 00:00:00 2001 From: gouqi11 <66834753+gouqi11@users.noreply.github.com> Date: Wed, 18 Jan 2023 23:34:18 +0800 Subject: [PATCH] fix(notify): add server_panic topic (#15826) --- .../content@cn/SERVER_PANICKED_FAILED.tmpl | 2 ++ .../content@en/SERVER_PANICKED_FAILED.tmpl | 2 ++ .../title@cn/SERVER_PANICKED_FAILED.tmpl | 2 ++ .../title@en/SERVER_PANICKED_FAILED.tmpl | 2 ++ pkg/apis/notify/event.go | 1 + pkg/cloudcommon/notifyclient/events.go | 1 + pkg/compute/models/guest_actions.go | 12 +++++------- pkg/notify/models/notification.go | 2 +- pkg/notify/models/robot.go | 1 - pkg/notify/models/topic.go | 12 ++++++++++++ 10 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 build/notify/root/opt/yunion/share/local-templates/content@cn/SERVER_PANICKED_FAILED.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/content@en/SERVER_PANICKED_FAILED.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/title@cn/SERVER_PANICKED_FAILED.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/title@en/SERVER_PANICKED_FAILED.tmpl diff --git a/build/notify/root/opt/yunion/share/local-templates/content@cn/SERVER_PANICKED_FAILED.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@cn/SERVER_PANICKED_FAILED.tmpl new file mode 100644 index 0000000000..e3af2f8426 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/content@cn/SERVER_PANICKED_FAILED.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +您在{{ $d.project }}项目的虚拟机{{ $d.name }}崩溃了 \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/content@en/SERVER_PANICKED_FAILED.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@en/SERVER_PANICKED_FAILED.tmpl new file mode 100644 index 0000000000..74a1779c23 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/content@en/SERVER_PANICKED_FAILED.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +The server {{ $d.name }} in project {{ $d.project }} panicked. \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/title@cn/SERVER_PANICKED_FAILED.tmpl b/build/notify/root/opt/yunion/share/local-templates/title@cn/SERVER_PANICKED_FAILED.tmpl new file mode 100644 index 0000000000..e3af2f8426 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/title@cn/SERVER_PANICKED_FAILED.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +您在{{ $d.project }}项目的虚拟机{{ $d.name }}崩溃了 \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/title@en/SERVER_PANICKED_FAILED.tmpl b/build/notify/root/opt/yunion/share/local-templates/title@en/SERVER_PANICKED_FAILED.tmpl new file mode 100644 index 0000000000..1610354d5f --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/title@en/SERVER_PANICKED_FAILED.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +The server {{ $d.name }} in project {{ $d.project }}panicked. \ No newline at end of file diff --git a/pkg/apis/notify/event.go b/pkg/apis/notify/event.go index 270f4f8e97..693e9feaf1 100644 --- a/pkg/apis/notify/event.go +++ b/pkg/apis/notify/event.go @@ -59,6 +59,7 @@ var ( ActionNetOutOfSync SAction = "net_out_of_sync" ActionMysqlOutOfSync SAction = "mysql_out_of_sync" ActionServiceAbnormal SAction = "service_abnormal" + ActionServerPanicked SAction = "server_panicked" ResultFailed SResult = "failed" ResultSucceed SResult = "succeed" diff --git a/pkg/cloudcommon/notifyclient/events.go b/pkg/cloudcommon/notifyclient/events.go index 98051ca820..7f0dc15364 100644 --- a/pkg/cloudcommon/notifyclient/events.go +++ b/pkg/cloudcommon/notifyclient/events.go @@ -59,6 +59,7 @@ var ( ActionNetOutOfSync = api.ActionNetOutOfSync ActionMysqlOutOfSync = api.ActionMysqlOutOfSync ActionServiceAbnormal = api.ActionServiceAbnormal + ActionServerPanicked = api.ActionServerPanicked ActionPendingDelete = api.ActionPendingDelete diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index b06864ec1b..c3c3e08eda 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -140,13 +140,11 @@ func (self *SGuest) PerformEvent(ctx context.Context, userCred mcclient.TokenCre db.OpsLog.LogEvent(self, db.ACT_GUEST_PANICKED, data.String(), userCred) logclient.AddSimpleActionLog(self, logclient.ACT_GUEST_PANICKED, data.String(), userCred, true) - self.NotifyServerEvent( - ctx, - userCred, - notifyclient.SERVER_PANICKED, - notify.NotifyPriorityNormal, - false, kwargs, true, - ) + notifyclient.EventNotify(ctx, userCred, notifyclient.SEventNotifyParam{ + Obj: self, + Action: notifyclient.ActionServerPanicked, + IsFail: true, + }) } return nil, nil } diff --git a/pkg/notify/models/notification.go b/pkg/notify/models/notification.go index 656d0c2988..a6382512af 100644 --- a/pkg/notify/models/notification.go +++ b/pkg/notify/models/notification.go @@ -650,7 +650,7 @@ func dataCleaning(tableName string) error { now := time.Now() monthsDaysAgo := now.AddDate(0, -1, 0).Format("2006-01-02 15:04:05") sqlStr := fmt.Sprintf( - "update %s set deleted = 1 where deleted = 0 and created_at < '%s'", + "delete from %s where created_at < '%s'", tableName, monthsDaysAgo, ) diff --git a/pkg/notify/models/robot.go b/pkg/notify/models/robot.go index c26045fb9a..43d205e7eb 100644 --- a/pkg/notify/models/robot.go +++ b/pkg/notify/models/robot.go @@ -85,7 +85,6 @@ func (rm *SRobotManager) InitializeData() error { } rq := RobotManager.Query() rq = rq.Filter(sqlchemy.OR(sqlchemy.IsEmpty(rq.Field("tenant_id")), sqlchemy.Equals(rq.Field("tenant_id"), "system"))) - rq.DebugQuery() npRobots := make([]SRobot, 0) err = db.FetchModelObjects(RobotManager, rq, &npRobots) if err != nil { diff --git a/pkg/notify/models/topic.go b/pkg/notify/models/topic.go index 68e01007ea..c55679e66d 100644 --- a/pkg/notify/models/topic.go +++ b/pkg/notify/models/topic.go @@ -101,6 +101,7 @@ const ( DefaultNetOutOfSync = "net out of sync" DefaultMysqlOutOfSync = "mysql out of sync" DefaultServiceAbnormal = "service abnormal" + DefaultServerPanicked = "server panicked" ) func (sm *STopicManager) InitializeData() error { @@ -126,6 +127,7 @@ func (sm *STopicManager) InitializeData() error { DefaultNetOutOfSync, DefaultMysqlOutOfSync, DefaultServiceAbnormal, + DefaultServerPanicked, ) q := sm.Query() topics := make([]STopic, 0, initSNames.Len()) @@ -411,6 +413,15 @@ func (sm *STopicManager) InitializeData() error { ) t.Results = tristate.True t.Type = notify.TOPIC_TYPE_AUTOMATED_PROCESS + case DefaultServerPanicked: + t.addResources( + notify.TOPIC_RESOURCE_SERVER, + ) + t.addAction( + notify.ActionServerPanicked, + ) + t.Results = tristate.False + t.Type = notify.TOPIC_TYPE_RESOURCE } if topic == nil { err := sm.TableSpec().Insert(ctx, t) @@ -676,6 +687,7 @@ func init() { notify.ActionNetOutOfSync: 26, notify.ActionMysqlOutOfSync: 27, notify.ActionServiceAbnormal: 28, + notify.ActionServerPanicked: 29, }, ) }