2017-12-24 25 views
2

저는 "스마트"로그 시스템을 구축하고 있습니다. 여기에서 고객 연결을 모니터링 할 수 있습니다. 예를 들어, 서버에 대한 연결 설정 시간을 시작 및 중지 할 수 있습니다.연결 로그 시스템 구축

RAW LOG는 : 나를 위해 중요한 무엇

Dec 19 00:00:03 172.16.20.24 pppoe,ppp,info <pppoe-customer1>: terminating... - peer is not responding 
Dec 19 00:00:03 172.16.20.24 pppoe,ppp,info,account customer1 logged out, 4486 1009521 23444247 12573 18159 
Dec 19 00:00:03 172.16.20.24 pppoe,ppp,info <pppoe-customer1>: disconnected 
Dec 19 00:00:07 172.16.20.24 pppoe,info PPPoE connection established from 60:E3:27:A2:60:09 
Dec 19 00:00:08 172.16.20.24 pppoe,ppp,info,account customer2 logged in, 10.171.3.185 
Dec 19 00:00:08 172.16.20.24 pppoe,ppp,info <pppoe-customer2>: authenticated 
Dec 19 00:00:08 172.16.20.24 pppoe,ppp,info <pppoe-customer2>: connected 
Dec 19 00:00:13 172.16.20.24 pppoe,info PPPoE connection established from C0:25:E9:7F:C0:41 
Dec 19 00:00:14 172.16.20.24 pppoe,ppp,error <ccfa>: user customer3 authentication failed 
Dec 19 00:00:32 172.16.20.24 pppoe,info PPPoE connection established from C0:25:E9:7F:C0:41 
Dec 19 00:00:36 172.16.20.24 pppoe,ppp,error <ccfb>: user customer3 authentication failed 
Dec 19 00:01:06 172.16.20.24 pppoe,info PPPoE connection established from C0:25:E9:7F:C0:41 

는 : 연결와 캡처 라인 문자열을 분리. 즉 가장 화려한 정규식하지 그래서, 난 아직 배우는 중이에요

import os 
import re 
import sys 

f = open('log.log','r') 
log = [] 
for line in f: 
if re.search(r': connected|: disconnected',line): 
    ob = dict() 
    ob['USER'] = re.search(r'<pppoe(.*?)>',line).group(0).replace("<pppoe-","").replace(">","") 
    ob['DATA'] = re.search(r'^\w{3} \d{2} \d{2}:\d{2}:\d{2}',line).group(0) 
    ob['CONNECTION'] = re.search(r': .*',line).group(0).replace(": ", "") 
    log.append(ob) 

,하지만 괜찮아요 :

나는이있어! 이 샘플에 도착하려면 지금이 로그 목록을 수정 필요 :

{"connection" : [{ 
"start" : "Dec 19 10:12:58", 
"username" : "customer2"} 

{"connection" : [{ 
"start" : "Dec 20 10:12:58", 
"username" : "customer1"} 

{"connection" : [{ 
"start" : "Dec 19 10:12:58", 
"stop" : Dec 22 10:04:35", 
"username" : "customer4"} 

{"connection" : [{ 
"start" : "Dec 19 10:12:58", 
"stop" : "Dec 24 10:04:35" 
"username" : "customer3"} 

내 장애물

    RAW 로그인이 지속적으로 생성되는
  • , 나는 경우 일부 사용자가 이미를 식별해야 존재합니다. 예 : 연결을 업데이트합니다 (고객 2가 연결을 삭제하고 등록해야 함).하지만 연결 상수가 끊어지면 어떻게됩니까? 예를 들어

:

Dec 19 10:20:58 172.16.20.24 pppoe,ppp,info <pppoe-customer2>: disconnected  
Dec 19 01:00:36 172.16.20.24 pppoe,ppp,error <ccfb>: user customer3 authentication failed 
Dec 19 01:01:06 172.16.20.24 pppoe,info PPPoE connection established from C0:25:E9:7F:C0:41 
Dec 19 10:21:38 172.16.20.24 pppoe,ppp,info <pppoe-customer2>: authenticated 
Dec 19 10:21:48 172.16.20.24 pppoe,ppp,info <pppoe-customer2>: connected 
Dec 19 10:22:38 172.16.20.24 pppoe,ppp,info <pppoe-customer3>: authenticated 
Dec 19 10:22:58 172.16.20.24 pppoe,ppp,info <pppoe-customer2>: disconnected 

우선 해제, 간단한 그것을 추가 할 수 있습니다.

다음 인증에서이 특정 사용자를 검색하고 새로운 "시작"연결 시간을 삽입하고 "중지"를 지워야합니다. 등등. 나에게

{"connection" : [{ 
"start" : "Dec 19 10:21:48" 
"username" : "customer2"} 
  • 다음 도전자, 그 새로운 발견 목록을 만들 수 있습니다.

이걸 만들려고했지만 작동하지 않습니다!

conn = [] 
for l in log: 
obcon = dict() 
if not obcon: 
    obcon['USER'] = l['USER'] 
    if l['DATA'] == 'connected': 
     obcon['START'] = l['DATA']  
     obcon['STOP'] = "" 
    else: 
     obcon['STOP'] = l['DATA'] 
conn.append(obcon) 

새 목록을 작성하기 전에 일부 사용자가 있는지 확인해야합니다. 그렇지 않은 경우 빌드 해 봅시다. 에서 [ '연결'] 나는 연결/시작을 식별 중지하는 데 사용 : 좀 더 구체적으로해야하는 경우

Disconnected -> STOP 
Connected -> START 

내가 그나마 알고있다. 아이디어가 필요합니다. 부디!

답변

1

내 의견으로는, logdict이어야하며 기존 사용자 데이터를보다 쉽게 ​​찾을 수 있습니다.
다음으로 re(...).group(0)을 어디서나 사용했으며 entire matching string입니다. 예를 들어, 사용자 이름을 추출 할 때 '<pppoe(.*?)>'을 썼지 만 group(1)에 있습니다 (정규식에서 괄호는 일치 추출에 사용됩니다).

나의 제안은 (주 - 나는 sysos의 수입을 제거 사용하지 않을으로)입니다 : 로그 파일이

import re 

f = open('log.log', 'r') 
log = dict() 
for line in f: 
    reg = re.search(r': ((?:dis)?connected)', line) # finds connected or disconnected 
    if reg is not None: 
     user = re.search(r'<pppoe-(.*?)>', line).group(1) 
     # if the user in the log, get it, else create it with empty dict 
     ob = log.setdefault(user, dict({'USER': user})) 
     ob['CONNECTION'] = reg.group(1) 
     time = re.search(r'^\w{3} \d{2} \d{2}:\d{2}:\d{2}', line).group(0) 
     if ob['CONNECTION'].startswith('dis'): 
      ob['END'] = time 
     else: 
      ob['START'] = time 
      if 'END' in ob: 
       ob.pop('END') 

경우

Dec 19 00:00:03 172.16.20.24 pppoe,ppp,info <pppoe-customer1>: terminating... - peer is not responding 
Dec 19 00:00:03 172.16.20.24 pppoe,ppp,info,account customer1 logged out, 4486 1009521 23444247 12573 18159 
Dec 19 00:00:03 172.16.20.24 pppoe,ppp,info <pppoe-customer1>: disconnected 
Dec 19 00:00:07 172.16.20.24 pppoe,info PPPoE connection established from 00:00:00:00:00:00 
Dec 19 00:00:08 172.16.20.24 pppoe,ppp,info,account customer2 logged in, 127.0.0.1 
Dec 19 00:00:08 172.16.20.24 pppoe,ppp,info <pppoe-customer2>: authenticated 
Dec 19 00:00:08 172.16.20.24 pppoe,ppp,info <pppoe-customer2>: connected 
Dec 19 00:00:13 172.16.20.24 pppoe,info PPPoE connection established from 00:00:00:00:00:00 
Dec 19 00:00:14 172.16.20.24 pppoe,ppp,error <ccfa>: user customer3 authentication failed 
Dec 19 00:02:03 172.16.20.24 pppoe,ppp,info,account customer2 logged out, 4486 1009521 23444247 12573 18159 
Dec 19 00:02:03 172.16.20.24 pppoe,ppp,info <pppoe-customer2>: disconnected 
Dec 19 00:02:08 172.16.20.24 pppoe,ppp,info,account customer3 logged in, 127.0.0.1 
Dec 19 00:02:08 172.16.20.24 pppoe,ppp,info <pppoe-customer3>: authenticated 
Dec 19 00:02:08 172.16.20.24 pppoe,ppp,info <pppoe-customer3>: connected 

log의 값 다음과 같이됩니다 :

{ 
    'customer1': { 
     'CONNECTION': 'disconnected', 
     'END': 'Dec 19 00:00:03', 
     'USER': 'customer1' 
    }, 
    'customer3': { 
     'START': 'Dec 19 00:02:08', 
     'CONNECTION': 'connected', 
     'USER': 'customer3' 
    }, 
    'customer2': { 
     'START': 'Dec 19 00:00:08', 
     'CONNECTION': 'disconnected', 
     'END': 'Dec 19 00:02:03', 
     'USER': 'customer2' 
    } 
} 
+0

이것은 아주 좋습니다! 하지만 또 다른 질문이 있습니다. 나는 11 LOGs 비슷한, 당신이 어떻게 생각하십니까? 함수에 넣고 각 LOG에 "인스턴스화"해야합니까? 로그에 행이 추가 될 때마다 FOR를 다시 수행합니까? 이해 했습니까, 아니면 혼란 스럽습니까? – TMoraes

+0

@TMoraes 흠, 나는 당신의 필요를 정확히 이해할 수 없습니다. 다시 설명해주세요. – DjLegolas

+0

질문을 업데이트 할 것입니다. – TMoraes