Всё что связанно с zabbix и мониторингом различных сервисов
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
5.2 KiB

  1. #!/bin/bash
  2. #-------------------------------------------------------------------
  3. # Скрипт для получения данных о клиентах и заданиях в Bareos
  4. # для использования в zabbix.
  5. #
  6. # Для работы нужен шаблон Template_Bareos_Clients.xml
  7. #-------------------------------------------------------------------
  8. # Author: Sergey Kalinin
  9. # https://nuk-svk.ru
  10. # svk@nuk-svk.ru
  11. #-------------------------------------------------------------------
  12. # Использование:
  13. # - Вывести список клиентов ввиде zabbix discovery JSON
  14. # bareos_get_all.sh clients-discovery
  15. #
  16. # - Вывести список названий заданий (job) для клиента
  17. # bareos_get_all.sh client-list-job _CLIENT_NAME_
  18. #
  19. # - Получить статусы всех заданий для клиента
  20. # bareos_get_all.sh client-get-jobs-status _CLIENT_NAME_
  21. #
  22. # - Получить статус задания
  23. # bareos_get_all.sh get-job-status _CLIENT_NAME_ _JOB_NAME_
  24. #
  25. # - Вывести количество задач с определенным статусом
  26. # bareos_get_all.sh count-jobs _JOB_STATUS
  27. #
  28. # Где:
  29. # _CLIENT_NAME_ - имя клиента в bareos
  30. # _JOB_NAME_ - название задачи в bareos
  31. # _JOB_STATUS_ - статус задания в терминах bareos:
  32. # T - Completed successfully
  33. # E - Terminated with errors
  34. # e - Non-fatal error
  35. # f - Fatal error
  36. # W - Terminated with warnings
  37. # (полный список доступен тут:
  38. # https://docs.bareos.org/Appendix/CatalogTables.html#jobstatus)
  39. #-------------------------------------------------------------------
  40. ZABBIX_AGENT=${ZABBIX_AGENT:-FALSE}
  41. ZABBIX_SENDER=${ZABBIX_SENDER:-/usr/bin/zabbix_sender}
  42. ZABBIX_AGENT_CONFIG=${ZABBIX_AGNT_CONFIG:-/etc/zabbix/zabbix_agentd.conf}
  43. ZABBIX_HOST="${ZABBIX_HOST:-bareos}"
  44. ZABBIX_AGENT=${ZABBIX_AGENT:-FALSE}
  45. ZABBIX_SENDER=${ZABBIX_SENDER:-/usr/bin/zabbix_sender}
  46. ZABBIX_AGENT_CONFIG=${ZABBIX_AGNT_CONFIG:-/etc/zabbix/zabbix_agentd.conf}
  47. ZABBIX_HOST="${ZABBIX_HOST:-bareos}"
  48. # За сколько дней выбирать задания
  49. JOB_AGE=2
  50. case "$1" in
  51. get-clients-list)
  52. #echo "run"
  53. echo list clients | bconsole | cut -d '|' -f 3 -s | tr -d " " | grep -E '[[:alnum:]]' | grep -E -v "^name"
  54. ;;
  55. clients-discovery)
  56. #CLIENTS_LIST=$(echo list clients | bconsole | cut -d '|' -f 3 -s | tr -d " " | grep -E '[[:alnum:]]' | grep -E -v "^name")
  57. #echo "${CLIENTS_LIST}"
  58. CLIENTS_LIST=$(${0} get-clients-list)
  59. CLIENTS_JSON="{\"data\":["
  60. for LINE in ${CLIENTS_LIST}; do
  61. JOB_NAMES=$($0 client-list-jobs ${LINE})
  62. CLIENTS_JSON="${CLIENTS_JSON}"
  63. for NAME in ${JOB_NAMES}; do
  64. CLIENTS_JSON="${CLIENTS_JSON} {\"{#BAREOSCLIENT}\":\"$LINE\", \"{#BAREOSJOB}\":\"$NAME\"},"
  65. done
  66. CLIENTS_JSON="${CLIENTS_JSON}"
  67. done
  68. JSON_STRING="$(echo $CLIENTS_JSON | sed 's/,\+$//') ]}"
  69. echo "$JSON_STRING"
  70. if [ "$ZABBIX_AGENT" = "FALSE" ]; then
  71. $ZABBIX_SENDER -vv -c $ZABBIX_AGENT_CONFIG -s "$ZABBIX_HOST" -k bareos.clients -o "${JSON_STRING}"
  72. fi
  73. ;;
  74. client-list-jobs)
  75. echo list jobs client="$2" | bconsole | awk -F "|" '{print $3}' | tr -d " " | grep -E -v "(^$)|(^name)" | sort -u
  76. ;;
  77. client-get-jobs-status)
  78. JOBS_LIST=$(${0} client-list-jobs ${2})
  79. for JOB in ${JOBS_LIST}; do
  80. ${0} get-job-status ${2} ${JOB}
  81. done
  82. ;;
  83. get-job-status)
  84. CLIENT=${2}
  85. JOB_NAME=${3}
  86. #JOB_STATUS=$(echo list job=\"${JOB_NAME}\" days=${JOB_AGE} | bconsole | awk -F "|" '{print $5 $10}' | grep -E -v "(^$)|(starttime)" | tail -1)
  87. JOB_STATUS=$(echo list job=\"${JOB_NAME}\" client=\"${CLIENT}\" | bconsole | awk -F "|" '{print $5 $10}' | grep -E -v "(^$)|(starttime)" | tail -1)
  88. echo "${CLIENT} ${JOB_NAME} ${JOB_STATUS}"
  89. if [ "$ZABBIX_AGENT" = "FALSE" ]; then
  90. $ZABBIX_SENDER -c $ZABBIX_AGENT_CONFIG -s "$ZABBIX_HOST" -k "bareos.clients.job.status[${CLIENT}, ${JOB_NAME}]" -o "${JOB_STATUS}"
  91. fi
  92. ;;
  93. jobs-list)
  94. JOB_STATUS="${1}"
  95. echo show jobs | bconsole | grep -i "^ name =" | cut -d '"' -f 2 | while read LINE; do
  96. if [[ $JOB_STATUS != "notrun" ]]; then
  97. 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
  98. # echo list job=\"$LINE\" | bconsole -c /etc/bareos/bconsole.conf | cut -d"|" -f10 | grep "${JOB_STATUS}" | while read RES; do
  99. if [[ -n $RES ]]
  100. then
  101. echo "${RES}"
  102. fi
  103. done
  104. fi
  105. done
  106. ;;
  107. get-all)
  108. CLIENTS_LIST=$(${0} get-clients-list)
  109. echo "${CLIENTS_LIST}"
  110. for CLIENT in ${CLIENTS_LIST}; do
  111. JOB_NAMES=$($0 client-list-jobs ${CLIENT})
  112. echo "${JOB_NAMES}"
  113. for JOB_NAME in ${JOB_NAMES}; do
  114. ${0} get-job-status ${CLIENT} ${JOB_NAME}
  115. done
  116. done
  117. ;;
  118. esac