213 lines
7.2 KiB
Bash
213 lines
7.2 KiB
Bash
|
#!/bin/bash
|
|||
|
###################################################################
|
|||
|
#
|
|||
|
# Скрипт для работы с Zabbix Rest API
|
|||
|
# Позволяет создавать группу узлов, шаблон, узел.
|
|||
|
# Создан по мотивам
|
|||
|
# https://www.reddit.com/r/zabbix/comments/bhdhgq/zabbix_api_example_using_just_bash_curl_and_jq/
|
|||
|
#
|
|||
|
# Автор: Сергей Калинин
|
|||
|
# https://nuk-svk.ru
|
|||
|
# svk@nuk-svk.ru
|
|||
|
#####################################################################
|
|||
|
#
|
|||
|
# Использование:
|
|||
|
# ./zabbix_create_host.sh
|
|||
|
#
|
|||
|
# Запуск без параметров создаст группу узлов, шаблон и узел если они
|
|||
|
# отсутствуют. Данные берутся из переменных окружения
|
|||
|
#####################################################################
|
|||
|
|
|||
|
|
|||
|
#####################################################################
|
|||
|
# Custom variables
|
|||
|
|
|||
|
BIN_DIR=${BIN_DIR:-/usr/local/bin}
|
|||
|
ETC_DIR=${ETC_DIR:-/usr/local/etc}
|
|||
|
LIB_DIR=${LIB_DIR:-/usr/local/lib}
|
|||
|
|
|||
|
zabbixServer=${ZABBIX_SERVER:-'http://zabbix.example.com'}
|
|||
|
zabbixUsername=${ZABBIX_USER}
|
|||
|
zabbixPassword=${ZABBIX_PASSWORD}
|
|||
|
zabbixHostGroup=${ZABBIX_HOST_GROUP:-'Virtual Hosts'}
|
|||
|
ZABBIX_HOST_NAME=${ZABBIX_HOST:-"DNS records check"}
|
|||
|
ZABBIX_TEMPLATE_NAME=${ZABBIX_TEMPLATE_NAME:-"Template_DNS_Check"}
|
|||
|
|
|||
|
#End of custom variables
|
|||
|
#####################################################################
|
|||
|
|
|||
|
header='Content-Type:application/json'
|
|||
|
zabbixApiUrl="$zabbixServer/api_jsonrpc.php"
|
|||
|
|
|||
|
function exit_with_error() {
|
|||
|
echo '********************************'
|
|||
|
echo "$errorMessage"
|
|||
|
echo '--------------------------------'
|
|||
|
echo 'INPUT'
|
|||
|
echo '--------------------------------'
|
|||
|
echo "$json"
|
|||
|
echo '--------------------------------'
|
|||
|
echo 'OUTPUT'
|
|||
|
echo '--------------------------------'
|
|||
|
echo "$result"
|
|||
|
echo '********************************'
|
|||
|
exit 1
|
|||
|
}
|
|||
|
|
|||
|
#####################################################################
|
|||
|
# Auth to zabbix
|
|||
|
# https://www.zabbix.com/documentation/3.4/manual/api/reference/user/login
|
|||
|
function auth() {
|
|||
|
errorMessage='*ERROR* - Unable to get Zabbix authorization token'
|
|||
|
json=$(cat ${LIB_DIR}/user.login.json)
|
|||
|
json=${json/USER/$zabbixUsername}
|
|||
|
json=${json/PASSWORD/$zabbixPassword}
|
|||
|
#echo $json
|
|||
|
result=$(curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl)
|
|||
|
|
|||
|
auth=$(echo $result | jq '.result')
|
|||
|
echo "Auth: $auth"
|
|||
|
if [ -z "$auth" ]; then
|
|||
|
exit_with_error
|
|||
|
fi
|
|||
|
echo "Login successful - Auth ID: $auth"
|
|||
|
}
|
|||
|
|
|||
|
#####################################################################
|
|||
|
# Create hostgroup
|
|||
|
function create_host_group() {
|
|||
|
if [ -z "$auth" ]; then
|
|||
|
auth
|
|||
|
fi
|
|||
|
errorMessage="*ERROR* - Unable to create hostgroup ID for host group named '$zabbixHostGroup'"
|
|||
|
json=`cat ${LIB_DIR}/hostgroup.create.json`
|
|||
|
json=${json/HOSTGROUP/$zabbixHostGroup}
|
|||
|
json=${json/AUTHID/$auth}
|
|||
|
result=`curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl`
|
|||
|
HOSTGROUP_ID=`echo $result | jq -r '.result | .groupids | .[0]'`
|
|||
|
if [ "$HOSTGROUP_ID" == "null" ]; then
|
|||
|
exit_with_error
|
|||
|
fi
|
|||
|
echo "Hostgroup '$zabbixHostGroup' was created with ID: $HOSTGROUP_ID"
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
#####################################################################
|
|||
|
# Get hostgroup
|
|||
|
function get_host_group() {
|
|||
|
if [ -z "$auth" ]; then
|
|||
|
auth
|
|||
|
fi
|
|||
|
errorMessage="*ERROR* - Unable to get hostgroup ID for host group named '$zabbixHostGroup'"
|
|||
|
json=`cat ${LIB_DIR}/hostgroup.get.json`
|
|||
|
json=${json/HOSTGROUP/$zabbixHostGroup}
|
|||
|
json=${json/AUTHID/$auth}
|
|||
|
result=`curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl`
|
|||
|
HOSTGROUP_ID=`echo $result | jq -r '.result | .[0] | .groupid'`
|
|||
|
if [ "$HOSTGROUP_ID" == "null" ]; then
|
|||
|
create_host_group
|
|||
|
fi
|
|||
|
echo "Hostgroup ID for '$zabbixHostGroup': $HOSTGROUP_ID"
|
|||
|
}
|
|||
|
|
|||
|
#####################################################################
|
|||
|
# Create template
|
|||
|
function create_template(){
|
|||
|
if [ -z "$auth" ]; then
|
|||
|
auth
|
|||
|
fi
|
|||
|
echo "Creating zabbix template '$ZABBIX_TEMPLATE_NAME'"
|
|||
|
errorMessage="*ERROR* - Unable to create Template ID for '$ZABBIX_TEMPLATE_NAME'"
|
|||
|
json=`cat ${LIB_DIR}/template.create.json`
|
|||
|
TEMPLATE_XML=$(cat ${LIB_DIR}/$ZABBIX_TEMPLATE_NAME.xml)
|
|||
|
TEMPLATE_XML="$(echo $TEMPLATE_XML | sed 's/"/\\"/g')"
|
|||
|
json=${json/XMLSTRING/$TEMPLATE_XML}
|
|||
|
json=${json/AUTHID/$auth}
|
|||
|
#echo $json
|
|||
|
#echo "curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl"
|
|||
|
result=`curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl`
|
|||
|
#echo $result
|
|||
|
#exit
|
|||
|
#RESULT=`echo $result | jq -r '.result'`
|
|||
|
if [ "$RESULT" == "null" ]; then
|
|||
|
exit_with_error
|
|||
|
else
|
|||
|
get_template
|
|||
|
#echo "Template '$ZABBIX_TEMPLATE_NAME' was created with ID: $TEMPLATE_ID"
|
|||
|
fi
|
|||
|
}
|
|||
|
|
|||
|
#####################################################################
|
|||
|
# Get template
|
|||
|
function get_template(){
|
|||
|
if [ -z "$auth" ]; then
|
|||
|
auth
|
|||
|
fi
|
|||
|
errorMessage="*ERROR* - Unable to get Template ID for '$ZABBIX_TEMPLATE_NAME'"
|
|||
|
json=`cat ${LIB_DIR}/template.get.json`
|
|||
|
json=${json/TEMPLATE_NAME/$ZABBIX_TEMPLATE_NAME}
|
|||
|
json=${json/AUTHID/$auth}
|
|||
|
result=`curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl`
|
|||
|
TEMPLATE_ID=`echo $result | jq -r '.result | .[0] | .templateid'`
|
|||
|
if [ "$TEMPLATE_ID" == "null" ]; then
|
|||
|
create_template
|
|||
|
fi
|
|||
|
echo "Template ID for '$ZABBIX_TEMPLATE_NAME': $TEMPLATE_ID"
|
|||
|
}
|
|||
|
|
|||
|
#####################################################################
|
|||
|
# Get host
|
|||
|
function get_host() {
|
|||
|
if [ -z "$auth" ]; then
|
|||
|
auth
|
|||
|
fi
|
|||
|
errorMessage="*ERROR* - Unable to get host ID for host '$zabbixHost'"
|
|||
|
json=`cat ${LIB_DIR}/host.get.json`
|
|||
|
json=${json/HOSTNAME/$ZABBIX_HOST_NAME}
|
|||
|
json=${json/AUTHID/$auth}
|
|||
|
#echo $json
|
|||
|
result=`curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl`
|
|||
|
#echo $result
|
|||
|
hostId=`echo $result | jq -r '.result | .[0] | .hostid'`
|
|||
|
# if [ "$hostId" == "null" ]; then exit_with_error; fi
|
|||
|
# echo "Host ID for '$zabbixHost': $hostId"
|
|||
|
if [ "$hostId" == "null" ]; then
|
|||
|
create_host
|
|||
|
#exit_with_error
|
|||
|
else
|
|||
|
echo "Host ID for '$zabbixHost': $hostId"
|
|||
|
fi
|
|||
|
}
|
|||
|
|
|||
|
#####################################################################
|
|||
|
# Create host
|
|||
|
function create_host() {
|
|||
|
if [ -z "$auth" ]; then
|
|||
|
auth
|
|||
|
fi
|
|||
|
if [ -z "$TEMPLATE_ID" ]; then
|
|||
|
get_template
|
|||
|
fi
|
|||
|
if [ -z "$HOSTGROUP_ID" ]; then
|
|||
|
get_host_group
|
|||
|
fi
|
|||
|
|
|||
|
echo "Create host \"$ZABBIX_HOST_NAME\""
|
|||
|
errorMessage="*ERROR* - Host '$zabbixHost' does not created"
|
|||
|
json=$(cat ${LIB_DIR}/host.create.json)
|
|||
|
json=${json/HOSTNAME/$ZABBIX_HOST_NAME}
|
|||
|
json=${json/HOSTGROUPID/$HOSTGROUP_ID}
|
|||
|
json=${json/TEMPLATE_ID/$TEMPLATE_ID}
|
|||
|
json=${json/AUTHID/$auth}
|
|||
|
#echo $json
|
|||
|
result=`curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl`
|
|||
|
#echo $result
|
|||
|
HOST_ID=`echo $result | jq -r '.result | .hostids | .[0]'`
|
|||
|
if [ -z "$HOST_ID" ]; then
|
|||
|
exit_with_error
|
|||
|
else
|
|||
|
echo "Host \"${ZABBIX_HOST_NAME}\" was created with id $HOST_ID"
|
|||
|
fi
|
|||
|
}
|
|||
|
|
|||
|
get_host
|