公司经过脚本自动初始化扩容服务器,可是存在一个问题,扩容后zabbix不能自动自动对服务器加入主机,须要手动添加,效率慢也容易出错,因而想着经过API自动添加主机监控。果真能够经过查看接口能够解决这个问题。https://www.zabbix.com/documentation/4.0/zh/manual/apiphp
新增接口前须要作个登陆,拿到登陆的token,而后拿到这个token,再去添加服务器。脚本以下:json
#!/bin/bash hostname='192.168.0.219' #被监控主机名称 ip='192.168.0.219' #被监控主机IP port=10050 #被监控主机zabbix_agentd端口 templateid=10001 #模板ID groupid=2 #须要加入的服务器组 zabbixhost='http://www.zabbix.com' #zabbix主机地址 header='Content-Type:application/json' request_data='{"jsonrpc" : "2.0","method" : "user.login","params" : {"user" : "admin" ,"password" : "zabbix"} ,"id" : 1 }' result=$(curl -s -XPOST -H ${header} -d "${request_data}" ${zabbixhost}/api_jsonrpc.php) token=$(echo $result|sed 's/.\+"result":"\([0-9a-z]\+\)".\+/\1/') save_request_data='{"jsonrpc": "2.0","method": "host.create","params": {"host": "'${hostname}'","interfaces": [{"type": 1,"main": 1,"useip": 1,"ip": "'${ip}'","dns": "","port": "'${port}'"}],"groups": [{"groupid": "'${groupid}'"}],"templates": [{"templateid": "'${templateid}'"}]},"auth": "'${token}'","id": 1 }' curl -s -XPOST -H ${header} -d "${save_request_data}" ${zabbixhost}/api_jsonrpc.php