|
| 1 | +#!/bin/bash -e |
| 2 | + |
| 3 | +# Each Metricbeat requires access to Elasticsearch to send Metric information and optionally (but at least one times) |
| 4 | +# access to Kibana in order to load the Metricbeat Dashboards into Kibana. |
| 5 | +# This script adjusts the configured list of Elasticsearch hosts as required by Metricbeat. In addition it is |
| 6 | +# using the first Elasticsearch host as the Kibana-Host if not given externally with KIBANA_HOST |
| 7 | + |
| 8 | +# Save the originally given parameters as they are forwarded to the original docker-entrypoint script |
| 9 | +params=$@ |
| 10 | + |
| 11 | +export METRICBEAT_KIBANA_ENABLED=false |
| 12 | +export METRICBEAT_ELASTICSEARCH_ENABLED=false |
| 13 | +export METRICBEAT_LOGSTASH_ENABLED=false |
| 14 | +export METRICBEAT_BEAT_ENABLED=false |
| 15 | +export METRICBEAT_MEMCACHED_ENABLED=false |
| 16 | +export METRICBEAT_SYSTEM_ENABLED=false |
| 17 | +export METRICBEAT_DOCKER_ENABLED=false |
| 18 | + |
| 19 | +# Don't start if the node-name is not set |
| 20 | +if [ -z "${METRICBEAT_NODE_NAME}" ];then |
| 21 | + echo "METRICBEAT_NODE_NAME is missing"; |
| 22 | + exit 55; |
| 23 | +fi |
| 24 | + |
| 25 | +if [ -z "${METRICBEAT_MODULES}" ];then |
| 26 | + echo "METRICBEAT_MODULES is missing"; |
| 27 | + exit 77; |
| 28 | +else |
| 29 | + # Parse the comma separated list |
| 30 | + IFS=', ' read -r -a array <<< "${METRICBEAT_MODULES}" |
| 31 | + for module in "${array[@]}" |
| 32 | + do |
| 33 | + if [ "${module}" = "kibana" ]; then |
| 34 | + echo "Enable metricbeat Kibana module" |
| 35 | + export METRICBEAT_KIBANA_ENABLED=true |
| 36 | + continue; |
| 37 | + fi |
| 38 | + if [ "${module}" = "elasticsearch" ]; then |
| 39 | + echo "Enable metricbeat Elasticsearch module" |
| 40 | + export METRICBEAT_ELASTICSEARCH_ENABLED=true |
| 41 | + continue; |
| 42 | + fi |
| 43 | + if [ "${module}" = "logstash" ]; then |
| 44 | + echo "Enable metricbeat Logstash module" |
| 45 | + export METRICBEAT_LOGSTASH_ENABLED=true |
| 46 | + continue; |
| 47 | + fi |
| 48 | + if [ "${module}" = "filebeat" ]; then |
| 49 | + echo "Enable metricbeat Beat module" |
| 50 | + export METRICBEAT_BEAT_ENABLED=true |
| 51 | + continue; |
| 52 | + fi |
| 53 | + if [ "${module}" = "memcached" ]; then |
| 54 | + echo "Enable metricbeat Memcached module" |
| 55 | + export METRICBEAT_MEMCACHED_ENABLED=true |
| 56 | + continue; |
| 57 | + fi |
| 58 | + if [ "${module}" = "system" ]; then |
| 59 | + echo "Enable metricbeat System module" |
| 60 | + export METRICBEAT_SYSTEM_ENABLED=true |
| 61 | + continue; |
| 62 | + fi |
| 63 | + if [ "${module}" = "docker" ]; then |
| 64 | + echo "Enable metricbeat Docker module" |
| 65 | + export METRICBEAT_DOCKER_ENABLED=true |
| 66 | + continue; |
| 67 | + fi |
| 68 | + done |
| 69 | +fi |
| 70 | + |
| 71 | +if [ "${METRICBEAT_ENABLED}" = "false" ];then |
| 72 | + echo "Metricbeat is disabled as parameter METRICBEAT_ENABLED is set to false"; |
| 73 | + exit 88; |
| 74 | +fi |
| 75 | + |
| 76 | +if [ -z "${METRICBEAT_USERNAME}" ] || [ -z "${METRICBEAT_PASSWORD}" ];then |
| 77 | + if [ "${ELASTICSEARCH_ANONYMOUS_ENABLED}" = "false" ];then |
| 78 | + echo "ELASTICSEARCH_ANONYMOUS_ENABLED is false, but parameter: METRICBEAT_USERNAME or METRICBEAT_PASSWORD is missing"; |
| 79 | + exit 98; |
| 80 | + fi |
| 81 | +fi |
| 82 | + |
| 83 | +if [ -z "${ELASTICSEARCH_HOSTS}" ];then |
| 84 | + echo "Parameter: ELASTICSEARCH_HOSTS is missing"; |
| 85 | + exit 99; |
| 86 | +fi |
| 87 | + |
| 88 | +if [ -z "${KIBANA_HOST}" ];then |
| 89 | + echo "Parameter KIBANA_HOST not given, using https://kibana:5601 as default"; |
| 90 | + kibanaHost="https://kibana:5601" # This is the default as given by Docker-Compose |
| 91 | + export KIBANA_HOST=$kibanaHost |
| 92 | + echo "KIBANA_HOST set to $kibanaHost" |
| 93 | +fi |
| 94 | + |
| 95 | +# All Elasticsearch hosts should be monitored by one Metricbeat |
| 96 | +# Therefore the list format of the given Elasticsearch hosts must be adjusted for Metricbeat |
| 97 | +elasticHosts=`echo ${ELASTICSEARCH_HOSTS} | awk ' |
| 98 | + function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s } |
| 99 | + function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s } |
| 100 | + function trim(s) { return rtrim(ltrim(s)); } |
| 101 | + { |
| 102 | + split($0, va, /,/); vl=""; for (v in va) { |
| 103 | + if (vl =="" ) |
| 104 | + vl = sprintf("\"%s\"", trim(va[v])); |
| 105 | + else |
| 106 | + vl = vl sprintf(",\"%s\"", trim(va[v])); |
| 107 | + } |
| 108 | + printf("'[%s]'", vl) |
| 109 | + }' |
| 110 | +` |
| 111 | + |
| 112 | +echo "Elasticsearch hosts: ${elasticHosts} will be monitored by Metricbeat" |
| 113 | +# Check if Kibana-Host is reachable and if not, disable Kibana-Monitoring |
| 114 | +curl -skf ${KIBANA_HOST} || rc=$? |
| 115 | +if [ \( "$rc" != "0" -a "$rc" != "" \) ] && [ "${SKIP_VALIDATION}" != true ]; then |
| 116 | + echo "KIBANA_HOST: ${KIBANA_HOST} is not reachable. Got negative returncode for command: curl -kv ${KIBANA_HOST}"; |
| 117 | + echo "Metricbeat Kibana monitoring will be disabled on this host."; |
| 118 | + export METRICBEAT_KIBANA_ENABLED=false; |
| 119 | +else |
| 120 | + # Only if Kibana is reachable, try to load Dashboards is enabled |
| 121 | + echo "Successfully connected to Kibana-Host: ${KIBANA_HOST}." |
| 122 | + if [ "${METRICBEAT_SETUP_DASHBOARDS}" != "false" ];then |
| 123 | + echo "Loading Metricbeat Dashboards into Kibana"; |
| 124 | + # Get the config file required to load Kibana-Dashboards |
| 125 | + for i in "$@" |
| 126 | + do |
| 127 | + case $i in |
| 128 | + -c) |
| 129 | + shift |
| 130 | + configFile="$1" |
| 131 | + break |
| 132 | + ;; |
| 133 | + *) |
| 134 | + shift |
| 135 | + ;; |
| 136 | + esac |
| 137 | + done |
| 138 | + echo "Calling metricbeat setup with config file: ${configFile}" |
| 139 | + if [ "${BATS_TEST}" != true ]; then |
| 140 | + metricbeat setup --strict.perms=false -e -c ${configFile} |
| 141 | + fi |
| 142 | + fi |
| 143 | +fi |
| 144 | + |
| 145 | +export ELASTICSEARCH_HOSTS=$elasticHosts |
| 146 | + |
| 147 | +# Skip, if running in a test |
| 148 | +if [ "${BATS_TEST}" != true ]; then |
| 149 | + # Finally call the original Docker-Entrypoint |
| 150 | + /usr/local/bin/docker-entrypoint $params |
| 151 | +fi |
0 commit comments