Zabbix Api的使用
zabbix官网文档:https://www.zabbix.com/documentation/2.2/manual/api,
Zabbix API是基于JSON-RPC 2.0规格,具体实现可以选择任何自己爱好的编程语言,可以采用Perl、Ruby、PHP之类的。
本文已python为例。python zabbix api模块较多,使用较为方便。
下面是各个语言zabbix模块及github连接,可共参考。
数据流程下面的流程图代表了Zabbix API 工作的典型工作流。验证(方法user.login)是获取验证ID的强制步骤。这个ID又允许我们调用API提供的任何权限允许的方法来进行操作。在之前的例子中没有提到user.logout方法,这也是一次验证ID能够重复使用的原因所在。使用user.logout方法后将会使验证ID失效,后面的操作将不能再使用此ID。
Pythonpy-zabbixby Alexey Dubkov- Zabbix Modules for Python (PyPIpy-zabbix, no python3)
ZabbixPythonApiby Frank Yao- Zabbix API for Python (no python3)
zabbixby gescheit- a Python library (PyPIzabbix-api)
PyZabbixby Luke Cyca- a Python module (PyPIpyzabbix, depends-on requests)
zabbix_apiby Grigoriy Netsman- scripts for creating and deleting hosts (depends on zabbix-api)
zabbix-clientby Jesús Losada- a Python library (PyPIzabbix-client)
zabbix-api-erigonesby Erigones- a Python library (PyPIzabbix-api-erigones)
pyZabbixSenderby Kurt Momberg- a zabbix_sender replacement for Python.
Zabbix APIby nelsonab(latest code seems to be ongithub) - a Ruby wrapper
Rubixby Dhruv Bansal- a Ruby library for working with the API and both retrieving and sending data to Zabbix server
zabbixapiby Express 42- a Ruby gem, see README ongithub
zabbyby Farzad Farid- a Ruby library and client for Zabbix
PerlZabbix-APIby SFR-ZABBIX- Perl distribution to access the Zabbix API
ZabbixAPIby Tomohiro Ikeda- a Perl library
Zabipiby Andrey Konovalov- Monitoring::Zabipi module that lets you use official Zabbix API documentation to create Perl applications interacting with Zabbix. Contains additional methods (such as queue.get) and hacks (such as expandNames parameter for item.get). Many examples of usage included in distributive.
Net-Zabbixby ksyz- Perl wrapper for Zabbix API
Zabbix-API-Clientby Matsumoto Ryosuke- Zabbix API client for Perl
Javazabbix-apiby hengyunabc- Java library to access Zabbix API
zabbix-senderby hengyunabc- Java library to use Zabbix sender protocol
PHPPhpZabbixApiby confirm IT solutions GmbH- a PHP wrapper class and a wrapper code generator
microzabbixapiconnectorby Alex Kashin- a Micro-Zabbix-Api-Connector with proxy usage support
PowerShellZabbixPosh Apiby simsaull- A Zabbix PowerShell Module
Zabbixby Benjamin RIOUAL- An other Zabbix module, based on Invoke-RestMethod
JavaScriptjqzabbixby Kodai Terashima- jQuery plugin for Zabbix API
zabbix.jsby Kristoffer Berdal- a library based around request.js
C#c# api libraryby cheezus- a C# library for .NET 2.0
C# Libraryby HenriqueCaires- a C# library for .NET 4.5
Gozabbixby Ryan Day- Zabbix API for Go
go-zabbix"by Alexey Dubkov" - Zabbix Packages for Go
zabbix-senderAlexey Palazhchenko- push data to Zabbix server's trapper items from Go application
zabbix.goAlexey Palazhchenko- Zabbix API for Go
目前我们使用pyzabbix模块,用json定义template文件。
下文讲解用法(api 参考官网手册):
#!/usr/bin/envpython#jiayun#version1.3frompyzabbiximportZabbixAPIimportjsonimportos,sysimportre,timeimportloggingrule=json.load(file('D:\pycharm\project\REGIONManageScript\qn_rolerule.json'))#template文件deflogin():zapi=ZabbixAPI("http://10.4.0.247")#登录zabbixzapi.login("admin","zabbix")returnzapidefget_hostgroups(group_name):returnzapi.hostgroup.get(search={"name":group_name},output="extend")#搜索输入的组别,提取组iddefget_hosts(groupid):groupids=[groupid]returnzapi.host.get(groupids=groupids,output="extend")#返回该组id下的所有host信息defget_drules():returnzapi.drule.get(output="extend")defget_templates_by_names(template_names):returnzapi.template.get(filter={"host":template_names})defcreate_group(group_name):#创建组ifnotzapi.hostgroup.exists(name=group_name):zapi.hostgroup.create(name=group_name)defcreate_host(group_name,host_name,ip):#创建主机并附加指定模板groups=get_hostgroups(group_name)host_name=host_name.lower()ip_tail=ip.split(".")[-1]domain="server-"+ip_tail+".0."+host_name+".ustack.in"forhostgroupingroups:groupid=hostgroup['groupid']ip_tail=ip.split(".")[-1]role=Noneforruinrule:range=rule[ru]['range']if"-"inrange:head=range.split("-")[0]tail=range.split("-")[1]ifint(ip_tail)<=int(tail)andint(ip_tail)>=int(head):role=ruelse:ifip_tail==range:role=rutemplate_names=rule[role]['templates']template_ids=get_templates_by_names(template_names)printdomain,ip,groupid,template_idszapi.host.create(host=domain,interfaces=[{"type":1,"main":1,"useip":1,"ip":ip,"dns":"","port":'10050'}],groups=[{"groupid":groupid}],templates=template_ids)print"AddSuccessfull!!!!!"#logging.info("%s,%s,%s,%sAddSuccessfull!!!!!"%(domain,ip,groupid,template_ids))defcreate_macro(group_name,traffic,value):#创建macro,不同主机有不同的macrogroups=get_hostgroups(group_name)forgroupingroups:hosts=get_hosts(group['groupid'])forhostinhosts:hostname=host["name"]hostid=host["hostid"]ifnotre.search("^server",hostname):continuem=re.search("[0-9]+",hostname).group()ifm=="1":continueifmin['64','65','66','67']:zapi.host.update(hostid=hostid,macros=[{"macro":"{$INP}","value":"35000"},{"macro":"{$OUP}","value":"35000"},{"macro":"{$INT}","value":"%s"%traffic},{"macro":"{$OUT}","value":"%s"%traffic},{"macro":"{$PDISK}","value":"%s"%value}])else:zapi.host.update(hostid=hostid,macros=[{"macro":"{$PDISK}","value":"%s"%value}])printhostname,hostid,m,traffic,valueif__name__=="__main__":zapi=login()region="qn"host_list=["31","32","35","36","39","40","44","45","46","47","48","49","50","53","54","61","62","63","64","65","68","69","70","71","72","73","74","75","76","77","79","80","81","82","83","84","85","86","87","88","89","90","91"]#添加主机,不建议用discoveryip_list=host_listiftype(ip_list)==str:print"%sMustbealist,pleasechecking!!!"%sys.argv[2]sys.exit()group_name="Region[%s0]"%region.upper()ifnotzapi.hostgroup.exists(name=group_name):create_group(group_name)ip={"qn":"10.4.0."}ifregioninip:fornuminip_list:value="20"traffic="300M"ipaddress=ip[region]+str(num)printgroup_name,region,ipaddresscreate_host(group_name,region,ipaddress)#传参至函数time.sleep(5)create_macro(group_name,traffic,value)else:print"youinputregionerror,pleasechecking"
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。