class ServiceMonitor {
constructor( services){
this.services = services
}
async monitor () {
let status = {
url : {},
alias: {}
}
for ( let service of this.services ) {
let isAlive = await this.ping ( service )
status.url [ '${service.address}:${service.port}' ] = isAlive
status.alias[ service.service ] = isAlive
}
return status
}
ping ( connection ) {
return new Promise ( ( resolve, reject )=>{
var tcpp = require('tcp-ping');
tcpp.ping( connection,function( err, data) {
let error = data.results [0].err
if ( !error ) {
resolve ( true )
}
if ( error ) {
resolve ( false )
}
});
})
}
}
( async function test () {
let services = [
{
service : "redis_local",
address : "localhost",
port : 6379,
timeout : 1000,
attempts: 1
},
{
service : "redis_prod",
address : "192.168.7.136",
port : 6379,
timeout : 1000,
attempts: 1
},
{
service : "ussd_prod",
address : "192.168.7.136",
port : 1989,
timeout : 1000,
attempts: 1
},
{
service : "fraud_guard_prod",
address : "192.168.7.136",
port : 1900,
timeout : 1000,
attempts: 1
}
],
status = await new ServiceMonitor ( services ).monitor ()
console.log ( status )
}())
source
share