Files
2026-05-25 20:01:37 +07:00

78 lines
3.4 KiB
PHP

<?php
class Replication extends MY_Controller
{
function __construct() {
parent::__construct();
$this->db = $this->load->database("regional", true);
}
function index() {
echo "Replication API";
}
function monitor() {
//master
$sql = "show master status";
$qry = $this->db->query($sql);
if (! $qry) {
echo json_encode( array("status" => "ERR" , "message" => "Errror Master Status : " . print_r($this->db->error(),true) ) );
exit;
}
$rows = $qry->result_array();
$master = array();
$master["status"] = "Running";
$master["file"] = $rows[0]["File"];
$master["position"] = $rows[0]["Position"];
$sql = "select * from m_branch where m_branchIsActive = 'Y'";
$qry = $this->db->query($sql);
if (! $qry ) {
echo json_encode( array("status" => "ERR" , "message" => "Error M_Branch : " . print_r($this->db->error(),true) ) );
exit;
}
$rows = $qry->result_array();
$slave = array();
foreach($rows as $r) {
$branchCode = $r["M_BranchCode"];
$branchName = $r["M_BranchName"];
$branchIP = $r["M_BranchIPAddress"];
$slave[] = array("code" => $branchCode, "name" => $branchName );
$idx = count($slave) - 1;
$url = "http://$branchIP/one-api/tools/replication/slave";
$get_rst = $this->get($url);
$rst = json_decode($get_rst,true);
if ($rst["status"] == "ERR" || ! $rst) {
$slave[$idx] = array_merge($slave[$idx], array("status"=>"Connection Issue (" . $rst["message"] . ")",
"Behind" => -1, "Table" => array(), "MasterLogFile"=> "", "ReadPosition" => 0 ));
} else {
$slave[$idx] = array_merge($slave[$idx], $rst);
}
}
$result = array( "status" => "OK" , "master" => $master , "slave" => $slave) ;
echo json_encode($result);
}
function get($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if ( curl_error($ch) != "" ) {
$result = json_encode(array("status" => "ERR", "message" => curl_error($ch) ));
}
curl_close($ch);
return $result;
}
function post($url,$data) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
$result = curl_exec($ch);
return $result;
}
}