本文介绍了Shell脚本-根据“时间戳"对"AWS cloudwatch指标" json数组进行排序ISO 8601 UTC格式的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Amazon cloudwatch ELB 延迟指标,如下所示.

I have an Amazon cloudwatch ELB Latency metrics like below.

{
"Datapoints": [
    {
        "Timestamp": "2016-10-18T12:11:00Z",
        "Average": 0.25880099632013942,
        "Minimum": 0.00071811676025390625,
        "Maximum": 3.2039437294006352,
        "Unit": "Seconds"
    },
    {
        "Timestamp": "2016-10-18T12:10:00Z",
        "Average": 0.25197337517680762,
        "Minimum": 0.00063610076904296875,
        "Maximum": 2.839790821075439,
        "Unit": "Seconds"
    },
    {
        "Timestamp": "2016-10-18T12:19:00Z",
        "Average": 0.2287127116954388,
        "Minimum": 0.00061678886413574219,
        "Maximum": 1.416410446166992,
        "Unit": "Seconds"
    }
 ]

}

我正在shell脚本中运行'awscli'来获取此消息,但数据未按时间顺序返回,并且时间戳采用ISO 8601 UTC格式.我需要根据时间戳对该数组进行排序,以按时间顺序获取数据.

i'm running 'awscli' inside shell script for getting this , but data is not returned in chronological order and timestamp is in ISO 8601 UTC format. I need to sort this array based on the Timestamp to get the data in chronological order.

我的目标:我从ELB RequestCount 指标中获得了另外一个cloudwatch指标数据,如下所示.

My Aim:I have one more cloudwatch metrics data from ELB RequestCount metrics like below.

{
"Datapoints": [
    {
        "Timestamp": "2016-10-18T12:11:00Z",
        "Sum": 217732.0,
        "Unit": "Count"
    },
    {
        "Timestamp": "2016-10-18T12:15:00Z",
        "Sum": 227120.0,
        "Unit": "Count"
    },
  ]

}

我一直在试图根据时间戳对这些指标进行排序,并在每个时间戳的等待时间和请求数量之间创建一个匹配项.另外,我必须计算开始时间和结束时间之间的时间差,这可能是从此处收到的格式无法实现的.

I was looking to sort these metrices based on the timestamp and create a match between the latency and number of request at each timestamp. Also , i have to calculate the time difference between the starttime and endtime which might not be possible from the format that is received here.

我正在使用Shell脚本,无法找到一种解决办法.任何帮助将非常感激. TIA

I'm using shell script and cannot figure out a way to so this. Any help would be really appreciated. TIA

推荐答案

JMESPATH具有 sort_by 方法可用于此目的-这里仅是一个示例

JMESPATH has a sort_by method that can be used for this - here is just an example

aws cloudwatch get-metric-statistics \
  --metric-name CPUUtilization \
  --start-time 2016-10-01T23:18:00 --end-time 2016-10-19T23:18:00 --period 3600 \
  --namespace AWS/EC2 --statistics Maximum \
  --dimensions Name=InstanceId,Value=<YOURINSTANCE> \
  --query 'sort_by(Datapoints,&Timestamp)[*]'

如果要使用jq,则可以使用jq的 sort_by 方法如下

If want to go with jq instead you'll use jq's sort_by method as follow

aws cloudwatch get-metric-statistics \
  --metric-name CPUUtilization \
  --start-time 2016-10-01T23:18:00 --end-time 2016-10-19T23:18:00 --period 3600 \
  --namespace AWS/EC2 --statistics Maximum \
  --dimensions Name=InstanceId,Value=<YOURINSTANCE> \
| jq -r '.Datapoints | sort_by(.Timestamp)[]'

如果您更是一个bash家伙,则可以使用linux sort命令

If you're more a bash guy, you can use linux sort command

aws cloudwatch get-metric-statistics \
  --metric-name CPUUtilization \
  --start-time 2016-10-01T23:18:00 --end-time 2016-10-19T23:18:00 --period 3600 \
  --namespace AWS/EC2 --statistics Maximum \
  --dimensions Name=InstanceId,Value=<YOURINSTANCE> \
  --output text \
| sort -k 3

日期在第三列中,因此您将对该列进行排序(-k 3)

the dates are in the 3rd column so you will sort this column(-k 3)

这篇关于Shell脚本-根据“时间戳"对"AWS cloudwatch指标" json数组进行排序ISO 8601 UTC格式的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 08:26