#! /bin/bash
export LANG=en_US.UTF-8
mysql_cluster_json=""
port=
if [ ! -e "/data/monitor/passwd" ];then
echo "[]"
exit
fi
pid=`ps aux |grep mysqld|grep -v mysqld_safe| grep -v grep | grep -v ps.sh | grep -v dump |head -n 1|awk '{print $2}'`
if [ $pid == "" ];then
echo "[]"
exit
else
port=`netstat -lntp|grep $pid/mysqld | awk '{print $4}'|awk -F ':' '{print $NF}'`
fi
mysql_cluster_json="'cpname':'mysql_cluster'"
passwd=`cat /data/monitor/passwd`
user=`echo $passwd|awk '{print $1}'`
password=`echo $passwd|awk '{print $2}'`
mysqlStatStr="mysql -u $user -p$password"
ip=`ip addr | grep 'state UP' -A2 | head -n3|tail -n1 | awk '{print $2}' | cut -f1 -d '/'`
master_ip=`$mysqlStatStr 2>/dev/null -e "show slave status\G"|grep "Master_Host"|awk '{print $2}'`
master_port=`$mysqlStatStr 2>/dev/null -e "show slave status\G"|grep "Master_Port"|awk '{print $2}'`
if [[ $master_ip == "" || $master_port == "" ]];then
echo "[]"
else
cluserName="${ip}_${port} ${master_ip}_${master_port}"
cluserName=`echo ${cluserName}|tr ' ' '\n'|sort`
cluserName=`echo ${cluserName}|tr ' ' '_'`
mysql_cluster_json="$mysql_cluster_json,'flags':[{'cluserName':'${cluserName}','nodeList':[{'nodeName':'${master_ip}_${master_port}','nodeIp':'$master_ip','port':'$master_port'},{'nodeName':'${ip}_${port}','nodeIp':'$ip','port':'$port'}]}]"
mysql_cluster_json="[{${mysql_cluster_json%}}]"
mysql_cluster_json=`echo ${mysql_cluster_json//\'/\"}`
echo $mysql_cluster_json
fi
exit