#!/usr/bin/env bash # exit if a command fails set -o errexit # exit if required variables are not set set -o nounset # return the exit status of the final command before a failure set -o pipefail # make sure the correct number of arguments are passed; if not, output syntax and exit if [ "$#" -ne 4 ]; then echo -e "\nUsage: $0 \n" exit 1 fi # commands required for script execution required_commands="curl" missing_counter=0 for command in ${required_commands}; do if ! hash "${command}" >/dev/null 2>&1; then printf "Command not found in PATH: %s\n" "${command}" >&2 ((missing_counter++)) fi done if ((missing_counter > 0)); then printf "%d or more commands are missing from PATH. Exiting.\n" "${missing_counter}" >&2 exit 1 fi # host-specific configs if [ "$(hostname)" == "crimson" ]; then api_token="$(cat "${HOME}/bin/.grafana_api_token.crimson.file")" server_url="https://grafana.seedno.de" dashboard_url="8008K41Wk/countries-and-networks" elif [ "$(hostname)" == "casa" ]; then api_token="$(cat "${HOME}/bin/.grafana_api_token.casa.file")" server_url="https://grafana.home.seedno.de" dashboard_url="8008K41Wk/countries-and-networks" else echo "Unknown grafana host." exit 1 fi # grafana org org_id="1" # filename for output image file_name="${1}" # panel to render panel_id="${2}" # start and end timestamps start_timestamp="${3}" end_timestamp="${4}" # resolution to render the image image_height="720" image_width="1280" # timezone timezone="America%2FChicago" function main() { local url url="${server_url}" url+="/render/d-solo" url+="/${dashboard_url}" url+="?orgId=${org_id}" url+="&refresh=5s" url+="&from=${start_timestamp}" url+="&to=${end_timestamp}" url+="&panelId=${panel_id}" url+="&width=${image_width}" url+="&height=${image_height}" url+="&tz=${timezone}" curl -s -H "Authorization: Bearer ${api_token}" "${url}" > "${file_name}" } # generate images main