由于我有很多实例,我想自动获取Cloudwatch屏幕快照。
但是当我尝试通过aws cli命令工具运行get-metric-widget-image时,总是会出错。
调用GetMetricWidgetImage操作时发生错误(ValidationError):MetricWidget属性'metricWidget'具有错误的JSON内容。
有人可以帮助我吗?谢谢。
我无法从AWS文档中找到示例。下面的链接中没有确切的例子。 https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/CloudWatch-Metric-Widget-Structure.html
我的命令是这样的。
aws cloudwatch get-metric-widget-image --metric-widget "{ "width":600,"height":395,"metrics":[["AWS/EC2","CPUUtilization","InstanceId","i-01234567890123456",{"stat":"Average"}]],"period":300,"start":"-P30D","end":"PT0H","stacked":false,"yAxis":{"left":{"min":0.1,"max":1},"right":{"min":0}},"title":"CPU","annotations":{"horizontal":[{"color":"#ff6961","label":"Troublethresholdstart","fill":"above","value":0.5}], "vertical":[{"visible":true, "color":"#9467bd","label":"Bugfixdeployed","value":"2018-11-19T07:25:26Z","fill":"after"}]}}}" --output-format "png"
答案 0 :(得分:4)
为请求获取正确json的最佳方法是使用CloudWatch Console构造图,然后单击Source
选项卡,选择Image API
视图,然后单击Copy Source
复制在那里生成的json。您还需要将json用单引号引起来,例如:
aws cloudwatch get-metric-widget-image --metric-widget \
'{
"width": 600,
"height": 395,
"metrics": [
[ "AWS/EC2", "CPUUtilization", "InstanceId", "i-01234567890123456", { "stat": "Average" } ]
],
"period": 300,
"stacked": false,
"yAxis": {
"left": {
"min": 0.1,
"max": 1
},
"right": {
"min": 0
}
},
"title": "CPU",
"annotations": {
"horizontal": [
{
"color": "#ff6961",
"label": "Troublethresholdstart",
"fill": "above",
"value": 0.5
}
],
"vertical": [
{
"visible": true,
"color": "#9467bd",
"label": "Bugfixdeployed",
"value": "2018-11-19T07:25:26Z",
"fill": "after"
}
]
},
"view": "timeSeries"
}'
对此的响应将是base64编码的图像,如下所示:
{
"MetricWidgetImage": "iVBORw0KGgoAAAANSUhEUgAAAlgAAAGLEAYA..."
}
如果您需要原始的png图像,则需要通过执行以下操作来解码响应:
aws cloudwatch get-metric-widget-image --metric-widget 'JSON_GOES_HERE' | grep MetricWidgetImage | awk '{split($0,a,"\""); print a[4]}' | base64 --decode > graph.png
答案 1 :(得分:0)
JSONLint说您的JSON末尾还有一个额外的}
。另外,请尝试用单引号'
包裹整个JSON块,以便于区分,而无需在JSON字符串中转义双引号。
这应该对您有用:
aws cloudwatch get-metric-widget-image --metric-widget '{ "width":600,"height":395,"metrics":[["AWS/EC2","CPUUtilization","InstanceId","i-01234567890123456",{"stat":"Average"}]],"period":300,"start":"-P30D","end":"PT0H","stacked":false,"yAxis":{"left":{"min":0.1,"max":1},"right":{"min":0}},"title":"CPU","annotations":{"horizontal":[{"color":"#ff6961","label":"Troublethresholdstart","fill":"above","value":0.5}], "vertical":[{"visible":true, "color":"#9467bd","label":"Bugfixdeployed","value":"2018-11-19T07:25:26Z","fill":"after"}]}}' --output-format "png"
答案 2 :(得分:0)
这是一个不同的答案。 https://github.com/kcrossen/CloudWatch_Remote_Monitor/blob/master/Source_Code/中有一个Python脚本,该脚本将摘要您上述Tartaglia提到的仪表板源代码,并为GetMetricWidgetImage生成正确的json参数。还有一个Kivy脚本来显示返回的PNG图像。
答案 3 :(得分:0)
这是我每天用于下载相同指标的图像的脚本。该脚本显示了如何使用变量参数调用View as: iPhone X
并将输出转换为png文件。
aws cloudwatch get-metric-widget-image
另一种有用的分析工具,我使用它使用ImageMagick convert -compose Multiply将全部或部分图形组合为一个。例如,
function getDbDailyMetricImage
{
local date=$1
local dbId=$2
local metric=${3:-'CPUUtilization'}
local metricMin=$4
local metricMax=$5
local dateF=$(date --date="$date" +%F)
local start="${dateF}T00:00:00.000Z"
local end="${dateF}T23:59:59.999Z"
echo "Downloading image for $dbId $metric [$metricMin .. $metricMax]" \
"and Time [$start .. $end]"
aws --region us-east-1 cloudwatch get-metric-widget-image --metric-widget \
'{
"metrics": [
[ "AWS/RDS", "'$metric'", "DBInstanceIdentifier", "'$dbId'",
{ "period": 300, "yAxis": "left" } ]
],
"yAxis": {
"left": {
"min": '$metricMin',
"max": '$metricMax'
}
},
"title": "'"$dateF $metric of $dbId vs Time UTC"'",
"legend": {
"position": "hidden"
},
"view": "timeSeries",
"stacked": true,
"period": 300,
"width": 1200,
"height": 800,
"start": "'$start'",
"end": "'$end'"
}' \
--output-format png --output text | base64 --decode > $metric-$dbId-$dateF.png
}
for daysAgo in {0..30}
do
getDbDailyMetricImage $(date --date="$daysAgo days ago" +%F) mydb1 CPUUtilization 0 100
getDbDailyMetricImage $(date --date="$daysAgo days ago" +%F) mydb1 ReadIOPS 0 10000
done