Last active
July 18, 2020 19:40
-
-
Save Luzifer/e5ec42273382128b43826e1de8386e83 to your computer and use it in GitHub Desktop.
Query Hetzner Cloud-Node for your servers
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 | |
set -euo pipefail | |
### | |
# Query Hetzner Cloud-Node for your servers | |
# | |
# Wanna know whether a specific outage affects one of your cloud nodes? | |
# Make a list of nodes before the outage happens and you'll know whether | |
# your server is hosted on the node with the outage... | |
# | |
# Requirements: mtr (tool), API token for your Hetzner Cloud Project in | |
# ENV variable HETZNER_TOKEN | |
# | |
# License: None. It's just some shell commands glued together. Do with | |
# it what you want, just don't blame me if everything goes south. | |
### | |
declare -A hosts | |
function check_env() { | |
command -v mtr &>/dev/null || { | |
echo "Missing MTR utility" >&2 | |
exit 1 | |
} | |
[[ -n ${HETZNER_TOKEN:-} ]] || { | |
echo 'Missing Hetzner API Token in $HETZNER_TOKEN' >&2 | |
exit 1 | |
} | |
} | |
function fetch_hosts() { | |
for iphost in $( | |
curl -sSfL \ | |
-H "Authorization: Bearer ${HETZNER_TOKEN}" \ | |
"https://api.hetzner.cloud/v1/servers" | | |
jq -r '.servers | .[] | .public_net.ipv4.ip + "|" + .name' | |
); do | |
local ip=$(echo "${iphost}" | cut -d '|' -f 1) | |
local name=$(echo "${iphost}" | cut -d '|' -f 2) | |
hosts[${name}]=${ip} | |
done | |
} | |
function get_cloudnode() { | |
mtr -r -c1 "${1}" | grep -Eo '[0-9]*.your-cloud.host' | cut -d '.' -f 1 | tail -n1 || echo "n/a" | |
} | |
function log() { | |
echo -e "\e[36m[$(date +%H:%M:%S)] $1\e[0m" >&2 | |
} | |
function main() { | |
check_env | |
log "Fetching hosts of account..." | |
fetch_hosts | |
log "Finding cloud nodes..." | |
for name in "${!hosts[@]}"; do | |
echo -e "Host ${name}\tCloud-Node $(get_cloudnode ${hosts[$name]})" | |
done | |
log "All done." | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment