I take the monitoring as the central node, so all IP addresses are extracted from Zabbix.
There are two ways to extract IP from the Zabbix database:
(1) Direct Fuzzy query hosts table:
For example, query the Operations department's Ip:select host from the hosts where name like "op%" order by host;
The complete code is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-import sysimport mysqldbreload (SYS) sys.setdefaultencoding (' Utf-8 ') def get_ IP (): File = open (' op_hosts ', ' W ') file.truncate () con = mysqldb.connect (host= ' localhost ', user= ' Zabbix ', passwd= ' Z Abbix ', db= ' Zabbix ', charset= ' utf8 ') cur = con.cursor () cur.execute (' Select host from the hosts where name like "op%" Orde R by host; ') File = open (' op_hosts ', ' a ') for data in Cur.fetchall (): File.write (data[0] + ' \ n ') if __name__== "__main__": g ET_IP ()
(2) query according to different groups:
Get GroupID from the groups table first
Gets the hostid belonging to the group from the Hosts_groups table
Finally get host and name from the hosts table
The complete code is as follows:
#!/usr/bin/env python# -*- coding: utf-8 -*-import osimport sysimport Mysqldbreload (SYS) sys.setdefaultencoding (' Utf-8 ') def get_ip (): ipdir = ' /root/ip ' for i in os.listdir (ipdir): file = open (Os.path.join (ipdir, i), ' W ') file.truncate () con = mysqldb.connect (host= ' localhost ', user = ' Zabbix ', passwd= ' Zabbix ', db= ' Zabbix ', charset= ' UTF8 ') cur = con.cursor () cur.execute (' Select host, name from hosts where hostid in (select hostid from hosts_groups where groupid in (select groupid from groups where groupid= "8")) order by name; ') file = Open ('/root/ip/linux ', ' a ') for data in cur.fetchall (): file.write (data[0] + ' ' + data[1] + ' \ n ') if __name__== "__main__": get_ip ()
This article is from the "11062687" blog, please be sure to keep this source http://11072687.blog.51cto.com/11062687/1738020
15. Get the IP list from the Zabbix database