Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing a syslog file
#1
Hi,

I'm trying to extract data from a system log file but, I cannot get the syntax right.
Particularly, I'm trying to get the username and its timestamp value so I can save them into a DB.

I appreciate some help.
TIA

filename:
Oct 10 08:51:04 washup20 Node-RED[17201]: 10 Oct 08:51:04 - [info] Started flows
Oct 10 08:51:04 washup20 Node-RED[17201]: 10 Oct 08:51:04 - [info] [mqtt-broker:server] Connected to broker: mqtt://192.168.1.230:1883
Oct 10 08:51:04 washup20 Node-RED[17201]: 10 Oct 08:51:04 - [audit] {"event":"comms.open","level":98,"timestamp":1633848664512}
Oct 10 08:51:04 washup20 Node-RED[17201]: 10 Oct 08:51:04 - [audit] {"event":"comms.auth","user":{"username":"admin","permissions":"*"},"level":98,"timestamp":1633848664540}
Oct 10 08:51:04 washup20 Node-RED[17201]: 10 Oct 08:51:04 - [info] [remote-access:Remote access] Using nodered02.remote-red.com on port 59153
Oct 10 08:51:04 washup20 Node-RED[17201]: 10 Oct 08:51:04 - [info] [remote-access:Remote access] starting ssh process
Oct 10 08:55:14 washup20 Node-RED[17201]: 10 Oct 08:55:14 - [audit] {"event":"auth.login.revoke","level":98,"user":{"username":"admin","permissions":"*"},"path":"/auth/revoke","ip":"192.168.1.28","timestamp":1633848914062}
import re
import json

filename = "systemfile.log"

# strip unneeded text from json format and save audit lines only
re_line= re.compile("audit")
data = []
with open(filename, "r") as in_file:
    # Loop over each log line
    for line in in_file:
        if re_line.search(line):
            data.append(line)

print(data)  # so far so good

for i in range(len(data)):
    print('user= {}'.format(int(data[0][i])))  #  <-- syntax error 


# for i in range(len(data)):
#     username = user[username]
#     timestamp =

# with open(data) as audits:
#     for line in audits:
#         audit = json.loads(line)
#         # process event dictionary
# print(audit['username']['timestamp'])
Reply


Messages In This Thread
Parsing a syslog file - by ebolisa - Oct-10-2021, 08:48 AM
RE: Parsing a syslog file - by snippsat - Oct-10-2021, 10:33 AM
RE: Parsing a syslog file - by ebolisa - Oct-10-2021, 10:49 AM
RE: Parsing a syslog file - by ndc85430 - Oct-10-2021, 10:53 AM
RE: Parsing a syslog file - by ebolisa - Oct-10-2021, 10:56 AM
RE: Parsing a syslog file - by snippsat - Oct-10-2021, 11:25 AM
RE: Parsing a syslog file - by ebolisa - Oct-10-2021, 11:43 AM
RE: Parsing a syslog file - by ebolisa - Oct-10-2021, 12:37 PM
RE: Parsing a syslog file - by snippsat - Oct-10-2021, 01:46 PM
RE: Parsing a syslog file - by ebolisa - Oct-10-2021, 03:14 PM
RE: Parsing a syslog file - by ebolisa - Oct-10-2021, 04:11 PM
RE: Parsing a syslog file - by snippsat - Oct-10-2021, 05:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Video doing data treatment on a file import-parsing a variable EmBeck87 15 3,135 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,827 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Parsing xml file deletes whitespaces. How to avoid it? Paqqno 0 1,095 Apr-01-2022, 10:20 PM
Last Post: Paqqno
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,352 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  Syslog server Fifoux082 5 2,881 Sep-15-2020, 07:08 PM
Last Post: Fifoux082
  File Name Parsing millpond 5 3,780 Aug-26-2020, 08:04 AM
Last Post: bowlofred
  Error while parsing tables from docx file aditi 1 3,871 Jul-14-2020, 09:24 PM
Last Post: aditi
  help parsing file aslezak 2 2,298 Oct-22-2019, 03:51 PM
Last Post: aslezak
  Python Script for parsing dictionary values from yaml file pawan6782 3 5,048 Sep-04-2019, 07:21 PM
Last Post: pawan6782
  Parsing an MBOX file Oliver 1 8,291 May-26-2019, 07:12 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020