脚本或使用自动化工具来批量添加Zabbix Agent,从而减少手动操作和提高效率
使用API添加主机可以减少人为错误的发生。通过自动化和脚本,可以确保正确的配置被应用到每个主机上,避免了手动操作可能导致的配置错误。
使用前提条件
1、zabbix server端模版已经调试完毕
2、zabbix agent 端配置文件和自定义监控调试完毕
Go获取token
package mainimport ("bytes""encoding/json""fmt""io/ioutil""net/http"
)type AuthResponse struct {JsonRPC string `json:"jsonrpc"`Result string `json:"result"`
}func main() {zabbixURL := "http://192.168.12.100/zabbix/api_jsonrpc.php"username := "your-username"password := "your-password"// 构建请求的JSON数据data := map[string]interface{}{"jsonrpc": "2.0","method": "user.login","params": map[string]string{"user": username,"password": password,},"id": 1,}// 将数据转换为JSON格式payload, err := json.Marshal(data)if err != nil {fmt.Println("JSON encoding error:", err)return}// 发送POST请求给Zabbix APIresp, err := http.Post(zabbixURL, "application/json", bytes.NewBuffer(payload))if err != nil {fmt.Println("API request error:", err)return}defer resp.Body.Close()// 读取响应的内容body, err := ioutil.ReadAll(resp.Body)if err != nil {fmt.Println("Response read error:", err)return}// 解析响应的JSON数据var authResp AuthResponseerr = json.Unmarshal(body, &authResp)if err != nil {fmt.Println("JSON decoding error:", err)return}// 输出令牌(token)fmt.Println("Token:", authResp.Result)
}
1、获取模版ID和组ID
[root@ops-public zabbix]# cat template-group.sh
ZABBIX_URL="http://192.168.12.100/zabbix"
username := "your-username"
password := "your-password"
# 登录获取认证令牌
AUTH_TOKEN=$(curl -s -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "user.login","params": {"user": "'"$USER"'","password": "'"$PASSWORD"'"},"id": 1
}' "$ZABBIX_URL/api_jsonrpc.php" | jq -r '.result')# 获取模板列表
TEMPLATE_RESULT=$(curl -s -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "template.get","params": {"output": ["templateid", "name"]},"auth": "'"$AUTH_TOKEN"'","id": 1
}' "$ZABBIX_URL/api_jsonrpc.php")echo "Template List:"
echo "$TEMPLATE_RESULT" | jq -r '.result[] | "Template ID: \(.templateid), Name: \(.name)"'# 获取主机组列表
HOSTGROUP_RESULT=$(curl -s -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "hostgroup.get","params": {"output": ["groupid", "name"]},"auth": "'"$AUTH_TOKEN"'","id": 1
}' "$ZABBIX_URL/api_jsonrpc.php")echo "Hostgroup List:"
echo "$HOSTGROUP_RESULT" | jq -r '.result[] | "Hostgroup ID: \(.groupid), Name: \(.name)"'
2、批量添加主机
添加到Virtual machines
挂载模版 ID为 10001和10274
[root@ops-public zabbix]# cat add_hosts.sh
#!/bin/bash
ZABBIX_URL="http://192.168.12.100/zabbix/api_jsonrpc.php"
username := "your-username"
password := "your-password"TOKEN=$(curl -s -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "user.login","params": {"user": "'"$USER"'","password": "'"$PASSWORD"'"},"id": 1
}' "$ZABBIX_URL" | jq -r '.result')NET="192.168.12."for HOST in $(seq 200 210); doHOST_IP="$NET$HOST"RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0","method": "host.create","params": {"host": "'"$HOST_IP"'","name": "'"$HOST_IP"'","interfaces": [{"type": 1,"main": 1,"useip": 1,"ip": "'"$HOST_IP"'","dns": "","port": 10050}],"groups": [{"groupid": "6"}],"templates": [{"templateid": "10274"},{"templateid": "10001"}]},"id": 1,"auth": "'"$TOKEN"'"}' "$ZABBIX_URL")HOST_ID=$(echo "$RESPONSE" | jq -r '.result.hostids[0]')HOST_NAME=$(echo "$RESPONSE" | jq -r '.result.hosts[0].host')echo "Added host $HOST_NAME with ID $HOST_ID"
done
批量添加主机并挂载模版
除了使用api方式,也可以使用自动注册主机来完成主机注册。