Brekeke Contact Center Suite Wiki

Sample Script for Heartbeat action: Switching Brekeke CCS Status

Note: Brekeke CCS version 2.7.8 or later is required.

*In this script, some REST APIs are used. For details, refer to the REST API – Brekeke CIM >>.

// Change CCS Status to Active
/***Need to change according to the environment*******************************/
var is_exec_restart = true;  // As default Change status to Active
var is_exec_stop = false;   // If true, Change status to Standby
var is_exec_oppsite_stop = false; // If true, Change status of opposite site to Standby

// var is_exec_report_sync_start = false;  //If true and scheduled report is used, execute the command specified by CMD_LSYNCD_START
// var is_exec_report_sync_stop = false;   //If true and scheduled report is used, execute the command specified by CMD_LSYNCD_STOP
// var CMD_LSYNCD_START = "sudo systemctl start lsyncd";
// var CMD_LSYNCD_STOP = "sudo systemctl stop lsyncd";

// URL of CIM,CA,CRM,RFS
var CIM_URL = "http://<cim_server>:<port>/cim";
var CA_URL = "http://<ca_server>:<port>/ca";
var CRM_URL = "http://<crm_server>:<port>/crm";
var RFS_URL = "http://<rfs_server>:<port>/rfs";
// If the property "webif.v1p.system.restart.password" is configured on the Advanced Option field on CIM and CA,
// set password.
// If not, no password setting is needed.
var CIM_PD = "";
var CA_PD = "";
var CRM_PD = "";
var RFS_PD = "";

// URL of CIM,CA,CRM,RFS on Peer Site
var OPPOSITE_CIM_URL = "http://<peer_cim_server>:<port>/cim";
var OPPOSITE_CA_URL = "http://<peer_ca_server>:<port>/ca";
var OPPOSITE_CRM_URL = "http://<peer_crm_server>:<port>/crm";
var OPPOSITE_RFS_URL = "http://<peer_rfs_server>:<port>/rfs";
// If the property "webif.v1p.system.restart.password" is configured on the Advanced Option field on Peer CIM and CA,
// set password.
// If not, no password setting is needed.
var OPPOSITE_CIM_PD = "";
var OPPOSITE_CA_PD = "";
var OPPOSITE_CRM_PD = "";
var OPPOSITE_RFS_PD = "";

// // Whether CA's scheduled report is used (false: no, true: yes)
// var CA_SCHEDULED_REPORT = false;

/***************************************************************************/
var OPERATION_RESTART = "/webif/v1p/system/restart";
var OPERATION_STOP = "/webif/v1p/system/stop";
var OPERATION_STATUS = "/webif/v1p/system/status";
var RETRY_GET_STATUS = 5;
var RETRY_RESTART = 5;
var RETRY_STOP = 5;
var URL = Java.type("java.net.URL");
var BufferedReader = Java.type("java.io.BufferedReader");
var BufferedWriter = Java.type("java.io.BufferedWriter");
var InputStreamReader = Java.type("java.io.InputStreamReader");
var OutputStreamWriter = Java.type("java.io.OutputStreamWriter");
var Thread = Java.type("java.lang.Thread");
var SimpleDateFormat = Java.type("java.text.SimpleDateFormat");
var Date = Java.type("java.util.Date");
var File = Java.type("java.io.File");
var FileReader = Java.type("java.io.FileReader");
var Boolean = Java.type("java.lang.Boolean");
var ProcessBuilder = Java.type("java.lang.ProcessBuilder");

//// Log output function (with timestamp) Heartbeat case 1
//// OUTPUT to log.log
//var Dump = Java.type("com.brekeke.util.Dump");
//var log = Dump.getLogger();
//var f_writeLogWithDate = function(msg) {
//    var df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
//    log.info(msg + "\ttimestamp=" + df.format(new Date()));
//}

// Log output function (with timestamp) Heartbeat case 2
// OUTPUT to ha.log
var f_writeLogWithDate = function(msg) {
    var df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
    out.println(msg + "\ttimestamp=" + df.format(new Date()));
}

// REST API execution function
var f_execOperation = function(url, ope, password) {
  var url = new URL(url + ope);
  var uc = null;
  var out = null;
  var ins = null;
  var ret = false;
  var ret_data = "";
  try {
    uc = url.openConnection();      // Get connection
    uc.setRequestMethod("POST");    // Method
    uc.setRequestProperty("Content-Type", "application/json");  // Content-Type
    uc.setDoOutput(true);       // Allow request body submission
    // Write request body
    out = new BufferedWriter(new OutputStreamWriter(uc.getOutputStream()));
    var content = "";
    if(password) {
      content = '{"password":"' + password + '"}';
    }
    out.write(content);
    out.flush();
    out.close();
    // Read response body
    var i = null;
    try {
      i = uc.getInputStream();
    } catch(ie) {
      i = uc.getErrorStream();
    }
    if(i == null) {
      f_writeLogWithDate("No response.");
    } else {
      ins = new BufferedReader(new InputStreamReader(i));
      var s;
      var response_text = "";
      while((s = ins.readLine()) !== null) {
        response_text += s;
        response_text += "\r\n";
      }
      var response_code = uc.getResponseCode();
      ins.close();
      f_writeLogWithDate("response code=" + response_code + ", response body="+ response_text);
      // Successful completion if the response code is 200
      if(response_code == 200) {
        ret = true;
      }
      ret_data = response_text;
    }
  } catch(e) {
    f_writeLogWithDate("Couldn't connect. detail=" + e.message);
  } finally {
    if(uc) {
      try {
        uc.disconnect();
      } catch(e){}
    }
  }
  return { result:ret, data:ret_data };
}
// Standby mode acquisition function
var f_getStandbyMode = function() {
  var standby_mode = null;
  var ret = null;
  var retry_cnt = 0;
  f_writeLogWithDate("Get cim standbymode.");
  do {
    f_writeLogWithDate("execute #" + (retry_cnt+1));
    ret = f_execOperation(CIM_URL, OPERATION_STATUS, CIM_PD);
    if(ret["result"]) {
      var jsonObject = JSON.parse(ret["data"]);
      standby_mode = jsonObject.standby;
      break;
    }
    retry_cnt++;
    Thread.sleep(1000);
  } while(retry_cnt < RETRY_GET_STATUS);
  if(!ret["result"]) {
    f_writeLogWithDate("Failed get cim standbymode.");
  }
  f_writeLogWithDate("standby mode=" + standby_mode);
  return standby_mode;
}

// CCS restart function
var f_restartCCS = function(cim_url, cim_password, ca_url, ca_password,crm_url, crm_password, rfs_url, rfs_password) {
  // Restart CIM
  var retry_cnt = 0;
  var ret = null;
  f_writeLogWithDate("Start restarting cim.");
  do {
    f_writeLogWithDate("execute #" + (retry_cnt+1));
    ret = f_execOperation(cim_url, OPERATION_RESTART, cim_password);
    if(ret["result"]) {
      break;
    }
    retry_cnt++;
    Thread.sleep(1000);
  } while(retry_cnt < RETRY_RESTART);
  if(!ret["result"]) {
    f_writeLogWithDate("Failed restarting cim.");
  }
  f_writeLogWithDate("End restarting cim.");
  // Restart CA
  retry_cnt = 0;
  ret = null;
  f_writeLogWithDate("Start restarting ca.");
  do {
    f_writeLogWithDate("execute #" + (retry_cnt+1));
    ret = f_execOperation(ca_url, OPERATION_RESTART, ca_password);
    if(ret["result"]) {
      break;
    }
    retry_cnt++;
    Thread.sleep(1000);
  } while(retry_cnt < RETRY_RESTART);
  if(!ret["result"]) {
    f_writeLogWithDate("Failed restarting ca.");
  }
  f_writeLogWithDate("End restarting ca.");
// Restart CRM
    retry_cnt = 0;
    ret = null;
    f_writeLogWithDate("Start restarting crm.");
    do {
        f_writeLogWithDate("execute #" + (retry_cnt+1));
        ret = f_execOperation(crm_url, OPERATION_RESTART, crm_password);
        if(ret["result"]) {
        break;
        }
        retry_cnt++;
        Thread.sleep(1000);
    } while(retry_cnt < RETRY_RESTART);
    if(!ret["result"]) {
        f_writeLogWithDate("Failed restarting crm.");
    }
    f_writeLogWithDate("End restarting crm.");
// Restart RFS
    retry_cnt = 0;
    ret = null;
    f_writeLogWithDate("Start restarting rfs.");
    do {
        f_writeLogWithDate("execute #" + (retry_cnt+1));
        ret = f_execOperation(rfs_url, OPERATION_RESTART, rfs_password);
        if(ret["result"]) {
        break;
        }
        retry_cnt++;
        Thread.sleep(1000);
    } while(retry_cnt < RETRY_RESTART);
    if(!ret["result"]) {
        f_writeLogWithDate("Failed restarting rfs.");
    }
    f_writeLogWithDate("End restarting rfs.");
}

// CCS Stop Function
var f_stopCCS = function(cim_url, cim_password, ca_url, ca_password,crm_url, crm_password, rfs_url, rfs_password) {
    // Stop CA
    var retry_cnt = 0;
    var ret = null;
    f_writeLogWithDate("Start stopping ca.");
    do {
    f_writeLogWithDate("execute #" + (retry_cnt+1));
    ret = f_execOperation(ca_url, OPERATION_STOP, ca_password);
    if(ret["result"]) {
        break;
    }
    retry_cnt++;
    Thread.sleep(1000);
    } while(retry_cnt < RETRY_STOP);
    if(!ret["result"]) {
    f_writeLogWithDate("Failed stopping ca.");
    }
    f_writeLogWithDate("End stopping ca.");
    // Stop CIM
    retry_cnt = 0;
    ret = null;
    f_writeLogWithDate("Start stopping cim.");
    do {
    f_writeLogWithDate("execute #" + (retry_cnt+1));
    ret = f_execOperation(cim_url, OPERATION_STOP, cim_password);
    if(ret["result"]) {
        break;
    }
    retry_cnt++;
    Thread.sleep(1000);
    } while(retry_cnt < RETRY_STOP);
    if(!ret["result"]) {
    f_writeLogWithDate("Failed stopping cim.");
    }
    f_writeLogWithDate("End stopping cim.");
    // Stop CRM
    retry_cnt = 0;
    ret = null;
    f_writeLogWithDate("Start stopping crm.");
    do {
        f_writeLogWithDate("execute #" + (retry_cnt+1));
        ret = f_execOperation(crm_url, OPERATION_STOP, crm_password);
        if(ret["result"]) {
        break;
        }
        retry_cnt++;
        Thread.sleep(1000);
    } while(retry_cnt < RETRY_STOP);
    if(!ret["result"]) {
        f_writeLogWithDate("Failed stopping crm.");
    }
    f_writeLogWithDate("End stopping crm.");
    // Stop RFS
    retry_cnt = 0;
    ret = null;
    f_writeLogWithDate("Start stopping rfs.");
    do {
        f_writeLogWithDate("execute #" + (retry_cnt+1));
        ret = f_execOperation(rfs_url, OPERATION_STOP, rfs_password);
        if(ret["result"]) {
        break;
        }
        retry_cnt++;
        Thread.sleep(1000);
    } while(retry_cnt < RETRY_STOP);
    if(!ret["result"]) {
        f_writeLogWithDate("Failed stopping rfs.");
    }
    f_writeLogWithDate("End stopping rfs.");
}
// Command execution function
var f_execCommand = function(cmd) {
  var br = null;
  var exec_cmd = cmd.split(" ");
  var pb = new ProcessBuilder(exec_cmd);
  pb.redirectErrorStream(true);   // Merge the contents of stderr into stdout
  try {
    // Start Process
    var process = pb.start();
    br = new BufferedReader(new InputStreamReader(process.getInputStream()));
    while(true) {
      var line = br.readLine();
      if(line == null) {
        break;
      }
      out.println(line);
    }
    // Waiting for process termination
    var ret = process.waitFor();
    f_writeLogWithDate("result = " + ret);
  } finally {
    if(br != null) {
      br.close();
    }
  }
}

if(is_exec_oppsite_stop) {
  // Opposite CCS stop
  f_writeLogWithDate("Start stopping oppsite ccs.");
  f_stopCCS(OPPOSITE_CIM_URL, OPPOSITE_CIM_PD, OPPOSITE_CA_URL, OPPOSITE_CA_PD,OPPOSITE_CRM_URL, OPPOSITE_CRM_PD, OPPOSITE_RFS_URL, OPPOSITE_RFS_PD);
  f_writeLogWithDate("End stopping oppsite ccs.");
}
if(is_exec_restart) {
  // Restart CCS
  f_writeLogWithDate("Start restarting own ccs.");
  f_restartCCS(CIM_URL, CIM_PD, CA_URL, CA_PD,CRM_URL, CRM_PD, RFS_URL, RFS_PD);
  f_writeLogWithDate("End restarting own ccs.");
}
if(is_exec_stop) {
  // Stop CCS
  f_writeLogWithDate("Start stopping own ccs.");
  f_stopCCS(CIM_URL, CIM_PD, CA_URL, CA_PD,CRM_URL, CRM_PD, RFS_URL, RFS_PD);
  f_writeLogWithDate("End stopping own ccs.");
}
// if(CA_SCHEDULED_REPORT) {
//   if(is_exec_report_sync_start) {
//     // Start lsyncd for file sync
//     f_writeLogWithDate("Start file sync.");
//     f_execCommand(CMD_LSYNCD_START);
//   }
//   if(is_exec_report_sync_stop) {
//     // stop lsyncd for file sync
//     f_writeLogWithDate("Stop file sync.");
//     f_execCommand(CMD_LSYNCD_STOP);
//   }
// }
Yes No
Suggest Edit