WHEREIS

서버 변동이 무수히 많아서 몇 백대, 몇 천대를 삭제할 때 (서비스 종료 등의 이유로)

zabbix web-ui 에서 작업하는 것은 상당히 불편하기도 하며 업무 효율도 떨어진다.

그렇다고 DB 에서 바로 삭제하기에는 연계 정보 등, 테이블 구조를 완벽히 파악하지 않으면 불가능하다

이럴 때 zabbix api 를 활용하여 삭제하면 편리하다.

다음은 H 사에서 재직 중 일때 작성한 스크립트이다.

 

------------------------------------------

 

#!/bin/bash 

 

fLIST=$1

 

zabbixid='131'

zabbixpass='12'

zabbixserver='서버 도메인'

 

 

print_Usage() {

 

    echo "

 

Usage : $0 ListFile

Example Contents of ListFile

test      3.3.3.3

test      1.1.1.1

"

}

 

 

 

if [ $# -ne 1 ] || [ ! -f $1 ]

 

then

 

    print_Usage

 

    exit

 

fi

 

get_token() {

 

curl -s -X POST -H 'Content-Type: application/json' \

-d '{ "jsonrpc": "2.0", "method": "user.login", "params": { "user": "'$zabbixid'", "password": "'$zabbixpass'" }, "auth": null, "id": "0" }' \

http://$zabbixserver/zabbix/api_jsonrpc.php

 

}

 

token_error=`get_token | grep -i error`

 

 

if [[ ! $token_error == "" ]]

then

echo -ne "\033[31mToken GET error!!\033[0m" \\n

exit 1

fi

 

echo -ne "\033[32m# Token GET compleate!!\033[0m" \\n

token=`get_token | cut -d : -f 3 | cut -d , -f 1`

echo $token

 

 

while read ip null

do

 

hostid="SELECT h.hostid FROM hosts h, interface d WHERE d.ip IN ('$ip') AND d.hostid=h.hostid;"

hostid_get=`mysql --login-path=goods DB_zabbix -N -e "$hostid"`

 

if [[ $hostid_get == "" ]]; then

continue

fi

 

 

delete_host() {

 

curl -s -H 'Content-Type: application/json-rpc' \

-d '{ "jsonrpc": "2.0", "method": "host.delete", "params": [ "'$hostid_get'" ], "auth": '$token', "id": 0'} \

http://$zabbixserver/zabbix/api_jsonrpc.php

}

 

add_error=`delete_host | grep -i error`

 

if [[ $add_error == "" ]]

then

           echo -ne "\033[32m$host delete success !!\033[0m" \\n

else

           echo -ne "\033[31m$host delete fail !!\033[0m" \\n

           continue

fi

 

sleep 5;

 

done < $fLIST

 

exit

 

 

=======================================================

 

# 로컬에서 관리하는 모드

http://www.devops-share.com/remove-a-host-using-zabbix-api/

#!/bin/bash # Remove-Zabbix Init script should run when an AWS instance goes down and remove itself from Zabbix Server # chkconfig: - 84 02 # description: Remove from zabbix # Source function library. . /etc/init.d/functions  start() { /bin/touch /var/lock/subsys/Remove-Zabbix }  stop() {         /etc/init.d/zabbix-agent stop         /bin/rm -f /var/lock/subsys/Remove-Zabbix         HOST_NAME=`echo $(hostname)`          USER='Your_Api_User'          PASS='Zabbix_Password'          ZABBIX_SERVER='zabbix.server.com'         API='http://zabbix.server.com/zabbix/api_jsonrpc.php'          # Authenticate with Zabbix API          authenticate() {                 echo `curl -s -H  'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"user.login\",\"params\":{\"user\":\""${USER}"\",\"password\":\""${PASS}"\"},\"auth\": null,\"id\":0}" $API`         }          AUTH_TOKEN=`echo $(authenticate)|jq -r .result`          # Get This Host HostId:          gethostid() {                echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"host.get\",\"params\":{\"output\":\"extend\",\"filter\":{\"host\":[\""$HOST_NAME"\"]}},\"auth\":\""${AUTH_TOKEN}"\",\"id\":0}" $API`         }          HOST_ID=`echo $(gethostid)|jq -r .result[0].hostid`          # Remove Host          remove_host() {                 echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"host.delete\",\"params\":[\""${HOST_ID}"\"],\"auth\":\""${AUTH_TOKEN}"\",\"id\":0}" $API`         }         RESPONSE=$(remove_host)         echo ${RESPONSE} }  case $1 in          start)           start         ;;          stop)             stop         ;;          restart)           stop           start         ;;  esac     exit 0

 

이 글을 공유합시다

facebook twitter kakaoTalk kakaostory naver band
loading