Всё что связанно с 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.

212 lines
7.2 KiB

  1. #!/bin/bash
  2. ###################################################################
  3. #
  4. # Скрипт для работы с Zabbix Rest API
  5. # Позволяет создавать группу узлов, шаблон, узел.
  6. # Создан по мотивам
  7. # https://www.reddit.com/r/zabbix/comments/bhdhgq/zabbix_api_example_using_just_bash_curl_and_jq/
  8. #
  9. # Автор: Сергей Калинин
  10. # https://nuk-svk.ru
  11. # svk@nuk-svk.ru
  12. #####################################################################
  13. #
  14. # Использование:
  15. # ./zabbix_create_host.sh
  16. #
  17. # Запуск без параметров создаст группу узлов, шаблон и узел если они
  18. # отсутствуют. Данные берутся из переменных окружения
  19. #####################################################################
  20. #####################################################################
  21. # Custom variables
  22. BIN_DIR=${BIN_DIR:-/usr/local/bin}
  23. ETC_DIR=${ETC_DIR:-/usr/local/etc}
  24. LIB_DIR=${LIB_DIR:-/usr/local/lib}
  25. zabbixServer=${ZABBIX_SERVER:-'http://zabbix.example.com'}
  26. zabbixUsername=${ZABBIX_USER}
  27. zabbixPassword=${ZABBIX_PASSWORD}
  28. zabbixHostGroup=${ZABBIX_HOST_GROUP:-'Virtual Hosts'}
  29. ZABBIX_HOST_NAME=${ZABBIX_HOST:-"DNS records check"}
  30. ZABBIX_TEMPLATE_NAME=${ZABBIX_TEMPLATE_NAME:-"Template_DNS_Check"}
  31. #End of custom variables
  32. #####################################################################
  33. header='Content-Type:application/json'
  34. zabbixApiUrl="$zabbixServer/api_jsonrpc.php"
  35. function exit_with_error() {
  36. echo '********************************'
  37. echo "$errorMessage"
  38. echo '--------------------------------'
  39. echo 'INPUT'
  40. echo '--------------------------------'
  41. echo "$json"
  42. echo '--------------------------------'
  43. echo 'OUTPUT'
  44. echo '--------------------------------'
  45. echo "$result"
  46. echo '********************************'
  47. exit 1
  48. }
  49. #####################################################################
  50. # Auth to zabbix
  51. # https://www.zabbix.com/documentation/3.4/manual/api/reference/user/login
  52. function auth() {
  53. errorMessage='*ERROR* - Unable to get Zabbix authorization token'
  54. json=$(cat ${LIB_DIR}/user.login.json)
  55. json=${json/USER/$zabbixUsername}
  56. json=${json/PASSWORD/$zabbixPassword}
  57. #echo $json
  58. result=$(curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl)
  59. auth=$(echo $result | jq '.result')
  60. echo "Auth: $auth"
  61. if [ -z "$auth" ]; then
  62. exit_with_error
  63. fi
  64. echo "Login successful - Auth ID: $auth"
  65. }
  66. #####################################################################
  67. # Create hostgroup
  68. function create_host_group() {
  69. if [ -z "$auth" ]; then
  70. auth
  71. fi
  72. errorMessage="*ERROR* - Unable to create hostgroup ID for host group named '$zabbixHostGroup'"
  73. json=`cat ${LIB_DIR}/hostgroup.create.json`
  74. json=${json/HOSTGROUP/$zabbixHostGroup}
  75. json=${json/AUTHID/$auth}
  76. result=`curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl`
  77. HOSTGROUP_ID=`echo $result | jq -r '.result | .groupids | .[0]'`
  78. if [ "$HOSTGROUP_ID" == "null" ]; then
  79. exit_with_error
  80. fi
  81. echo "Hostgroup '$zabbixHostGroup' was created with ID: $HOSTGROUP_ID"
  82. }
  83. #####################################################################
  84. # Get hostgroup
  85. function get_host_group() {
  86. if [ -z "$auth" ]; then
  87. auth
  88. fi
  89. errorMessage="*ERROR* - Unable to get hostgroup ID for host group named '$zabbixHostGroup'"
  90. json=`cat ${LIB_DIR}/hostgroup.get.json`
  91. json=${json/HOSTGROUP/$zabbixHostGroup}
  92. json=${json/AUTHID/$auth}
  93. result=`curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl`
  94. HOSTGROUP_ID=`echo $result | jq -r '.result | .[0] | .groupid'`
  95. if [ "$HOSTGROUP_ID" == "null" ]; then
  96. create_host_group
  97. fi
  98. echo "Hostgroup ID for '$zabbixHostGroup': $HOSTGROUP_ID"
  99. }
  100. #####################################################################
  101. # Create template
  102. function create_template(){
  103. if [ -z "$auth" ]; then
  104. auth
  105. fi
  106. echo "Creating zabbix template '$ZABBIX_TEMPLATE_NAME'"
  107. errorMessage="*ERROR* - Unable to create Template ID for '$ZABBIX_TEMPLATE_NAME'"
  108. json=`cat ${LIB_DIR}/template.create.json`
  109. TEMPLATE_XML=$(cat ${LIB_DIR}/$ZABBIX_TEMPLATE_NAME.xml)
  110. TEMPLATE_XML="$(echo $TEMPLATE_XML | sed 's/"/\\"/g')"
  111. json=${json/XMLSTRING/$TEMPLATE_XML}
  112. json=${json/AUTHID/$auth}
  113. #echo $json
  114. #echo "curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl"
  115. result=`curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl`
  116. #echo $result
  117. #exit
  118. #RESULT=`echo $result | jq -r '.result'`
  119. if [ "$RESULT" == "null" ]; then
  120. exit_with_error
  121. else
  122. get_template
  123. #echo "Template '$ZABBIX_TEMPLATE_NAME' was created with ID: $TEMPLATE_ID"
  124. fi
  125. }
  126. #####################################################################
  127. # Get template
  128. function get_template(){
  129. if [ -z "$auth" ]; then
  130. auth
  131. fi
  132. errorMessage="*ERROR* - Unable to get Template ID for '$ZABBIX_TEMPLATE_NAME'"
  133. json=`cat ${LIB_DIR}/template.get.json`
  134. json=${json/TEMPLATE_NAME/$ZABBIX_TEMPLATE_NAME}
  135. json=${json/AUTHID/$auth}
  136. result=`curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl`
  137. TEMPLATE_ID=`echo $result | jq -r '.result | .[0] | .templateid'`
  138. if [ "$TEMPLATE_ID" == "null" ]; then
  139. create_template
  140. fi
  141. echo "Template ID for '$ZABBIX_TEMPLATE_NAME': $TEMPLATE_ID"
  142. }
  143. #####################################################################
  144. # Get host
  145. function get_host() {
  146. if [ -z "$auth" ]; then
  147. auth
  148. fi
  149. errorMessage="*ERROR* - Unable to get host ID for host '$zabbixHost'"
  150. json=`cat ${LIB_DIR}/host.get.json`
  151. json=${json/HOSTNAME/$ZABBIX_HOST_NAME}
  152. json=${json/AUTHID/$auth}
  153. #echo $json
  154. result=`curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl`
  155. #echo $result
  156. hostId=`echo $result | jq -r '.result | .[0] | .hostid'`
  157. # if [ "$hostId" == "null" ]; then exit_with_error; fi
  158. # echo "Host ID for '$zabbixHost': $hostId"
  159. if [ "$hostId" == "null" ]; then
  160. create_host
  161. #exit_with_error
  162. else
  163. echo "Host ID for '$zabbixHost': $hostId"
  164. fi
  165. }
  166. #####################################################################
  167. # Create host
  168. function create_host() {
  169. if [ -z "$auth" ]; then
  170. auth
  171. fi
  172. if [ -z "$TEMPLATE_ID" ]; then
  173. get_template
  174. fi
  175. if [ -z "$HOSTGROUP_ID" ]; then
  176. get_host_group
  177. fi
  178. echo "Create host \"$ZABBIX_HOST_NAME\""
  179. errorMessage="*ERROR* - Host '$zabbixHost' does not created"
  180. json=$(cat ${LIB_DIR}/host.create.json)
  181. json=${json/HOSTNAME/$ZABBIX_HOST_NAME}
  182. json=${json/HOSTGROUPID/$HOSTGROUP_ID}
  183. json=${json/TEMPLATE_ID/$TEMPLATE_ID}
  184. json=${json/AUTHID/$auth}
  185. #echo $json
  186. result=`curl --silent --show-error --insecure --header $header --data "$json" $zabbixApiUrl`
  187. #echo $result
  188. HOST_ID=`echo $result | jq -r '.result | .hostids | .[0]'`
  189. if [ -z "$HOST_ID" ]; then
  190. exit_with_error
  191. else
  192. echo "Host \"${ZABBIX_HOST_NAME}\" was created with id $HOST_ID"
  193. fi
  194. }
  195. get_host