Skip to content

Instantly share code, notes, and snippets.

@HeroCC
Created August 5, 2020 14:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save HeroCC/76b3b0da82ae272469658b09b4358c6b to your computer and use it in GitHub Desktop.
Metricbeat Template auto-load
#!/bin/bash
set -euo pipefail
# This is a hack since Metricbeat doesn't support importing templates through LogStash, only a direct elastic connection
# This script checks to see if there is a newer version of Metricbeat avaliable, and if there is, runs it so the new template gets installed.
# This works around the issue I described here https://discuss.elastic.co/t/illegal-argument-exception-when-metricbeat-begins-new-index/243269
# You should only run this script on the machine running elasticsearch
# ALWAYS run this BEFORE using a new version of metricbeat
# Requires Docker (or podman) and jq. Tweak as you may.
ORCHESTRATOR="docker"
PULL_STATUS="old"
IMAGE_NAME="elastic/metricbeat"
ELASTIC_PASSWORD="${ELASTIC_PASSWORD:-}"
METRICBEAT_EXTRAS="${METRICBEAT_EXTRAS:- -E output.elasticsearch.username='elastic' -E output.elasticsearch.password='$ELASTIC_PASSWORD'}"
IMAGE_VERSION="$(curl --silent 'https://hub.docker.com/v2/repositories/elastic/metricbeat/tags' | jq -r '(.results[]["name"])' | grep '7.*' | uniq | sort -r | head -n 1)"
IMAGE_NAME="$IMAGE_NAME:$IMAGE_VERSION"
which podman && ORCHESTRATOR="podman" # CentOS 8 uses podman, not docker
if [[ "$($ORCHESTRATOR images -q --filter=reference=$IMAGE_NAME | wc -l)" -le 0 ]]; then
PULL_STATUS="new"
fi
if [[ "$PULL_STATUS" != "old" ]]; then
echo "Docker image appears to be new, updating template"
$ORCHESTRATOR pull "$IMAGE_NAME"
echo "Starting metricbeat index setup"
$ORCHESTRATOR run --rm --network host $IMAGE_NAME setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]' $METRICBEAT_EXTRAS
else
echo "Docker image appears to be up-to-date, nothing to do"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment