如何使用 aws cloudwatch get-metric-widget-image?

How to use aws cloudwatch get-metric-widget-image?

我想自动获取 Cloudwatch 屏幕截图,因为我有很多实例。

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

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

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

我无法从 aws doc 中找到示例。下面没有确切的例子 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" 

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"

为您的请求获取正确 json 的最佳方法是使用 CloudWatch 控制台构建图表,然后单击 Source 选项卡,select 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([=12=],a,"\""); print a[4]}' | base64 --decode > graph.png

这里有一个不同的答案。在 https://github.com/kcrossen/CloudWatch_Remote_Monitor/blob/master/Source_Code/ 中有一个 Python 脚本,它将消化上面 Tartaglia 提到的仪表板源代码,并为 GetMetricWidgetImage 生成正确的 json 参数。还有一个 Kivy 脚本来显示返回的 PNG 图像。

这是我用来下载每天相同指标的图像的脚本。该脚本显示了如何使用可变参数调用 aws cloudwatch get-metric-widget-image 并将输出转换为 png 文件。

function getDbDailyMetricImage
{
    local date=
    local dbId=
    local metric=${3:-'CPUUtilization'}
    local metricMin=
    local metricMax=

    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

另一个有用的分析工具,我使用它通过 ImageMagick convert -compose Multiply 将所有或部分图表组合成一个。例如,

convert ReadIOPS-mydb1-2019-0*.png -compose Multiply -layers flatten ReadIOPS-mydb1-2019-composite.png