This repository was archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 702
/
Copy pathindex.js
89 lines (79 loc) · 2.56 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
89
/**
* HTTP options plugin
*
* Add options to a HTTP/HTTPS poller on a per-check basis
*
* 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/httpOptions
*
* Usage
* -----
* Add the custom HTTP/HTTPS options in the 'HTTP Options' textarea displayed
* in the check Edit page, in YAML format. For instance:
*
* method: HEAD
* headers:
* User-Agent: This Is Uptime Calling
* X-My-Custom-Header: FooBar
*
* See the Node documentation for a list of available options.
*
* When Uptime polls a HTTP or HTTPS check, the custom options override
* the ClientRequest options.
*/
var fs = require('fs');
var ejs = require('ejs');
var yaml = require('js-yaml');
var express = require('express');
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;
if (!dirtyCheck.http_options) return;
var http_options = dirtyCheck.http_options;
try {
var options = yaml.safeLoad(dirtyCheck.http_options);
checkDocument.setPollerParam('http_options', options);
} catch (e) {
if (e instanceof YAMLException) {
throw new Error('Malformed YAML configuration ' + dirtyCheck.http_options);
} else throw e;
}
});
dashboard.on('checkEdit', function(type, check, partial) {
if (type !== 'http' && type !== 'https') return;
check.http_options = '';
var options = check.getPollerParam('http_options');
if (options) {
try {
options = yaml.safeDump(options);
} catch (e) {
if (e instanceof YAMLException) {
throw new Error('Malformed HTTP options');
} else throw e;
}
check.setPollerParam('http_options', options);
}
partial.push(ejs.render(template, { locals: { check: check } }));
});
options.app.use(express.static(__dirname + '/public'));
};
exports.initMonitor = function(options) {
options.monitor.on('pollerCreated', function(poller, check, details) {
if (check.type !== 'http' && check.type !== 'https') return;
var options = check.pollerParams && check.pollerParams.http_options;
if (!options) return;
// add the custom options to the poller target
for (var key in options) {
poller.target[key] = options[key];
}
return;
});
};