config option to disable database access, abstraction of alarm mail sending
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
"brokerUser": "wn",
|
||||
"brokerPass": "locutus",
|
||||
"brokerCa": "/home/wn/server-ca.crt",
|
||||
"disableDatabaseAccess": true,
|
||||
"mongoDbUrl": "mongodb://localhost/hottis",
|
||||
"smtpHost": "localhost",
|
||||
"smtpPort": 25,
|
||||
|
30
src/log.ts
30
src/log.ts
@ -1,5 +1,8 @@
|
||||
import * as chalk from 'chalk'
|
||||
import * as moment from 'moment'
|
||||
import * as config from './config'
|
||||
import * as nodemailer from 'nodemailer'
|
||||
|
||||
|
||||
enum Level {
|
||||
All,
|
||||
@ -23,6 +26,33 @@ export function setLevel(value: string): void {
|
||||
}
|
||||
}
|
||||
|
||||
export function sendAlarmMail(message : string): void {
|
||||
let transport = nodemailer.createTransport({
|
||||
host: config.dict.smtpHost,
|
||||
port: config.dict.smtpPort,
|
||||
secure: false,
|
||||
tls: {
|
||||
rejectUnauthorized: false
|
||||
}
|
||||
});
|
||||
|
||||
let mail : nodemailer.SendMailOptions = {
|
||||
from: config.dict.smtpSender,
|
||||
to: config.dict.smtpReceiver,
|
||||
subject: "Alarm from Dispatcher",
|
||||
text: message
|
||||
};
|
||||
|
||||
transport.sendMail(mail)
|
||||
.then((v : nodemailer.SentMessageInfo) => {
|
||||
info(`Alarm mail sent, ${message}, ${v.response}`)
|
||||
})
|
||||
.catch((reason : any) => {
|
||||
error(`Failure when sending alarm mail: ${message}, ${reason}`)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
export function info(message: string): void {
|
||||
if (level < Level.NoDebugNoInfo) {
|
||||
console.log(`${timestamp()} ${chalk.bold.cyan('[ II ]')} ${message}`)
|
||||
|
@ -24,9 +24,10 @@ let missingeventdetector : MissingEventDetector.MissingEventDetector =
|
||||
new MissingEventDetector.MissingEventDetector()
|
||||
dispatcher.register(ESP_THERM_TOPIC, 'MissingEventDetector', missingeventdetector)
|
||||
|
||||
let mongo : MongoSave.MongoSave = new MongoSave.MongoSave(config.dict.mongoDbUrl)
|
||||
dispatcher.register(ESP_THERM_TOPIC, 'MongoSave', mongo);
|
||||
|
||||
if (! config.dict.disableDatabaseAccess) {
|
||||
let mongo : MongoSave.MongoSave = new MongoSave.MongoSave(config.dict.mongoDbUrl)
|
||||
dispatcher.register(ESP_THERM_TOPIC, 'MongoSave', mongo);
|
||||
}
|
||||
|
||||
dispatcher.exec()
|
||||
log.info("Dispatcher running")
|
||||
|
@ -1,4 +1,3 @@
|
||||
import * as nodemailer from 'nodemailer'
|
||||
import * as log from './log'
|
||||
import * as CallChain from './callchain'
|
||||
import * as Processor from './processor'
|
||||
@ -20,20 +19,11 @@ class ClientEntry {
|
||||
class MissingEventProcessor extends Processor.AProcessor {
|
||||
private timer : NodeJS.Timer
|
||||
private clientMap : Map<string, ClientEntry> = new Map<string, ClientEntry>()
|
||||
private smtp : nodemailer.Transporter
|
||||
|
||||
|
||||
constructor() {
|
||||
super("MissingEventProcessor")
|
||||
|
||||
this.smtp = nodemailer.createTransport({
|
||||
host: config.dict.smtpHost,
|
||||
port: config.dict.smtpPort,
|
||||
secure: false,
|
||||
tls: {
|
||||
rejectUnauthorized: false
|
||||
}
|
||||
});
|
||||
|
||||
this.timer = setInterval(() => {
|
||||
this.clientMap.forEach((value : ClientEntry, key : string) : void => {
|
||||
@ -42,20 +32,7 @@ class MissingEventProcessor extends Processor.AProcessor {
|
||||
log.info(`Checking ${key}, elapsed: ${elapsedTime / 1000}, avg. delay: ${value.avgDelay / 1000}`)
|
||||
|
||||
if ((value.avgDelay != 0) && (elapsedTime > (value.avgDelay * 3))) {
|
||||
let mail : nodemailer.SendMailOptions = {
|
||||
from: config.dict.smtpSender,
|
||||
to: config.dict.smtpReceiver,
|
||||
subject: `Missing Event Detected for ${key}`,
|
||||
text: `Missing Event Detected: ${key}, elapsed: ${elapsedTime / 1000}, avg. delay: ${value.avgDelay / 1000}`
|
||||
};
|
||||
|
||||
this.smtp.sendMail(mail)
|
||||
.then((v : nodemailer.SentMessageInfo) => {
|
||||
log.info(`Missing event info mail sent, ${v.response}`)
|
||||
})
|
||||
.catch((reason : any) => {
|
||||
log.error(`Failure when sending missing event info: ${reason}`)
|
||||
})
|
||||
log.sendAlarmMail(`Missing Event Detected: ${key}, elapsed: ${elapsedTime / 1000}, avg. delay: ${value.avgDelay / 1000}`)
|
||||
}
|
||||
})
|
||||
}, CHECK_PERIOD * 1000)
|
||||
|
Reference in New Issue
Block a user