Last active
August 29, 2015 13:58
-
-
Save rahit/10009627 to your computer and use it in GitHub Desktop.
Script to analyze log file and monitor changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Script to analyze log file and monitor changes | |
@Author: K.M. Tahsin Hassan Rahit <[email protected]> (12-21206-1) | |
''' | |
from Tkinter import * | |
from bsddb.dbtables import _columns_key | |
from difflib import Differ | |
from glob import glob | |
import os | |
import thread | |
import time | |
class AppFrame(Frame): | |
dirPath = '/var/log/*.log' | |
def __init__(self, master): | |
Frame.__init__(self, master) | |
self.grid() | |
self.initWidgets() | |
self.lpThreads = [] | |
self.exitFlag = False | |
def initWidgets(self): | |
self.logFiles = glob(self.dirPath) | |
self.leftFrame = LabelFrame(self, text="Log options") | |
self.leftFrame.grid() | |
self.rightFrame = LabelFrame(self, text="Monitor Log") | |
self.rightFrame.grid(row=0, column=1, columnspan=2, rowspan=3) | |
self.startButton = Button(self.leftFrame, text="Start", command=self.startCallback) | |
self.stopButton = Button(self.leftFrame, text="Stop", command=self.stopCallback, state=DISABLED ) | |
self.fileListBox = Listbox(self.leftFrame, selectmode=MULTIPLE, height=20) | |
self.status = Label(self.leftFrame, text="Not Monitoring") | |
for logFile in self.logFiles: | |
self.fileListBox.insert(END, logFile) | |
self.fileListBox.grid() | |
self.startButton.grid() | |
self.stopButton.grid() | |
self.status.grid() | |
self.logDisplayText = Text(self.rightFrame, wrap=WORD, height=30) | |
self.logDisplayText.grid() | |
self.logDisplayText.insert(INSERT, "Script to analyze log file and monitor changes") | |
self.logDisplayText.insert(INSERT, "\nAuthor: K.M. Tahsin Hassan Rahit <[email protected]> (12-21206-1)\n") | |
def startCallback(self): | |
self.exitFlag = False | |
self.status['text'] = 'Monitoring' | |
self.startButton['state'] = DISABLED | |
self.stopButton['state'] = NORMAL | |
self.filesToLog = {self.fileListBox.get(logFile) : {} for logFile in self.fileListBox.curselection()} | |
for fileToLog in self.filesToLog: | |
thread.start_new_thread(self.logIt, (fileToLog,)) | |
def stopCallback(self): | |
self.exitFlag = True | |
self.startButton['state'] = NORMAL | |
self.stopButton['state'] = DISABLED | |
self.status['text'] = 'Not Monitoring' | |
def logIt(self, fileLocation): | |
fileToMonitor = open(fileLocation, 'r') | |
prevFileContents = fileToMonitor.read().splitlines() | |
fileToMonitor.close() | |
with open('loggify.log', 'w+') as fileToLog: | |
fileToLog.write("") | |
while not self.exitFlag: | |
with open(fileLocation, 'r') as fileToMonitor: | |
curFileContents = fileToMonitor.read().splitlines() | |
newContents = list(set(curFileContents) - set(prevFileContents)) | |
if newContents: | |
with open('loggify.log', 'r') as fileToLog: | |
prevText = fileToLog.read() | |
curText = '\n\n' + fileLocation + ' logged this:\n--------------------------------------------\n' + '\n'.join(newContents) | |
textToAdd = prevText + curText | |
self.logDisplayText.insert(INSERT, curText) | |
with open('loggify.log', 'w+') as fileToLog: | |
fileToLog.write(textToAdd) | |
prevFileContents = curFileContents | |
time.sleep(2) | |
if self.exitFlag: | |
thread.exit() | |
def main(): | |
baseFrame = Tk() | |
baseFrame.title("Loggify bt Rahit : Simple GUI to log everything") | |
baseFrame.geometry("750x500") | |
appFrame = AppFrame(baseFrame) | |
baseFrame.mainloop() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment