[vlma-devel] commit: Make the watchdog work on Windows. (Adrien Grand )
git version control
git at videolan.org
Wed Apr 1 21:25:46 CEST 2009
vlma | branch: master | Adrien Grand <jpountz at videolan.org> | Wed Apr 1 19:51:01 2009 +0200| [ca6a3a17331b484e471a9f1dee10493085df4a4d] | committer: Adrien Grand
Make the watchdog work on Windows.
> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=ca6a3a17331b484e471a9f1dee10493085df4a4d
---
vlma-watchdog/src/vlc.py | 119 +++++++++++++++++++++++++++++++++++++---------
1 files changed, 97 insertions(+), 22 deletions(-)
diff --git a/vlma-watchdog/src/vlc.py b/vlma-watchdog/src/vlc.py
old mode 100644
new mode 100755
index 5c9be64..490ae01
--- a/vlma-watchdog/src/vlc.py
+++ b/vlma-watchdog/src/vlc.py
@@ -9,6 +9,15 @@
import logging, math, os, signal, sys, threading, time, conf, web, socket, subprocess
+platform_is_windows = False
+if sys.platform.find("win") >= 0:
+ platform_is_windows = True
+ try:
+ import win32api, win32con, wmi, pythoncom
+ except ImportError, e:
+ print "You need to install the win32api and the wmi modules for Python in order to run the watchdog"
+ sys.exit(1)
+
global vlcInstance
# Main classes
@@ -45,8 +54,9 @@ class VLCRunner(threading.Thread):
self.__stdout_reader.setInput(process.stdout)
self.__stderr_reader.setInput(process.stderr)
self.__pid = process.pid
+ self.logger.info("PID is %d", self.__pid)
self.__lock.release()
- exit_status = os.waitpid(self.__pid, 0)[1]
+ exit_status = process.wait()
self.__lock.acquire()
self.startTime = 0
self.__lock.release()
@@ -64,8 +74,22 @@ class VLCRunner(threading.Thread):
version = self.__version
self.__lock.release()
if version is None:
- out = os.popen2("%s --version" %conf.VLC_EXE)[1]
- version = out.readline().rstrip("\n")
+ if platform_is_windows:
+ key_name = "SOFTWARE\\VideoLAN\\VLC"
+ try:
+ reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, key_name, 0, win32con.KEY_READ)
+ except:
+ try:
+ reg_key = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, key_name, 0, win32con.KEY_READ)
+ except:
+ logger.error("Could not find the version of VLC")
+ return "Unknown version"
+ version = win32api.RegQueryValueEx(reg_key, "Version")[0]
+ reg_key.close()
+ version = "VLC media player " + version
+ else:
+ out = os.popen2("%s --version" %conf.VLC_EXE)[1]
+ version = out.readline().rstrip("\n")
self.__lock.acquire()
self.__version = version
self.__lock.release()
@@ -91,10 +115,15 @@ class VLCRunner(threading.Thread):
self.__shouldRun = False
pid = self.__pid
self.__lock.release()
- if (pid > 0):
+ if pid > 0:
try:
- os.kill(pid, 9)
- except OSError:
+ if platform_is_windows:
+ handle = win32api.OpenProcess(1, False, pid)
+ win32api.TerminateProcess(handle, 0)
+ win32api.CloseHandle(handle)
+ else:
+ os.kill(pid, 9)
+ except:
# Process already stopped
pass
self.join()
@@ -258,12 +287,21 @@ class Monitor(threading.Thread):
self.vlc.restart()
continue
+ def _initWMI(self):
+ # Needed for WMI
+ # See http://timgolden.me.uk/python/wmi.html, "Using WMI in a thread or in a Service"
+ pythoncom.CoInitialize()
+
def getCpuLoad(self):
- try:
+ if platform_is_windows:
+ self._initWMI()
+ cpu_load = 0.
+ procs = wmi.WMI().query("Select LoadPercentage from Win32_Processor")
+ for proc in procs:
+ cpu_load += float(proc.LoadPercentage) / 100
+ return cpu_load
+ else:
return os.getloadavg()[0]
- except Exception, e:
- logger.error(e)
- return 0.
def _getPidProperty(self, property):
try:
@@ -271,35 +309,69 @@ class Monitor(threading.Thread):
result = os.popen(cmd).readline().strip(' ').strip('\n')
return float(result)
except Exception, e:
- logger.error(e)
+ self.logger.error(e)
return 0.
def getVlcCpu(self):
- return self._getPidProperty("pcpu")
+ if platform_is_windows:
+ # TODO
+ return 0.
+ else:
+ return self._getPidProperty("pcpu")
def getVlcMem(self):
- return self._getPidProperty("pmem")
+ if platform_is_windows:
+ # TODO
+ return 0.
+ else:
+ return self._getPidProperty("pmem")
def _getTrafficX(self, param):
try:
result = os.popen("cat /proc/net/dev")
result.readline()
- while(True):
+ while True:
+ line = result.readline()
+ if not line:
+ return 0.
+ elif conf.NETWORK_INTERFACE in line:
+ return float(line.replace(":", " ").split()[param])
+ except Exception, e:
+ self.logger.warn(e)
+ return 0.
+
+ def _getTrafficXWin(self, param):
+ try:
+ result = os.popen("netstat -e")
+ line = result.readline()
+ while True:
line = result.readline()
if not line:
return 0.
else:
- if conf.NETWORK_INTERFACE in line:
- return float(line.replace(":", " ").split()[param])
+ # protect your eyes, the following lines of code are really ugly
+ tokens = line.split()
+ if len(tokens) == 3:
+ try:
+ return long(tokens[param])
+ except Exception, e:
+ self.logger.warn(e)
+ continue
except Exception, e:
- logger.error(e)
+ self.logger.warn(e)
return 0.
def getTrafficIn(self):
- return self._getTrafficX(1)
+ if platform_is_windows:
+ return self._getTrafficXWin(1)
+ else:
+ return self._getTrafficX(1)
def getTrafficOut(self):
- return self._getTrafficX(9)
+ if platform_is_windows:
+ return self._getTrafficXWin(2)
+ else:
+ return self._getTrafficX(9)
def signalHandler(signum, frame):
@@ -308,6 +380,7 @@ def signalHandler(signum, frame):
vlcInstance.stop()
sys.exit(0)
+
def check_port(port):
"""Check whether the provided port is available"""
s = socket.socket()
@@ -322,6 +395,7 @@ def check_port(port):
return result
+
if __name__ == '__main__':
# Init logging
logger = logging.getLogger('')
@@ -330,9 +404,10 @@ if __name__ == '__main__':
logger.setLevel(conf.LOG_LEVEL)
# Signal handling
signal.signal(signal.SIGINT, signalHandler)
- signal.signal(signal.SIGHUP, signalHandler)
signal.signal(signal.SIGTERM, signalHandler)
- signal.signal(signal.SIGQUIT, signalHandler)
+ if not platform_is_windows:
+ signal.signal(signal.SIGHUP, signalHandler)
+ signal.signal(signal.SIGQUIT, signalHandler)
# Check that ports are not in use
msg = "Cannot listen on port %d (%s), port is already in use"
if not check_port(conf.VLC_TELNET_PORT):
@@ -350,6 +425,6 @@ if __name__ == '__main__':
webserver = web.Server(vlcInstance, monitor)
webserver.start()
# Main thread
- while(True):
+ while True:
time.sleep(1)
More information about the vlma-devel
mailing list