简体   繁体   English

Shell脚本-根据ISO 8601 UTC格式的“时间戳”属性值对“ AWS cloudwatch指标” json数组进行排序

[英]Shell script - Sorting 'AWS cloudwatch metrics' json array based on the “Timestamp” property value which comes in ISO 8601 UTC format

I have an Amazon cloudwatch ELB Latency metrics like below. 我有一个如下所示的Amazon cloudwatch ELB 延迟指标。

{
"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"
    }
 ]

} }

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. 我正在外壳脚本中运行“ awscli”以获取此信息,但未按时间顺序返回数据,并且时间戳采用ISO 8601 UTC格式。 I need to sort this array based on the Timestamp to get the data in chronological order. 我需要根据时间戳对该数组进行排序,以按时间顺序获取数据。

My Aim: I have one more cloudwatch metrics data from ELB RequestCount metrics like below. 我的目标:我从ELB RequestCount指标多一个CloudWatch的指标数据如下图所示。

{
"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. 另外,我必须计算开始时间和结束时间之间的时间差,这可能无法从此处接收的格式中获得。

I'm using shell script and cannot figure out a way to so this. 我正在使用shell脚本,无法找到一种解决方法。 Any help would be really appreciated. 任何帮助将非常感激。 TIA TIA

JMESPATH has a sort_by method that can be used for this - here is just an example JMESPATH具有可用于此目的的sort_by方法-这仅是示例

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)[*]'

If want to go with jq instead you'll use jq's sort_by method as follow 如果要使用jq,则可以使用jq的sort_by方法,如下所示

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)[]'

If you're more a bash guy, you can use linux sort command 如果您更是一个bash家伙,则可以使用linux sort命令

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

the dates are in the 3rd column so you will sort this column( -k 3 ) 日期在第三列中,因此您将对该列进行排序( -k 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM