shell 处理json格式数据

#!/bin/bash
# 查看大客户(A B C D E F)截图是否失败
# author xiaozhi,last update on 2017-10-13,16:16:16
# version:1.0
# value: 0 正常;1 错误;//5 接口异常
export PATH=/home/caozhi/ven_work/bin:/usr/local/java/bin:/usr/local/java/jre/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/bin:/home/caozhi/bin

ts=`date -d '1 min ago' +%s`
metric="video_gout_screenshot_whether_success"
endpoint=`hostname`

function post_curl () {
    curl -X POST -d "[{\"metric\": \"$metric\", \"endpoint\": \"$endpoint\", \"timestamp\": $ts,\"step\": 600,\"value\": $value,\"counterType\": \"GAUGE\",\"tags\": \"$tags\"}]" http://127.0.0.1:1988/v1/push
}

cat > /home/caozhi/emmby/ssjs.json <<-EOF
{
  "size": 0,
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "Logstash.Norm.event:4 AND Logstash.Norm.unName:(A B C D E F) AND Logstash.taskId:\"\"",
          "analyze_wildcard": true
        }
      },
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "@timestamp": {
                  "gte": $(($ts-300))000,
                  "lte": $(($ts))000,
                  "format": "epoch_millis"
                }
              }
            }
          ],
          "must_not": []
        }
      }
    }
  },
  "aggs": {
    "2": {
      "terms": {
        "field": "Logstash.Norm.unName",
        "size": 0,
        "order": {
          "_count": "desc"
        }
      },
      "aggs": {
        "3": {
          "terms": {
            "field": "Logstash.Norm.name",
            "size": 0,
            "order": {
              "_count": "desc"
            }
          }
        }
      }
    }
  }
}
EOF

# 调用es接口
curl -X POST 'http://10.64.2.150:9200/**log*/_search?pretty' -d @/home/caozhi/emmby/ssjs.json -w "http_code=%{http_code}" 2>&1 > /home/caozhi/lizis

/bin/grep "http_code=200" /home/caozhi/lizis
    if [ $? -eq 0 ]
        then
        # 查看是否有截图失败3次以上的 (截图失败会重试2次)
            /bin/grep '\"doc_count\"' /home/caozhi/lizis | awk  -F ":" '$1!~/^        "doc_count"/{print $0}' | /bin/grep -Eo '\"doc_count\" : ([3-9]|[1-9]{2,})' && sed -nr '1N;:t;$!N;s/.*"doc_count" : (1|2)$/"doc_count" : 1/;tt;P;D' /home/caozhi/lizis > /home/caozhi/sslizi
                if [ $? -eq 0 ]
                    then
                    # 提取失败3次以上的unname和stream_name (只取5条信息)
                    sed 's/\"doc_count\" : 1//' /home/caozhi/sslizi | awk '$1~/key/{print }' | awk  -F ":" '{if($1~/^        "key"/){k=$2}else{print k$2}}' | sed 's/"//g'| sed 's/,//2' | sed 's/[[:space:]]*//g' | sed 's/,/_/' | tail -n 5 > /home/caozhi/ssmessage
                        # 将失败的发到falcon上
                        while read line; do
                            tags=`echo stream=$line`
                            value=1
                            post_curl
                            sleep 1
                            done < /home/caozhi/ssmessage
                    else
                        value=0
                        post_curl
                fi
        else
            value=0
            post_curl
    fi

rm -f /home/caozhi/lizis
rm -f /home/caozhi/sslizi
rm -f /home/caozhi/ssmessage
rm -f /home/caozhi/emmby/ssjs.json

以下是返回的json串,把用户名和流名提取出来

{
  "took" : 532,
  "timed_out" : false,
  "_shards" : {
    "total" : 800,
    "successful" : 800,
    "failed" : 0
  },
  "hits" : {
    "total" : 42,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "2" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ {
        "key" : "A客户",
        "doc_count" : 2,
        "3" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [ {
            "key" : "-gSMDqTPOHU",
            "doc_count" : 1
          }, {
            "key" : "zirx63ztWdw",
            "doc_count" : 1
          } ]
        }
      }, {
        "key" : "B客户",
        "doc_count" : 4,
        "3" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [ {
            "key" : "02855ada239c4b98a11a61148cc6a190",
            "doc_count" : 2
          }, {
            "key" : "17bf24bfe1dc44b79b697b68d3f70c2f",
            "doc_count" : 1
          }, {
            "key" : "3c923d582e744e22b2cae4d444cc3887",
            "doc_count" : 1
          } ]
        }
      }, {
        "key" : "C客户",
        "doc_count" : 9,
        "3" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [ {
            "key" : "live_118573_4109338",
            "doc_count" : 5
          }, {
            "key" : "live_1850704_6444580",
            "doc_count" : 3
          }, {
            "key" : "live_333906_9822127",
            "doc_count" : 1
          } ]
        }
      } ]
    }
  }
}
http_code=200

猜你喜欢

转载自blog.csdn.net/xiaozhiit/article/details/78370558