新增接口前需要做个登录,拿到登录的token,然后拿到这个token,再去添加服务器。脚本如下:
#!/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