浏览代码

add ElasticSearch-CW.py.

xdc 5 年之前
父节点
当前提交
38986c7d3d
共有 1 个文件被更改,包括 31 次插入0 次删除
  1. 31 0
      ElasticSearch-CW.py

+ 31 - 0
ElasticSearch-CW.py

@@ -0,0 +1,31 @@
+import boto3
+import re,json,base64,gzip
+import requests
+from requests_aws4auth import AWS4Auth
+
+region = 'cn-northwest-1'
+service = 'es'
+credentials = boto3.Session().get_credentials()
+awsauth = AWS4Auth(credentials.access_key,credentials.secret_key,region,service,session_token=credentials.token)
+
+host = 'https://search-es-test-rssutkotldfmnca64hpfbi5jya.cn-northwest-1.es.amazonaws.com.cn'
+index = 'lambda-cw'
+type_a = 'lambda-type'
+url = host + '/' + index + '/' + type_a
+
+headers = {"Content-Type":"application/json"}
+
+
+def lambda_handler(event, context):
+
+    de_content = base64.b64decode(event['awslogs']['data'])
+    ret = json.loads(gzip.decompress(de_content).decode('utf8'))
+
+    # log_msg = ret['logEvents'][0]['message']
+    # tup_log = log_msg.split(' ', -1)
+    # tup_log = json.loads(tup_log)
+    # print(type(tup_log))
+
+    r = requests.post(url,auth=awsauth,json=ret,headers=headers)
+    print(r)
+