diff --git a/src/config.js b/src/config.js index c470f6f..bae0d09 100644 --- a/src/config.js +++ b/src/config.js @@ -6,6 +6,8 @@ class Config { constructor() { this.version = require(path.dirname(require.main.filename) + '/../package.json').version; + require('nstats')(null, null, this.version); + process.title = 'Sky Puppy v' + this.version; try { this.path = diff --git a/src/health-check.js b/src/health-check.js index 0b48565..6e614fa 100644 --- a/src/health-check.js +++ b/src/health-check.js @@ -278,14 +278,15 @@ class HealthCheck { service.status.last_unhealthy = process.hrtime.bigint(); } } - this.stats.updateService(service.name, service.status); + await this.stats.updateService(service.name, service.status); - this.alerts.alert(service); + await this.alerts.alert(service); service.status.last_status = service.status.up; const tout = service.config.interval - Number(process.hrtime.bigint() - startTime) / 1000000; + log.debug('tout: ' + (tout > 0 ? tout : 0)); this.services[service.name]._sTimeoutHandler = setTimeout( async () => { this._runCheck(service); diff --git a/src/index.js b/src/index.js index d50875a..ac25c72 100755 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,5 @@ #!/usr/bin/env node -const nstats = require('nstats')(); const Stats = require('./misc/stats.js'); const stats = new Stats(); @@ -8,7 +7,7 @@ async function start() { const { NBars } = await import('nbars'); const HealthCheck = require('./health-check.js'); const healthCheck = new HealthCheck(stats, NBars); - + const nstats = require('nstats')(); const app = require('fastify')({ logger: false }); diff --git a/src/misc/stats.js b/src/misc/stats.js index f181411..c50f753 100644 --- a/src/misc/stats.js +++ b/src/misc/stats.js @@ -7,24 +7,25 @@ class Stats { for (var i = 0; i < this.services.length; i++) { if (this.services[i].name == name) { this.services[i].status = status; + this.services.date = Date.now(); return; } } - this.services.push({ name, status }); + this.services.push({ name, status, date: Date.now() }); } toPrometheus() { var pstring = ''; for (var i = 0; i < this.services.length; i++) { - pstring += `sky_puppy_service_status{service="${this.services[i].name}", name="${this.services[i].name}" } ${this.services[i].status.up} ${Date.now()} \n`; - pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="healthy"} ${this.services[i].status.count.healthy} ${Date.now()} \n`; - pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy"} ${this.services[i].status.count.unhealthy} ${Date.now()} \n`; - pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy_status"} ${this.services[i].status.count.unhealthy_status} ${Date.now()} \n`; - pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy_response_time"} ${this.services[i].status.count.unhealthy_response_time} ${Date.now()} \n`; - pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="down"} ${this.services[i].status.count.down} ${Date.now()} \n`; - pstring += `sky_puppy_service_response_time{service="${this.services[i].name}", name="${this.services[i].name}"} ${this.services[i].status.time} ${Date.now()} \n`; - pstring += `sky_puppy_service_response_code{service="${this.services[i].name}", message="${this.services[i].status.message}", name="${this.services[i].name}"} ${Number(this.services[i].status.code) || -1} ${Date.now()} \n`; + pstring += `sky_puppy_service_status{service="${this.services[i].name}", name="${this.services[i].name}" } ${this.services[i].status.up} ${this.services[i].date} \n`; + pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="healthy"} ${this.services[i].status.count.healthy} ${this.services[i].date} \n`; + pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy"} ${this.services[i].status.count.unhealthy} ${this.services[i].date} \n`; + pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy_status"} ${this.services[i].status.count.unhealthy_status} ${this.services[i].date} \n`; + pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy_response_time"} ${this.services[i].status.count.unhealthy_response_time} ${this.services[i].date} \n`; + pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="down"} ${this.services[i].status.count.down} ${this.services[i].date} \n`; + pstring += `sky_puppy_service_response_time{service="${this.services[i].name}", name="${this.services[i].name}"} ${this.services[i].status.time} ${this.services[i].date} \n`; + pstring += `sky_puppy_service_response_code{service="${this.services[i].name}", message="${this.services[i].status.message}", name="${this.services[i].name}"} ${Number(this.services[i].status.code) || -1} ${this.services[i].date} \n`; } return pstring; } diff --git a/test/sky-puppy-config.json b/test/sky-puppy-config.json index 9b8b6a5..fecfb77 100644 --- a/test/sky-puppy-config.json +++ b/test/sky-puppy-config.json @@ -1,220 +1,220 @@ { - "checkers": { - "request": {}, - "sky-puppy-checker-template": { - "foo": "bar", - "code_messages": { - "200": "Override me plz", - "500": "Yikes its down" - } - } - }, - "alerters": { - "discord_down": { - "uri": "http://127.0.0.1:4270/alert/test", - "json": true, - "method": "POST", - "body": { - "embeds": [ - { - "title": "{{service_name}} is {{alert_type}}!", - "description": "This service was healthy for {{last_healthy_total_duration}} seconds! {{message}}", - "color": 14828098, - "footer": { - "text": "" - }, - "timestamp": "{{timestamp}}" - } - ], - "username": "Sky Puppy", - "avatar_url": "https://i.imgur.com/J5vIVSt.png" - } - }, - "discord_unhealthy": { - "uri": "http://127.0.0.1:4270/alert/test", - "json": true, - "method": "POST", - "body": { - "embeds": [ - { - "title": "{{service_name}} is {{alert_type}}!", - "description": "This service was healthy for {{last_healthy_total_duration}} seconds! {{message}}", - "color": 14852674, - "footer": { - "text": "" - }, - "timestamp": "{{timestamp}}" - } - ], - "username": "Sky Puppy", - "avatar_url": "https://i.imgur.com/J5vIVSt.png" - } - }, - "discord_healthy": { - "uri": "http://127.0.0.1:4270/alert/test", - "json": true, - "method": "POST", - "body": { - "embeds": [ - { - "title": "{{service_name}} is {{alert_type}}!", - "description": "Carry on, looks like things are back! We were down for {{last_unhealthy_total_duration}} seconds. {{message}}", - "color": 6480450, - "footer": { - "text": "" - }, - "timestamp": "{{timestamp}}" - } - ], - "username": "Sky Puppy", - "avatar_url": "https://i.imgur.com/3rfFeOu.png" - } - } - }, - "services": { - "timeout-test": { - "interval": 5, - "checker": { - "name": "request", - "settings": { - "uri": "http://127.0.0.1:4270/wait/random/0/1", - "timeout": 2 + "checkers": { + "request": {}, + "sky-puppy-checker-template": { + "foo": "bar", + "code_messages": { + "200": "Override me plz", + "500": "Yikes its down" + } } - }, - "expected_response_time": 500, - "alerts": [ - { - "type": "down", - "alerter": "discord_down" - }, - { - "type": "unhealthy_response_time", - "for": 1, - "alerter": "discord_unhealthy" - }, - { - "type": "healthy", - "alerter": "discord_healthy" - } - ] }, - "status-test": { - "interval": 5, - "checker": { - "name": "request", - "settings": { - "uri": "http://127.0.0.1:4270/error/random", - "timeout": 2 - } - }, - "start_delay": 1, - "alerts": [ - { - "type": "down", - "alerter": "discord_down" + "alerters": { + "discord_down": { + "uri": "http://127.0.0.1:4270/alert/test", + "json": true, + "method": "POST", + "body": { + "embeds": [ + { + "title": "{{service_name}} is {{alert_type}}!", + "description": "This service was healthy for {{last_healthy_total_duration}} seconds! {{message}}", + "color": 14828098, + "footer": { + "text": "" + }, + "timestamp": "{{timestamp}}" + } + ], + "username": "Sky Puppy", + "avatar_url": "https://i.imgur.com/J5vIVSt.png" + } }, - { - "type": "unhealthy_status", - "for": 4, - "alerter": "discord_unhealthy" + "discord_unhealthy": { + "uri": "http://127.0.0.1:4270/alert/test", + "json": true, + "method": "POST", + "body": { + "embeds": [ + { + "title": "{{service_name}} is {{alert_type}}!", + "description": "This service was healthy for {{last_healthy_total_duration}} seconds! {{message}}", + "color": 14852674, + "footer": { + "text": "" + }, + "timestamp": "{{timestamp}}" + } + ], + "username": "Sky Puppy", + "avatar_url": "https://i.imgur.com/J5vIVSt.png" + } }, - { - "type": "healthy", - "alerter": "discord_healthy" + "discord_healthy": { + "uri": "http://127.0.0.1:4270/alert/test", + "json": true, + "method": "POST", + "body": { + "embeds": [ + { + "title": "{{service_name}} is {{alert_type}}!", + "description": "Carry on, looks like things are back! We were down for {{last_unhealthy_total_duration}} seconds. {{message}}", + "color": 6480450, + "footer": { + "text": "" + }, + "timestamp": "{{timestamp}}" + } + ], + "username": "Sky Puppy", + "avatar_url": "https://i.imgur.com/3rfFeOu.png" + } } - ] }, - "put-test": { - "interval": 3, - "checker": { - "name": "request", - "settings": { - "uri": "http://127.0.0.1:4270/error/flipflop", - "timeout": 2, - "json": true, - "method": "PUT", - "body": { - "test": "sweet" - } - } - }, - "start_delay": 2, - "alerts": [ - { - "type": "unhealthy", - "alerter": "discord_unhealthy" + "services": { + "timeout-test": { + "interval": 5, + "checker": { + "name": "request", + "settings": { + "uri": "http://127.0.0.1:4270/wait/random/0/1", + "timeout": 2 + } + }, + "expected_response_time": 500, + "alerts": [ + { + "type": "down", + "alerter": "discord_down" + }, + { + "type": "unhealthy_response_time", + "for": 1, + "alerter": "discord_unhealthy" + }, + { + "type": "healthy", + "alerter": "discord_healthy" + } + ] }, - { - "type": "healthy", - "alerter": "discord_healthy" - } - ] - }, - "alert-error-status-test": { - "interval": 1, - "checker": { - "name": "request", - "settings": { - "uri": "http://127.0.0.1:4270/error/in/5", - "timeout": 2, - "json": true, - "method": "POST", - "body": { - "test": "sweet" - } - } - }, - "start_delay": 0, - "alerts": [ - { - "type": "down", - "alerter": "discord_down" + "status-test": { + "interval": 5, + "checker": { + "name": "request", + "settings": { + "uri": "http://127.0.0.1:4270/error/random", + "timeout": 2 + } + }, + "start_delay": 1, + "alerts": [ + { + "type": "down", + "alerter": "discord_down" + }, + { + "type": "unhealthy_status", + "for": 4, + "alerter": "discord_unhealthy" + }, + { + "type": "healthy", + "alerter": "discord_healthy" + } + ] }, - { - "type": "unhealthy_status", - "alerter": "discord_unhealthy" + "put-test": { + "interval": 3, + "checker": { + "name": "request", + "settings": { + "uri": "http://127.0.0.1:4270/error/flipflop", + "timeout": 2, + "json": true, + "method": "PUT", + "body": { + "test": "sweet" + } + } + }, + "start_delay": 2, + "alerts": [ + { + "type": "unhealthy", + "alerter": "discord_unhealthy" + }, + { + "type": "healthy", + "alerter": "discord_healthy" + } + ] }, - { - "type": "healthy", - "alerter": "discord_healthy" - } - ] - }, - "sky-puppy-checker-template-test": { - "interval": 2, - "checker": { - "name": "sky-puppy-checker-template", - "settings": { - "bar": "test" + "alert-error-status-test": { + "interval": 1, + "checker": { + "name": "request", + "settings": { + "uri": "http://127.0.0.1:4270/error/in/5", + "timeout": 2, + "json": true, + "method": "POST", + "body": { + "test": "sweet" + } + } + }, + "start_delay": 0, + "alerts": [ + { + "type": "down", + "alerter": "discord_down" + }, + { + "type": "unhealthy_status", + "alerter": "discord_unhealthy" + }, + { + "type": "healthy", + "alerter": "discord_healthy" + } + ] }, - "code_messages": { - "200": "Yup its up" + "sky-puppy-checker-template-test": { + "interval": 2, + "checker": { + "name": "sky-puppy-checker-template", + "settings": { + "bar": "test" + }, + "code_messages": { + "200": "Yup its up" + } + }, + "expected_response_time": 500, + "alerts": [ + { + "type": "down", + "alerter": "discord_down" + }, + { + "type": "unhealthy_response_time", + "for": 1, + "alerter": "discord_unhealthy" + }, + { + "type": "healthy", + "alerter": "discord_healthy" + } + ] } - }, - "expected_response_time": 500, - "alerts": [ - { - "type": "down", - "alerter": "discord_down" - }, - { - "type": "unhealthy_response_time", - "for": 1, - "alerter": "discord_unhealthy" - }, - { - "type": "healthy", - "alerter": "discord_healthy" + }, + "skypuppy": { + "version": "1.0.0", + "log": { + "enable": true, + "colors": true, + "level": "info" } - ] - } - }, - "skypuppy": { - "version": "1.0.0", - "log": { - "enable": true, - "colors": true, - "level": "info" } - } -} +} \ No newline at end of file