fix(notify): add some template and notification (#15669)

This commit is contained in:
gouqi11
2023-01-02 12:00:39 +08:00
committed by GitHub
parent 732db665d1
commit edee04fcc7
28 changed files with 227 additions and 6 deletions
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
数据库 {{$d.ip}} 的主从同步不一致,请及时检查。
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
{{ $d.service_name }}服务的网络拓扑信息同步不一致,请及时检查。
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
{{ $d.account }}:您的密码有效期将过,请及时登录平台更新密码。
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
云账号{{ $d.name }}状态异常,请及时检查。
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
服务{{ d.service_name}} worker阻塞半小时,请及时检查。
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
The primary and secondary synchronization of the database ({{ $d.ip }}) is inconsistent, please check in time.
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
{{ $d.service_name }}: The network topology information of the service is inconsistent, please check in time.
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
{{ $d.account }}:Your password is valid and will expire soon. Please log in to the platform in time to update your password.
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
The account {{ $d.name }} status is abnormal. Please check in time.
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
The service: {{ d.service_name}} worker has been block 30 minutes.Please verify the service in time.
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
数据库 {{$d.ip}} 的主从同步不一致,请及时检查。
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
{{ $d.service_name }}服务的网络拓扑信息同步不一致,请及时检查。
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
{{ $d.account }}:您的密码有效期将过,请及时登录平台更新密码。
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
云账号{{ $d.name}}状态异常,请及时检查。
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
服务{{ d.service_name}} worker阻塞半小时,请及时检查。
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
The primary and secondary synchronization of the database ({{ $d.ip }}) is inconsistent, please check in time.
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
{{ $d.service_name }}: The network topology information of the service is inconsistent, please check in time.
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
{{ $d.account }}:Your password is valid and will expire soon. Please log in to the platform in time to update your password.
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
The account {{ $d.name }} has been locked due to abnormal login. Please verify the situation. If you need to unlock the user, please go to the user list to enable the user.
@@ -0,0 +1,2 @@
{{- $d := .resource_details -}}
The service: {{ d.service_name}} worker has been block 30 minutes.Please verify the service in time.
+20
View File
@@ -20,13 +20,17 @@ import (
"sync"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/httputils"
api "yunion.io/x/onecloud/pkg/apis/notify"
"yunion.io/x/onecloud/pkg/appsrv"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
npk "yunion.io/x/onecloud/pkg/mcclient/modules/notify"
)
const (
@@ -161,6 +165,11 @@ func (h *APIHelper) doSync(ctx context.Context) (changed bool, err error) {
}
h.modelSets = mss
if !r.Correct {
// 发送消息通知
err := sendSyncErrNotify(s)
if err != nil {
log.Errorf("unable to EventNotify: %s", err)
}
return false, errors.Wrap(ErrSync, "incorrect")
}
changed = r.Changed
@@ -181,3 +190,14 @@ func (h *APIHelper) adminClientSession(ctx context.Context) *mcclient.ClientSess
h.mcclientSession = auth.GetAdminSession(ctx, region)
return h.mcclientSession
}
func sendSyncErrNotify(s *mcclient.ClientSession) error {
params := api.NotificationManagerEventNotifyInput{}
params.Event = api.Event.WithAction(api.ActionNetOutOfSync).WithResourceType(api.TOPIC_RESOURCE_NET).String()
params.AdvanceDays = 0
message := &jsonutils.JSONDict{}
message.Add(jsonutils.NewString(consts.GetServiceType()), "service_name")
params.ResourceDetails = message
_, err := npk.Notification.PerformClassAction(s, "event-notify", jsonutils.Marshal(params))
return err
}
+3
View File
@@ -126,6 +126,9 @@ const (
TOPIC_RESOURCE_CLOUDPODS_COMPONENT = "cloudpods_component"
TOPIC_RESOURCE_USER = "user"
TOPIC_RESOURCE_ACTION_LOG = "action_log"
TOPIC_RESOURCE_ACCOUNT_STATUS = "account"
TOPIC_RESOURCE_WORKER = "worker"
TOPIC_RESOURCE_NET = "net"
SUBSCRIBER_TYPE_ROLE = "role"
SUBSCRIBER_TYPE_ROBOT = "robot"
+6 -1
View File
@@ -52,7 +52,12 @@ var (
ActionLock SAction = "lock"
ActionExceedCount SAction = "exceed_count"
ActionExceedCount SAction = "exceed_count"
ActionSyncAccountStatus SAction = "sync_account_status"
ActionPasswordExpireSoon SAction = "password_expire_soon"
ActionWorkerBlock SAction = "woker_block"
ActionNetOutOfSync SAction = "net_out_of_sync"
ActionMysqlOutOfSync SAction = "mysql_out_of_sync"
ResultFailed SResult = "failed"
ResultSucceed SResult = "succeed"
+2
View File
@@ -56,6 +56,8 @@ var (
ActionCreateBackupServer = api.ActionCreateBackupServer
ActionDelBackupServer = api.ActionDelBackupServer
ActionSyncStatus = api.ActionSyncStatus
ActionNetOutOfSync = api.ActionNetOutOfSync
ActionMysqlOutOfSync = api.ActionMysqlOutOfSync
ActionPendingDelete = api.ActionPendingDelete
+23 -5
View File
@@ -40,11 +40,13 @@ import (
"yunion.io/x/onecloud/pkg/apis"
proxyapi "yunion.io/x/onecloud/pkg/apis/cloudcommon/proxy"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/apis/notify"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
"yunion.io/x/onecloud/pkg/cloudcommon/db/proxy"
"yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
"yunion.io/x/onecloud/pkg/cloudcommon/validators"
"yunion.io/x/onecloud/pkg/compute/options"
"yunion.io/x/onecloud/pkg/httperrors"
@@ -1348,7 +1350,7 @@ func migrateCloudprovider(cloudprovider *SCloudprovider) error {
secret, err := cloudprovider.getPassword()
if err != nil {
account.markAccountDiscconected(context.Background(), auth.AdminCredential())
account.markAccountDisconected(context.Background(), auth.AdminCredential())
log.Errorf("Get password from provider %s error %v", cloudprovider.Name, err)
} else {
err = account.savePassword(secret)
@@ -1755,7 +1757,7 @@ func (manager *SCloudaccountManager) OrderByExtraFields(
return q, nil
}
func (account *SCloudaccount) markAccountDiscconected(ctx context.Context, userCred mcclient.TokenCredential) error {
func (account *SCloudaccount) markAccountDisconected(ctx context.Context, userCred mcclient.TokenCredential) error {
_, err := db.UpdateWithLock(ctx, account, func() error {
account.ErrorCount = account.ErrorCount + 1
account.HealthStatus = api.CLOUD_PROVIDER_HEALTH_UNKNOWN
@@ -1764,10 +1766,13 @@ func (account *SCloudaccount) markAccountDiscconected(ctx context.Context, userC
if err != nil {
return err
}
if account.Status == api.CLOUD_PROVIDER_CONNECTED {
account.EventNotify(ctx, userCred, notify.ActionSyncAccountStatus)
}
return account.SetStatus(userCred, api.CLOUD_PROVIDER_DISCONNECTED, "")
}
func (account *SCloudaccount) markAllProvidersDicconnected(ctx context.Context, userCred mcclient.TokenCredential) error {
func (account *SCloudaccount) markAllProvidersDisconnected(ctx context.Context, userCred mcclient.TokenCredential) error {
providers := account.GetCloudproviders()
for i := 0; i < len(providers); i += 1 {
err := providers[i].markProviderDisconnected(ctx, userCred, "cloud account disconnected")
@@ -2037,8 +2042,8 @@ func (account *SCloudaccount) syncAccountStatus(ctx context.Context, userCred mc
account.MarkSyncing(userCred, true)
subaccounts, err := account.probeAccountStatus(ctx, userCred)
if err != nil {
account.markAllProvidersDicconnected(ctx, userCred)
account.markAccountDiscconected(ctx, userCred)
account.markAllProvidersDisconnected(ctx, userCred)
account.markAccountDisconected(ctx, userCred)
return errors.Wrap(err, "account.probeAccountStatus")
}
account.markAccountConnected(ctx, userCred)
@@ -2861,3 +2866,16 @@ func (self *SCloudaccount) PerformProjectMapping(ctx context.Context, userCred m
}
return nil, refreshPmCaches()
}
// 同步云账号消息通知
func (account *SCloudaccount) EventNotify(ctx context.Context, userCred mcclient.TokenCredential, action notify.SAction) {
var resourceType string
resourceType = notify.TOPIC_RESOURCE_ACCOUNT_STATUS
notifyclient.EventNotify(ctx, userCred, notifyclient.SEventNotifyParam{
Obj: account,
ResourceType: resourceType,
Action: action,
AdvanceDays: 0,
})
}
+72
View File
@@ -21,11 +21,16 @@ import (
"encoding/hex"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/apis/notify"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
"yunion.io/x/onecloud/pkg/httperrors"
o "yunion.io/x/onecloud/pkg/keystone/options"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/seclib2"
"yunion.io/x/onecloud/pkg/util/stringutils2"
)
@@ -194,3 +199,70 @@ func (passwd *SPassword) IsExpired() bool {
}
return false
}
// 定时任务判断用户是否需要密码过期通知
func CheckAllUserPasswordIsExpired(ctx context.Context, userCred mcclient.TokenCredential, startRun bool) {
pwds := []SPassword{}
pwdQ := PasswordManager.Query()
pwdQ = pwdQ.Desc("created_at")
err := db.FetchModelObjects(PasswordManager, pwdQ, &pwds)
if err != nil {
log.Errorln("fetch Password error:", err)
return
}
hasCheckedPwd := make(map[int]struct{})
for _, pwd := range pwds {
if _, isExist := hasCheckedPwd[pwd.LocalUserId]; isExist {
continue
}
if pwd.ExpiresAt.IsZero() {
continue
}
hasCheckedPwd[pwd.LocalUserId] = struct{}{}
err = pwd.NeedSendNotify(ctx, userCred)
if err != nil {
log.Errorln(errors.Wrap(err, "pwd.NeedSendNotify"))
}
}
}
func (pwd *SPassword) NeedSendNotify(ctx context.Context, userCred mcclient.TokenCredential) error {
expireTime := time.Date(pwd.ExpiresAt.Year(), pwd.ExpiresAt.Month(), pwd.ExpiresAt.Day(), 0, 0, 0, 0, time.Local)
nowTime := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local)
sub := expireTime.Sub(nowTime)
subDay := sub.Hours() / 24
switch {
case subDay == 7:
localUser, err := LocalUserManager.fetchLocalUser("", "", pwd.LocalUserId)
if err != nil {
return errors.Wrap(err, "fetchLocalUser error:")
}
pwd.EventNotify(ctx, userCred, notify.ActionPasswordExpireSoon, localUser.Name, 7)
case subDay == 1:
localUser, err := LocalUserManager.fetchLocalUser("", "", pwd.LocalUserId)
if err != nil {
return errors.Wrap(err, "fetchLocalUser error:")
}
pwd.EventNotify(ctx, userCred, notify.ActionPasswordExpireSoon, localUser.Name, 1)
}
return nil
}
// 密码即将失效消息通知
func (pwd *SPassword) EventNotify(ctx context.Context, userCred mcclient.TokenCredential, action notify.SAction, userName string, advanceDays int) {
resourceType := notify.TOPIC_RESOURCE_USER
detailsDecro := func(ctx context.Context, details *jsonutils.JSONDict) {
details.Set("account", jsonutils.NewString(userName))
}
pwd.Password = ""
notifyclient.EventNotify(ctx, userCred, notifyclient.SEventNotifyParam{
Obj: pwd,
ObjDetailsDecorator: detailsDecro,
ResourceType: resourceType,
Action: action,
AdvanceDays: advanceDays,
})
}
+1
View File
@@ -90,6 +90,7 @@ func StartService() {
cron.AddJobAtIntervalsWithStartRun("CalculateIdentityQuotaUsages", time.Duration(opts.CalculateQuotaUsageIntervalSeconds)*time.Second, models.IdentityQuotaManager.CalculateQuotaUsages, true)
cron.AddJobEveryFewHour("AutoPurgeSplitable", 4, 30, 0, db.AutoPurgeSplitable, false)
cron.AddJobEveryFewDays("CheckAllUserPasswordIsExpired", 4, 30, 0, 0, models.CheckAllUserPasswordIsExpired, true)
cron.Start()
defer cron.Stop()
+60
View File
@@ -95,6 +95,11 @@ const (
DefaultChecksumTestFailed = "checksum test failed"
DefaultUserLock = "user lock"
DefaultActionLogExceedCount = "action log exceed count"
DefaultSyncAccountStatus = "cloud account sync status"
DefaultPasswordExpireDue1Day = "password expire due 1 day"
DefaultPasswordExpireDue7Day = "password expire due 7 day"
DefaultNetOutOfSync = "net out of sync"
DefaultMysqlOutOfSync = "mysql out of sync"
)
func (sm *STopicManager) InitializeData() error {
@@ -114,6 +119,11 @@ func (sm *STopicManager) InitializeData() error {
DefaultChecksumTestFailed,
DefaultUserLock,
DefaultActionLogExceedCount,
DefaultSyncAccountStatus,
DefaultPasswordExpireDue1Day,
DefaultPasswordExpireDue7Day,
DefaultNetOutOfSync,
DefaultMysqlOutOfSync,
)
q := sm.Query()
topics := make([]STopic, 0, initSNames.Len())
@@ -328,6 +338,50 @@ func (sm *STopicManager) InitializeData() error {
notify.ActionExceedCount,
)
t.Type = notify.TOPIC_TYPE_RESOURCE
case DefaultSyncAccountStatus:
t.addResources(
notify.TOPIC_RESOURCE_ACCOUNT_STATUS,
)
t.addAction(
notify.ActionSyncAccountStatus,
)
t.Type = notify.TOPIC_TYPE_AUTOMATED_PROCESS
case DefaultPasswordExpireDue1Day:
t.addResources(
notify.TOPIC_RESOURCE_USER,
)
t.addAction(
notify.ActionPasswordExpireSoon,
)
t.Type = notify.TOPIC_TYPE_SECURITY
t.AdvanceDays = 1
case DefaultPasswordExpireDue7Day:
t.addResources(
notify.TOPIC_RESOURCE_USER,
)
t.addAction(
notify.ActionPasswordExpireSoon,
)
t.Type = notify.TOPIC_TYPE_SECURITY
t.AdvanceDays = 7
case DefaultNetOutOfSync:
t.addResources(
notify.TOPIC_RESOURCE_NET,
)
t.addAction(
notify.ActionNetOutOfSync,
)
t.Type = notify.TOPIC_TYPE_AUTOMATED_PROCESS
t.AdvanceDays = 0
case DefaultMysqlOutOfSync:
t.addResources(
notify.TOPIC_RESOURCE_DBINSTANCE,
)
t.addAction(
notify.ActionMysqlOutOfSync,
)
t.Type = notify.TOPIC_TYPE_AUTOMATED_PROCESS
t.AdvanceDays = 0
}
if topic == nil {
err := sm.TableSpec().Insert(ctx, t)
@@ -557,6 +611,8 @@ func init() {
notify.TOPIC_RESOURCE_DB_TABLE_RECORD: 35,
notify.TOPIC_RESOURCE_USER: 36,
notify.TOPIC_RESOURCE_ACTION_LOG: 37,
notify.TOPIC_RESOURCE_ACCOUNT_STATUS: 38,
notify.TOPIC_RESOURCE_NET: 39,
},
)
converter.registerAction(
@@ -585,6 +641,10 @@ func init() {
notify.ActionChecksumTest: 21,
notify.ActionLock: 22,
notify.ActionExceedCount: 23,
notify.ActionSyncAccountStatus: 24,
notify.ActionPasswordExpireSoon: 25,
notify.ActionNetOutOfSync: 26,
notify.ActionMysqlOutOfSync: 27,
},
)
}