一、API介绍
通过API,实现完全自动化添加删除agent、关联模板等操作:
API安装官方文档:https://www.zabbix.com/documentation/3.4/zh/manual/api
API提供两个功能,一个是查询,一个是管理。
提供用户密码密码验证并发送post请求到zabbix server端,获取result返回值token:
curl 命令:
-s: 静默模式 -X:指定操作方法 -H:自定义header内容 -d:指定提交的http 内容
(1)获取token值,需要指定zabbix server服务器的用户名和密码
[[email protected] ~]# curl -s -X POST -H ‘Content-Type:application/json‘ -d ‘ { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", # zabbix server用户名 "password": "123456" # zabbix serever密码 }, "id": 1 }‘ http://192.168.7.100/zabbix/api_jsonrpc.php | python -m json.tool # zabbix server服务器主机的IP地址 # 以下是获取的token值 { "id": 1, "jsonrpc": "2.0", "result": "d22b88e46b411f3db0997d54afb06c0e" # 获取的token值 }
(2)获取所有主机信息
# curl -s -X POST -H ‘Content-Type:application/json‘ -d ‘ { "jsonrpc": "2.0", "method": "host.get", "params": { "output": ["host"] }, "auth": "d22b88e46b411f3db0997d54afb06c0e", #上面查询到的token值 "id": 1 }‘ http://192.168.7.100/zabbix/api_jsonrpc.php | python -m json.tool # zabbix server服务器IP地址 # 以下是所有主机的返回值 { "id": 1, "jsonrpc": "2.0", "result": [ { "host": "Zabbix server", "hostid": "10084" }, { "host": "192.168.7.101", "hostid": "10278" }, { "host": "192.168.7.101-nginx", "hostid": "10289" }, { "host": "192.168.7.102", "hostid": "10290" } ] }
(3)获取所有用户信息
[[email protected] ~]# curl -s -X POST -H ‘Content-Type:application/json‘ -d ‘ { "jsonrpc": "2.0", "method": "user.get", "params": { "output": "extend" }, "auth": "d22b88e46b411f3db0997d54afb06c0e", #上面获取的token值 "id": 1 }‘ http://192.168.7.100/zabbix/api_jsonrpc.php | python -m json.tool # 以下是返回值 { "id": 1, "jsonrpc": "2.0", "result": [ { "alias": "Admin", "attempt_clock": "0", "attempt_failed": "0", "attempt_ip": "", "autologin": "1", "autologout": "0", "lang": "zh_CN", "name": "Zabbix", "refresh": "30s", "rows_per_page": "50", "surname": "Administrator", "theme": "default", "type": "3", "url": "", "userid": "1" }, { "alias": "guest", "attempt_clock": "0", "attempt_failed": "0", "attempt_ip": "", "autologin": "0", "autologout": "15m", "lang": "en_GB", "name": "", "refresh": "30s", "rows_per_page": "50", "surname": "", "theme": "default", "type": "1", "url": "", "userid": "2" }, { "alias": "VIP", "attempt_clock": "0", "attempt_failed": "0", "attempt_ip": "", "autologin": "0", "autologout": "0", "lang": "zh_CN", "name": "", "refresh": "30s", "rows_per_page": "50", "surname": "", "theme": "default", "type": "3", "url": "", "userid": "3" } ] }
(4)获取zabbix网页上的模板信息
# 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": "d22b88e46b411f3db0997d54afb06c0e", "id": 1 }‘ http://192.168.7.100/zabbix/api_jsonrpc.php | python -m json.tool #以下是返回值 { "id": 1, "jsonrpc": "2.0", "result": [ { "auto_compress": "1", "available": "0", "description": "", "disable_until": "0", "error": "", "errors_from": "0", "flags": "0", "host": "NGINX_Check_Statuc", "ipmi_authtype": "-1", "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": "NGINX_Check_Statuc", "proxy_address": "", "proxy_hostid": "0", "snmp_available": "0", "snmp_disable_until": "0", "snmp_error": "", "snmp_errors_from": "0", "status": "3", "templateid": "10284", "tls_accept": "1", "tls_connect": "1", "tls_issuer": "", "tls_psk": "", "tls_psk_identity": "", "tls_subject": "" } ] }
二、通过API添加主机
通过API添加主机命令格式:
API添加主机为预先知道要添加的主机IP、预先安装并配置好zabbix agent、预先知道要关联的模板ID/组ID等信息,然后同API提交请求添加
(1)查看需要添加主机群组的ID号,此时的ID号:2
(2)查看NGINX_Check_Static模板的ID号是:10284
(3)查看proxy ID号
(4)将要添加的模板都关联到NGINX_Check_Static模板下,例如:proxy的主动模板以及agent的模板
(5)添加192.168.7.101主机(主动proxy代理模式)
# curl -s -X POST -H ‘Content-Type:application/json‘ -d ‘ { "jsonrpc": "2.0", "method": "host.create", # 定义方法,N多种 "params": { "host": "API Add Host Test 192.168.7.101", # 添加显示的主机名称 "proxy_hostid": "10277", # 加上此行就是经过proxy进行主动模式代理,去掉就不需要proxy主机代理 "interfaces": [ { "type": 1, # 类型为1表示是agent,2是SNMP,3是IMPI,4是JMX "main": 1, # more是接口 "useip": 1, # 0是DNS地址,1是IP地址 "ip": "192.168.7.101", # 添加zabbix主机的IP地址 "dns": "", "port": "10050" # agent端口 } ], "groups": [ { "groupid": "2" # 查到的群组ID } ], "templates": [ { "templateid": "10284" # 关联的模板ID } ] }, "auth": "d22b88e46b411f3db0997d54afb06c0e", #token值 "id": 1 }‘ http://192.168.7.100/zabbix/api_jsonrpc.php | python -m json.tool # 192.168.7.100是zabbix server服务器的IP地址 # 返回值 { "id": 1, "jsonrpc": "2.0", "result": { "hostids": [ "10292" ] } }
使用API批量添加主机(主动proxy代理模式)
(1)编辑一下添加的主机模板
[[email protected] ~]# cat linux-zabbix-add.sh #!/bin/bash IP=" 192.168.7.101 192.168.7.102 " 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}‘", # 主机的IP地址 "name": "linux36-nginx-web_‘${node_ip}‘", # 添加可见的主机名称 "proxy_hostid": "10277", # 主动模式proxy的ID号 "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "‘${node_ip}‘", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "2" } ], "templates": [ { "templateid": "10284" } ] }, "auth": "d22b88e46b411f3db0997d54afb06c0e", "id": 1 }‘ http://192.168.7.100/zabbix/api_jsonrpc.php | python -m json.tool done
(2)加上执行权限,并执行脚本
[[email protected] ~]# chmod +x linux-zabbix-add.sh [[email protected] ~]# bash linux-zabbix-add.sh { "id": 1, "jsonrpc": "2.0", "result": { "hostids": [ "10294" ] } } { "id": 1, "jsonrpc": "2.0", "result": { "hostids": [ "10295" ] } }
在zabbix网页上查看此时添加的主机
1、查看zabbix网页上添加的主机状态
2、查看192.168.7.101主机的数据
3、查看192.168.7.102主机的数据
原文地址:https://www.cnblogs.com/struggle-1216/p/12369986.html
时间: 2024-10-07 17:30:00