From 96242dce0dc8a816b326b0c1daa2fda39c9f92e1 Mon Sep 17 00:00:00 2001 From: Matthew Macdonald-Wallace Date: Thu, 22 Jul 2021 09:38:27 +0100 Subject: [PATCH] Expose check status and response time to Prometheus --- server/model/monitor.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 04feea6b..ca9d6c9f 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -1,4 +1,4 @@ - +const Prometheus = require('prom-client'); const dayjs = require("dayjs"); const utc = require('dayjs/plugin/utc') var timezone = require('dayjs/plugin/timezone') @@ -10,6 +10,16 @@ const {R} = require("redbean-node"); const {BeanModel} = require("redbean-node/dist/bean-model"); const {Notification} = require("../notification") +const monitor_response_time = new Prometheus.Gauge({ + name: 'monitor_response_time', + help: 'Monitor Response Time (ms)', + labelNames: ['monitor_name'] +}); +const monitor_status = new Prometheus.Gauge({ + name: 'montor_status', + help: 'Monitor Status (1 = UP, 0= DOWN)', + labelNames: ['monitor_name'] +}); /** * status: * 0 = DOWN @@ -143,12 +153,21 @@ class Monitor extends BeanModel { bean.important = false; } + + monitor_status.set({ + monitor_name: this.name + }, bean.status) + if (bean.status === 1) { console.info(`Monitor #${this.id} '${this.name}': Successful Response: ${bean.ping} ms | Interval: ${this.interval} seconds | Type: ${this.type}`) } else { console.warn(`Monitor #${this.id} '${this.name}': Failing: ${bean.msg} | Type: ${this.type}`) } + monitor_response_time.set({ + monitor_name: this.name + }, bean.ping) + io.to(this.user_id).emit("heartbeat", bean.toJSON()); await R.store(bean)