#!/usr/bin/python
import sys
import getopt
import json
import os
import commands
import time
import re
def get_arginfo(argv):
try:
options,args = getopt.getopt(argv , "hf:u:p:" , ["help" , "ip=" , "user=" , "passwd="])
except getopt.GetoptError:
sys.exit()
arginfo = {}
for option,value in options:
value_1 = format(value)
option_1 = format(option)
arginfo[option_1] = value_1
return arginfo
def getcluster_esinfo(ip,port):
ip = str(ip)
p = str(port)
cluster_info = {}
cluster_url = 'http://'+ ip + ':' + p + '/_cluster/health'
node_url = 'http://'+ ip + ':' + p + '/_cat/nodes?h=http_address,pid'
cmd = "curl -XGET " + "'" + cluster_url +"'" +" 2>/dev/null"
node_cmd = "curl -XGET " + "'" + node_url +"'" +" 2>/dev/null"
#get cluster info
#print "%s"%cmd
(status, result) = commands.getstatusoutput(cmd)
#print " %s " %result
cluster_init = json.loads(result)
#get pid
(status, result) = commands.getstatusoutput(node_cmd)
node_init = result.split("\n")
#print "%s" %node_init
i = 0
ip_port_str = str(ip + ':' + p)
#print "%s" %ip_port_str
while i < len(node_init):
ip_port = node_init[i].split()
#print "%s" %str(ip_port[0])
#print "%s" %ip_port_str
if ( ip_port_str == str(ip_port[0]) ):
cluster_info["FLAGPID"] = ip_port[1]
break
else :
cluster_info["FLAGPID"] =''
i = i + 1
# get es cluster time
pid = cluster_info["FLAGPID"]
#print "%s"%pid
etime_cmd = "ps -eo pid,etime| grep " + pid + "| awk '{if($1=='" +pid + "') $1=\"\";print $0}'"
starttime_cmd = "ps -eo pid,lstart| grep " + pid + "| awk '{if($1=='" + pid + "') $1=\"\";print $0}'"
(status, result) = commands.getstatusoutput(etime_cmd)
cluster_info["etime"] = result
(status, result) = commands.getstatusoutput(starttime_cmd)
cluster_info["starttime"] = result
cluster_info["cpname"] = "elasticsearch_cluster"
cluster_info["cluster_name"] = cluster_init["cluster_name"]
cluster_info["CLUSTER_HEALTH"] = cluster_init["status"]
cluster_info["number_of_nodes"] = cluster_init["number_of_nodes"]
cluster_info["unassigned_shards"] = cluster_init["unassigned_shards"]
cluster_info["number_of_pending_tasks"] = cluster_init["number_of_pending_tasks"]
cluster_info["number_of_in_flight_fetch"] = cluster_init["number_of_in_flight_fetch"]
cluster_info["active_primary_shards"] = cluster_init["active_primary_shards"]
cluster_info["task_max_waiting_in_queue_millis"] = cluster_init["task_max_waiting_in_queue_millis"]
cluster_info["active_shards_percent_as_number"] = cluster_init["active_shards_percent_as_number"]
cluster_info["active_shards"] = cluster_init["active_shards"]
cluster_info["initializing_shards"] = cluster_init["initializing_shards"]
jsonStr = json.dumps(cluster_info)
print "%s" % jsonStr
if ( len(jsonStr) > 0 ) :
return 1
else :
return 0
if __name__ == '__main__':
arginfo = get_arginfo(sys.argv[1:])
#hostinfo = arginfo["-I"]
local_ip_cmd = "ip addr | grep 'state UP' -A2 | head -n3|tail -n1 | awk '{print $2}' | cut -f1 -d '/'"
#local_ip_cmd = "ifconfig 2>/dev/null|sed -n 2p|awk '{ print $2 }'|awk -F : '{ print $2 }'"
(status, result) = commands.getstatusoutput(local_ip_cmd)
local_ip = result
port_list = []
ip = arginfo["-f"].split(",")
for ip_info in ip :
remode_ip = ip_info.split(":")[0]
remode_port = ip_info.split(":")[1]
if ( local_ip == remode_ip ) :
port_list.append(remode_port)
for port in port_list:
#print "%s" %port
#print "%s" %local_ip
flag = getcluster_esinfo(local_ip,port)
if ( flag == 1 ):
break