Skip to content

Commit 344b6f0

Browse files
committed
Added module based checkers! Now you can write custom checkers to check any thing.
1 parent e86782c commit 344b6f0

File tree

6 files changed

+277
-176
lines changed

6 files changed

+277
-176
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ npm install -g sky-puppy
1515
sky-puppy
1616
```
1717

18+
## Checkers
19+
20+
- request (native)
21+
1822
## Sample Config
1923

2024
Sky Puppy looks for a file called `sky-puppy-config.json` in the folder it is ran at.
@@ -142,4 +146,3 @@ All notable changes to this project will be documented in this file. Dates are d
142146
143147
- init commit config based is done REST endpoints to come [`b03ca2e`](https://github.com/Phara0h/sky-puppy/commit/b03ca2ee43ac5754f020d1963dcb9e201cd47e0d)
144148
- Initial commit [`b7535a3`](https://github.com/Phara0h/sky-puppy/commit/b7535a3a3990fe081f932dc6e1079a86bdf9842f)
145-

README.nbs

+11-8
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,17 @@ Sky Puppy looks for a file called `sky-puppy-config.json` in the folder it is ra
8787
"your_service": {
8888
"interval": 3,
8989
"start_delay": 2,
90-
"request": {
91-
"uri": "http://127.0.0.1/a/cool/endpoint",
92-
"timeout": 2,
93-
"json": true,
94-
"method": "PUT",
95-
"body": {
96-
"test": "sweet"
97-
},
90+
"checker": {
91+
"name": "request",
92+
"settings": {
93+
"uri": "http://127.0.0.1/a/cool/endpoint",
94+
"timeout": 2,
95+
"json": true,
96+
"method": "PUT",
97+
"body": {
98+
"test": "sweet"
99+
},
100+
}
98101
},
99102
"alerts": [
100103
{

src/checkers/request.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var fasquest = require('fasquest');
2+
const client = {
3+
https: require('https'),
4+
http: require('http')
5+
};
6+
7+
fasquest.agent = {
8+
http: new client.http.Agent({
9+
keepAlive: false
10+
}),
11+
https: new client.https.Agent({
12+
keepAlive: false
13+
})
14+
};
15+
16+
class Request {
17+
constructor(config, service, settings) {
18+
this.config = config;
19+
this.service = service;
20+
this.settings = settings;
21+
}
22+
async init() {
23+
if (this.settings) {
24+
this.settings.method = this.settings.method || 'GET';
25+
}
26+
this.settings.timeout = Math.round((this.settings.timeout || 60) * 1000);
27+
this.settings.resolveWithFullResponse = true;
28+
this.settings.simple = false;
29+
30+
this.service.config.expected_response_time =
31+
this.service.expected_response_time || this.settings.timeout;
32+
33+
if (!this.settings.headers) {
34+
this.settings.headers = {};
35+
}
36+
this.settings.headers[
37+
'User-Agent'
38+
] = `Sky-Puppy / ${this.config.skypuppy.version} (Health Check Service)`;
39+
}
40+
41+
async check() {
42+
try {
43+
var res = await fasquest.request(this.settings);
44+
45+
return {
46+
code: res.statusCode
47+
};
48+
} catch (e) {
49+
throw e.err;
50+
}
51+
}
52+
}
53+
54+
module.exports = function(config, service, settings) {
55+
return new Request(config, service, settings);
56+
};

src/config.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Config {
1717
console.log(
1818
'Error loading config. Creating and using default one at ' + this.path
1919
);
20-
this.saveConfig();
2120
}
2221
if (!this.alerters) {
2322
this.settings.alerters = {};
@@ -27,9 +26,16 @@ class Config {
2726
this.settings.services = {};
2827
}
2928

29+
if (!this.checkers) {
30+
this.settings.checkers = {
31+
request: {}
32+
};
33+
}
34+
3035
this.settings.skypuppy = this.settings.skypuppy || {
3136
version: this.version
3237
};
38+
this.saveConfig();
3339
}
3440

3541
addService(name, service) {
@@ -87,6 +93,13 @@ class Config {
8793
return this.settings.alerters;
8894
}
8995

96+
getChecker(name) {
97+
return this.checkers[name];
98+
}
99+
get checkers() {
100+
return this.settings.checkers;
101+
}
102+
90103
get skypuppy() {
91104
return this.settings.skypuppy;
92105
}

src/health-check.js

+52-38
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,50 @@
11
const fs = require('fs');
2-
var fasquest = require('fasquest');
3-
const client = {
4-
https: require('https'),
5-
http: require('http')
6-
};
7-
8-
fasquest.agent = {
9-
http: new client.http.Agent({
10-
keepAlive: false
11-
}),
12-
https: new client.https.Agent({
13-
keepAlive: false
14-
})
15-
};
162

173
const Config = require('./config.js');
184
var config = new Config();
195
const Alerts = require('./alerts.js');
206

217
class HealthCheck {
228
constructor(stats, nbars) {
9+
this.checkers = {};
2310
this.services = {};
2411
this.alerters = {};
2512
this.stats = stats;
2613
this.alerts = new Alerts(stats, config, nbars);
27-
14+
var checkersKeys = Object.keys(config.checkers);
2815
var servicesKeys = Object.keys(config.services);
2916

17+
for (var i = 0; i < checkersKeys.length; i++) {
18+
var checker = {
19+
...config.getChecker(checkersKeys[i])
20+
};
21+
22+
if (checkersKeys[i] == 'request') {
23+
this.checkers[checkersKeys[i]] = {
24+
mod: require('./checkers/request'),
25+
settings: checker,
26+
name: checkersKeys[i]
27+
};
28+
} else {
29+
try {
30+
this.checkers[checkersKeys[i]] = {
31+
mod: require(checker.name),
32+
settings: checker,
33+
name: checkersKeys[i]
34+
};
35+
} catch (e) {
36+
console.error(`Failed to load ${checkersKeys[i]}!`);
37+
process.exit();
38+
}
39+
}
40+
}
41+
3042
for (var i = 0; i < servicesKeys.length; i++) {
31-
this.startService(servicesKeys[i], {
43+
var service = {
3244
...config.getService(servicesKeys[i])
33-
});
45+
};
46+
47+
this.startService(servicesKeys[i], service);
3448
}
3549
}
3650

@@ -94,13 +108,13 @@ class HealthCheck {
94108
return false;
95109
}
96110

97-
startService(name, service) {
111+
async startService(name, service) {
98112
if (!this.services[name]) {
99113
console.log(`Starting service ${name} ...`);
100114
var nService = {
101115
name,
102116
enabled: true,
103-
delete: false,
117+
checker: {},
104118
config: {
105119
...service,
106120
interval: Math.round((service.interval || 5) * 1000), // default 5 seconds
@@ -119,23 +133,22 @@ class HealthCheck {
119133
}
120134
}
121135
};
122-
123-
nService.config.request.method = nService.config.request.method || 'GET';
124-
nService.config.request.timeout = Math.round(
125-
(nService.config.request.timeout || 60) * 1000
126-
);
127-
nService.config.request.resolveWithFullResponse = true;
128-
nService.config.request.simple = false;
129-
130-
nService.config.expected_response_time =
131-
service.expected_response_time || nService.config.request.timeout;
132-
133-
if (!nService.config.request.headers) {
134-
nService.config.request.headers = {};
136+
var checker = this.checkers[nService.config.checker.name];
137+
var checkerSettings = {};
138+
139+
if (checker.settings) {
140+
checkerSettings = {
141+
...checker.settings,
142+
...nService.config.checker.settings
143+
};
144+
} else {
145+
checkerSettings = {
146+
...nService.config.checker.settings
147+
};
135148
}
136-
nService.config.request.headers[
137-
'User-Agent'
138-
] = `Sky-Puppy / ${config.skypuppy.version} (Health Check Service)`;
149+
150+
nService.checker = await checker.mod(config, nService, checkerSettings);
151+
await nService.checker.init();
139152

140153
this.services[name] = nService;
141154

@@ -157,11 +170,11 @@ class HealthCheck {
157170
const oldStatus = service.status.up;
158171

159172
try {
160-
var res = await fasquest.request(service.config.request);
173+
var res = await service.checker.check();
161174

162175
service.status.time =
163176
Number(process.hrtime.bigint() - startTime) / 1000000;
164-
service.status.code = res.statusCode;
177+
service.status.code = res.code;
165178
service.status.up = 1;
166179

167180
if (service.config.expected_status != service.status.code) {
@@ -193,7 +206,8 @@ class HealthCheck {
193206
service.status.count.down++;
194207
service.status.up = -1;
195208
service.status.code = 0;
196-
console.log(service.name, e.err.message);
209+
210+
console.log(service.name, e.message);
197211
}
198212

199213
if (service.status.last_status == null) {

0 commit comments

Comments
 (0)