您好,登錄后才能下訂單哦!
zabbix官網(wǎng)文檔:https://www.zabbix.com/documentation/2.2/manual/api,
Zabbix API是基于JSON-RPC 2.0規(guī)格,具體實(shí)現(xiàn)可以選擇任何自己愛好的編程語(yǔ)言,可以采用Perl、Ruby、PHP之類的。
本文已python為例。python zabbix api模塊較多,使用較為方便。
下面是各個(gè)語(yǔ)言zabbix模塊及github連接,可共參考。
下面的流程圖代表了Zabbix API 工作的典型工作流。驗(yàn)證(方法user.login)是獲取驗(yàn)證ID的強(qiáng)制步驟。這個(gè)ID又允許我們調(diào)用API提供的任何權(quán)限允許的方法來進(jìn)行操作。在之前的例子中沒有提到user.logout方法,這也是一次驗(yàn)證ID能夠重復(fù)使用的原因所在。使用user.logout方法后將會(huì)使驗(yàn)證ID失效,后面的操作將不能再使用此ID。
py-zabbixby Alexey Dubkov - Zabbix Modules for Python (PyPI py-zabbix, no python3)
ZabbixPythonApiby Frank Yao - Zabbix API for Python (no python3)
zabbixby gescheit - a Python library (PyPI zabbix-api)
PyZabbixby Luke Cyca - a Python module (PyPI pyzabbix, 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 (PyPI zabbix-client)
zabbix-api-erigonesby Erigones - a Python library (PyPI zabbix-api-erigones)
pyZabbixSenderby Kurt Momberg - a zabbix_sender replacement for Python.
Zabbix APIby nelsonab (latest code seems to be on github) - 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 on github
zabbyby Farzad Farid - a Ruby library and client for Zabbix
Zabbix-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
zabbix-api by hengyunabc - Java library to access Zabbix API
zabbix-sender by hengyunabc - Java library to use Zabbix sender protocol
PhpZabbixApi by confirm IT solutions GmbH - a PHP wrapper class and a wrapper code generator
microzabbixapiconnector by Alex Kashin - a Micro-Zabbix-Api-Connector with proxy usage support
ZabbixPosh Api by simsaull - A Zabbix PowerShell Module
Zabbix by Benjamin RIOUAL - An other Zabbix module, based on Invoke-RestMethod
jqzabbix by Kodai Terashima - jQuery plugin for Zabbix API
zabbix.js by Kristoffer Berdal - a library based around request.js
c# api library by cheezus - a C# library for .NET 2.0
C# Library by HenriqueCaires - a C# library for .NET 4.5
zabbixby 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 參考官網(wǎng)手冊(cè)):
#!/usr/bin/env python #jiayun #version 1.3 from pyzabbix import ZabbixAPI import json import os,sys import re,time import logging rule = json.load(file('D:\pycharm\project\REGION Manage Script\qn_rolerule.json')) #template 文件 def login(): zapi= ZabbixAPI("http://10.4.0.247") #登錄zabbix zapi.login("admin","zabbix") return zapi def get_hostgroups(group_name): return zapi.hostgroup.get(search={"name":group_name },output="extend") #搜索輸入的組別,提取組id def get_hosts(groupid): groupids = [groupid] return zapi.host.get(groupids=groupids,output="extend") #返回該組id 下的所有host 信息 def get_drules(): return zapi.drule.get(output="extend") def get_templates_by_names(template_names): return zapi.template.get(filter={"host": template_names}) def create_group(group_name): #創(chuàng)建組 if not zapi.hostgroup.exists(name=group_name): zapi.hostgroup.create(name=group_name) def create_host(group_name,host_name,ip): #創(chuàng)建主機(jī)并附加指定模板 groups=get_hostgroups(group_name) host_name=host_name.lower() ip_tail=ip.split(".")[-1] domain = "server-"+ ip_tail +".0." + host_name + ".ustack.in" for hostgroup in groups: groupid=hostgroup['groupid'] ip_tail=ip.split(".")[-1] role = None for ru in rule: range = rule[ru]['range'] if "-" in range: head = range.split("-")[0] tail = range.split("-")[1] if int(ip_tail) <= int(tail) and int(ip_tail) >=int(head): role = ru else: if ip_tail == range: role = ru template_names = rule[role]['templates'] template_ids = get_templates_by_names(template_names) print domain,ip,groupid,template_ids zapi.host.create(host=domain,interfaces=[{ "type":1, "main":1, "useip":1, "ip":ip, "dns":"", "port":'10050' }],groups=[{"groupid":groupid}],templates=template_ids) print "Add Successfull!!!!!" #logging.info("%s,%s,%s,%s Add Successfull!!!!!"%(domain,ip,groupid,template_ids)) def create_macro(group_name,traffic,value): #創(chuàng)建macro,不同主機(jī)有不同的macro groups=get_hostgroups(group_name) for group in groups: hosts=get_hosts(group['groupid']) for host in hosts: hostname=host["name"] hostid=host["hostid"] if not re.search("^server",hostname):continue m=re.search("[0-9]+",hostname).group() if m == "1":continue if m in ['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}]) print hostname ,hostid,m,traffic,value if __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"] #添加主機(jī),不建議用discovery ip_list=host_list if type(ip_list) == str: print "%s Must be a list,please checking !!!"%sys.argv[2] sys.exit() group_name="Region [%s 0]"% region.upper() if not zapi.hostgroup.exists(name=group_name): create_group(group_name) ip={"qn":"10.4.0."} if region in ip: for num in ip_list: value="20" traffic="300M" ipaddress=ip[region]+str(num) print group_name,region,ipaddress create_host(group_name,region,ipaddress) #傳參至函數(shù) time.sleep(5) create_macro(group_name,traffic,value) else: print "you input region error,please checking"
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。