Hi,

I'm writing an event log monitor that will run as a service, and to do that I'm going to follow the advice someone gave me on this list earlier and implement a producer/consumer model, where one thread listens for new event log entries and pushes them onto a Queue, and the second thread reads from the Queue and mails the entry to the admin(me).

I started by writing a simple service that does nothing but spawn a single thread, which in turn does nothing but listen for an error sent to the event log. Once it gets an error it logs a warning and terminates. I did this so I could experiment - problem is, it doesn't work. It doesn't complain when it
runs self.monitor.start() but the thread never runs. No warning is ever logged, and it never prints to the little debug file. No exception is thrown. I pulled the code out and ran it as a regular program, and it works fine. But the thread never seems to spawn in the service.


Am I missing something here? Any help would be appreciated.

Thanks,

Jan

#!/usr/bin/env python

# This service looks for DB2 error messages in the event log
# When it find them, it fires off an e-mail containing their details.

import win32serviceutil
import win32service
import win32event
import dwmi
import smtplib
from threading import Thread
from time import sleep

class EvtLogMonitor (Thread):
""" This class runs in a separate thread and is responsible for
monitoring the event log."""
def __init__(self):
Thread.__init__(self)
self.c = dwmi.WMI(privileges = ["Security"])
self.watcher = self.c.watch_for(
notification_type = "Creation",
wmi_class = "Win32_NTLogEvent",
Type = "error")
def run():
""" Main point of execution for thread """
fd=open("C:\ellis\tmp\thread.txt",w)
fd.write("Thread started!")
fd.close()
import servicemanager
servicemanager.LogWarningMsg("Thread started!")
error = self.watcher(100000)
servicemanager.LogWarningMsg("Detected error message from "+error.SourceName)



class PythonDB2ErrorMonitor (win32serviceutil.ServiceFramework):
""" Service that monitors the event log for error messages """
_svc_name_ = "PythonDB2ErrorMonitor"
_svc_display_name_ = "Python DB2 Error Monitor"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
# Create an event to wait on
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
self.monitor = EvtLogMonitor()
def SvcStop(self):
""" Called when the service receives the stop signal """
import servicemanager
# Inform the SCM we are stopping
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# Log that we are stopping
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(self._svc_name_,''))
# wait for thread to join
self.monitor.join()
# Set the event
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
""" Called when the service starts running """
import servicemanager
# Log that we started
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
try:
self.monitor.start()
except:
servicemanager.LogWarningMsg("Could not start thread!")
# Wait for the stop event
win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)


if __name__ == "__main__":
win32serviceutil.HandleCommandLine(PythonDB2ErrorMonitor)


_______________________________________________
Python-win32 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to