How to get the IP and hostname of all hosts on zabbix
This article is about how to get the IP and hostname of all hosts on zabbix. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
zabbix
zabbix ([`zæbiks]) is an enterprise-class open source solution for distributed system monitoring and network monitoring based on a web-based interface.
zabbix monitors various network parameters to ensure the safe operation of server systems; and provides flexible notification mechanisms to allow system administrators to quickly locate/resolve existing problems.
zabbix consists of two parts, zabbix server and optional zabbix agent.
zabbix server can provide remote server/network status monitoring, data collection and other functions through SNMP, zabbix agent, ping, port monitoring and other methods. It can run on Linux, Solaris, HP-UX, AIX, Free BSD, Open BSD, OS X and other platforms.
The following code introduces you to get the IP and hostname of all hosts on zabbix. The code is as follows:
#coding: utf-8#Get IP and hostname of all hosts on zabbix import requestsimport jsonimport csvimport timedef get_token(): data = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": username, "password": password }, "id": 0 } r = requests.get(zaurl, headers=header, data=json.dumps(data)) auth = json.loads(r.text) return auth["result"]def getHosts(token): data = { "jsonrpc": "2.0", "method": "host.get", "params": { "output": [ "hostid", "host" ], "selectInterfaces": [ "interfaceid", "ip" ] }, "id": 2, "auth": token, } request = requests.post(zaurl, headers=header, data=json.dumps(data)) dict = json.loads(request.content)# print (dict['result']) return dict['result']if __name__ == "__main__": zaurl="http://xx.xx.xx.xx/zabbix/api_jsonrpc.php" header = {"Content-Type": "application/json"} username = "xx" password = "xx" token = get_token() hostlist = getHosts(token) datafile = "zabbix.txt" fdata = open(datafile,'w') for i in hostlist: hostid = i['hostid'] hostip = i['host'] fdata.write(hostep + ' ' + hosted + '\n') fdata.close() Thank you for reading! About "how to get the IP and host name of all hosts on zabbix" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!