Skip to content

Commit d467cf5

Browse files
authored
feat: EFK update
1 parent 571fc2b commit d467cf5

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

Server.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from flask import Flask, request, session
22
from flask_socketio import SocketIO, emit, join_room
3-
# from elasticsearch import Elasticsearch
4-
# from elasticsearch import helpers
3+
from elasticsearch import Elasticsearch
4+
from elasticsearch import helpers
55
import datetime
66
import redis
77
import requests
@@ -44,18 +44,18 @@ class Settings(BaseSettings):
4444

4545

4646
### elk, kibana
47-
# es = Elasticsearch(f'{ES_IP}:{ES_PORT}') ## 변경
48-
# es.info()
47+
es = Elasticsearch(f'{ES_IP}:{ES_PORT}') ## 변경
48+
es.info()
4949

50-
# def utc_time():
51-
# return datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
50+
def utc_time():
51+
return datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
5252

53-
# def make_index(es, index_name):
54-
# if es.indices.exists(index=index_name):
55-
# es.indices.delete(index=index_name)
56-
# es.indices.create(index=index_name)
53+
def make_index(es, index_name):
54+
if es.indices.exists(index=index_name):
55+
es.indices.delete(index=index_name)
56+
es.indices.create(index=index_name)
5757

58-
# index_name= 'webrtc_room'
58+
index_name= 'webrtc_room'
5959

6060
@app.route('/')
6161
def hello():
@@ -79,12 +79,12 @@ def on_create_room(data):
7979
create_room_request(data, request.sid)
8080

8181

82-
# elk
83-
# room_id = data["room_id"]
84-
# date = datetime.datetime.now()
85-
# now = date.strftime('%m/%d/%y %H:%M:%S')
86-
# doc_create= {"des":"create room", "room_id":room_id, "@timestamp": utc_time()}
87-
# es.index(index=index_name, doc_type="log", body=doc_create)
82+
# elk
83+
room_id = data["room_id"]
84+
date = datetime.datetime.now()
85+
now = date.strftime('%m/%d/%y %H:%M:%S')
86+
doc_create= {"des":"create room", "room_id":room_id, "@timestamp": utc_time()}
87+
es.index(index=index_name, doc_type="log", body=doc_create)
8888

8989

9090
@socketio.on("join-room")
@@ -105,10 +105,10 @@ def on_join_room(data):
105105
print("[{}] New member joined: {}<{}>".format(room_id, display_name, sid))
106106

107107
### elk
108-
# date = datetime.datetime.now()
109-
# now = date.strftime('%m/%d/%y %H:%M:%S')
110-
# doc_join= {"des":"New member joined", "room_id":room_id, "sid": sid, "@timestamp": utc_time()}
111-
# es.index(index=index_name, doc_type="log", body=doc_join)
108+
date = datetime.datetime.now()
109+
now = date.strftime('%m/%d/%y %H:%M:%S')
110+
doc_join= {"des":"New member joined", "room_id":room_id, "sid": sid, "@timestamp": utc_time()}
111+
es.index(index=index_name, doc_type="log", body=doc_join)
112112
emit("user-connect", {"sid": sid, "name": display_name}, broadcast=True, include_self=False, room=room_id)
113113

114114
message = {
@@ -144,10 +144,10 @@ def on_disconnect():
144144
display_name = names_sid[sid]
145145

146146
### elk
147-
# now = datetime.datetime.now()
148-
# now = now.strftime('%m/%d/%y %H:%M:%S')
149-
# doc_disconnect= {"des":"user-disconnect", "room_id":room_id, "sid": sid, "@timestamp": utc_time()}
150-
# es.index(index=index_name, doc_type="log", body=doc_disconnect)
147+
now = datetime.datetime.now()
148+
now = now.strftime('%m/%d/%y %H:%M:%S')
149+
doc_disconnect= {"des":"user-disconnect", "room_id":room_id, "sid": sid, "@timestamp": utc_time()}
150+
es.index(index=index_name, doc_type="log", body=doc_disconnect)
151151

152152
print("[{}] Member left: {}<{}>".format(room_id, display_name, sid))
153153
message = {
@@ -194,10 +194,10 @@ def send_message(message):
194194
room_id = message["room_id"]
195195

196196
### elk
197-
# date = datetime.datetime.now()
198-
# now = date.strftime('%m/%d/%y %H:%M:%S')
199-
# doc_chatting= {"des" : "chatting", "room_id" : room_id, "chatting message" : text,"@timestamp": utc_time()}
200-
# es.index(index=index_name, doc_type="log", body=doc_chatting)
197+
date = datetime.datetime.now()
198+
now = date.strftime('%m/%d/%y %H:%M:%S')
199+
doc_chatting= {"des" : "chatting", "room_id" : room_id, "chatting message" : text,"@timestamp": utc_time()}
200+
es.index(index=index_name, doc_type="log", body=doc_chatting)
201201

202202
data = {
203203
"text": text,
@@ -266,4 +266,4 @@ def exit_room(socketID):
266266
host="0.0.0.0",
267267
port=5000
268268
)
269-
# make_index(es, index_name)
269+
make_index(es, index_name)

0 commit comments

Comments
 (0)