Skip to content

Instantly share code, notes, and snippets.

@SuperQ
Created July 27, 2018 13:15
Show Gist options
  • Save SuperQ/3b3e215133864fd90bc5d197cb48c794 to your computer and use it in GitHub Desktop.
Save SuperQ/3b3e215133864fd90bc5d197cb48c794 to your computer and use it in GitHub Desktop.
Prometheus json/yaml URL to file_sd_config downloader.
#!/bin/bash
#
# Description: Download a URL and write out a file_sd_config.
# Author: Ben Kochie <superq@gmail.com>
set -o pipefail
if [[ $# -ne 2 || $1 == "-h" ]] ; then
echo "usage $(basename $0) <url> <sd file>"
exit
fi
url="$1"
sd_file="$2"
tmpfile="$(mktemp /tmp/url_sd_config.XXXXXXX)"
cleanup() {
rm "${tmpfile}"
}
trap cleanup EXIT
# Use location to follow any redirects.
curl --silent --compressed --fail --location --output "${tmpfile}" "${url}"
if [[ $? -ne 0 ]] ; then
echo "Failed to get ${url}"
exit 1
fi
cat "${tmpfile}" | sponge "${sd_file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment