Tuesday, July 5, 2016

Remote Failover in Mirth Connect

This is a script that polls a remote Mirth Connect server for the status of it's channels. If a channel specified in 'monitor_list' is not started (or starting), it will auto-deploy a local failover channel and then auto-undeploy the same channel once it detects the primary channel starting.

var info = {
URL: 'https://localhost:8443',
user: 'admin',
pass: 'admin',
version: '3.0.3.7171',
timeout: 10000
};

function monitor(channel,state){
var channel_failover = channel+"_failover"
if(state=="Starting"){
logger.info("Primary Channel: "+channel+" starting. Undeploying failover channel.");
ChannelUtil.undeployChannel(channel_failover);
}
else if(state!="Started"){
if(!ChannelUtil.isChannelDeployed(channel_failover)){
logger.info("WARNING! CHANNEL FAILURE! Channel: "+channel+" Recorded @ "+DateUtil.getCurrentDate("yyyyMMdd@HH:mm:ss"));
ChannelUtil.deployChannel(channel_failover);
logger.info("Deploying Failover Channel");
}
else{
if(ChannelUtil.getChannelState(channel_failover)=="Started"){
}
else{
ChannelUtil.startChannel(channel_failover);
}
}
}
return;
}

var monitor_list = [];
monitor_list[0] = "Remote_Failover_Test";
var monitorLog = {"Remote_Failover_Test" : "FAILED"};
var client = new com.mirth.connect.client.core.Client(info.URL,info.timeout);
var loginStatus = client.login(info.user,info.pass,info.version);
var status = client.getChannelStatusList().toString().split("channelId=");
var statLen = status.length-1;
var subStatus;
var chaName;
var chaState;


for(var i=1;i<=(statLen);i++){
subStatus = status[i].split(",lifetimeStatistics=")[0];
if(subStatus.indexOf("deployedDate=<null>")<0){
chaName = subStatus.split("name=")[1].split(",state=")[0].toString();
chaState = subStatus.split(",state=")[1].split(",")[0].toString();
for(var k in monitor_list){
if(monitor_list[k] == chaName){
monitorLog[chaName]=chaState;
}
}
}
}

for(var k in monitorLog){
//logger.info(k+" "+monitorLog[k]);
monitor(k,monitorLog[k]);
}

return;

No comments:

Post a Comment