You are not logged in.

#1 2016-02-26 15:38:00

Knight
Member
From: ShenZhen/Asia
Registered: 2015-11-19
Posts: 18

Seek a python script through zabbix api to list all host

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

#2 2016-02-27 12:02:27

Knight
Member
From: ShenZhen/Asia
Registered: 2015-11-19
Posts: 18

Re: Seek a python script through zabbix api to list all host

no one know that?

Offline

#3 2016-02-27 13:06:20

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,835
Website

Re: Seek a python script through zabbix api to list all host


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

#4 2016-02-27 13:53:22

progandy
Member
Registered: 2012-05-17
Posts: 5,190

Re: Seek a python script through zabbix api to list all host

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

#5 2016-02-27 16:03:34

Knight
Member
From: ShenZhen/Asia
Registered: 2015-11-19
Posts: 18

Re: Seek a python script through zabbix api to list all host

progandy wrote:

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)

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

Board footer

Powered by FluxBB