zabbix-helpers/bareos/bareos_get_all.sh

124 lines
5.2 KiB
Bash
Raw Normal View History

2024-11-06 10:16:00 +03:00
#!/bin/bash
#-------------------------------------------------------------------
# Скрипт для получения данных о клиентах и заданиях в Bareos
# для использования в zabbix.
#
# Для работы нужен шаблон Template_Bareos_Clients.xml
#-------------------------------------------------------------------
# Author: Sergey Kalinin
# https://nuk-svk.ru
# svk@nuk-svk.ru
#-------------------------------------------------------------------
# Использование:
# - Вывести список клиентов ввиде zabbix discovery JSON
# bareos_get_all.sh clients-discovery
#
# - Вывести список названий заданий (job) для клиента
# bareos_get_all.sh client-list-job _CLIENT_NAME_
#
# - Получить статусы всех заданий для клиента
# bareos_get_all.sh client-get-jobs-status _CLIENT_NAME_
#
# - Получить статус задания
# bareos_get_all.sh get-job-status _CLIENT_NAME_ _JOB_NAME_
#
# - Вывести количество задач с определенным статусом
# bareos_get_all.sh count-jobs _JOB_STATUS
#
# Где:
# _CLIENT_NAME_ - имя клиента в bareos
# _JOB_NAME_ - название задачи в bareos
# _JOB_STATUS_ - статус задания в терминах bareos:
# T - Completed successfully
# E - Terminated with errors
# e - Non-fatal error
# f - Fatal error
# W - Terminated with warnings
# (полный список доступен тут:
# https://docs.bareos.org/Appendix/CatalogTables.html#jobstatus)
#-------------------------------------------------------------------
ZABBIX_AGENT=${ZABBIX_AGENT:-FALSE}
ZABBIX_SENDER=${ZABBIX_SENDER:-/usr/bin/zabbix_sender}
ZABBIX_AGENT_CONFIG=${ZABBIX_AGNT_CONFIG:-/etc/zabbix/zabbix_agentd.conf}
ZABBIX_HOST="${ZABBIX_HOST:-bareos}"
ZABBIX_AGENT=${ZABBIX_AGENT:-FALSE}
ZABBIX_SENDER=${ZABBIX_SENDER:-/usr/bin/zabbix_sender}
ZABBIX_AGENT_CONFIG=${ZABBIX_AGNT_CONFIG:-/etc/zabbix/zabbix_agentd.conf}
ZABBIX_HOST="${ZABBIX_HOST:-bareos}"
# За сколько дней выбирать задания
JOB_AGE=2
case "$1" in
get-clients-list)
#echo "run"
echo list clients | bconsole | cut -d '|' -f 3 -s | tr -d " " | grep -E '[[:alnum:]]' | grep -E -v "^name"
;;
clients-discovery)
#CLIENTS_LIST=$(echo list clients | bconsole | cut -d '|' -f 3 -s | tr -d " " | grep -E '[[:alnum:]]' | grep -E -v "^name")
#echo "${CLIENTS_LIST}"
CLIENTS_LIST=$(${0} get-clients-list)
CLIENTS_JSON="{\"data\":["
for LINE in ${CLIENTS_LIST}; do
JOB_NAMES=$($0 client-list-jobs ${LINE})
CLIENTS_JSON="${CLIENTS_JSON}"
for NAME in ${JOB_NAMES}; do
CLIENTS_JSON="${CLIENTS_JSON} {\"{#BAREOSCLIENT}\":\"$LINE\", \"{#BAREOSJOB}\":\"$NAME\"},"
done
CLIENTS_JSON="${CLIENTS_JSON}"
done
JSON_STRING="$(echo $CLIENTS_JSON | sed 's/,\+$//') ]}"
echo "$JSON_STRING"
if [ "$ZABBIX_AGENT" = "FALSE" ]; then
$ZABBIX_SENDER -vv -c $ZABBIX_AGENT_CONFIG -s "$ZABBIX_HOST" -k bareos.clients -o "${JSON_STRING}"
fi
;;
client-list-jobs)
echo list jobs client="$2" | bconsole | awk -F "|" '{print $3}' | tr -d " " | grep -E -v "(^$)|(^name)" | sort -u
;;
client-get-jobs-status)
JOBS_LIST=$(${0} client-list-jobs ${2})
for JOB in ${JOBS_LIST}; do
${0} get-job-status ${2} ${JOB}
done
;;
get-job-status)
CLIENT=${2}
JOB_NAME=${3}
#JOB_STATUS=$(echo list job=\"${JOB_NAME}\" days=${JOB_AGE} | bconsole | awk -F "|" '{print $5 $10}' | grep -E -v "(^$)|(starttime)" | tail -1)
JOB_STATUS=$(echo list job=\"${JOB_NAME}\" client=\"${CLIENT}\" | bconsole | awk -F "|" '{print $5 $10}' | grep -E -v "(^$)|(starttime)" | tail -1)
echo "${CLIENT} ${JOB_NAME} ${JOB_STATUS}"
if [ "$ZABBIX_AGENT" = "FALSE" ]; then
$ZABBIX_SENDER -c $ZABBIX_AGENT_CONFIG -s "$ZABBIX_HOST" -k "bareos.clients.job.status[${CLIENT}, ${JOB_NAME}]" -o "${JOB_STATUS}"
fi
;;
jobs-list)
JOB_STATUS="${1}"
echo show jobs | bconsole | grep -i "^ name =" | cut -d '"' -f 2 | while read LINE; do
if [[ $JOB_STATUS != "notrun" ]]; then
echo list job=\"$LINE\" jobstatus=$JOB_STATUS | bconsole -c /etc/bareos/bconsole.conf | cut -d"|" -f10 -s | tr -d ' '| grep "^${JOB_STATUS}$" | while read RES; do
# echo list job=\"$LINE\" | bconsole -c /etc/bareos/bconsole.conf | cut -d"|" -f10 | grep "${JOB_STATUS}" | while read RES; do
if [[ -n $RES ]]
then
echo "${RES}"
fi
done
fi
done
;;
get-all)
CLIENTS_LIST=$(${0} get-clients-list)
echo "${CLIENTS_LIST}"
for CLIENT in ${CLIENTS_LIST}; do
JOB_NAMES=$($0 client-list-jobs ${CLIENT})
echo "${JOB_NAMES}"
for JOB_NAME in ${JOB_NAMES}; do
${0} get-job-status ${CLIENT} ${JOB_NAME}
done
done
;;
esac