如何使用AWS CloudWatch get-metric-widget-image? [英] How to use aws cloudwatch get-metric-widget-image?

查看:251
本文介绍了如何使用AWS CloudWatch get-metric-widget-image?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我有很多实例,我想自动获取Cloudwatch屏幕快照。

I would like to get Cloudwatch screenshot automatically since I have many instances.

但是当我尝试通过aws cli命令工具运行get-metric-widget-image时,我总是会出错。

But when I try to run get-metric-widget-image by aws cli command tool, I always get error.


调用GetMetricWidgetImage操作时发生错误(ValidationError):MetricWidget属性'metricWidget'具有错误的JSON内容。

An error occurred (ValidationError) when calling the GetMetricWidgetImage operation: MetricWidget property 'metricWidget' has a bad JSON content.

有人可以帮助我吗?谢谢。

Is there anyone who could help me out? Thanks.

我无法从AWS文档中找到示例。下面的链接中没有确切的例子。
https://docs.aws .amazon.com / AmazonCloudWatch / latest / APIReference / CloudWatch-Metric-Widget-Structure.html

I could not find an example from aws doc. No exact example in below link. 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" 


推荐答案

为您的请求获取正确json的最佳方法是使用CloudWatch Console构造图形,然后单击 Source 标签,选择 Image API 视图,然后单击 Copy Source 复制在那里生成的json。您还需要将json用单引号引起来,例如:

The best way to get the correct json for your request is to use CloudWatch Console to construct the graph, then click on the Source tab, select Image API view and click Copy Source to copy the json generated there. You also need to wrap the json in single quotes, like this:

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编码的图像,例如:

Response to this will be a base64 encoded image, like this:

{
    "MetricWidgetImage": "iVBORw0KGgoAAAANSUhEUgAAAlgAAAGLEAYA..."
}

如果您需要原始png图片,您需要通过执行以下操作来解码响应:

If you need the raw png image, you'll need to decode the response by doing something like this:

aws cloudwatch get-metric-widget-image --metric-widget 'JSON_GOES_HERE' | grep MetricWidgetImage | awk '{split($0,a,"\""); print a[4]}' | base64 --decode > graph.png

这篇关于如何使用AWS CloudWatch get-metric-widget-image?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆