readnodeID(){
    url=$(echo "$ip:9200/nginx_status/_search?_source=node_id,active_conn&pretty")
	
	echo $url
	
	curl -u elastic:$pwd -X POST $url -H 'Content-Type: application/json' -d'
	{
	  "from": 0,
	  "size": 2000,
	  "sort": [
		{
		  "@timestamp": {
			"order": "desc"
		  }
		}
	  ]
	}' >> ./node
}


sortId(){
	cat ./node | grep node_id | sort | uniq |awk {'print $3'} | sed 's/\"//g' >id.txt
	cat id.txt
}



getnode(){
	python3 log.py info.txt | awk {'print $2'}  | sort | uniq  >node_ip.txt
	
	nmap -sS -Pn -n -p 5000 -iL node_ip.txt --open | grep report | awk {'print $5'} >5000.txt
	
	python3 log.py info.txt 5000.txt | awk {'print $3 " " $4'} | sort | uniq
}

forIDToIP(){

	while IFS= read -r line; do
	  echo "get id $line "

	  curl -u elastic:$pwd -X POST "$ip:9200/http_access/_search?_source=host,nid&pretty" -H 'Content-Type: application/json' -d"
		{
		  \"from\": 0,
		  \"size\": 20,
		  \"query\": {
			\"query_string\": {
			  \"query\": \"host:*.*.*.* AND nid:$line\"  
			}
		  }
		}" >>info.txt
		
	done < id.txt
}


ip=$2
pwd=$1

if [ $3 -eq 1 ]; then
   readnodeID
elif [ $3 -eq 2 ]; then
   sortId
elif [ $3 -eq 3 ]; then
   forIDToIP
elif [ $3 -eq 4 ]; then
   getnode
fi
