Skip to content

Instantly share code, notes, and snippets.

@hehu80
Created May 23, 2019 06:00
Show Gist options
  • Save hehu80/33d0d5f46c236ce2ac555778ed9afc0d to your computer and use it in GitHub Desktop.
Save hehu80/33d0d5f46c236ce2ac555778ed9afc0d to your computer and use it in GitHub Desktop.
#!/bin/bash
DEFAULT_START_TIME="-PT1H"
DEFAULT_END_TIME="P0D"
DEFAULT_DOWNLOAD_JSON="download.json"
download()
{
local imageJson="${1}"
local startTime="${2}"
local endTime="${3}"
local yAxis="${4}"
local tempFile=$(mktemp)
local imageFile="image.png"
local imagePrefix="${5}"
imageJson=$(echo "$imageJson" | jq ".start = \"$startTime\"")
imageJson=$(echo "$imageJson" | jq ".end = \"$endTime\"")
if [ "$yAxis" != "" ]; then
imageJson=$(echo "$imageJson" | jq ".yAxis.left.max = $yAxis")
echo "use $yAxis max for y-axis"
fi
imageFile=$(echo "$imageJson" | jq -r ".title" | tr [A-Z] [a-z] | tr " " "_")
if [ "$imagePrefix" != "" ]; then
imageFile="$imagePrefix""_$imageFile.png"
else
imageFile="$imageFile.png"
fi
echo "start download $imageFile ..."
echo "$imageJson" > "$tempFile"
aws cloudwatch get-metric-widget-image --metric-widget "file://$tempFile" --output-format png | jq -r '.MetricWidgetImage' | base64 --decode >| "$imageFile"
rm "$tempFile"
}
usage()
{
echo "usage: download.sh [-f file.json] [-s starttime] [-e endtime] [-y yaxismax] [-p image_file_prefix]"
}
startTime="$DEFAULT_START_TIME"
endTime="$DEFAULT_END_TIME"
downloadJson="$DEFAULT_DOWNLOAD_JSON"
yAxis=""
imagePrefix=""
while [ "$1" != "" ]; do
case $1 in
-f | --file ) shift
downloadJson=$1
;;
-s | --starttime ) shift
startTime=$1
;;
-e | --endtime ) shift
endTime=$1
;;
-y | --ymax ) shift
yAxis=($(echo $1 | tr "," " "))
;;
-p | --prefix ) shift
imagePrefix=$1
;;
* ) usage
exit 1
esac
shift
done
i=0
jq -c '.[]' "$downloadJson" | while read imageJson; do
download "$imageJson" "$startTime" "$endTime" "${yAxis[$i]}" "$imagePrefix"
((i++))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment