I really don’t know if I can change the format because is take it from
syslog server. I do not configure the syslog.
I wasn’t asking you to change the format. The text you cited had no
timestamp etc.
Below is an example of the format that I receive:
Jan 13 16:54:45 192.168.0.20 date=2022-01-13 time=16:51:15 devname=“FG100E4Q16000698” devid=“FG100E4Q16000698” logid=“0001000014” type=“traffic” subtype=“local” level=“notice” vd=“root” eventtime=1642114275 srcip=172.16.2.15 srcname=“PC-1” srcport=58871 srcintf=“lan” srcintfrole=“lan” dstip=192.168.0.20 dstport=443 dstintf=“root” dstintfrole=“undefined” sessionid=56340 proto=6 action=“close” policyid=5 policytype=“local-in-policy” service=“HTTPS” dstcountry=“Reserved” srccountry=“Reserved” trandisp=“noop” app=“Web Management(HTTPS)” duration=1 sentbyte=724 rcvdbyte=313 sentpkt=5 rcvdpkt=4 appcat=“unscanned” devtype=“Windows PC” osname=“Windows” osversion=“NT 10.0” mastersrcmac=“00:00:ee:67:47:39” srcmac=“00:00:ee:67:47:39” srcserver=0
All the logs follow that structure.
Ok. This is better. We can use part of my suggested approach, then write
some custom parsing.
So, suppose the above is in a variable “line”. You can write this:
mon, mday, hhmmss, hostpart, logline = line.split(None, 4)
which will plit the line on whitespace up to 4 times. That gets the
fixed month, month day, time, hostpart into distinct variables and the
rest into “logline”. Then you parse “logline” specially as a sequence of
assignment statements.
So looking at your line you seem to have either:
varname="quoted text here"
or:
varname=nonwhitespace-here
This is probably most handily parsed with regular expressions. Do you
know anything about them?
Basicly the approach is:
- make an empty dict to hold the field values
- while logline is not empty, match a regexp for a single assignment
- store the varname and value in the dict, with the varname as the key
- set logline to the text after the matched regexp
- repeat until the logline is empty r the regexp does not match
After that your dict will have al the values from the line, ready for
use in your JSON.
Cheers,
Cameron Simpson cs@cskk.id.au