/
tracking-pixel-bell.py
70 lines (55 loc) · 1.85 KB
/
tracking-pixel-bell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
from discord_webhook import DiscordWebhook, DiscordEmbed
from dotenv import load_dotenv
import os
from time import sleep
load_dotenv()
SLEEP=15
LOG_FILE="/var/log/apache2/forensic_log-10080.log"
KEYWORDS=["customerid","productid"]
WEBHOOK=os.getenv('DISCORD_WEBHOOK')
def LastNlines(fname, N):
assert N >= 0
pos = N + 1
lines = []
with open(fname) as f:
while len(lines) <= N:
try:
f.seek(-pos, 2)
except IOError:
f.seek(0)
break
finally:
lines = list(f)
pos *= 2
return lines[-N:]
def bell_discord(webhook_url,line):
text="\n".join(line.split("|"))
webhook = DiscordWebhook(url=webhook_url)
embed = DiscordEmbed(title="Ding Ding Ding! Email opened!", color="03b2f8")
embed.set_description(text)
webhook.add_embed(embed)
response = webhook.execute()
def count_lines(file):
with open(file, 'r') as fp:
lines = sum(1 for line in fp)
return lines
# Monitor the last lines of a log file to send a webhook message if we find a specific keyword in it
def main():
prev_nlines=count_lines(LOG_FILE)
while(1):
curr_nlines=count_lines(LOG_FILE)
if (curr_nlines>prev_nlines):
new_nlines=curr_nlines-prev_nlines
print("Exploring %s new lines added to the log" % new_nlines)
new_lines=LastNlines(LOG_FILE,new_nlines)
for line in new_lines:
for keyword in KEYWORDS:
if (keyword in line):
bell_discord(WEBHOOK,line)
# This also covers the case when the log rotates
# and the current number of lines is lower than the previous number of lines
prev_nlines=curr_nlines
print("Sleeping")
sleep(SLEEP)
main()