You are not logged in.
hello everyone!I have met a question.I want to edit a script through zabbix api to list all the host?
Seek a python script through zabbix api to list host?
Last edited by Knight (2016-02-27 03:07:02)
Offline
no one know that?
Offline
Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD
Making lemonade from lemons since 2015.
Offline
There are a few python libraries that implement the API:
https://www.zabbix.org/wiki/Docs/api/libraries#Python
pyzabbix even has an example that does pretty much what you want:
https://github.com/lukecyca/pyzabbix/wiki
from pyzabbix import ZabbixAPI
from pprint import pprint
zapi = ZabbixAPI("http://zabbixserver.example.com")
zapi.login("zabbix user", "zabbix pass")
print "Connected to Zabbix API Version %s" % zapi.api_version()
for h in zapi.host.get(output="extend"):
pprint(h)
Last edited by progandy (2016-02-27 14:10:22)
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Offline
There are a few python libraries that implement the API:
https://www.zabbix.org/wiki/Docs/api/libraries#Pythonpyzabbix even has an example that does pretty much what you want:
https://github.com/lukecyca/pyzabbix/wikifrom pyzabbix import ZabbixAPI from pprint import pprint zapi = ZabbixAPI("http://zabbixserver.example.com") zapi.login("zabbix user", "zabbix pass") print "Connected to Zabbix API Version %s" % zapi.api_version() for h in zapi.host.get(output="extend"): pprint(h)
thank you I have solved it,below is my code:
#coding:utf-8
import json
import urllib2
url = "http://zabbix.knight.ren/api_jsonrpc.php"
header = {"Content-Type": "application/json"}
# request json
data = json.dumps(
{
"jsonrpc":"2.0",
"method":"host.get",
"params":{
"output":["hostid","name"],
"filter":{"host":""}
},
"auth":"8c67fd696eb3a6a877569b9bc34d6c22",
"id":1,
})
# create request object
request = urllib2.Request(url,data)
for key in header:
request.add_header(key,header[key])
# get host list
try:
result = urllib2.urlopen(request)
except URLError as e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server could not fulfill the request.'
print 'Error code: ', e.code
else:
response = json.loads(result.read())
result.close()
print "Number Of Hosts: ", len(response['result'])
for host in response['result']:
print "Host ID:",host['hostid'],"Host Name:",host['name']
Offline