zoukankan      html  css  js  c++  java
  • zabbix-API使用

    zabbix扫描与zabbix-API比较

    zabbix扫描:占用zabbix-server端的大量资源,同时添加主机时只有ip地址,这样分不清是什么业务
    
    zabbix-API:添加方便,通过命令即可添加主机
    

    官方网址

    https://www.zabbix.com/documentation/4.0/zh/manual/api
    

    任意一台能ping通zabbix-server执行

    栗子:( 注意json格式 )
    查询用户信息

    curl -s -X POST -H 'Content-Type:application/json' -d '
    {
        "jsonrpc": "2.0",
        "method": "user.login",
        "params": {
            "user": "Admin",
            "password": "zabbix"
        },
        "id": 1
    }' http://172.31.1.20/zabbix/api_jsonrpc.php | python3 -m json.tool
    

    上面的命令获取出来的token需要记录起来,后面要用到

    35755d0fba51a9eb2851c852ba62af5b

    get用户信息

    curl -s -X POST -H 'Content-Type:application/json' -d '
    {
        "jsonrpc": "2.0",
        "method": "user.get",
        "params": {
            "output": "extend"
        },
        "auth": "35755d0fba51a9eb2851c852ba62af5b",
        "id": 1
    }' http://172.31.1.20/zabbix/api_jsonrpc.php | python3 -m json.tool
    

    获取单个模板信息

    curl -s -X POST -H 'Content-Type:application/json' -d '
    {
        "jsonrpc": "2.0",
        "method": "template.get",
        "params": {
            "output": "extend",
            "filter": {
                "host": [
                    "NGINX_Check_Statuc"
                ]
            }
        },
        "auth": "35755d0fba51a9eb2851c852ba62af5b",
        "id": 1
    }' http://172.31.1.20/zabbix/api_jsonrpc.php | python3 -m json.tool
    

    获取多个模板信息

    curl -s -X POST -H 'Content-Type:application/json' -d '
    {
        "jsonrpc": "2.0",
        "method": "template.get",
        "params": {
            "output": "extend",
            "filter": {
                 "host": [
                    "tomcat-template-m44",
                    "mysql-m44"
                ]
            }
        },
        "auth": "35755d0fba51a9eb2851c852ba62af5b",
        "id": 1
    }' http://172.31.1.20/zabbix/api_jsonrpc.php | python3 -m json.tool
    

    如果需要获取template和组还有代理的id

    组id

    template-id

    代理id

    通过脚本调用API快速添加:

    root@zabbix-server:~# cat zabbix-add-host.sh
    #!/bin/bash
    IP="
    172.31.10.105
    172.31.10.106
    172.31.10.107
    172.31.10.108
    "
    for node_ip in ${IP};do
    curl -s -X POST -H 'Content-Type:application/json' -d '
    {
        "jsonrpc": "2.0",
        "method": "host.create",
        "params": {
            "host": "'${node_ip}'",
            "name": "m44-tomcat_'${node_ip}'",
            "proxy_hostid": "10275",
            "interfaces": [
                {
                    "type": 1,
                    "main": 1,
                    "useip": 1,
                    "ip": "'${node_ip}'",
                    "dns": "",
                    "port": "10050"
                }
            ],
            "groups": [
                {
                    "groupid": "15"
                }
            ],
            "templates": [
                {
                    "templateid": "10001"  #添加基础模板
                }
            ]
        },
        "auth": "35755d0fba51a9eb2851c852ba62af5b",
        "id": 1
    }' http://172.31.1.20/zabbix/api_jsonrpc.php | python3 -m json.tool
    done
    

    扩展:调用其他文件执行

    [root@zabbix-web02 ~]# cat number_ip_host.sh
    #!/bin/bash
    #
    #********************************************************************
    #Author:                xuanlv
    #QQ:                    360956175
    #Date:                  2021-08-05
    #FileName:             number_ip_host.sh
    #URL:                   https://www.cnblogs.com/xuanlv-0413/
    #Description:          The test script
    #Copyright (C):         2021 All rights reserved
    #********************************************************************
    IP=$1
    
    for node_ip in `cat ${IP}`;do  #这里用cat命令才能for循环
    curl -s -X POST -H 'Content-Type:application/json' -d '
    {
        "jsonrpc": "2.0",
        "method": "host.create",
        "params": {
            "host": "'${node_ip}'",
            "name": "m44-tomcat_'${node_ip}'",
            "proxy_hostid": "10275",
            "interfaces": [
                {
                    "type": 1,
                    "main": 1,
                    "useip": 1,
                    "ip": "'${node_ip}'",
                    "dns": "",
                    "port": "10050"
                }
            ],
            "groups": [
                {
                    "groupid": "15"
                }
            ],
            "templates": [
                {
                    "templateid": "10001"
                }
            ]
        },
        "auth": "35755d0fba51a9eb2851c852ba62af5b",
        "id": 1
    }' http://172.31.1.20/zabbix/api_jsonrpc.php | python3 -m json.tool
    
    echo ${node_ip}
    done
    

    执行

    [root@zabbix-web02 ~]# bash number_ip_host.sh ip.txt
    
  • 相关阅读:
    npm的qs包的使用:stringify()将json对象序列化成表单数据
    git放弃本地某个文件或所有文件的修改
    git关联本地分支和远程分支, 在本地删除远程已经不存在的分支
    vs code代码提示的问题
    js BOM(二)offset系列,scroll系列,client系列,封装变速动画函数
    解决antd design的Modal组件弹出卡顿的问题
    js BOM(一)window、location、history、navigator、定时器setInterval,setTimeout,轮播图
    BizCharts渲染多条曲线时color的使用
    js DOM(三)节点、元素创建3种方式、为元素绑定多个事件addEventListener、attachEvent
    HDU 1081 To The Max
  • 原文地址:https://www.cnblogs.com/xuanlv-0413/p/15171592.html
Copyright © 2011-2022 走看看