简体   繁体   English

Shell 脚本 - 基于“时间戳”属性对“AWS cloudwatch 指标”json 数组进行排序,原始输出包括统计信息

[英]Shell script - Sorting 'AWS cloudwatch metrics' json array based on the “Timestamp” property with raw output including statistics

I am running aws cli aws cloudwatch get-metric-statistics --metric-name CPUUtilization --start-time 2010-02-20T12:00:00 --end-time 2010-02-20T15:00:00 --period 60 --namespace AWS/EC2 --extended-statistics p80 --dimensions Name=InstanceId,Value=i-0b123423423我正在运行 aws cli aws cloudwatch get-metric-statistics --metric-name CPUUtilization --start-time 2010-02-20T12:00:00 --end-time 2010-02-20T15:00:00 --period 60 --namespace AWS/EC2 --extended-statistics p80 --dimensions Name=InstanceId,Value=i-0b123423423

the output comes as输出为

{
  "Label": "CPUUtilization",
  "Datapoints": [
    {
      "Timestamp": "2020-02-20T12:15:00Z",
      "Unit": "Percent",
      "ExtendedStatistics": {
        "p80": 0.16587132264856133
      }
    },

How do i get the output in the below format's (2 Columns)我如何获得以下格式的输出(2 列)

19.514049550078127  2020-02-13T20:15:00Z
12.721997782508938  2020-02-13T19:15:00Z
13.318820949213313  2020-02-13T18:15:00Z
15.994192991030545  2020-02-13T17:15:00Z
18.13096421299414   2020-02-13T16:15:00Z

with Heading as CPUUtilization (2 columns)标题为 CPUUtilization(2 列)

CPUUtilization
19.514049550078127  2020-02-13T20:15:00Z
12.721997782508938  2020-02-13T19:15:00Z
13.318820949213313  2020-02-13T18:15:00Z
15.994192991030545  2020-02-13T17:15:00Z
18.13096421299414   2020-02-13T16:15:00Z

And in single column并在单列中

19.514049550078127  
12.721997782508938  
13.318820949213313  
15.994192991030545  
18.13096421299414   

How can achieve this ?怎样才能做到这一点?

Assuming the input file is input.json , then:假设输入文件是input.json ,那么:

To output in the 2 columns format:要以 2 列格式输出:

jq -r  '.Datapoints[] | [.ExtendedStatistics.p80, .Timestamp] | @tsv' input.json | sort -nr

With Heading as CPUUtilization (2 columns):标题为 CPUUtilization(2 列):

echo CPUUtilization; jq -r  '.Datapoints[] | [.ExtendedStatistics.p80, .Timestamp] | @tsv' input.json | sort -nr 

And in single column:在单列中:

jq -r  '.Datapoints[] | [.ExtendedStatistics.p80] | @tsv' input.json | sort -nr

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

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