forked from aerospike/aerospike-client-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerts.js
110 lines (103 loc) · 2.75 KB
/
alerts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// *****************************************************************************
// Copyright 2013-2019 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// *****************************************************************************
var winston
var nodemailer
var transporter
var severity = {
HIGH: 0,
MEDIUM: 1,
LOW: 2
}
var action = {
SENDEMAIL: 0,
PRINTCONSOLE: 1,
PRINTFILE: 2
}
var mediumSevCount = 0
var lowSevCount = 0
// setup winston logger to log it to file or console
// Setup node mailers to generate email alerts to
// send emails in case of errors.
var alertMode
function setupAlertSystem (alert) {
if (alert.mode === 'EMAIL') {
nodemailer = require('nodemailer')
transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'gmail account',
pass: 'password'
}
})
alertMode = alert.mode
} else {
winston = require('winston')
if (alert.mode === 'FILE') {
winston.add(winston.transports.file, { filename: alert.filename })
winston.remove(winston.transports.Console)
}
alertMode = alert.mode
}
}
function handleAlert (alertInfo, sev) {
var generateAlert = false
switch (sev) {
case severity.HIGH:
generateAlert = true
break
case severity.MEDIUM:
mediumSevCount++
if (mediumSevCount % 3 === 0) {
generateAlert = true
}
break
case severity.LOW:
lowSevCount++
if (lowSevCount % 6 === 0) {
generateAlert = true
}
break
default:
// do nothing
break
}
if (generateAlert) {
if (alertMode === 'EMAIL') {
// send an email.
var email = {
from: 'Aerospike alert account',
to: '[email protected]',
subject: 'Nodejs Longevity Alert',
text: JSON.stringify(alertInfo)
}
transporter.sendMail(email, function (error, info) {
if (error) {
console.log(error)
}
})
} else if (alertMode === 'FILE' || alertMode === 'CONSOLE') {
winston.info(alertInfo)
}
} else {
// should we store the temporary information in some structure.
}
}
module.exports = {
handleAlert: handleAlert,
setupAlertSystem: setupAlertSystem,
severity: severity,
action: action
}