-
-
Save mcchae/79200deb956374d36083 to your computer and use it in GitHub Desktop.
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
########################################################################################## | |
import win32service | |
import win32serviceutil | |
import win32api | |
import win32event | |
import subprocess | |
import center2snmp | |
import snmpagent | |
import time | |
import os | |
import servicemanager | |
########################################################################################## | |
def pid_running(pid): | |
ps = subprocess.Popen(r'tasklist.exe /NH /FI "PID eq %d"' % (pid), shell=True, stdout=subprocess.PIPE) | |
output = ps.stdout.read() | |
ps.stdout.close() | |
ps.wait() | |
if str(pid) in output: | |
return True | |
return False | |
########################################################################################## | |
class TwIntService(win32serviceutil.ServiceFramework): | |
#===================================================================================== | |
_svc_name_ = "Center2SnmpService" | |
_svc_display_name_ = "Center to SNMP Service" | |
_svc_description_ = "Center to SNMP Service by Future Systems" | |
#===================================================================================== | |
def __init__(self, args): | |
win32serviceutil.ServiceFramework.__init__(self, args) | |
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) | |
self.cronperiod = None | |
#===================================================================================== | |
def SvcStop(self): | |
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) | |
win32event.SetEvent(self.hWaitStop) | |
#===================================================================================== | |
def subprocess_start(self): | |
cmds = [ | |
'C:/python27/pythonw.exe', | |
'-m', | |
'center2snmp', | |
"C:/python27/center2snmp.ini" | |
] | |
self.center2snmp = subprocess.Popen(cmds) | |
cmds = [ | |
'C:/python27/pythonw.exe', | |
'-m', | |
'snmpagent', | |
"C:/python27/center2snmp.ini" | |
] | |
self.snmpagent = subprocess.Popen(cmds) | |
time.sleep(1) | |
self.center2snmp_pid = self.center2snmp.pid | |
servicemanager.LogInfoMsg("center2snmp - started PID <%s>" % self.center2snmp_pid) | |
self.snmpagent_pid = self.snmpagent.pid | |
servicemanager.LogInfoMsg("snmpagent - started PID <%s>" % self.snmpagent_pid) | |
#===================================================================================== | |
def subprocess_stop(self): | |
self.center2snmp.terminate() | |
self.snmpagent.terminate() | |
#===================================================================================== | |
def SvcDoRun(self): | |
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, '')) | |
self.timeout = 2000 # 2 seconds | |
self.subprocess_start() | |
while True: | |
# Wait for service stop signal, if I timeout, loop again | |
rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout) | |
# Check to see if self.hWaitStop happened | |
if rc == win32event.WAIT_OBJECT_0: | |
self.subprocess_stop() | |
break | |
else: | |
try: | |
# servicemanager.LogInfoMsg("checking pids (%s,%s)!" % (self.center2snmp_pid, self.snmpagent_pid)) #For Event Log | |
bStoppeed = False | |
if not pid_running(self.center2snmp_pid): | |
servicemanager.LogInfoMsg("center2snmp - STOPPED!") | |
bStoppeed = True | |
if not pid_running(self.snmpagent_pid): | |
servicemanager.LogInfoMsg("snmpagent - STOPPED!") | |
bStoppeed = True | |
if bStoppeed: | |
self.subprocess_stop() | |
self.subprocess_start() | |
except Exception, e: | |
raise | |
########################################################################################## | |
def ctrlHandler(ctrlType): | |
return True | |
########################################################################################## | |
if __name__ == '__main__': | |
win32api.SetConsoleCtrlHandler(ctrlHandler, True) | |
win32serviceutil.HandleCommandLine(TwIntService) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment