Last active
February 17, 2022 15:04
-
-
Save ThePsyjo/e046e1b452db14b15ee643b4a0c21224 to your computer and use it in GitHub Desktop.
fix_policies.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~/.es has to contain {"username": "...", "password": "...", "endpoint": "https://..."} | |
es_config(){ jq -r ".$1" < ~/.es; } | |
es_api () | |
{ | |
url=$1; | |
shift; | |
curl -ku $(es_config username):$(es_config password) -s "$(es_config endpoint):9200/${url}" "$@" | |
} | |
plist="policy1 policy2" | |
#plist="test_policy" | |
for p in ${plist} | |
do | |
[[ -z "${p}" ]] && continue | |
# echo "=== $p" | |
policy="$(es_api _opendistro/_ism/policies/${p})" | |
# echo "${policy}" | |
patterns="$(jq -r '.policy | select(.ism_template != null) | .ism_template.index_patterns[]' <<< "${policy}" | paste -sd,)" | |
[[ -z "$patterns" ]] && { echo "no index patterns found in ${p}"; continue; } | |
# echo "checking patterns '$patterns'..." | |
for idx in $(es_api "_opendistro/_ism/explain/${patterns}" | jq -r 'if .error then empty else . end | to_entries[] | select(.value."index.opendistro.index_state_management.policy_id" == null) | .key') | |
do | |
echo -n "$idx is missing policy ${p}" | |
if [[ "$1" == "go" ]] | |
then | |
echo -n ', fixing... ' | |
es_api "_opendistro/_ism/add/${idx}" -XPOST -H 'Content-type: application/json' -d "{\"policy_id\": \"${p}\"}" | |
echo ' done' | |
else | |
echo | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey @Venka619, you can add this script to a curl image and just schedule it as a cronjob
Just make sure that both
curl
andjq
are available in the image.