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 703
/
index.js
78 lines (69 loc) · 2.41 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
/**
* Pattern matcher plugin
*
* Adds the ability to HTTP & HTTPS pollers to test the response body against a pattern
*
* Installation
* ------------
* This plugin is enabled by default. To disable it, remove its entry
* from the `plugins` key of the configuration:
*
* // in config/production.yaml
* plugins:
* # - ./plugins/patternMatcher
*
* Usage
* -----
* Add a pattern to http checks in the dashboard UI.
* Patterns are regexp, for instance '/hello/i'.
*
* When Uptime polls a check with a pattern, it tests the pattern against the
* response body. If the pattern is not found, the check is considered failed.
*/
var fs = require('fs');
var ejs = require('ejs');
var matchPattern = '^/(.*?)/(g?i?m?y?)$';
var template = fs.readFileSync(__dirname + '/views/_detailsEdit.ejs', 'utf8');
exports.initWebApp = function(options) {
var dashboard = options.dashboard;
dashboard.on('populateFromDirtyCheck', function(checkDocument, dirtyCheck, type) {
if (type !== 'http' && type !== 'https') return;
var match = dirtyCheck.match;
if (match) {
if (match.indexOf('/') !== 0) {
match = '/' + match + '/';
}
var matchParts = match.match(new RegExp(matchPattern));
try {
// check that the regexp doesn't crash
new RegExp(matchParts[1], matchParts[2]);
} catch (e) {
throw new Error('Malformed regular expression ' + dirtyCheck.match);
}
}
checkDocument.setPollerParam('match', match);
});
dashboard.on('checkEdit', function(type, check, partial) {
if (type !== 'http' && type !== 'https') return;
partial.push(ejs.render(template, { locals: { check: check } }));
});
};
exports.initMonitor = function(options) {
options.monitor.on('pollerPolled', function(check, res, details) {
if (check.type !== 'http' && check.type !== 'https') return;
var pattern = check.pollerParams && check.pollerParams.match;
if (!pattern) return;
var matchParts = pattern.match(new RegExp(matchPattern));
var regexp;
try {
// check that the regexp doesn't crash (should only happen if the data was altered in the database)
regexp = new RegExp(matchParts[1], matchParts[2]);
} catch (e) {
throw new Error('Malformed pattern in check configuration: ' + pattern);
}
if (!res.body.match(regexp)) {
throw new Error('Response body does not match pattern ' + pattern);
}
return;
});
};