Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
- dashboard defaults to all watchers, added "*" to explicitly include all watchers too.
- added portInUse check.
- added proper exception handling for httpcheck promise
  • Loading branch information
shukriadams committed Mar 17, 2020
1 parent eb027fe commit feb862a
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 32 deletions.
32 changes: 20 additions & 12 deletions src/lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,14 @@ module.exports = {
sendgrid : null

}, rawSettings);

for (const name in rawSettings.dashboards){

let dashboard = rawSettings.dashboards[name];

rawSettings.dashboards[name] = Object.assign({
__name : name, // node name, attached here for convenience
__safeName : sanitize(name), // nodename, made safe for filesystems
name : name, // users can add their own convenient name, if not this defaults to node name
watchers : '' //force empty list
}, dashboard);
}

let allWatcherNames = [];
// process watchers first, we need these to process dashboards
for (const name in rawSettings.watchers){

let watcher = rawSettings.watchers[name];

allWatcherNames.push(name);

rawSettings.watchers[name] = Object.assign({
__name : name,
Expand All @@ -72,6 +64,22 @@ module.exports = {
}, watcher);
}

for (const name in rawSettings.dashboards){

let dashboard = rawSettings.dashboards[name];

rawSettings.dashboards[name] = Object.assign({
__name : name, // node name, attached here for convenience
__safeName : sanitize(name), // nodename, made safe for filesystems
name : name, // users can add their own convenient name, if not this defaults to node name
watchers : '*' // force to all watchers
}, dashboard);

// if dashboard is set to * watchers, replace it's watchers list with literal names of all watchers
if (rawSettings.dashboards[name].watchers.trim() === '*')
rawSettings.dashboards[name].watchers = allWatcherNames.join(',');
}

_settings = rawSettings;

// default values
Expand Down
72 changes: 72 additions & 0 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"handlebars-layouts": "3.1.4",
"handlebars": "4.7.3",
"netcat": "1.5.0",
"jsonfile": "5.0.0",
"s-ago": "2.0.1",
"js-yaml": "3.13.1",
Expand Down
2 changes: 2 additions & 0 deletions src/routes/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ module.exports = function(app){
let cronJobs = daemon.cronJobs.slice(0).filter((job)=>{
if (!job.config.enabled)
return null;

if (!dashboardWatchers.includes(job.config.__name))
return null;

return job;
});

Expand Down
8 changes: 6 additions & 2 deletions src/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ watchers:
url: http://example.com
# call this script to perform up test
test: user/example
recipients : me,bob
recipients : me,bob
servicecheck:
interval: "1 * * * *"
host: 127.0.0.1
port: 3000
test : system/portInUse

dashboards:
all:
# name is optional, node name "all" will be use as fallback
name : All the things
watchers: example, another
single:
watchers: example
39 changes: 21 additions & 18 deletions src/tests/system/httpcheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@ module.exports = async function(watcher){
throw `Watcher "${watcher.__name}" is missing a url. test/system/basic requires a url`;

return new Promise((resolve, reject)=>{
let code = null;

request( { uri: watcher.url },
function(error, response) {
if (error){
if (error.errno === 'ENOTFOUND' || error.errno === 'EAI_AGAIN')
error = `${watcher.url} could not be reached.`;

return reject(error);
try {
let code = null;
request( { uri: watcher.url },
function(error, response) {
if (error){
if (error.errno === 'ENOTFOUND' || error.errno === 'EAI_AGAIN')
error = `${watcher.url} could not be reached.`;

return reject(error);
}

if (code && (code < 200 || code > 299)) // allow all code 2**
return reject(`Unexpected HTTP code ${code}`);

resolve(response);
}

if (code && (code < 200 || code > 299)) // allow all code 2**
return reject(`Unexpected HTTP code ${code}`);

resolve(response);
}
).on('response', function(response) {
code = response.statusCode;
})
).on('response', function(response) {
code = response.statusCode;
})
} catch(ex){
reject(ex);
}
});
}
26 changes: 26 additions & 0 deletions src/tests/system/portInUse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Uses Netcat to check if a TCP port is in use
*/
const NetcatClient = require('netcat/client');

module.exports = async function(watcher){
if (!watcher.host)
throw `Watcher "${watcher.__name}" is missing a "host" entry.`;

if (!watcher.port)
throw `Watcher "${watcher.__name}" is missing a "port" entry.`;

return new Promise((resolve, reject)=>{
try {
const nc = new NetcatClient();
nc.addr(watcher.host).scan(watcher.port, function(ports){
if (ports[watcher.port] === 'open')
return resolve('port open');

return reject('port closed');
})
}catch(ex){
reject(ex);
}
});
}

0 comments on commit feb862a

Please sign in to comment.