Skip to content

Commit cb54e72

Browse files
committed
Improving alert type emailer
1 parent 03aa422 commit cb54e72

File tree

4 files changed

+98
-37
lines changed

4 files changed

+98
-37
lines changed

alert-service/service/service.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,47 @@ func (as *AlertService) ProcessMsg(msg hub.Message) {
126126
func (as *AlertService) ProcessAlertInsertion(params hub.Fields) {
127127
alert := params["alert"].(model.Alert)
128128

129+
// check if alert notification is enabled
130+
if alert.IsCode(model.AlertCodeNewServer) && !as.Config.AlertService.Emailer.AlertType.NewHost {
131+
return
132+
}
133+
134+
if alert.IsCode(model.AlertCodeNewDatabase) && !as.Config.AlertService.Emailer.AlertType.NewDatabase {
135+
return
136+
}
137+
138+
if alert.IsCode(model.AlertCodeNewLicense) && !as.Config.AlertService.Emailer.AlertType.NewLicense {
139+
return
140+
}
141+
142+
if alert.IsCode(model.AlertCodeNewOption) && !as.Config.AlertService.Emailer.AlertType.NewOption {
143+
return
144+
}
145+
146+
if alert.IsCode(model.AlertCodeUnlistedRunningDatabase) && !as.Config.AlertService.Emailer.AlertType.NewUnlistedRunningDatabase {
147+
return
148+
}
149+
150+
if alert.IsCode(model.AlertCodeIncreasedCPUCores) && !as.Config.AlertService.Emailer.AlertType.NewHostCpu {
151+
return
152+
}
153+
154+
if alert.IsCode(model.AlertCodeMissingPrimaryDatabase) && !as.Config.AlertService.Emailer.AlertType.MissingPrimaryDatabase {
155+
return
156+
}
157+
158+
if alert.IsCode(model.AlertCodeMissingDatabase) && !as.Config.AlertService.Emailer.AlertType.MissingDatabase {
159+
return
160+
}
161+
162+
if alert.IsCode(model.AlertCodeAgentError) && !as.Config.AlertService.Emailer.AlertType.AgentError {
163+
return
164+
}
165+
166+
if alert.IsCode(model.AlertCodeNoData) && !as.Config.AlertService.Emailer.AlertType.NoData {
167+
return
168+
}
169+
129170
//Create the subject and message
130171
var subject, message string
131172

alert-service/service/service_test.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ func TestProcessMsg_AlertInsertion(t *testing.T) {
4343
AlertService: config.AlertService{
4444
Emailer: config.Emailer{
4545
To: []string{"[email protected]"},
46+
Enabled: true,
47+
AlertType: config.AlertType{
48+
NewHost: true,
49+
NewDatabase: true,
50+
NewLicense: true,
51+
NewOption: true,
52+
NewUnlistedRunningDatabase: true,
53+
NewHostCpu: true,
54+
MissingPrimaryDatabase: true,
55+
MissingDatabase: true,
56+
AgentError: true,
57+
NoData: true,
58+
},
4659
},
4760
},
4861
},
@@ -105,6 +118,19 @@ func TestProcessAlertInsertion_WithHostname(t *testing.T) {
105118
AlertService: config.AlertService{
106119
Emailer: config.Emailer{
107120
To: []string{"[email protected]"},
121+
Enabled: true,
122+
AlertType: config.AlertType{
123+
NewHost: true,
124+
NewDatabase: true,
125+
NewLicense: true,
126+
NewOption: true,
127+
NewUnlistedRunningDatabase: true,
128+
NewHostCpu: true,
129+
MissingPrimaryDatabase: true,
130+
MissingDatabase: true,
131+
AgentError: true,
132+
NoData: true,
133+
},
108134
},
109135
},
110136
},
@@ -148,6 +174,19 @@ func TestProcessAlertInsertion_WithoutHostname(t *testing.T) {
148174
AlertService: config.AlertService{
149175
Emailer: config.Emailer{
150176
To: []string{"[email protected]"},
177+
Enabled: true,
178+
AlertType: config.AlertType{
179+
NewHost: true,
180+
NewDatabase: true,
181+
NewLicense: true,
182+
NewOption: true,
183+
NewUnlistedRunningDatabase: true,
184+
NewHostCpu: true,
185+
MissingPrimaryDatabase: true,
186+
MissingDatabase: true,
187+
AgentError: true,
188+
NoData: true,
189+
},
151190
},
152191
},
153192
},
@@ -189,7 +228,20 @@ func TestProcessAlertInsertion_EmailerError(t *testing.T) {
189228
Config: config.Configuration{
190229
AlertService: config.AlertService{
191230
Emailer: config.Emailer{
192-
To: []string{"[email protected]"},
231+
To: []string{"[email protected]"},
232+
Enabled: true,
233+
AlertType: config.AlertType{
234+
NewHost: true,
235+
NewDatabase: true,
236+
NewLicense: true,
237+
NewOption: true,
238+
NewUnlistedRunningDatabase: true,
239+
NewHostCpu: true,
240+
MissingPrimaryDatabase: true,
241+
MissingDatabase: true,
242+
AgentError: true,
243+
NoData: true,
244+
},
193245
},
194246
},
195247
},

data-service/service/alert_thrower.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ import (
2727

2828
// ThrowNewDatabaseAlert create and insert in the database a new NEW_DATABASE alert
2929
func (hds *HostDataService) throwNewDatabaseAlert(dbname string, hostname string) error {
30-
if !hds.Config.AlertService.Emailer.AlertType.NewDatabase {
31-
return nil
32-
}
33-
3430
alr := model.Alert{
3531
ID: primitive.NewObjectIDFromTimestamp(hds.TimeNow()),
3632
AlertAffectedTechnology: model.TechnologyOracleDatabasePtr,
@@ -51,10 +47,6 @@ func (hds *HostDataService) throwNewDatabaseAlert(dbname string, hostname string
5147

5248
// ThrowNewServerAlert create and insert in the database a new NEW_SERVER alert
5349
func (hds *HostDataService) throwNewServerAlert(hostname string) error {
54-
if !hds.Config.AlertService.Emailer.AlertType.NewHost {
55-
return nil
56-
}
57-
5850
alr := model.Alert{
5951
ID: primitive.NewObjectIDFromTimestamp(hds.TimeNow()),
6052
AlertAffectedTechnology: nil,
@@ -101,10 +93,6 @@ func (hds *HostDataService) createNewLicenseAlert(hostname, dbname string, licen
10193

10294
// ThrowNewEnterpriseLicenseAlert create and insert in the database a new NEW_LICENSE alert
10395
func (hds *HostDataService) throwNewLicenseAlert(alerts []model.Alert) error {
104-
if !hds.Config.AlertService.Emailer.AlertType.NewLicense {
105-
return nil
106-
}
107-
10896
if len(alerts) == 0 {
10997
return nil
11098
}
@@ -153,10 +141,6 @@ func (hds *HostDataService) throwNewLicenseAlert(alerts []model.Alert) error {
153141
}
154142

155143
func (hds *HostDataService) throwNewOptionAlerts(alerts []model.Alert) error {
156-
if !hds.Config.AlertService.Emailer.AlertType.NewOption {
157-
return nil
158-
}
159-
160144
if len(alerts) == 0 {
161145
return nil
162146
}
@@ -198,10 +182,6 @@ func (hds *HostDataService) throwNewOptionAlerts(alerts []model.Alert) error {
198182

199183
// ThrowUnlistedRunningDatabasesAlert create and insert in the database a new UNLISTED_RUNNING_DATABASE alert
200184
func (hds *HostDataService) throwUnlistedRunningDatabasesAlert(alerts []model.Alert) error {
201-
if !hds.Config.AlertService.Emailer.AlertType.NewUnlistedRunningDatabase {
202-
return nil
203-
}
204-
205185
if len(alerts) == 0 {
206186
return nil
207187
}
@@ -248,10 +228,6 @@ func (hds *HostDataService) throwUnlistedRunningDatabasesAlert(alerts []model.Al
248228
}
249229

250230
func (hds *HostDataService) throwAugmentedCPUCoresAlert(hostname string, previousCPUCores, newCPUCores int) error {
251-
if !hds.Config.AlertService.Emailer.AlertType.NewHostCpu {
252-
return nil
253-
}
254-
255231
alr := model.Alert{
256232
ID: primitive.NewObjectIDFromTimestamp(hds.TimeNow()),
257233
AlertAffectedTechnology: nil,
@@ -271,10 +247,6 @@ func (hds *HostDataService) throwAugmentedCPUCoresAlert(hostname string, previou
271247
}
272248

273249
func (hds *HostDataService) throwMissingPrimaryDatabase(hostname, secondaryDbName string) error {
274-
if !hds.Config.AlertService.Emailer.AlertType.MissingPrimaryDatabase {
275-
return nil
276-
}
277-
278250
alert := model.Alert{
279251
AlertCategory: model.AlertCategoryEngine,
280252
AlertAffectedTechnology: nil,
@@ -293,10 +265,6 @@ func (hds *HostDataService) throwMissingPrimaryDatabase(hostname, secondaryDbNam
293265
}
294266

295267
func (hds *HostDataService) throwAgentErrorsAlert(hostname string, errs []model.AgentError) error {
296-
if !hds.Config.AlertService.Emailer.AlertType.AgentError {
297-
return nil
298-
}
299-
300268
b := strings.Builder{}
301269
prefix := ""
302270

@@ -330,10 +298,6 @@ func (hds *HostDataService) throwAgentErrorsAlert(hostname string, errs []model.
330298
const dbNamesOtherInfo = "dbNames"
331299

332300
func (hds *HostDataService) throwMissingDatabasesAlert(hostname string, dbNames []string, alertSeverity string) error {
333-
if !hds.Config.AlertService.Emailer.AlertType.MissingDatabase {
334-
return nil
335-
}
336-
337301
sort.Strings(dbNames)
338302
description := fmt.Sprintf("The databases %q on %q are missing compared to the previous hostdata",
339303
strings.Join(dbNames, ", "), hostname)

model/alert.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,7 @@ fields:
120120

121121
return true
122122
}
123+
124+
func (a Alert) IsCode(code string) bool {
125+
return a.AlertCode == code
126+
}

0 commit comments

Comments
 (0)