INT 21h

Hi, I am Vladimir Smagin, sysadmin. Telegram Email GPG

Zabbix: удаление хоста из консоли

№ 9051 В разделе "Администрирование" от July 7th, 2018,

Требует установки пакета jq

Скрипт для вызова из консоли с передачей параметров

#!/bin/bash

HOST_NAME="$3"

USER="$1"
PASS="$2"
ZABBIX_SERVER='monitor.server.com'
API="http://$ZABBIX_SERVER/zabbix/api_jsonrpc.php"

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`

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() {
    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}

Скрипт для запуска на виртуальной машине перед тем, как она будет выключена. Пригодна для использования на autoscale фермах Amazon.

#!/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='username' 
        PASS='password' 
        ZABBIX_SERVER='monitor.server.com'
        API="http://$ZABBIX_SERVER/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

Нет комментариев »

Leave a Reply

Your email address will not be published. Required fields are marked *

*


© Vladimir Smagin, 2005-2018. Копирование материалов без разрешения запрещено. GPG 0x29AE91B1A37721C3
Яндекс.Метрика