15. Get the IP list from the Zabbix database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.