设为首页 收藏本站
查看: 1440|回复: 0

[经验分享] zabbix批量更新主机关联的模板

[复制链接]

尚未签到

发表于 2019-1-24 13:40:35 | 显示全部楼层 |阅读模式
背景
zabbix的前台页面其实已经功能非常强大了,但特别情况下还是无法满足我们的需求的。
例如同事跑来找我说,想批量对一批主机管理一个新的模板,但这些主机不在同一个主机组,
即没办法利用前台页面的批量更新的。所有我看了下,就只能借助了zabbix api去处理了。
思路
思路其实很简单了:
读取目标文件,遍历所有的主机
查询出当前主机已经关联的模板
对主机进行更新,在原有关联模板的基础上追加新的模块
涉及API
host.get
template.get
host.update
大致就如上三个api就足够完成我们的要求了。
具体实现方法
最近翻书发现一个很不错的python库,pyzabbix,实现的非常巧妙,于是我放弃了自己造轮子的打算。
回头有空和大家一起分享下,这里就不罗嗦了。
工具有了,思路有了,那怕具体实现么?
实现1:
#!/usr/bin/python2.6
# -*- coding: utf-8 -
*-# Last modified: 2014-09-25 20:07
# Author: kevin
# Mail: huangsicong@letv.com
# propose:  批量刷新host关联的模版
import MySQLdb
import sys
import os
import copy
import time
import logging
import logging.config
from pyzabbix import ZabbixAPIfrom configure import *

zabbix_server = "http://125.1*2.*1.*3/zabbix/"
zabbix_user = "zabbix_api_*c"
zabbix_passwd = "m*b*yhPl*9zR"
def connect_zabbix_db_init():
    try:
            conn=MySQLdb.connect(host=host,user=user,passwd=passwd,port=port)
            cur=conn.cursor()
            conn.select_db('zabbix')
            cur.execute('set names utf8')
            return cur
    except MySQLdb.Error,e:
            logging.error("Mysql Error %d: %s" % (e.args[0], e.args[1]))
            def connect_zabbix_db_close(cur):
    cur.close()
def get_host_template(cur,hostid):
    sql = 'select ht.templateid from hosts_templates ht, hosts h where  h.hostid = ht.hostid and h.hostid=%s' %(hostid)
    cur.execute(sql)
    template_list = cur.fetchall()
    return template_list

cur = connect_zabbix_db_init()
zapi = ZabbixAPI(zabbix_server)
zapi.login(zabbix_user,zabbix_passwd)
'''获取需要添加的模版的templateid'''
res = zapi.template.get(filter={'host':'syslog addition_EmbedThunderMan'})#res 返回结果[{u'hostid': u'11661', u'templateid': u'11661'}]
templateid = res[0]['templateid']
templateid = int(templateid)

f = open('host_updata_template.txt')for host in f:
        host = host.strip()
        '''获取host对应的hostid'''
        res = zapi.host.get(filter={'host':host})
        #res 返回'''[{u'hostid': u'18666'}]'''
        hostid = res[0]['hostid']
        '''获取host原有的模版templateid'''
        res = get_host_template(cur,hostid)
        template_old = [ int(m[0]) for m in res]
        print template_old
        template_old_format= [{'templateid':int(m[0])} for m in res]
        if templateid in template_old:
                continue
        template_new = copy.deepcopy(template_old_format)
        template_new.append({'templateid':templateid})
        res = zapi.host.update(hostid=hostid,templates=template_new)
        print res

connect_zabbix_db_close(cur)
#!/usr/bin/python2.6
# -*- coding: utf-8 -*-
# Last modified: 2014-09-25 20:07
# Author: kevin
# Mail: huangsicong@letv.com
# propose:  批量刷新host关联的模版
import MySQLdb
import sys
import os
import copy
import time
import logging
import logging . config
from pyzabbix import ZabbixAPI
from configure import *
zabbix_server = "http://125.1*2.*1.*3/zabbix/"
zabbix_user = "zabbix_api_*c"
zabbix_passwd = "m*b*yhPl*9zR"
def connect_zabbix_db_init ( ) :
try :
conn = MySQLdb . connect ( host = host , user = user , passwd = passwd , port = port )
cur = conn . cursor ( )
conn . select_db ( 'zabbix' )
cur . execute ( 'set names utf8' )
return cur
except MySQLdb . Error , e :
logging . error ( "Mysql Error %d: %s" % ( e . args [ 0 ] , e . args [ 1 ] ) )
def connect_zabbix_db_close ( cur ) :
cur . close ( )
def get_host_template ( cur , hostid ) :
sql = 'select ht.templateid from hosts_templates ht, hosts h where  h.hostid = ht.hostid and h.hostid=%s' % ( hostid )
cur . execute ( sql )
template_list = cur . fetchall ( )
return template_list
cur = connect_zabbix_db_init ( )
zapi = ZabbixAPI ( zabbix_server )
zapi . login ( zabbix_user , zabbix_passwd )
'' '获取需要添加的模版的templateid' ''
res = zapi . template . get ( filter = { 'host' : 'syslog addition_EmbedThunderMan' } )
#res 返回结果[{u'hostid': u'11661', u'templateid': u'11661'}]
templateid = res [ 0 ] [ 'templateid' ]
templateid = int ( templateid )
f = open ( 'host_updata_template.txt' )
for host in f :
host = host . strip ( )
'' '获取host对应的hostid' ''
res = zapi . host . get ( filter = { 'host' : host } )
#res 返回'''[{u'hostid': u'18666'}]'''
hostid = res [ 0 ] [ 'hostid' ]
'' '获取host原有的模版templateid' ''
res = get_host_template ( cur , hostid )
template_old = [ int ( m [ 0 ] ) for m in res ]
print template_old
template_old_format = [ { 'templateid' : int ( m [ 0 ] ) } for m in res ]
if templateid in template_old :
continue
template_new = copy . deepcopy ( template_old_format )
template_new . append ( { 'templateid' : templateid } )
res = zapi . host . update ( hostid = hostid , templates = template_new )
print res
connect_zabbix_db_close ( cur )
实现1主要是当时大致看了下,没有找到获取主机管理的模板的api,所有就写了条
sql去获取的,主机和模板的对应关系其实就是保存在hosts_templates中,知道这个
其实就没有特别要说的了。
实现2是国庆在家整理文档时,又看了下zabbix 的api,发现其实template.get就
足够满足我们的需求了。所有就在原来的基础上写了一个新的版本,具体看代码吧。
实现2:
#!/usr/bin/python2.6# -*- coding: utf-8 -*-# Last modified: 2014-10-05 12:02# Author: kevin# Mail: huangsicong@letv.com# propose:  批量刷新host管理的模版import osimport copy
from pyzabbix import ZabbixAPI

zabbix_server = "http://125.1*2.*1.*3/zabbix/"
zabbix_user = "zabbix_api_*c"
zabbix_passwd = "m*b*yhPl*9zR"

zapi = ZabbixAPI(zabbix_server)
zapi.login(zabbix_user,zabbix_passwd)
'''获取需要添加的模版的templateid'''
res = zapi.template.get(filter={'host':'syslog addition_EmbedThunderMan'})#res 返回结果[{u'hostid': u'11661', u'templateid': u'11661'}]
templateid = res[0]['templateid']
templateid = int(templateid)

f = open('host_updata_template.txt')for host in f:
        host = host.strip()
        '''获取host对应的hostid'''
        res = zapi.host.get(filter={'host':host})
        #res 返回'''[{u'hostid': u'18666'}]'''
        try:
                hostid = res[0]['hostid']
        except:
                continue
        '''获取host原有的模版templateid'''
        res = zapi.template.get(output="hostid",selectParentTemplates="refer",hostids="10017")
        template_old = [ int(m['templateid']) for m in res]
        template_old_format= [{'templateid':int(m['templateid'])} for m in res]
        print '-'*100
        print template_old_format
        if templateid in template_old:
                continue
        template_new = copy.deepcopy(template_old_format)
        template_new.append({'templateid':templateid})
        res = zapi.host.update(hostid=hostid,templates=template_new)
        print template_new
#!/usr/bin/python2.6
# -*- coding: utf-8 -*-
# Last modified: 2014-10-05 12:02
# Author: kevin
# Mail: huangsicong@letv.com
# propose:  批量刷新host管理的模版
import os
import copy
from pyzabbix import ZabbixAPI
zabbix_server = "http://125.1*2.*1.*3/zabbix/"
zabbix_user = "zabbix_api_*c"
zabbix_passwd = "m*b*yhPl*9zR"
zapi = ZabbixAPI ( zabbix_server )
zapi . login ( zabbix_user , zabbix_passwd )
'' '获取需要添加的模版的templateid' ''
res = zapi . template . get ( filter = { 'host' : 'syslog addition_EmbedThunderMan' } )
#res 返回结果[{u'hostid': u'11661', u'templateid': u'11661'}]
templateid = res [ 0 ] [ 'templateid' ]
templateid = int ( templateid )
f = open ( 'host_updata_template.txt' )
for host in f :
host = host . strip ( )
'' '获取host对应的hostid' ''
res = zapi . host . get ( filter = { 'host' : host } )
#res 返回'''[{u'hostid': u'18666'}]'''
try :
hostid = res [ 0 ] [ 'hostid' ]
except :
continue
'' '获取host原有的模版templateid' ''
res = zapi . template . get ( output = "hostid" , selectParentTemplates = "refer" , hostids = "10017" )
template_old = [ int ( m [ 'templateid' ] ) for m in res ]
template_old_format = [ { 'templateid' : int ( m [ 'templateid' ] ) } for m in res ]
print '-' * 100
print template_old_format
if templateid in template_old :
continue
template_new = copy . deepcopy ( template_old_format )
template_new . append ( { 'templateid' : templateid } )
res = zapi . host . update ( hostid = hostid , templates = template_new )
print template_new
看看是不是更简洁了,而且维护性更强了。
运行结果:
----------------------------------------------------------------------------------------------------
[{'templateid': 11661}, {'templateid': 12625}, {'templateid': 15369}, {'templateid': 17510}, {'templateid': 18002}, {'templateid': 61604}]
[{'templateid': 11661}, {'templateid': 12625}, {'templateid': 15369}, {'templateid': 17510}, {'templateid': 18002}, {'templateid': 61604}, {'templateid': 61614}]
----------------------------------------------------------------------------------------------------
[{'templateid': 11661}, {'templateid': 12625}, {'templateid': 15369}, {'templateid': 17510}, {'templateid': 18002}, {'templateid': 61604}]
[{'templateid': 11661}, {'templateid': 12625}, {'templateid': 15369}, {'templateid': 17510}, {'templateid': 18002}, {'templateid': 61604}, {'templateid': 61614}]
----------------------------------------------------------------------------------------------------
[{'templateid': 11661}, {'templateid': 12625}, {'templateid': 15369}, {'templateid': 17510}, {'templateid': 18002}, {'templateid': 61604}]
[{'templateid': 11661}, {'templateid': 12625}, {'templateid': 15369}, {'templateid': 17510}, {'templateid': 18002}, {'templateid': 61604}, {'templateid': 61614}]
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
[ { 'templateid' : 11661 } , { 'templateid' : 12625 } , { 'templateid' : 15369 } , { 'templateid' : 17510 }, { 'templateid' : 18002 } , { 'templateid' : 61604 } ]
[ { 'templateid' : 11661 } , { 'templateid' : 12625 } , { 'templateid' : 15369 } , { 'templateid' : 17510 }, { 'templateid' : 18002 } , { 'templateid' : 61604 } , { 'templateid' : 61614 } ]
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
[ { 'templateid' : 11661 } , { 'templateid' : 12625 } , { 'templateid' : 15369 } , { 'templateid' : 17510 }, { 'templateid' : 18002 } , { 'templateid' : 61604 } ]
[ { 'templateid' : 11661 } , { 'templateid' : 12625 } , { 'templateid' : 15369 } , { 'templateid' : 17510 }, { 'templateid' : 18002 } , { 'templateid' : 61604 } , { 'templateid' : 61614 } ]
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
[ { 'templateid' : 11661 } , { 'templateid' : 12625 } , { 'templateid' : 15369 } , { 'templateid' : 17510 }, { 'templateid' : 18002 } , { 'templateid' : 61604 } ]
[ { 'templateid' : 11661 } , { 'templateid' : 12625 } , { 'templateid' : 15369 } , { 'templateid' : 17510 }, { 'templateid' : 18002 } , { 'templateid' : 61604 } , { 'templateid' : 61614 } ]
  
  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-667073-1-1.html 上篇帖子: Zabbix 微信告警脚本 下篇帖子: zabbix监控H3C交换机
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表