update_targets.sh
This file contains 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
#!/bin/bash | |
######################################## PLEASE MODIFY THEASE PARAMETERS ######################################## | |
SM_DOMAIN="stream-manager.com" # Stream Manager domain name. It is using for API calls to get information about deployed nodes | |
SM_API_KEY="abc123" # Stream Manager API key. It is using for API calls to get information about deployed nodes | |
ENVIRONMENT="Prod" # Set your environment. This parameter could be using for select different environment in Grafana WEB applicatioon. | |
ADDITIONAL_TARGETS=(192.168.1.201 example.com) # In this array you can set additional target hosts like Stream Managers and etc. | |
################################################################################################################## | |
targets_array=() | |
targets_folder="/etc/prometheus/targets" | |
rm ./temp.txt | |
log_i() { | |
log | |
printf "\033[0;36m [INFO] --- %s \033[0m\n" "${@}" | |
} | |
log_u() { | |
log | |
printf "\033[0;32m [UPDATE] --- %s \033[0m\n" "${@}" | |
} | |
log_d() { | |
log | |
printf "\033[0;33m [DELETE] --- %s \033[0m\n" "${@}" | |
} | |
log() { | |
echo -n "[$(date '+%Y-%m-%d %H:%M:%S')]" | |
} | |
### GET NODES FROM STREAM MANAGER AND TARGETS JSON GENERATION | |
node_groups=$(curl -s "https://${SM_DOMAIN}/streammanager/api/4.0/admin/nodegroup?accessToken=${SM_API_KEY}" |jq -r '.[].name') | |
for group in $node_groups | |
do | |
curl -s "https://${SM_DOMAIN}/streammanager/api/4.0/admin/nodegroup/${group}/node?accessToken=${SM_API_KEY}" | jq -r '.[] | [.address, "'${group}'"] | join(" ")' >> temp.txt | |
done | |
tags=$(cat temp.txt | awk '{print $2}' |uniq) | |
for index in $tags | |
do | |
target_json="${targets_folder}/${index}.json" | |
node_ips=$(cat temp.txt |grep "$index" | awk '{print $1}') | |
for y in $node_ips | |
do | |
targets_array+=('"'${y}':9100",') | |
done | |
targets=$(echo "${targets_array[@]}" | sed '$s/,$//') | |
file_new='[{"targets":['${targets}'],"labels":{"env":"'$ENVIRONMENT'","job":"'${index}'"}}]' | |
file_old=$(cat "$target_json") | |
if [[ "$file_old" != "$file_new" ]]; then | |
log_i "Node group name: ${index} - Nodes IPs: ${targets_array[*]}" | |
log_u "Update ips in the file: $target_json" | |
echo "$file_new" > "$target_json" | |
fi | |
targets_array=() | |
done | |
### CHECK AND DELETE UNUSED JSON TARGET FILES | |
files=$(ls ${targets_folder}/ | grep '.json' | sed 's/.json//g') | |
for index1 in $files | |
do | |
file_exist=false | |
for index2 in $tags | |
do | |
if [[ "$index2" == "$index1" ]]; then | |
file_exist=true | |
fi | |
done | |
if [ "$file_exist" = false ] ; then | |
log_d "Delete file: ${index1}.json" | |
rm -r "${targets_folder}/${index1}.json" | |
fi | |
done | |
### ADDITIONAL TARGETS JSON GENERATION | |
target_json="${targets_folder}/additional_targets.json" | |
for x in ${!ADDITIONAL_TARGETS[*]} | |
do | |
targets_array+=('"'${ADDITIONAL_TARGETS[${x}]}':9100",') | |
done | |
targets=$(echo "${targets_array[@]}" | sed '$s/,$//') | |
file_new='[{"targets":['${targets}'],"labels":{"env":"'$ENVIRONMENT'","job":"Additional-targets"}}]' | |
file_old=$(cat "$target_json") | |
if [[ "$file_old" != "$file_new" ]]; then | |
log_i "Group name: additional-targets - IPs: ${targets_array[*]}" | |
log_u "Update ips in the file: $target_json" | |
echo "$file_new" > "$target_json" | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment