Zabbix Two development of synchronous CMDB host and template bindings

Source: Internet
Author: User

Recently in doing Zabbix two development; find Zabbix in the automation to provide a strong support, below to share the CMDB and Zabbix to get through, the CMDB platform inside all the machines synchronized to the Zabbix database, and then the batch template binding:

Development environment:

zabbix:2.4

Python:flask framework. Front-end Bootstrap,jquey:


Implementation ideas:

One

Create a third managed table (defined as a cache table) in the CMDB table and the Zabbix host table, use the Zabbix API to insert the host in the database into the third cache table, and then insert the data that is not in the cache table into the cache tables through the comparison with the CMDB library. Then synchronize to the Zabbix database.

Two

The binding template uses the Host.update API, but the API will delete the already bound template after binding the template, so here we go through all the previous templates and then bind the front and back together.


Models structure of the cache table:

Class Zbhost (db. Model): __tablename__ = ' zbhost ' id = db. Column (db. Integer, primary_key=true) Cmdb_hostid = db. Column (db. Integer, index=true, unique = True) HostID = db. Column (db. Integer, index=true, unique = True) host = db. Column (db. String ()) IP = db. Column (db. String (32))

Get Data:

Def init_cmdb ():     #取host   (in server table)     hosts =  Api_action ("Server.get")     for h in hosts:         data = {' Cmdb_hostid ': h[' id ']}         Db.session.query (Zbhost). filter_by (ip=h[' inner_ip ']). Update (data)     db.session.commit ()          #更新到cache表,  ip    def init_ Zabbix ():     #第一步   Remove all host, ip,host,id    zb_hosts =   zabbix_server.get_hosts ()     zb_hosts_interface = zabbix_server.get_ Interface ([z[' HostID '] for z in zb_hosts]) #    data = []     for h in zb_hosts:        h[' IP '] = zb_hosts_interface[h[' HostID '] #       data.append (h)     ## #数据插入数据库         db.session.add (Zbhost (**h))     db.session.commit ()


Sync host to Zabbix:


@main. Route ("/resource/monitor/ajax/sync_host_to_zabbix",  methods=[' POST ') Def sync_host_to_zabbix () :     #接收参数      #hostid  groupid    if  request.method ==  "POST":         from app.common.zabbix  import create_zabbix_host        params = dict ( Request.Form)         hostids = params[' hostids '][0].split (', ')         ret = create_zabbix_host (hostids=params[' hostids ') [0].split (', '), groupid=params[' GroupID '][0])         if len (ret)  == len (hostids):             return   ' 1 '         else:              Return json.dumps (ret)     return  "500" 


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/8C/wKioL1bqe-qQ6vR2AAAqSuGlM8k873.png "title=" Sync.png "alt=" Wkiol1bqe-qq6vr2aaaqsuglm8k873.png "/>

Zabbix Interface:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7D/8C/wKioL1bqfCywW0ePAAC9f6m3iSg482.png "title=" Hostlist.png "alt=" Wkiol1bqfcyww0epaac9f6m3isg482.png "/>


To this point the synchronization host is complete; The following batch binding template:

Def link_template (Self, hostid, templateids):         Templates = []        for id in templateids:             templates.append ({"TemplateID":  ID})             print templates             print hostid         try:            ret =  Self.zb.host.update (hostid=hostid, templates=templates)              return ret        except exception  as e:            return e.message   View:  @main. RouTe ("/monitor/ajax/link_zabbix_template",  methods=[' POST ']) def link_zabbix_template ():     from app.common.zabbix import link_template    if  request.method ==  "POST":         #1, get front-end data          params = dict (Request.Form)          #{' hostids ':  [u ' 10106 '],  ' template_ids ':  [u ' 10001 ']}         hostids = params[' Hostids '][0].split (', ')          template_ids = params[' Template_ids '][0].split (', ')          ret_data = link_template (Hostids, template_ids)          error = none        for r in ret_data:             try:                 hostids.remove (r[' Hostids '][0])              except Exception, e:                 error = e.message         if not hostids:             return  "1"         else:             return error    return  "500"


Host binding Template:

650) this.width=650; "style=" width:904px;height:261px;float:left; "src=" http://s1.51cto.com/wyfs02/M02/7D/8C/ Wkiol1bqfumc2u1aaabfxzgf36w607.png "title=" Bangding.png "height=" 261 "hspace=" 0 "border=" 0 "vspace=" 0 "width=" 904 " alt= "Wkiol1bqfumc2u1aaabfxzgf36w607.png"/>



After binding:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/8F/wKiom1bqfqHxuYrpAAAlFDe3dGA172.png "title=" After binding. png "alt=" Wkiom1bqfqhxuyrpaaalfde3dga172.png "/>


Login Zabbix View:


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/8C/wKioL1bqf6ewS8fZAADKvWPk_IM045.png "title=" Zabbix _ Bind. png "alt=" Wkiol1bqf6ews8fzaadkvwpk_im045.png "/>


To this Zabbix basic synchronization host, the binding template has been completed, Zabbix provides a very good API, we can use to achieve a lot of the functions we need, the above steps have basically completed our daily needs of the function, of course, the removal of the template to remove the host, add maintenance cycle, etc.;

This article is from the "Little Luo" blog, please be sure to keep this source http://xiaoluoge.blog.51cto.com/9141967/1752312

Zabbix Two development of synchronous CMDB host and template bindings

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.