aws cli cloudwatch describe-alarms 其中警报名称类似于 "something"

aws cli cloudwatch describe-alarms where alarm name is like "something"

我有一个包含 SQS 队列列表的 bash 数组:

sqsQueues=($(aws sqs list-queues | jq '.[]' --compact-output | sed 's/\[\|\]//g' | sed -E 's/"|,/ /g'))

现在我想检查该数组中的值,看看是否有任何 cloudwatch 警报的名称与 SQS 队列的名称相匹配。

基本上,我想要这样的东西:

aws cloudwatch describe-alarms --query "MetricAlarms[?AlarmName contains($filteredQueueName)]"

其中 $filteredQueueName 是我要比较的当前队列的名称。

我知道上面的 jq 代码无效。但我不确定如何询问“向我显示 AlarmName 是 LIKE $filteredQueueName 的所有 cloudwatch 警报。我在 jq 手册中阅读了命令 contains()页面,但我很难在上下文中应用它。

谢谢

我可以使用 printfecho 命令协助 inspecting/debuging 数组中的所有元素:

 printf "%s\n" ${sqsQueues[@]}

数组从0开始。

 echo "qsQueues[0]=${sqsQueues[0]}"

遍历数组:

for filteredQueueName in qsQueues; do
   echo "filteredQueueName=$filteredQueueName"
done

希望对您有所帮助。

考虑当 A​​larmName 包含 Bill:

时获取警报配置的脚本
pankaj@pankaj-mac TP % cat descalarm.sh 
filteredQueueName="Bill"
aws cloudwatch describe-alarms --query "MetricAlarms[?contains(AlarmName, \`${filteredQueueName}\`) == \`true\`]"
pankaj@pankaj-mac TP % ./descalarm.sh 
[
    {
        "AlarmName": "Billing Alarm",
        "AlarmArn": "arn:aws:cloudwatch:us-xx-1:xxxx:alarm:Billing Alarm",
        "AlarmDescription": "Send me an email if my bill goes above US",
        "AlarmConfigurationUpdatedTimestamp": "2019-12-07T05:16:57.795000+00:00",
        "ActionsEnabled": true,
        "OKActions": [],
        "AlarmActions": [
            "arn:aws:sns:us-xx-1:xxxx:Billing_Alarm"
        ],
        "InsufficientDataActions": [],
        "StateValue": "INSUFFICIENT_DATA",
        "StateReason": "Unchecked: Initial alarm creation",
        "StateUpdatedTimestamp": "2019-12-07T05:16:57.795000+00:00",
        "MetricName": "EstimatedCharges",
        "Namespace": "AWS/Billing",
        "Statistic": "Maximum",
        "Dimensions": [
            {
                "Name": "Currency",
                "Value": "USD"
            }
        ],
        "Period": 21600,
        "EvaluationPeriods": 1,
        "DatapointsToAlarm": 1,
        "Threshold": 10.0,
        "ComparisonOperator": "GreaterThanThreshold",
        "TreatMissingData": "missing"
    }
]
pankaj@pankaj-mac TP % 

另一个使用 A​​larmName 包含 xxx 获取警报的示例。没错,没有发现告警,返回空数组。

pankaj@pankaj-mac TP % cat descalarm.sh 
filteredQueueName="xxx"
aws cloudwatch describe-alarms --query "MetricAlarms[?contains(AlarmName, \`${filteredQueueName}\`) == \`true\`]"
pankaj@pankaj-mac TP % ./descalarm.sh 
[]
pankaj@pankaj-mac TP %