参考文档:https://www.zabbix.com/documentation/6.0/zh/manual/api/reference/host/create
根据zabbix版本选择适合的API文档参考
#!/bin/bash
username="Admin"
password="zabbix"
zabbix_api="http://www.qingtongqing.cc:19080/api_jsonrpc.php"
zabbix_ip=$(hostname -I)
server_name=$(hostname)
#判断是否安装jq工具
jq_install() {
if ! rpm -q jq > /dev/null; then
yum install -y jq > /dev/null 2>&1
fi
}
# ZABBIX登录获取身份验证令牌
# 创建Zabbix主机
create_zabbix_host() {
echo -e "\033[33m正在创建Zabbix主机......\033[0m"
auth=$(curl -s -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "'$username'",
"password": "'$password'"
},
"id": 1,
"auth": null
}' "$zabbix_api" | jq -r '.result')
if [[ -n "$auth" ]]; then
create_host_response=$(curl -s -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "'$server_name'",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "'$zabbix_ip'",
"dns": 0,
"port": "10050"
}
],
"groups": [
{
"groupid": "5"
},
{
"groupid": "20"
}
],
"tags": [
{
"tag": "Host name",
"value": "'$server_name'"
}
],
"templates": [
{
"templateid": "10574"
}
]
},
"auth": "'$auth'",
"id": 1
}' "'$zabbix_api'" | jq -r '.result')
if [[ $(echo "$create_host_response" | jq -r '.result') == "null" ]]; then
echo -e "\033[31mZabbix主机创建失败......\033[0m"
exit 1
else
echo -e "\033[32mZabbix主机创建成功......\033[0m"
fi
else
echo -e "\033[31mZabbix身份验证失败......\033[0m"
exit 1
fi
}
create_zabbix_host
jq_install
注释:
1 代表启用
0 代表不启用
groupid
templateid