This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 704
/
Copy pathindex.js
88 lines (87 loc) · 2.99 KB
/
index.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
/**
* Email plugin
*
* Notifies all events (up, down, paused, restarted) by email
*
* Installation
* ------------
* This plugin is disabled by default. To enable it, add its entry
* to the `plugins` key of the configuration:
*
* // in config/production.yaml
* plugins:
* - ./plugins/email
*
* Usage
* -----
* This plugin sends an email each time a check is started, goes down, or goes back up.
* When the check goes down, the email contains the error details:
*
* Object: [Down] Check "FooBar" just went down
* On Thursday, September 4th 1986 8:30 PM,
* a test on URL "http://foobar.com" failed with the following error:
*
* Error 500
*
* Uptime won't send anymore emails about this check until it goes back up.
* ---------------------------------------------------------------------
* This is an automated email sent from Uptime. Please don't reply to it.
*
* Configuration
* -------------
* Here is an example configuration:
*
* // in config/production.yaml
* email:
* method: SMTP # possible methods are SMTP, SES, or Sendmail
* transport: # see https://github.com/andris9/nodemailer for transport options
* service: Gmail
* auth:
* user: [email protected]
* pass: gursikso
* event:
* up: true
* down: true
* paused: false
* restarted: false
* message:
* from: 'Fred Foo <[email protected]>'
* to: '[email protected], [email protected]'
* # The email plugin also uses the main `url` param for hyperlinks in the sent emails
*/
var fs = require('fs');
var nodemailer = require('nodemailer');
var moment = require('moment');
var CheckEvent = require('../../models/checkEvent');
var ejs = require('ejs');
exports.initWebApp = function(options) {
var config = options.config.email;
var mailer = nodemailer.createTransport(config.method, config.transport);
var templateDir = __dirname + '/views/';
CheckEvent.on('afterInsert', function(checkEvent) {
if (!config.event[checkEvent.message]) return;
checkEvent.findCheck(function(err, check) {
if (err) return console.error(err);
var filename = templateDir + checkEvent.message + '.ejs';
var renderOptions = {
check: check,
checkEvent: checkEvent,
url: options.config.url,
moment: moment,
filename: filename
};
var lines = ejs.render(fs.readFileSync(filename, 'utf8'), renderOptions).split('\n');
var mailOptions = {
from: config.message.from,
to: config.message.to,
subject: lines.shift(),
text: lines.join('\n')
};
mailer.sendMail(mailOptions, function(err2, response) {
if (err2) return console.error('Email plugin error: %s', err2);
console.log('Notified event by email: Check ' + check.name + ' ' + checkEvent.message);
});
});
});
console.log('Enabled Email notifications');
};