@@ -58,17 +58,18 @@ func checkDatabasePerformanceInNamespace(namespace string) error {
58
58
func monitorCluster (cluster unstructured.Unstructured ) {
59
59
notificationInfo := api.Info {}
60
60
getClusterDatabaseInfo (cluster , & notificationInfo )
61
- //notificationInfo.DatabaseClusterName, notificationInfo.DatabaseType, notificationInfo.Namespace, notificationInfo.DatabaseClusterUID = cluster.GetName(), cluster.GetLabels()[api.DatabaseTypeLabel], cluster.GetNamespace(), string(cluster.GetUID())
62
- //status, found, err := unstructured.NestedString(cluster.Object, "status", "phase")
63
- //if err != nil || !found {
64
- // log.Printf("Unable to get %s status in ns %s: %v", notificationInfo.DatabaseClusterName, notificationInfo.Namespace, err)
65
- //}
66
61
debt , _ , _ := checkDebt (notificationInfo .Namespace )
67
62
if ! debt {
68
63
return
69
64
}
70
- notificationInfo .NotificationType = notification .ExceptionType
71
65
notificationInfo .ExceptionType = "阀值"
66
+ if value , ok := api .CPUNotificationInfoMap [notificationInfo .DatabaseClusterUID ]; ok {
67
+ notificationInfo = * value
68
+ } else if value , ok := api .MemNotificationInfoMap [notificationInfo .DatabaseClusterUID ]; ok {
69
+ notificationInfo = * value
70
+ } else if value , ok := api .DiskNotificationInfoMap [notificationInfo .DatabaseClusterUID ]; ok {
71
+ notificationInfo = * value
72
+ }
72
73
switch notificationInfo .ExceptionStatus {
73
74
case api .StatusDeleting , api .StatusCreating , api .StatusStopping , api .StatusStopped , api .StatusUnknown :
74
75
break
@@ -84,58 +85,99 @@ func monitorCluster(cluster unstructured.Unstructured) {
84
85
85
86
func handleCPUMemMonitor (notificationInfo * api.Info ) {
86
87
if cpuUsage , err := CPUMemMonitor (notificationInfo , "cpu" ); err == nil {
87
- processUsage (cpuUsage , api .DatabaseCPUMonitorThreshold , "CPU" , notificationInfo , api . CPUMonitorNamespaceMap )
88
+ processUsage (cpuUsage , api .DatabaseCPUMonitorThreshold , api . CPUChinese , notificationInfo )
88
89
} else {
89
90
log .Printf ("Failed to monitor CPU: %v" , err )
90
91
}
91
92
if memUsage , err := CPUMemMonitor (notificationInfo , "memory" ); err == nil {
92
- processUsage (memUsage , api .DatabaseMemMonitorThreshold , "内存" , notificationInfo , api . MemMonitorNamespaceMap )
93
+ processUsage (memUsage , api .DatabaseMemMonitorThreshold , api . MemoryChinese , notificationInfo )
93
94
} else {
94
95
log .Printf ("Failed to monitor Memory: %v" , err )
95
96
}
96
97
}
97
98
98
99
func handleDiskMonitor (notificationInfo * api.Info ) {
99
100
if maxUsage , err := checkPerformance (notificationInfo , "disk" ); err == nil {
100
- processUsage (maxUsage , api .DatabaseDiskMonitorThreshold , "磁盘" , notificationInfo , api . DiskMonitorNamespaceMap )
101
+ processUsage (maxUsage , api .DatabaseDiskMonitorThreshold , api . DiskChinese , notificationInfo )
101
102
} else {
102
103
log .Printf ("Failed to monitor Disk: %v" , err )
103
104
}
104
105
}
105
106
106
- func processUsage (usage float64 , threshold float64 , performanceType string , notificationInfo * api.Info , monitorMap map [ string ] bool ) {
107
+ func processUsage (usage float64 , threshold float64 , performanceType string , notificationInfo * api.Info ) {
107
108
notificationInfo .PerformanceType = performanceType
108
109
usageStr := strconv .FormatFloat (usage , 'f' , 2 , 64 )
109
- if performanceType == "CPU" {
110
+ if notificationInfo . PerformanceType == api . CPUChinese {
110
111
notificationInfo .CPUUsage = usageStr
111
- } else if performanceType == "内存" {
112
+ } else if performanceType == api . MemoryChinese {
112
113
notificationInfo .MemUsage = usageStr
113
- } else if performanceType == "磁盘" {
114
+ } else if performanceType == api . DiskChinese {
114
115
notificationInfo .DiskUsage = usageStr
115
116
}
116
- if usage >= threshold && ! monitorMap [notificationInfo .DatabaseClusterUID ] {
117
- alertMessage := notification .GetNotificationMessage (notificationInfo )
118
- notificationInfo .FeishuWebHook = api .FeishuWebhookURLMap ["FeishuWebhookURLImportant" ]
119
- if err := notification .SendFeishuNotification (notificationInfo , alertMessage ); err != nil {
120
- log .Printf ("Failed to send notification: %v" , err )
117
+ if usage >= threshold {
118
+ if _ , ok := api .CPUNotificationInfoMap [notificationInfo .DatabaseClusterUID ]; ! ok && notificationInfo .PerformanceType == api .CPUChinese {
119
+ processException (notificationInfo , threshold )
120
+ }
121
+ if _ , ok := api .MemNotificationInfoMap [notificationInfo .DatabaseClusterUID ]; ! ok && notificationInfo .PerformanceType == api .MemoryChinese {
122
+ processException (notificationInfo , threshold )
121
123
}
122
- monitorMap [notificationInfo .DatabaseClusterUID ] = true
123
- if performanceType != "磁盘" {
124
- return
124
+ if _ , ok := api .DiskNotificationInfoMap [notificationInfo .DatabaseClusterUID ]; ! ok && notificationInfo .PerformanceType == api .DiskChinese {
125
+ processException (notificationInfo , threshold )
125
126
}
126
- ZNThreshold := NumberToChinese ( int ( threshold ))
127
- if err := notification . SendToSms ( notificationInfo , api . ClusterName , "数据库" + performanceType + "超过百分之" + ZNThreshold ); err != nil {
128
- log . Printf ( "Failed to send Sms: %v" , err )
127
+ } else if usage < threshold {
128
+ if _ , ok := api . CPUNotificationInfoMap [ notificationInfo . DatabaseClusterUID ]; ok && notificationInfo . PerformanceType == api . CPUChinese {
129
+ processRecovery ( notificationInfo )
129
130
}
130
- } else if usage < threshold && monitorMap [notificationInfo .DatabaseClusterUID ] {
131
- notificationInfo .NotificationType = "recovery"
132
- notificationInfo .RecoveryTime = time .Now ().Add (8 * time .Hour ).Format ("2006-01-02 15:04:05" )
133
- alertMessage := notification .GetNotificationMessage (notificationInfo )
134
- notificationInfo .FeishuWebHook = api .FeishuWebhookURLMap ["FeishuWebhookURLImportant" ]
135
- if err := notification .SendFeishuNotification (notificationInfo , alertMessage ); err != nil {
136
- log .Printf ("Failed to send notification: %v" , err )
131
+ if _ , ok := api .MemNotificationInfoMap [notificationInfo .DatabaseClusterUID ]; ok && notificationInfo .PerformanceType == api .MemoryChinese {
132
+ processRecovery (notificationInfo )
137
133
}
138
- delete (monitorMap , notificationInfo .DatabaseClusterUID )
134
+ if _ , ok := api .DiskNotificationInfoMap [notificationInfo .DatabaseClusterUID ]; ok && notificationInfo .PerformanceType == api .DiskChinese {
135
+ processRecovery (notificationInfo )
136
+ }
137
+ }
138
+ }
139
+
140
+ func processException (notificationInfo * api.Info , threshold float64 ) {
141
+ notificationInfo .NotificationType = notification .ExceptionType
142
+ alertMessage := notification .GetNotificationMessage (notificationInfo )
143
+ notificationInfo .FeishuWebHook = api .FeishuWebhookURLMap ["FeishuWebhookURLImportant" ]
144
+ if err := notification .SendFeishuNotification (notificationInfo , alertMessage ); err != nil {
145
+ log .Printf ("Failed to send notification: %v" , err )
146
+ }
147
+ if notificationInfo .PerformanceType == api .CPUChinese {
148
+ api .CPUNotificationInfoMap [notificationInfo .DatabaseClusterUID ] = notificationInfo
149
+ return
150
+ }
151
+ if notificationInfo .PerformanceType == api .MemoryChinese {
152
+ api .MemNotificationInfoMap [notificationInfo .DatabaseClusterUID ] = notificationInfo
153
+ return
154
+ }
155
+ if notificationInfo .PerformanceType == api .DiskChinese {
156
+ api .DiskNotificationInfoMap [notificationInfo .DatabaseClusterUID ] = notificationInfo
157
+ }
158
+ ZNThreshold := NumberToChinese (int (threshold ))
159
+ if err := notification .SendToSms (notificationInfo , api .ClusterName , "数据库" + notificationInfo .PerformanceType + "超过百分之" + ZNThreshold ); err != nil {
160
+ log .Printf ("Failed to send Sms: %v" , err )
161
+ }
162
+ }
163
+
164
+ func processRecovery (notificationInfo * api.Info ) {
165
+ notificationInfo .NotificationType = "recovery"
166
+ notificationInfo .RecoveryStatus = notificationInfo .ExceptionStatus
167
+ notificationInfo .RecoveryTime = time .Now ().Add (8 * time .Hour ).Format ("2006-01-02 15:04:05" )
168
+ alertMessage := notification .GetNotificationMessage (notificationInfo )
169
+ notificationInfo .FeishuWebHook = api .FeishuWebhookURLMap ["FeishuWebhookURLImportant" ]
170
+ if err := notification .SendFeishuNotification (notificationInfo , alertMessage ); err != nil {
171
+ log .Printf ("Failed to send notification: %v" , err )
172
+ }
173
+ if notificationInfo .PerformanceType == api .CPUChinese {
174
+ delete (api .CPUNotificationInfoMap , notificationInfo .DatabaseClusterUID )
175
+ }
176
+ if notificationInfo .PerformanceType == api .MemoryChinese {
177
+ delete (api .MemNotificationInfoMap , notificationInfo .DatabaseClusterUID )
178
+ }
179
+ if notificationInfo .PerformanceType == api .DiskChinese {
180
+ delete (api .DiskNotificationInfoMap , notificationInfo .DatabaseClusterUID )
139
181
}
140
182
}
141
183
0 commit comments