#!/usr/bin/env python
#coding:utf8
import requests
import json
headers = {'Content-Type': 'application/json-rpc'}
url = "http://127.0.0.1:81/api_jsonrpc.php"
def getToken(username,passwd):
data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": username,
"password": passwd
},
"id": 0
}
request = requests.post(url=url,headers=headers,data=json.dumps(data))
dict = json.loads(request.text)
return dict['result']
def getHosts(token_num):
data = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 2,
"auth": token_num,
}
request = requests.post(url=url,headers=headers,data=json.dumps(data))
dict = json.loads(request.content)
#print dict['result']
return dict['result']
def get_Non_Item(data,token_num):
dict = {}
list = data
for i in list:
host_id = i['hostid']
data = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ["itemid","key_","state","hostid"],
"hostids": host_id,
"search": {
"key_": "docker.["
},
"sortfield": "name"
},
"auth": token_num,
"id": 1
}
request = requests.post(url=url,headers=headers,data=json.dumps(data))
dict = json.loads(request.content)
#print dict['result']
#return dict['result']
for i in dict['result']:
print i['key_'],i['state'],i['itemid'],i['hostid']
def getProc(data):
dict = {}
list = data
for i in list:
host = i['host']
inter = i['interfaces']
for j in inter:
ip = j['ip']
dict[host] = ip
return dict
def getData(dict):
data = dict
ip_list = [ ]
for key in data.keys():
ip = data[key]
ip_list.append(ip)
ip_list = list(set(ip_list))
ip_list.sort()
return ip_list
def getGroup(ip_list):
ip_group = { }
ips = ip_list
for i in ips:
print i
if __name__ == "__main__":
username = 'Admin'
passwd = 'zabbix'
token_num = getToken(username,passwd)
data = getHosts(token_num)
item_data = get_Non_Item(data,token_num)
#hosts = getProc(data)
#ip_list = getData(hosts)
#getGroup(ip_list)