Add Host หลายๆตัวพร้อมกันใน Zabbix ด้วย Shell Script
อ้างอิงจาก Post ที่แล้วเรื่องการ Add Host Group จำนวนหลายๆ Host Group ใน Zabbix
https://www.zabbixinthailand.com/wp-admin/post.php?post=267&action=edit <<
เราจะใช้หลักการเดียวกันมาทำการ Add Host เข้าไป โดยหลักการนั้นคือ การใช้ API มาเป็นตัวช่วยค่ะ
สิ่งที่เราต้องมี คือ
- List ของ Host Group ที่เราจะ Add
โดยจะใส่ไว้ใน file ชื่อ listHostname เพื่อให้ Script ไปเรียกหาได้ - Script มีการใช้ jq ดังนั้นเราต้องมี jq ในเครื่องด้วย
Centos : yum -y install jq
Ubuntu : apt-get install jq
#!/bin/bash
#This script name is Host-Add.sh
#This script required file "listHostname" for read host information
#File "listHostname" including .. Hostname|IP|HOSTGroup|TemplateID
#Modify By stitchietare
ZABBIX_USER=UserZabbixAPI
ZABBIX_PASS=PasswordZabbixAPI
ZABBIX_SERVER='10.xx.xx.xxx'
ZBAPI='http://10.xx.xx.xxx/zabbix/api_jsonrpc.php'
#Authenticate with Zabbix API
authenticate() {
echo `curl -k -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"user.login\",\"params\":{\"user\":\""${ZABBIX_USER}"\",\"password\":\""${ZABBIX_PASS}"\"},\"auth\": null,\"id\":0}" $ZBAPI`
}
AUTH_TOKEN=`echo $(authenticate)|jq -r .result`
for i in $(cat listHostname);
do
HOST_NAME=$(echo $i | awk -F"|" '{print $1}')
IP=$(echo $i | awk -F"|" '{print $2}')
HOSTGROUPID=$(echo $i | awk -F"|" '{print $3}')
TEMPLATEID=$(echo $i | awk -F"|" '{print $4}')
curl -k -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\":\"2.0\",\"method\":\"host.create\",\"params\": {\"host\":\"$HOST_NAME\",\"interfaces\": [{\"type\": 1,\"main\": 1,\"useip\": 1,\"ip\": \"$IP\",\"dns\": \"\",\"port\": \"10050\"}],\"groups\": [{\"groupid\": \"$HOSTGROUPID\"}],\"templates\": [{\"templateid\": \"$TEMPLATEID\"}]},\"auth\":\"$AUTH_TOKEN\",\"id\":1}" $ZBAPI
echo "Add ${HOST_NAME} Success"
done
ตัวอย่าง File ListHostname
Webserver|10.10.0.10|57|10318
Webserver2|10.10.0.12|27|10318
Webserver4|10.10.0.14|57|10318
Webserver = Hostname
10.10.0.10 = IP
57 = HostGroupid
10318 = Templateid
แล้วเราจะได้ค่า HostGroupid , Templateid มาจากไหน ????
# วิธีการหา HostGroupid
curl -s -i -X POST -H ‘Content-Type: application/json-rpc’ -d ‘ {
“jsonrpc”: “2.0”,
“method”: “hostgroup.get”,
“params”: {
“output”: [ “groupid”],
“filter”:{“name”:[“PreProd-Webserver“] }
},
“id”: 1,
“auth”: “9462d1255e9485745dc63d6dffbf6f9b“
}’ http://10.xx.xx.xxx/zabbix/api_jsonrpc.php
ผลลัพธ์ที่ได้ {“jsonrpc”:”2.0″,”result”:[{“groupid”:”57“}],”id”:1}
#วิธีการหา Templateid
curl -s -i -X POST -H ‘Content-Type: application/json-rpc’ -d ‘ {
“jsonrpc”: “2.0”,
“method”: “template.get”,
“params”: {
“output”: [ “extend”],
“filter”:{“host”:[“Templates/Applications“] }
},
“id”: 1,
“auth”: “9462d1255e94853745dc63d6dffbf6f9b“
}’ http://10.xx.xx.xxx/zabbix/api_jsonrpc.php
ผลลัพธ์ที่ได้ {“jsonrpc”:”2.0″,”result”:[{“templateid”:”10318“}],”id”:1}
“auth”: “9462d1255e94853745dc63d6dffbf6f9b” เป็น Token ที่ใช้ในการ Curl ในแต่ละครั้ง
เราจะได้มาจาก
curl -k -s -H ‘Content-Type: application/json-rpc’ -d “{\”jsonrpc\”: \”2.0\”,\”method\”:\”user.login\”,\”params\”:{\”user\”:\””UserZabbixAPI“\”,\”password\”:\””PasswordZabbixAPI“\”},\”auth\”: null,\”id\”:0}” http://10.xx.xx.xxx/zabbix/api_jsonrpc.php
ผลลัพธ์ที่ได้ : {“jsonrpc”:”2.0″,”result”:”9462d1255e94853745dc63d6dffbf6f9b“,”id”:0}
ดังนั้น ไฟล์ทั้งหมดจะมี
- Host-Add.sh
- ListHostname
Run : ./Host-Add.sh
{“jsonrpc”:”2.0″,”result”:{“hostids”:[“10326″]},”id”:1}Add Webserver Success
{“jsonrpc”:”2.0″,”result”:{“hostids”:[“10327″]},”id”:1}Add Webserver2 Success
{“jsonrpc”:”2.0″,”result”:{“hostids”:[“10328″]},”id”:1}Add Webserver4 Success