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

[经验分享] Zabbix Api的使用

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-30 10:13:15 | 显示全部楼层 |阅读模式
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。
Python
  • 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.



Ruby
  • 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

Perl
  • 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

Java
  • zabbix-api by hengyunabc - Java library to access Zabbix API
  • zabbix-sender by hengyunabc - Java library to use Zabbix sender protocol

PHP
  • 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

PowerShell
  • ZabbixPosh Api by simsaull - A Zabbix PowerShell Module
  • Zabbix by Benjamin RIOUAL - An other Zabbix module, based on Invoke-RestMethod

JavaScript
  • jqzabbix by Kodai Terashima - jQuery plugin for Zabbix API
  • zabbix.js by Kristoffer Berdal - a library based around request.js

C#Go
  • 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 参考官网手册):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/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):                                                         #创建组
    if not zapi.hostgroup.exists(name=group_name):
        zapi.hostgroup.create(name=group_name)
def create_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"
    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):                                           #创建macro,不同主机有不同的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"]       #添加主机,不建议用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)                                  #传参至函数
            time.sleep(5)
            create_macro(group_name,traffic,value)
    else:
        print "you input region error,please checking"






运维网声明 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-145255-1-1.html 上篇帖子: Zabbix 实现微信报警 下篇帖子: zabbix通过fping监控ip地址
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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