Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 查询AWS ELB目标组目标健康状态,并根据某些条件执行功能_Json_Amazon Web Services_Shell_Jq_Amazon Elb - Fatal编程技术网

Json 查询AWS ELB目标组目标健康状态,并根据某些条件执行功能

Json 查询AWS ELB目标组目标健康状态,并根据某些条件执行功能,json,amazon-web-services,shell,jq,amazon-elb,Json,Amazon Web Services,Shell,Jq,Amazon Elb,我有一个需求,需要根据AWS中目标组内目标的targetAlth状态调用某些函数或(比如)命令。我执行以下操作以获取目标组中的目标列表:- aws elbv2 --region us-east-1 describe-target-health --target-group-arn=****** { "TargetHealthDescriptions": [{ "Target": { "Id": "***",

我有一个需求,需要根据AWS中目标组内目标的
targetAlth
状态调用某些函数或(比如)命令。我执行以下操作以获取目标组中的目标列表:-

aws elbv2 --region us-east-1 describe-target-health --target-group-arn=******

{
    "TargetHealthDescriptions": [{
            "Target": {
                "Id": "***",
                "Port": 80
            },
            "TargetHealth": {
                "State": "initial",
                "Reason": "Elb.RegistrationInProgress",
                "Description": "Target registration is in progress"
            }
        },
        {
            "Target": {
                "Id": "***",
                "Port": 80
            },
            "TargetHealth": {
                "State": "unhealthy",
                "Reason": "Target.Timeout",
                "Description": "Connection to target timed out"
            }
        },
        {
            "HealthCheckPort": "80",
            "Target": {
                "Id": "***",
                "Port": 80
            },
            "TargetHealth": {
                "State": "healthy"
            }
        },
        {
            "HealthCheckPort": "80",
            "Target": {
                "Id": "***",
                "Port": 80
            },
            "TargetHealth": {
                "State": "healthy"
            }
        },
        {
            "HealthCheckPort": "80",
            "Target": {
                "Id": "***",
                "Port": 80
            },
            "TargetHealth": {
                "State": "healthy"
            }
        },
        {
            "HealthCheckPort": "80",
            "Target": {
                "Id": "***",
                "Port": 80
            },
            "TargetHealth": {
                "State": "healthy"
            }
        },
        {
            "HealthCheckPort": "80",
            "Target": {
                "Id": "***",
                "Port": 80
            },
            "TargetHealth": {
                "State": "healthy"
            }
        }
    ]
}
一旦我得到了所有目标的JSON响应,我想做一个If条件,以确保只有当目标数>4且所有目标都正常时,才执行xyz。。。所以说像

if ( countoftarget >4 && alltargethasstate == healthy) then 
    echo "*****" 
else 
    sleep 2 minutes and keep checking the loop

有没有一种方法可以使用
jq
解析所有目标的state值,并实现一个shell脚本来执行此循环条件。

这里有一个小片段,可以帮助您在不使用
jq
的情况下实现此目的

HEALTHY_COUNT=`aws elbv2 --region us-east-1 describe-target-health --target-group-arn=******* \
--query 'TargetHealthDescriptions[?TargetHealth.State==\`healthy\`].[Target.Id]' --output text | wc -l`
echo $HEALTHY_COUNT

if [ $HEALTHY_COUNT -eq 1 ]; then
    echo "All izzz well!!!"
else 
    echo "Recheck how many healthy"
fi
这里有趣的部分是
查询
,用于控制was cli的输出。请参阅此部分的详细信息,特别是本节
“如何使用--query选项筛选输出”。查询选项基于此处记录的内容,在使用AWS CLI时非常漂亮。

这里有一个小剪贴,可以帮助您在不使用
jq
的情况下实现这一点

HEALTHY_COUNT=`aws elbv2 --region us-east-1 describe-target-health --target-group-arn=******* \
--query 'TargetHealthDescriptions[?TargetHealth.State==\`healthy\`].[Target.Id]' --output text | wc -l`
echo $HEALTHY_COUNT

if [ $HEALTHY_COUNT -eq 1 ]; then
    echo "All izzz well!!!"
else 
    echo "Recheck how many healthy"
fi
这里有趣的部分是
查询
,用于控制was cli的输出。请参阅此部分的详细信息,特别是本节
“如何使用--query选项筛选输出”。查询选项基于此处记录的内容,在使用AWS CLI时非常漂亮。

此处使用jq的潜在优势之一是可以轻松避免多次调用AWS

[.TargetHealthDescriptions[]
| select(.TargetHealth.State == "healthy")]
| . as $targets # in case you want to do something with them if the counting condition is satisfied
| length | if . > 4 then "found \(.) targets" else empty end

在这里使用jq的潜在优势之一是,您可以轻松避免多次调用aws

[.TargetHealthDescriptions[]
| select(.TargetHealth.State == "healthy")]
| . as $targets # in case you want to do something with them if the counting condition is satisfied
| length | if . > 4 then "found \(.) targets" else empty end

我想做一些类似的事情,但我想继续循环以检查目标组中的所有实例是否正常,然后我想继续。我该怎么做?我想做一些类似的事情,但我想继续循环以检查目标组中的所有实例是否正常,然后我想继续。我该怎么做?