Przeglądaj źródła

Adds a new script cwlogs.py for pulling cloudwatch logs to a file

I made this to help myself, Brad asked me to share it.
Duane Waddle 3 lat temu
rodzic
commit
1a3f6d8bc9
1 zmienionych plików z 45 dodań i 0 usunięć
  1. 45 0
      bin/cwlogs.py

+ 45 - 0
bin/cwlogs.py

@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+#----------------------------------------------------------------------------
+# Dumps a cloudwatch logs log group to a text file.  The main intention for
+# this is to make codebuild-in-packer use cases easier.  The CWL UI is not
+# optimized for viewing long logfiles.
+#
+# Use something like
+# AWS_PROFILE=mdr-common-services-gov ./cwlogs.py       \
+#    /aws/codebuild/xdr-ec2-threatq-image               \
+#    52a94d94-cacb-4cff-8b35-f5aeb39c9c00
+#----------------------------------------------------------------------------
+import sys
+import boto3
+
+if __name__ == "__main__":
+
+    client = boto3.client('logs')
+
+    keepgoing = True
+    nextToken = None
+
+    while keepgoing is True:
+
+        kwargs = {
+                'logGroupName': sys.argv[1],
+                'logStreamName': sys.argv[2],
+                'startFromHead': True
+        }
+
+        if nextToken is not None:
+            kwargs['nextToken'] = nextToken
+        elif 'nextToken' in kwargs:
+            kwargs.pop('nextToken')
+
+        resp=client.get_log_events(**kwargs)
+        if 'events' in resp:
+            for e in resp.get('events'):
+                print(e.get('message'),end='')
+            if len(resp.get('events')) == 0:
+                keepgoing = False
+
+        if 'nextForwardToken' in resp:
+            nextToken = resp['nextForwardToken']
+        else:
+            nextToken = None