質問編集履歴
6
修正を追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -27,13 +27,15 @@
|
|
27
27
|
|
28
28
|
### 発生している問題・エラーメッセージ
|
29
29
|
|
30
|
-
ストリーム方式csv読み込み
|
30
|
+
ストリーム方式csv読み込み、配列に加えた後にdocumentへ入れる想定
|
31
31
|
|
32
32
|
```
|
33
33
|
エラーメッセージ:
|
34
34
|
|
35
|
-
[ERROR] Runtime.
|
35
|
+
[ERROR] Runtime.UserCodesSyntaxError : Syntax error in module `sample` : unexpected indent (sample.py, line47)
|
36
|
-
|
36
|
+
Trackback (most recent call last):
|
37
|
+
File "/var/task/sample.py" Line 47
|
38
|
+
for row in csv.reader(textIo):
|
37
39
|
|
38
40
|
```
|
39
41
|
|
@@ -48,6 +50,15 @@
|
|
48
50
|
import io
|
49
51
|
import BytesIO
|
50
52
|
from requests_aws4auth import AWS4Auth
|
53
|
+
|
54
|
+
#配列
|
55
|
+
vmanagename = []
|
56
|
+
ip = []
|
57
|
+
hostname = []
|
58
|
+
session1 = []
|
59
|
+
session2 = []
|
60
|
+
sessionmax = []
|
61
|
+
|
51
62
|
|
52
63
|
region = '' # e.g. us-west-1
|
53
64
|
service = 'es'
|
5
タイトル変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Lambda(Python)でS3の
|
1
|
+
Lambda(Python)でS3のcsvを読み込んで、Opensearchへ転送したい
|
test
CHANGED
File without changes
|
4
変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Lambda(Python)でS3(csv)をOpensearchへ転送
|
1
|
+
Lambda(Python)でS3の(csv)読み込んでをOpensearchへ転送したい
|
test
CHANGED
File without changes
|
3
編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,12 +2,13 @@
|
|
2
2
|
|
3
3
|
AWSのLambda、S3、Opensearchの使用を想定しています。
|
4
4
|
|
5
|
-
|
5
|
+
サンプルソースから、以下の動作は確認できた。
|
6
6
|
Amazon OpenSearch Service へのストリーミングデータをロードする - Amazon OpenSearch Service
|
7
7
|
https://docs.aws.amazon.com/ja_jp/opensearch-service/latest/developerguide/integrations.html
|
8
8
|
→「Amazon S3 からストリーミングデータをロードする」
|
9
9
|
1.S3へのcsvアップロードをトリガーとし
|
10
10
|
2.csvファイルをOpensearchへ転送する
|
11
|
+
|
11
12
|
という動作は確認できたのですが、
|
12
13
|
実際に利用するCSVはlogでなどではなく
|
13
14
|
以下のような単純なコンマ区切りのファイルになります。
|
@@ -15,28 +16,24 @@
|
|
15
16
|
|
16
17
|
### 実現したいこと
|
17
18
|
|
18
|
-
正規表現取り込みではなく、サンプルをもとに
|
19
|
-
コンマ区切りのcsv取り込みロジックに変更したいと考えております。
|
20
|
-
- [ ] 正規表現
|
19
|
+
- [ ] 正規表現取り込みをcsv読み取りへ変更したい。
|
21
20
|
|
22
|
-
|
21
|
+
- [ ] ゴールは、取り込んだcsvデータをOpensearch上でグラフ化(視覚化)を実現したい
|
23
|
-
|
22
|
+
※フォーマットについて随時確認中。
|
24
23
|
|
25
24
|
csv中身例:
|
26
|
-
vmanage, ip, host, session1, session2, session
|
25
|
+
vmanage name, ip, host, session1, session2, sessionmax
|
27
|
-
vmanage, 1.1.1.1, test, 10, 9,
|
26
|
+
vmanage-1, 1.1.1.1, testhost, 10, 9, 20
|
28
27
|
|
29
28
|
### 発生している問題・エラーメッセージ
|
30
29
|
|
30
|
+
ストリーム方式csv読み込みを試みるもエラーが発生している
|
31
31
|
|
32
32
|
```
|
33
33
|
エラーメッセージ:
|
34
|
-
Cloud Watchより
|
35
34
|
|
36
|
-
[ERROR]
|
35
|
+
[ERROR] Runtime.ImportModuleError: Unable to import module `sample` : No module named `BytesIO`
|
37
|
-
Trackback (most recent call last):
|
36
|
+
Trackback (most recent call last):
|
38
|
-
File "/var/task/sample.py", line 37, in handler
|
39
|
-
lines = body.splitlines(',')
|
40
37
|
|
41
38
|
```
|
42
39
|
|
@@ -47,6 +44,9 @@
|
|
47
44
|
import boto3
|
48
45
|
import re
|
49
46
|
import requests
|
47
|
+
import csv
|
48
|
+
import io
|
49
|
+
import BytesIO
|
50
50
|
from requests_aws4auth import AWS4Auth
|
51
51
|
|
52
52
|
region = '' # e.g. us-west-1
|
@@ -63,11 +63,6 @@
|
|
63
63
|
|
64
64
|
s3 = boto3.client('s3')
|
65
65
|
|
66
|
-
# Regular expressions used to parse some simple log lines
|
67
|
-
ip_pattern = re.compile('(\d+\.\d+\.\d+\.\d+)')
|
68
|
-
time_pattern = re.compile('\[(\d+\/\w\w\w\/\d\d\d\d:\d\d:\d\d:\d\d\s-\d\d\d\d)\]')
|
69
|
-
message_pattern = re.compile('\"(.+)\"')
|
70
|
-
|
71
66
|
# Lambda execution starts here
|
72
67
|
def handler(event, context):
|
73
68
|
for record in event['Records']:
|
@@ -79,56 +74,26 @@
|
|
79
74
|
# Get, read, and split the file into lines
|
80
75
|
obj = s3.get_object(Bucket=bucket, Key=key)
|
81
76
|
body = obj['Body'].read()
|
82
|
-
|
77
|
+
textIo = io.TextIOWrapper(io.BytesIO(body))
|
83
78
|
|
84
79
|
# Match the regular expressions to each line and index the JSON
|
80
|
+
for row in lines csv.reader(textIo):
|
81
|
+
print(row)
|
82
|
+
vmanagename = row[0]
|
83
|
+
ip = row[1]
|
85
|
-
|
84
|
+
hostname = row[2]
|
86
|
-
|
85
|
+
session1 = row[3]
|
86
|
+
session2 = row[4]
|
87
|
-
i
|
87
|
+
sessionmax = row[5]
|
88
|
-
timestamp = time_pattern.search(line).group(1)
|
89
|
-
message = message_pattern.search(line).group(1)
|
90
88
|
|
91
|
-
document = { "ip": ip, "t
|
89
|
+
document = { "vmanagename ": vmanagename , "ip ": ip , "hostname ": hostname , "session1 ": session1 , "session2": session2, "sessionmax ": sessionmax }
|
92
90
|
r = requests.post(url, auth=awsauth, json=document, headers=headers)
|
93
91
|
```
|
94
92
|
|
95
93
|
### 試したこと
|
96
|
-
・正規表現を廃止し
|
97
94
|
・csvのコンマ区切りを読み取り
|
98
|
-
・
|
95
|
+
・csvを辞書型にしたほうが良いか
|
99
|
-
既存のsearch(line).groupをの箇所をどのように変更すればよいか
|
100
|
-
検討がつかず。。。ご助力いただきたく。
|
101
|
-
|
102
|
-
```Python/Lambda
|
103
|
-
# Regular expressions used to parse some simple log lines
|
104
|
-
# 正規表現検索しない
|
105
|
-
# ip_pattern = re.compile('(\d+\.\d+\.\d+\.\d+)')
|
106
|
-
# time_pattern = re.compile('\[(\d+\/\w\w\w\/\d\d\d\d:\d\d:\d\d:\d\d\s-\d\d\d\d)\]')
|
107
|
-
# message_pattern = re.compile('\"(.+)\"')
|
108
|
-
|
109
|
-
# Lambda execution starts here
|
110
|
-
def handler(event, context):
|
111
|
-
for record in event['Records']:
|
112
|
-
|
113
|
-
# Get the bucket name and key for the new file
|
114
|
-
bucket = record['s3']['bucket']['name']
|
115
|
-
key = record['s3']['object']['key']
|
116
|
-
|
117
|
-
# Get, read, and split the file into lines
|
118
|
-
obj = s3.get_object(Bucket=bucket, Key=key)
|
119
|
-
body = obj['Body'].read()
|
120
|
-
# csvはカンマ区切りで
|
121
|
-
lines = body.splitlines(',')
|
122
|
-
|
123
|
-
# Match the regular expressions to each line and index the JSON
|
124
|
-
for line in lines:
|
125
|
-
line = line.decode("utf-8")
|
126
|
-
|
96
|
+
・その他の方法について検討中
|
127
|
-
ip = ip_pattern.search(line).group(1)
|
128
|
-
timestamp = time_pattern.search(line).group(1)
|
129
|
-
message = message_pattern.search(line).group(1)
|
130
|
-
```
|
131
|
-
|
132
97
|
|
133
98
|
### 補足情報(FW/ツールのバージョンなど)
|
134
99
|
|
2
試したこと、エラーを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,7 +30,14 @@
|
|
30
30
|
|
31
31
|
|
32
32
|
```
|
33
|
-
エラーメッセージ:
|
33
|
+
エラーメッセージ:
|
34
|
+
Cloud Watchより
|
35
|
+
|
36
|
+
[ERROR] TypeError : an interger is required (got type stre)
|
37
|
+
Trackback (most recent call last):
|
38
|
+
File "/var/task/sample.py", line 37, in handler
|
39
|
+
lines = body.splitlines(',')
|
40
|
+
|
34
41
|
```
|
35
42
|
|
36
43
|
### 該当のソースコード
|
@@ -92,6 +99,37 @@
|
|
92
99
|
既存のsearch(line).groupをの箇所をどのように変更すればよいか
|
93
100
|
検討がつかず。。。ご助力いただきたく。
|
94
101
|
|
102
|
+
```Python/Lambda
|
103
|
+
# Regular expressions used to parse some simple log lines
|
104
|
+
# 正規表現検索しない
|
105
|
+
# ip_pattern = re.compile('(\d+\.\d+\.\d+\.\d+)')
|
106
|
+
# time_pattern = re.compile('\[(\d+\/\w\w\w\/\d\d\d\d:\d\d:\d\d:\d\d\s-\d\d\d\d)\]')
|
107
|
+
# message_pattern = re.compile('\"(.+)\"')
|
108
|
+
|
109
|
+
# Lambda execution starts here
|
110
|
+
def handler(event, context):
|
111
|
+
for record in event['Records']:
|
112
|
+
|
113
|
+
# Get the bucket name and key for the new file
|
114
|
+
bucket = record['s3']['bucket']['name']
|
115
|
+
key = record['s3']['object']['key']
|
116
|
+
|
117
|
+
# Get, read, and split the file into lines
|
118
|
+
obj = s3.get_object(Bucket=bucket, Key=key)
|
119
|
+
body = obj['Body'].read()
|
120
|
+
# csvはカンマ区切りで
|
121
|
+
lines = body.splitlines(',')
|
122
|
+
|
123
|
+
# Match the regular expressions to each line and index the JSON
|
124
|
+
for line in lines:
|
125
|
+
line = line.decode("utf-8")
|
126
|
+
#以下の箇所をどのように変更すればよいか
|
127
|
+
ip = ip_pattern.search(line).group(1)
|
128
|
+
timestamp = time_pattern.search(line).group(1)
|
129
|
+
message = message_pattern.search(line).group(1)
|
130
|
+
```
|
131
|
+
|
132
|
+
|
95
133
|
### 補足情報(FW/ツールのバージョンなど)
|
96
134
|
|
97
135
|
なし
|
1
変更したい箇所について補足を追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -86,8 +86,11 @@
|
|
86
86
|
```
|
87
87
|
|
88
88
|
### 試したこと
|
89
|
-
・未動作
|
90
|
-
・
|
89
|
+
・正規表現を廃止し
|
90
|
+
・csvのコンマ区切りを読み取り
|
91
|
+
・読み取ったcsvを辞書型でjsonにして送信したい
|
92
|
+
既存のsearch(line).groupをの箇所をどのように変更すればよいか
|
93
|
+
検討がつかず。。。ご助力いただきたく。
|
91
94
|
|
92
95
|
### 補足情報(FW/ツールのバージョンなど)
|
93
96
|
|