公司經過腳本自動初始化擴容服務器,可是存在一個問題,擴容後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