Skip to content

Commit

Permalink
Script que fazer um check se o host existe no Zabbix
Browse files Browse the repository at this point in the history
  • Loading branch information
lsa1es committed Nov 11, 2015
1 parent b878071 commit ea51318
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions HostExist/HostExist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/sh
#
# Script faz o check se o host existe ou nao no Zabbix - API ZABBIX
#
# Luiz Sales - [email protected]
# redhate.me - lsales.biz
#
# 14/10/15
#


API='http://localhost/api_jsonrpc.php'

# CONSTANT VARIABLES
ZABBIX_USER=""
ZABBIX_PASS=""

NAME=$1
help() {
echo
echo "$0 <host>"
echo
echo
}

authenticate()
{
wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{
\"jsonrpc\": \"2.0\",
\"method\": \"user.login\",
\"params\": {
\"user\": \"$ZABBIX_USER\",
\"password\": \"$ZABBIX_PASS\"},
\"id\": 0}" | cut -d'"' -f8
}
AUTH_TOKEN=$(authenticate)

get_host_id() {
wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{
\"jsonrpc\": \"2.0\",
\"method\": \"host.get\",
\"params\": {
\"output\": \"extend\",
\"filter\": {
\"host\" : [ \"$NAME\" ] }
},
\"auth\": \"$AUTH_TOKEN\",
\"id\": 2 }"
}
get_name_id() {
wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{
\"jsonrpc\": \"2.0\",
\"method\": \"host.get\",
\"params\": {
\"output\": \"extend\",
\"filter\": {
\"name\" : [ \"$NAME\" ] }
},
\"auth\": \"$AUTH_TOKEN\",
\"id\": 2 }"
}

HOSTID=$(get_host_id | awk -v RS='{"' -F\" '/^hostid/ {print $3}')
NAMEID=$(get_name_id | awk -v RS='{"' -F\" '/^hostid/ {print $3}')

if [ -z $HOSTID ] && [ -z $NAMEID ];then
echo "Host: $1 Nao Existe no Zabbix"
else
echo "Host: $1 Existe"
fi

0 comments on commit ea51318

Please sign in to comment.