@ -6,6 +6,7 @@ var timezone = require('dayjs/plugin/timezone')
dayjs . extend ( utc )
dayjs . extend ( utc )
dayjs . extend ( timezone )
dayjs . extend ( timezone )
const axios = require ( "axios" ) ;
const axios = require ( "axios" ) ;
const { UP , DOWN , PENDING } = require ( "../util" ) ;
const { debug } = require ( "../util" ) ;
const { debug } = require ( "../util" ) ;
const { tcping , ping , checkCertificate } = require ( "../util-server" ) ;
const { tcping , ping , checkCertificate } = require ( "../util-server" ) ;
const { R } = require ( "redbean-node" ) ;
const { R } = require ( "redbean-node" ) ;
@ -24,7 +25,6 @@ const customAgent = new https.Agent({
* 1 = UP
* 1 = UP
* /
* /
class Monitor extends BeanModel {
class Monitor extends BeanModel {
async toJSON ( ) {
async toJSON ( ) {
let notificationIDList = { } ;
let notificationIDList = { } ;
@ -43,6 +43,7 @@ class Monitor extends BeanModel {
url : this . url ,
url : this . url ,
hostname : this . hostname ,
hostname : this . hostname ,
port : this . port ,
port : this . port ,
maxretries : this . maxretries ,
weight : this . weight ,
weight : this . weight ,
active : this . active ,
active : this . active ,
type : this . type ,
type : this . type ,
@ -54,21 +55,25 @@ class Monitor extends BeanModel {
start ( io ) {
start ( io ) {
let previousBeat = null ;
let previousBeat = null ;
let retries = 0 ;
const beat = async ( ) => {
const beat = async ( ) => {
if ( ! previousBeat ) {
if ( ! previousBeat ) {
previousBeat = await R . findOne ( "heartbeat" , " monitor_id = ? ORDER BY time DESC" , [
previousBeat = await R . findOne ( "heartbeat" , " monitor_id = ? ORDER BY time DESC" , [
this . id
this . id
] )
] )
}
}
const isFirstBeat = ! previousBeat ;
let bean = R . dispense ( "heartbeat" )
let bean = R . dispense ( "heartbeat" )
bean . monitor _id = this . id ;
bean . monitor _id = this . id ;
bean . time = R . isoDateTime ( dayjs . utc ( ) ) ;
bean . time = R . isoDateTime ( dayjs . utc ( ) ) ;
bean . status = 0 ;
bean . status = DOWN ;
// Duration
// Duration
if ( previous Beat) {
if ( ! isFirst Beat) {
bean . duration = dayjs ( bean . time ) . diff ( dayjs ( previousBeat . time ) , 'second' ) ;
bean . duration = dayjs ( bean . time ) . diff ( dayjs ( previousBeat . time ) , 'second' ) ;
} else {
} else {
bean . duration = 0 ;
bean . duration = 0 ;
@ -98,7 +103,7 @@ class Monitor extends BeanModel {
debug ( "Cert Info Query Time: " + ( dayjs ( ) . valueOf ( ) - certInfoStartTime ) + "ms" )
debug ( "Cert Info Query Time: " + ( dayjs ( ) . valueOf ( ) - certInfoStartTime ) + "ms" )
if ( this . type === "http" ) {
if ( this . type === "http" ) {
bean . status = 1 ;
bean . status = UP ;
} else {
} else {
let data = res . data ;
let data = res . data ;
@ -110,7 +115,7 @@ class Monitor extends BeanModel {
if ( data . includes ( this . keyword ) ) {
if ( data . includes ( this . keyword ) ) {
bean . msg += ", keyword is found"
bean . msg += ", keyword is found"
bean . status = 1 ;
bean . status = UP ;
} else {
} else {
throw new Error ( bean . msg + ", but keyword is not found" )
throw new Error ( bean . msg + ", but keyword is not found" )
}
}
@ -121,30 +126,52 @@ class Monitor extends BeanModel {
} else if ( this . type === "port" ) {
} else if ( this . type === "port" ) {
bean . ping = await tcping ( this . hostname , this . port ) ;
bean . ping = await tcping ( this . hostname , this . port ) ;
bean . msg = ""
bean . msg = ""
bean . status = 1 ;
bean . status = UP ;
} else if ( this . type === "ping" ) {
} else if ( this . type === "ping" ) {
bean . ping = await ping ( this . hostname ) ;
bean . ping = await ping ( this . hostname ) ;
bean . msg = ""
bean . msg = ""
bean . status = 1 ;
bean . status = UP ;
}
}
retries = 0 ;
} catch ( error ) {
} catch ( error ) {
if ( ( this . maxretries > 0 ) && ( retries < this . maxretries ) ) {
retries ++ ;
bean . status = PENDING ;
}
bean . msg = error . message ;
bean . msg = error . message ;
}
}
// Mark as important if status changed
// * ? -> ANY STATUS = important [isFirstBeat]
if ( ! previousBeat || previousBeat . status !== bean . status ) {
// UP -> PENDING = not important
// * UP -> DOWN = important
// UP -> UP = not important
// PENDING -> PENDING = not important
// * PENDING -> DOWN = important
// PENDING -> UP = not important
// DOWN -> PENDING = this case not exists
// DOWN -> DOWN = not important
// * DOWN -> UP = important
let isImportant = isFirstBeat ||
( previousBeat . status === UP && bean . status === DOWN ) ||
( previousBeat . status === DOWN && bean . status === UP ) ||
( previousBeat . status === PENDING && bean . status === DOWN ) ;
// Mark as important if status changed, ignore pending pings,
// Don't notify if disrupted changes to up
if ( isImportant ) {
bean . important = true ;
bean . important = true ;
// Do not send if first beat is UP
// Send only if the first beat is DOWN
if ( previousBeat || bean . status !== 1 ) {
if ( ! isFirstBeat || bean . status === DOWN ) {
let notificationList = await R . getAll ( ` SELECT notification.* FROM notification, monitor_notification WHERE monitor_id = ? AND monitor_notification.notification_id = notification.id ` , [
let notificationList = await R . getAll ( ` SELECT notification.* FROM notification, monitor_notification WHERE monitor_id = ? AND monitor_notification.notification_id = notification.id ` , [
this . id
this . id
] )
] )
let text ;
let text ;
if ( bean . status === 1 ) {
if ( bean . status === UP ) {
text = "✅ Up"
text = "✅ Up"
} else {
} else {
text = "🔴 Down"
text = "🔴 Down"
@ -165,8 +192,10 @@ class Monitor extends BeanModel {
bean . important = false ;
bean . important = false ;
}
}
if ( bean . status === 1 ) {
if ( bean . status === UP ) {
console . info ( ` Monitor # ${ this . id } ' ${ this . name } ': Successful Response: ${ bean . ping } ms | Interval: ${ this . interval } seconds | Type: ${ this . type } ` )
console . info ( ` Monitor # ${ this . id } ' ${ this . name } ': Successful Response: ${ bean . ping } ms | Interval: ${ this . interval } seconds | Type: ${ this . type } ` )
} else if ( bean . status === PENDING ) {
console . warn ( ` Monitor # ${ this . id } ' ${ this . name } ': Pending: ${ bean . msg } | Type: ${ this . type } ` )
} else {
} else {
console . warn ( ` Monitor # ${ this . id } ' ${ this . name } ': Failing: ${ bean . msg } | Type: ${ this . type } ` )
console . warn ( ` Monitor # ${ this . id } ' ${ this . name } ': Failing: ${ bean . msg } | Type: ${ this . type } ` )
}
}
@ -293,7 +322,7 @@ class Monitor extends BeanModel {
}
}
total += value ;
total += value ;
if ( row . status === 0 ) {
if ( row . status === 0 || row . status === 2 ) {
downtime += value ;
downtime += value ;
}
}
}
}