cheatsheet/aws/cloudwatch

ログストリームをまたいで検索

下記を参照 [新機能]CloudWatch Logs がアップデート、ログストリームをまたいで検索できるように! | Developers.IO

更に時間を指定する場合、–start-time と –end-time をつけると絞り込みができる。

この時刻は UTC で Unix date をミリ秒で表現する必要がある( 1000 倍する )

04/19 16:30 の場合 ( unix date 出して、UTC との時差、9 時間を引いて、ミリ秒にしている echo $(( ($(date -d “2017-04-19 16:30” +%s) - 60 _ 60 _ 9) * 1000 )) => 1492554600000

04/19 17:30 の場合 echo $(( ($(date -d “2017-04-19 17:30” +%s) - 60 _ 60 _ 9) * 1000 )) => 1492558200000

検索例) aws logs filter-log-events –region ap-northeast-1 –log-group-name sns/ap-northeast-1/11111111111111/app/APNS/ExampleProduction –max-items 300000 –start-time 1492554600000 –end-time 1492558200000

–log-group-name の値は AWS WebUI の CloudWatch -> ログ で ロググループ一覧が確認できますので目的のグループ名をコピーします。

json で下記のような内容が帰ってきます。

        {
            "ingestionTime": 1492557762448,
            "timestamp": 1492557754740,
            "message": "{\"notification\":{\"messageMD5Sum\":\"fc53b1c97882fcb26f4e4cc09a28ed33\",\"messageId\":\"64d9dfc6-9131-52f6-b0f8-8eb630c9bb99\",\"timestamp\":\"2017-04-18 23:22:32.637\"},\"delivery\":{\"deliveryId\":\"da0c4e61-c32f-574c-a284-1f1162d59c54\",\"destination\":\"arn:aws:sns:ap-northeast-1:111111111111111:endpoint/APNS/ExampleProduction/081dd629-e5f3-3183-aed7-7856b350c1c0\",\"dwellTimeMs\":428,\"attempts\":1,\"token\":\"73218b54fdeca94c8bf0118bbcf9517c8e7f839644cd8958499e034450166d86\"},\"status\":\"SUCCESS\"}",
            "eventId": "33285150182547702425068028516826924020442338242811985920",
            "logStreamName": "87"
        }

ので、下記のようにすると件数が出ます。

aws logs filter-log-events --region ap-northeast-1 --log-group-name sns/ap-northeast-1/1111111111111111111/app/APNS/ExampleProduction --max-items 300000 --start-time 1492554600000 --end-time 1492558200000 | grep 'eventId' | wc -l