zabbix是一款非常主流监控软件,以简单易用为人称道。zabbix拥有完善的api方便大家通过第三方调用,所以今天介绍一下如何利用python完成对zabbix api调用。

   zabbix api主要通过http协议进行通讯,这里我们使用数据是json格式数据进行交互。

   这里先放一个官方文档的传送门方便大家翻阅


介绍

zabbix api的地址是“/api_jsonrpc.php”这里先用linux系统命令做个演示:如何获取zabbix的登录token,下面返回的结果是一个json对应的result是结果,也就是你想要得到的token


[root@salt-node1 zabbix]# curl -XPOST http://192.168.198.116/api_jsonrpc.php  -H 'Content-Type:application/json' -d '{
>     "jsonrpc": "2.0",
>     "method": "user.login",
>     "params": {
>         "user": "admin",
>         "password": "zabbix"
>     },
>     "id": 0
> }'
{"jsonrpc":"2.0","result":"f2e8bbaf7e5290d51914a78a0328f19e","id":0}


看上去只是一个post的http请求那我们就用python来搞一下吧

首先我们选的是urllib2模块,之所以用这个是因为python本身自带此模块增加系统的兼容性

[root@salt-node1 tmp]# python zabbix.py 
f037e64b7018fe987c3b1d3e1d717ecb
[root@salt-node1 tmp]# cat zabbix.py
#coding:utf-8
import json
import urllib2
import logging
logging.basicConfig(filename='./my_log_test.log',format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',level=logging.INFO)

zbx_url = 'http://192.168.199.224:88/api_jsonrpc.php'
zabbix_user = 'admin'
zabbix_pwd = 'dianjoy.com'

def get_token():
    url = zbx_url
    #这里定义一个header的字典,方便填写全部的http头信息
    header = {"Content-Type": "application/json"}
    # 这里是一个需要发送的json数据
    data = '''{
            "jsonrpc": "2.0",
            "method": "user.login",
            "params": {
                "user": "%s",
                "password": "%s"
            },
            "id": 0
        }''' % (zabbix_user,zabbix_pwd)
    # 创建一个请求的对象
    request = urllib2.Request(url,data)
    #遍历获取http头信息
    for key in header:
        request.add_header(key,header[key])
    #发送请求,最终返回token
    try:
        result = urllib2.urlopen(request)
    except urllib2.URLError as e:
        print "Auth Failed, Please Check Your Name And Password:",e.code
    else:
        response = json.loads(result.read())
        result.close()
        return response['result']
        
print get_zbx_token()


现在你已经获取到zabbix的token了就可以做一些别的事情了,

这里大家可以尝试获取一下主机信息

例子:

这是一个curl完成和获取主机信息的操作

[root@salt-node1 tmp]#  curl -k  -H 'Content-Type: application/json' http://192.168.198.116/api_jsonrpc.php -d '{
>         "jsonrpc": "2.0",
>         "method": "host.get",
>         "params": {
>         "output": "extend",
>         "filter": {
>             "name": [
>                 "Zabbix server"
>             ]
>         }
>         },
>         "auth": "d6e4eb7e6bd884fec2ccffe4205d5960",
>         "id": 1 }'
{"jsonrpc":"2.0","result":[{"hostid":"10084","proxy_hostid":"0","host":"Zabbix server","status":"0","disable_until":"0","error":"","available":"1","errors_from":"0","lastaccess":"0","ipmi_authtype":"-1","ipmi_privilege":"2","ipmi_username":"","ipmi_password":"","ipmi_disable_until":"0","ipmi_available":"0","snmp_disable_until":"0","snmp_available":"0","maintenanceid":"0","maintenance_status":"0","maintenance_type":"0","maintenance_from":"0","ipmi_errors_from":"0","snmp_errors_from":"0","ipmi_error":"","snmp_error":"","jmx_disable_until":"0","jmx_available":"0","jmx_errors_from":"0","jmx_error":"","name":"Zabbix server","flags":"0","templateid":"0","description":"","tls_connect":"1","tls_accept":"1","tls_issuer":"","tls_subject":"","tls_psk_identity":"","tls_psk":""}],"id":1}


现在用python完成上面操作,因为zabbix api请求的json内主要三部分:操作zabbix方法,params,token会产生变化,所有这里构建了一个方法,只需要传入这三个参数即可

def zbx_req(zbx_action, zbx_params, zbx_token):
    ''' get host info '''
    header = {"Content-Type": "application/json"}
    
    #zabbix server的url地址
    url = zbx_url
    
    #构建请求的json数据
    data='''{
    "jsonrpc": "2.0",
    "method": "%s",
    "params": %s,
    "auth": "%s",
    "id": 1 }''' % (zbx_action, zbx_params, zbx_token)

    request = urllib2.Request(url, data)
    for key in header:
        request.add_header(key, header[key])
    # 请求zabbix获取返回结果
    try:
        result = urllib2.urlopen(request)
    except urllib2.URLError as e:
        print "request Failed:", e.code
    else:
        #因为各种应用场景结果可能不通做了如下判断
        #如果返回的json里面有error这个key代表请求失败,输出错误信息并返回False
        #如果返回json没有result这个key代表结果也不是预期
        #其它的直接返回json的result key的内容
        response = json.loads(result.read())
        if 'error' in response:
            print response['error']
            return False
        elif not response['result']:
            print response
            return False
        else:
            return response['result']
        result.close()

#方法创建好了,这里把参数传入调用下下就好
zbx_token = get_zbx_token()
zbx_action = 'host.get'
zbx_params = '''{
    "output": "extend",
    "filter": {
    "name": ["Zabbix server"]
    }
    }'''
print zbx_req(zbx_action,zbx_params,zbx_token)


脚本写执行下脚本试试把


[root@salt-node1 tmp]# python zabbix.py 
[{u'available': u'1', u'tls_connect': u'1', u'maintenance_type': u'0', u'ipmi_errors_from': u'0', u'ipmi_username': u'', u'snmp_disable_until': u'0', u'ipmi_authtype': u'-1', u'ipmi_disable_until': u'0', u'lastaccess': u'0', u'snmp_error': u'', u'tls_psk': u'', u'ipmi_privilege': u'2', u'jmx_error': u'', u'jmx_available': u'0', u'maintenanceid': u'0', u'snmp_available': u'0', u'tls_psk_identity': u'', u'status': u'0', u'description': u'', u'tls_accept': u'1', u'host': u'Zabbix server', u'disable_until': u'0', u'ipmi_password': u'', u'templateid': u'0', u'tls_issuer': u'', u'ipmi_available': u'0', u'maintenance_status': u'0', u'snmp_errors_from': u'0', u'ipmi_error': u'', u'proxy_hostid': u'0', u'hostid': u'10084', u'name': u'Zabbix server', u'jmx_errors_from': u'0', u'jmx_disable_until': u'0', u'flags': u'0', u'error': u'', u'maintenance_from': u'0', u'tls_subject': u'', u'errors_from': u'0'}]



拓展1

相信大家学到这里还不满足,懒是永无止境的,我根据自己情况继续把用法继续封装,根据自己的需求封装一个创建主机的方法

class Zbx_api(object):
    def zbx_create_host(self,hostname,inter_ip,group_id,temlpate_id):
        self.hostname = hostname
        self.inter_ip = inter_ip
        self.group_id = group_id
        self.temlpate_id = temlpate_id
        '''创建主机
            hostname :主机名
            inter_ip :主机IP
            gourp_id :组id
            template_id : 监控模版id
            zbx_create_host('salt-node1','192.168.198.116','4','10001')
        '''
        zbx_action = 'host.create'
        zbx_params = '''{
            "host": "%s",
            "interfaces": [
                {
                    "type": 1,
                    "main": 1,
                    "useip": 1,
                    "ip": "%s",
                    "dns": "",
                    "port": "10050"
                }
            ],
            "groups": [
                {
                    "groupid": "%s"
                }
            ],
            "templates": [
                {
                    "templateid": "%s"
                }
            ],
            "inventory_mode": 0,
            "inventory": {
                "macaddress_a": "nginxs.net",
                "macaddress_b": "nginxs.net"
            }
        }''' % (hostname,inter_ip,group_id,temlpate_id)
        r=Zbx_base_api(zbx_action,zbx_params)
        return r.zbx_req()







拓展2

例如我经常需要把一个group的主机的eth0网卡和CPU负载创建一个screen那我们得用python完成下面几项操作:

1. 创建一个方法获取一个主机组的指定graph id

2. 创建一个方法只要传入screen name,graph id就可以创建一个screen


    def zbx_create_screen(self,screen_name,screen_high,screen_width):
        '''创建screen
        name : SCREEN的名字
        hsize: screen的行数
        vsize: screen的列数
        screenitems: screen里面的item
            resourcetype : 当前数据源的类型"graph","map","url"更多请看传送门https://www.zabbix.com/documentation/2.4/manual/api/reference/screenitem/object#screen_item
            resourceid: itemid
            rowspan: 占据行数
            colspan:占据列数
            x: 屏幕x坐标轴位置
            y:  屏幕y坐标轴位置
         '''
        self.screen_name = screen_name
        self.screen_high = screen_high
        self.screen_width = screen_width
        zbx_action = 'screen.create'
        zbx_params = '''{
        "name": "%s",
        "hsize": %s,
        "vsize": %s,
        "screenitems": [
            {
                "resourcetype": 0,
                "resourceid": "524",
                "rowspan": 1,
                "colspan": 1,
                "x": 0,
                "y": 2
            }
        ]
    }''' % (self.screen_name,self.screen_high,self.screen_width)
        print   zbx_params
        r=Zbx_base_api(zbx_action,zbx_params)
        return zbx_req()

今天不早了先写到这里明天继续