三:(下)之zabbix 功能之API化

阅读(1,668)

一:通过API,实现完全自动化添加删除agent、关联模板等操作:

#官方文档:https://www.zabbix.com/documentation/3.0/manual/api

#API提供两个功能,一个是查询,一个是管理

1.1.提供用户密码密码验证并发送post请求到zabbix server端,获取result返回值token:

curl 命令:
-s: 静默模式
-X:指定操作方法
-H:自定义header内容
-d:指定提交的http 内容

[root@Zabbix tianqi]# curl -s -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "user",
        "password": "password"
    },
    "id": 1
}' http://zabbixhf.chinacloudapp.cn/zabbix/api_jsonrpc.php | python -m json.tool
#以下为zabbix 返回的数据
{
    "id": 1,
    "jsonrpc": "2.0",
    "result": "09238af2adacc132a3ad8e9226b33cac"
}

1.2.使用返回的token从zabbix server获取所有主机列表信息;

[root@Zabbix tianqi]# curl -s -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["host"]
    },
    "auth": "7df71c2fff700290629b53287c9aabf2",
    "id": 1
}' http://zabbixhf.chinacloudapp.cn/zabbix/api_jsonrpc.php | python -m json.tool
以下为返回的部分主机信息:
{
    "id": 1,
    "jsonrpc": "2.0",
    "result": [
        {
            "host": "Zabbix server",
            "hostid": "10084"
        },
        {
            "host": "MongoDB1",
            "hostid": "10105"
        },

    ]
}

1.3.通过filter获取模板的指定信息:

[root@Zabbix tianqi]# curl -s -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "template.get",
    "params": {
        "output": "extend",
        "filter": {
            "host": [
                "Template OS Linux"
            ]
        }
    },
    "auth": "1ea06d6453877704252e480a94b94ca8",
    "id": 1
}'  http://zabbixhf.chinacloudapp.cn/zabbix/api_jsonrpc.php | python -m json.tool
{
    "id": 1,
    "jsonrpc": "2.0",
    "result": [
        {
            "available": "0",
            "description": "",
            "disable_until": "0",
            "error": "",
            "errors_from": "0",
            "flags": "0",
            "host": "Template OS Linux",
            "ipmi_authtype": "0",
            "ipmi_available": "0",
            "ipmi_disable_until": "0",
            "ipmi_error": "",
            "ipmi_errors_from": "0",
            "ipmi_password": "",
            "ipmi_privilege": "2",
            "ipmi_username": "",
            "jmx_available": "0",
            "jmx_disable_until": "0",
            "jmx_error": "",
            "jmx_errors_from": "0",
            "lastaccess": "0",
            "maintenance_from": "0",
            "maintenance_status": "0",
            "maintenance_type": "0",
            "maintenanceid": "0",
            "name": "Template OS Linux",
            "proxy_hostid": "0",
            "snmp_available": "0",
            "snmp_disable_until": "0",
            "snmp_error": "",
            "snmp_errors_from": "0",
            "status": "3",
            "templateid": "10001",
            "tls_accept": "1",
            "tls_connect": "1",
            "tls_issuer": "",
            "tls_psk": "",
            "tls_psk_identity": "",
            "tls_subject": ""
        }
    ]
}

2.通过python脚本进行调用API或token:

yum install python-pip

pip install requests #需要调用requests模块

2.1:获取token的python脚本内容:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import requests
import json

url = 'http://zabbixhf.chinacloudapp.cn/zabbix/api_jsonrpc.php'
post_data = {
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "username",
        "password": "password"
    },
    "id": 1
}
post_header = {'Content-Type': 'application/json'}

ret = requests.post(url, data=json.dumps(post_data), headers=post_header)

zabbix_ret = json.loads(ret.text)
if not zabbix_ret.has_key('result'):
    print 'login error'
else:
    print zabbix_ret.get('result')

2.3:执行结果:

[root@Zabbix tianqi]# python zabbix_auth.py  
a0f7b0452d488745030577fa47a6bf79

3.通过API添加主机:

2.3.1:#原理为在程序里面定义好主机名、IP、添加的组合关联的模板ID,然后同API提交请求添加,实现方式内容如下:

#https://www.zabbix.com/documentation/3.0/manual/api  #官方文档

[root@Zabbix tianqi]# curl -s -X POST -H 'Content-Type:application/json' -d '
> {
>     "jsonrpc": "2.0",
>     "method": "host.create", #定义方法,N多钟
>     "params": {
>         "host": "API Add Host Test", #自定义添加后的agent的名称
>         "interfaces": [
>             {
>                 "type": 1, #类型为1表示agent,2是SNMP,3是IMPI,4是JMX
>                 "main": 1, #more接口
>                 "useip": 1, #0是DNS,1是IP地址
>                 "ip": "192.168.0.24", #添加的zabbix agent的IP地址
>                 "dns": "",
>                 "port": "10050" #agent端口
>             }
>         ],
>         "groups": [
>             {
>                 "groupid": "2" #添加到的组的ID
>             }
>         ],
>         "templates": [
>             {
>                 "templateid": "10001" #关联的模板的ID
>             }
>         ]
>     },
>     "auth": "f1cb833bf46bf2962b181f69b365ac6c", #超时后需要重新获取
>     "id": 1
> }' http://zabbixhf.chinacloudapp.cn/zabbix/api_jsonrpc.php | python -m json.tool:

2.3.2:#执行结果:

 

2.3.3:查看添加完成后的host信息:

2.4:查看是否将主机添加完成并出图:

2.4.1:主机添加完成效果如下:

 2.4.2:查看图形:

 


Warning: count(): Parameter must be an array or an object that implements Countable in /home/nginx/wordpress/wp-includes/class-wp-comment-query.php on line 405

发表评论

邮箱地址不会被公开。 必填项已用*标注