113 lines
4.3 KiB
Python
Executable File
113 lines
4.3 KiB
Python
Executable File
#!/usr/bin/python3
|
|
import time
|
|
import re
|
|
import requests
|
|
import json
|
|
import os
|
|
|
|
|
|
INTERVAL=10
|
|
LOOP_CONTROL_EVERY=6
|
|
|
|
f = open("/etc/logveillance/token")
|
|
TOKEN = f.readline().splitlines()[0]
|
|
f.close()
|
|
f = open("/etc/logveillance/room")
|
|
ROOM = f.readline().splitlines()[0]
|
|
f.close()
|
|
f = open("/etc/logveillance/server")
|
|
SERVER = f.readline().splitlines()[0]
|
|
f.close()
|
|
f = open("/etc/logveillance/logfile")
|
|
filename = f.readline().splitlines()[0]
|
|
f.close()
|
|
|
|
URL=SERVER + "/_matrix/client/r0/rooms/"+ROOM+"/send/m.room.message?access_token=" + TOKEN
|
|
|
|
RULE = [
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*dhcpd\[\d*\]:\s*DHCPACK\s*on \S* to \S*( \(\S*\))?( via \S*)?",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*dhcpd\[\d*\]:\s*DHCPOFFER on \S* to \S*( \(\S*\))?( via \S*)?",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*dhcpd\[\d*\]:\s*DHCPREQUEST for \S*( \(\S*\))? from \S*( \(\S*\))?( via \S*)?",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*dhcpd\[\d*\]:\s*DHCPRELEASE of \S*( \(\S*\))? from \S*( \(\S*\))?( via \S*)?",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*dhcpd\[\d*\]:\s*reuse_lease: lease age .*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*dhcpd\[\d*\]:\s*Wrote .*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*dhcpd\[\d*\]:\s*uid lease \S*( \(\S*\))? for client \S* is duplicate on \S*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*\S* Succeeded.",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*Started\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*Starting\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*Reloaded\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*Reloading\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*Reached\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*Listening\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*Stopping\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*Stopped\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*Startup\s*finished\s*in\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*Created\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*Closed\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*systemd\[\d*\]:\s*Removed\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*ntpd\[\d*\]:\s*Listen\s*normally\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*ntpd\[\d*\]:\s*Listen\s*and\s*drop\s*on\s*.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*ntpd\[\d*\]:\s*Listening\s*on\s*routing\s*socket\s*on.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*CRON\[\d*\]:\s*\(\S*\)\s*CMD.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*in\.tftpd\[\d*\]:\s*RRQ\s*from\s*\S*\s*filename.*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*rsyslogd:\s*\[.*\] rsyslogd was HUPed",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*kernel:\s*\[.*\] EXT4-fs \(.*\): mounted filesystem without journal. Opts: .*",
|
|
"\w*\s*[0-9]{1,2}\s*\d*:\d*:\d*\s*\w*\s*EXT4-fs\s*\(\S*\): write access unavailable, skipping orphan cleanup"
|
|
]
|
|
|
|
def matchrule(ligne,rulelist):
|
|
res = False
|
|
cpt = len(rulelist)
|
|
while cpt != 0 and not res:
|
|
cpt = cpt - 1
|
|
objmatch = rulelist[cpt].fullmatch(ligne)
|
|
if objmatch != None:
|
|
res = True
|
|
return res
|
|
|
|
relist = []
|
|
for tmp in RULE:
|
|
relist.append(re.compile(tmp))
|
|
|
|
fd = open(filename)
|
|
firstline_control = fd.readline().splitlines()[0]
|
|
inode_control = os.stat(filename).st_ino
|
|
fd.seek(0,2)
|
|
loop_control = 0
|
|
content = ""
|
|
while True:
|
|
loop_control = (loop_control + 1) % LOOP_CONTROL_EVERY
|
|
where = fd.tell()
|
|
ligne = fd.readline()
|
|
if ligne:
|
|
ligne = ligne.splitlines()
|
|
if ligne != []:
|
|
ligne = ligne[0]
|
|
if not matchrule(ligne,relist):
|
|
content = content + ligne + '\n'
|
|
else:
|
|
time.sleep(INTERVAL)
|
|
fd.seek(where)
|
|
if loop_control == 0:
|
|
where = fd.tell()
|
|
fd.seek(0,0)
|
|
firstline = fd.readline().splitlines()[0]
|
|
fd.seek(where)
|
|
inode = os.stat(filename).st_ino
|
|
if inode_control != inode:
|
|
inode_control = inode
|
|
fd.close()
|
|
fd = open(filename)
|
|
firstline_control = fd.readline().splitlines()[0]
|
|
fd.seek(0,0)
|
|
if firstline_control != firstline:
|
|
firstline_control = firstline
|
|
fd.seek(0,0)
|
|
if content != "":
|
|
try:
|
|
payload=json.dumps({"msgtype":"m.text","body":content})
|
|
r = requests.post(URL, data=payload)
|
|
content = ""
|
|
except:
|
|
print("Unexpected error:", sys.exc_info()[0])
|