-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor.py
69 lines (55 loc) · 1.59 KB
/
monitor.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
69
""" monitor.py
Monitoring Application for Application's log file.
Log file is written to in append-only fashion.
@author: Shreyas Moudgalya{shreyas@hawk.iit.edu}
"""
from argparse import ArgumentParser
from datetime import datetime
from send_email import *
import time
GLOBAL_TOTAL = 0
"""Consume the input log file
Invoke parseLine() for each new line of log"""
def main():
#p=ArgumentParser()
#p.addArgument("log_file",help="file to write application logs to")
#args = p.parse_args()
try:
logfile = open("logfile.txt", "r")
#print ("Name of the file: ", logfile.name)
except Exception as e:
print(str(e))
parseLine(logfile)
def parseFile(logfile):
#logfile.seek(0,2) #Go to the end of the file
while True:
line = logfile.readline()
#print(line)
if not line:
time.sleep(0.1)
continue
yield line
""" Parses a give line of the log
Calls send_email(msg) with all stats info when ERROR line has occurred """
def parseLine(logfile):
parseFileGenerator = parseFile(logfile)
for line in parseFileGenerator:
item = line.replace('[',"")
items = item.split("]")
incidentTime = items[0]
logName = items[1]
levelName = items[2]
errorMessage = items[3]
if levelName == "ERROR":
print(line.rstrip('\n'))
global GLOBAL_TOTAL
GLOBAL_TOTAL += 1
message = """
##SERVER ALERT
##Received: {incidentdate}
##Total Alerts: {total:7d}
##Log Name: {log}
##The Log Error is {m}""".format(incidentdate=incidentTime,total=GLOBAL_TOTAL,log=logName,m=errorMessage)
send_email(message)
if __name__ == "__main__":
main()