From ebdee2eb7c6ef6988ff9c07fe81ddea164aa566a Mon Sep 17 00:00:00 2001 From: Julien Guay Date: Mon, 12 Dec 2022 21:59:55 +0000 Subject: [PATCH] add script to workaround around datafeeds with unparseable data Automate running no operation for datafeeds containing unparseable data - https://github.com/elastic/elasticsearch/pull/92168 --- ml_92168_workaround.sh | 82 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 ml_92168_workaround.sh diff --git a/ml_92168_workaround.sh b/ml_92168_workaround.sh new file mode 100755 index 0000000..cf1f2a6 --- /dev/null +++ b/ml_92168_workaround.sh @@ -0,0 +1,82 @@ +#!/bin/bash +### please change the values for next 2 lines +ESurl=https://localhost:9200 +username=elastic +### + +function promptPassword () { + echo -n "Enter a password for user [${username}]: " + read -s password +} + +function checkCommandInstalled () { + if ! command -v "$1" &> /dev/null + then + echo "command [${1}] could not be found, please ensure it is installed and in the PATH" + exit + fi +} + +function checkPrerequisites () { + checkCommandInstalled "curl" + checkCommandInstalled "jq" +} + +function checkESVersion () { + ESVersion=$(curl -s -k -u "${username}:${password}" "${ESurl}" | jq --raw-output '.version.number') + if [[ ! "$version" = 8* ]]; then + echo "Version of elasticsearch expected from 8.0.0 to 8.5.3 - detected [${ESVersion}], stopping execution" + exit + fi +} + +function buildListOfDatafeeds () { + datafeeds=($(curl -k -u "${username}:${password}" "${ESurl}"/_ml/datafeeds/_stats | jq --raw-output '.datafeeds[].datafeed_id')) +} + +function fixDataFeed () { + echo "INFO : starting no operation update to datafeed [${1}]" + #If datafeed is started, it needs to be stopped before the update, then restarted + if [[ $(curl -s -k -u "${username}:${password}" "${ESurl}"/_ml/datafeeds/"$1" | jq --raw-output '.datafeeds[0].state') = "started" ]]; then + echo "Stoping datafeed [${1}] Do not stop script before it gets restarted" + curl -s -k -u "${username}:${password}" -XPOST "${ESurl}"/_ml/datafeeds/"$1"/_stop + echo "Updating datafeed [${1}]" https://github.com/elastic/sdh-ml/issues/401 + curl -s -k -u "${username}:${password}" -XPOST "${ESurl}"/_ml/datafeeds/"$1"/_update -d '{}' + echo "Restarting datafeed [${1}]" + curl -s -k -u "${username}:${password}" -XPOST "${ESurl}"/_ml/datafeeds/"$1"/_start + else + echo "Updating datafeed [${1}]" https://github.com/elastic/sdh-ml/issues/401 + curl -s -k -u "${username}:${password}" -XPOST "${ESurl}"/_ml/datafeeds/"$1"/_update -d '{}' + fi +} + +function detect500DatafeedResponse () { + for datafeed in "${datafeeds[@]}" + do + response_code=$(curl -k -u "${username}:${password}" -s -o /dev/null -I -w "%{http_code}" "${ESurl}"/_ml/datafeeds/) + if [[ "$response_code" = 500 ]]; then + echo "ERROR detected on datafeed [${datafeed}]" + if [[ "$fixDatafeeds" = true ]]; then + fixDataFeed "$datafeed" + else + dataFeedsToFix=$((dataFeedsToFix+1)) + fi + else + echo "INFO : datafeed [${datafeed}] can be parsed, status code [${response_code}]" + fi + done +} + +function runALL() { + checkPrerequisites + promptPassword + checkESVersion + buildListOfDatafeeds + detect500DatafeedResponse + if [[ ! "$dataFeedsToFix" = true ]]; then + echo "SUMMARY: detected [${dataFeedsToFix}] to fix, please run script with : " + echo " fixDatafeeds=true ./ml_92168_workaround.sh" + fi +} + +runALL \ No newline at end of file