[vlma-devel] commit: Ability to restart VLC or fetch logs remotely. (Adrien Grand )
git version control
git at videolan.org
Sun Nov 2 22:41:27 CET 2008
vlma | branch: master | Adrien Grand <jpountz at videolan.org> | Sun Nov 2 00:53:00 2008 +0100| [fc1ab36c63f40415614bb3438758c5b7bb28d7f4] | committer: Adrien Grand
Ability to restart VLC or fetch logs remotely.
> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=fc1ab36c63f40415614bb3438758c5b7bb28d7f4
---
vlma-watchdog/src/conf.py | 19 ++++++++++
vlma-watchdog/src/vlc.py | 65 +++++++++++++++++++++---------------
vlma-watchdog/src/web.py | 82 ++++++++++++++++++++++++++++++++++++++++-----
3 files changed, 130 insertions(+), 36 deletions(-)
diff --git a/vlma-watchdog/src/conf.py b/vlma-watchdog/src/conf.py
new file mode 100644
index 0000000..7f124dc
--- /dev/null
+++ b/vlma-watchdog/src/conf.py
@@ -0,0 +1,19 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+LOG_LEVEL = 20
+
+VLC_EXE = "/usr/bin/vlc"
+VLC_LOGS = "vlc.log"
+VLC_ARGS = "--color -vvv --intf telnet --extraintf logger --logfile %s --logmode text" %(VLC_LOGS)
+
+CPU_LOAD_THRESHOLD = 6.0
+VLC_CPU_THRESHOLD = 80.0
+VLC_MEMORY_THRESHOLD = 80.0
+
+NETWORK_INTERFACE = "eth0"
+
+SERVER_PORT = 4213
+SERVER_LOGIN = "videolan"
+SERVER_PASSWORD = "admin"
+
diff --git a/vlma-watchdog/src/vlc.py b/vlma-watchdog/src/vlc.py
index 5c2f1e0..4ba5210 100644
--- a/vlma-watchdog/src/vlc.py
+++ b/vlma-watchdog/src/vlc.py
@@ -1,25 +1,9 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
-# Configuration
-
-LOG_LEVEL = 20
-
-VLC_ARGS = "/usr/bin/vlc -I telnet --color -vvv"
-
-CPU_LOAD_THRESHOLD = 6.0
-VLC_CPU_THRESHOLD = 80.0
-VLC_MEMORY_THRESHOLD = 80.0
-
-NETWORK_INTERFACE = "eth0"
-
-SERVER_PORT = 4213
-SERVER_LOGIN = "videolan"
-SERVER_PASSWORD = "admin"
-
# Imports
-import logging, os, signal, sys, threading, time, web
+import logging, os, signal, sys, threading, time, conf, web
global vlcInstance
@@ -36,9 +20,10 @@ class VLCRunner(threading.Thread):
def run(self):
while (self.__shouldRun):
- self.logger.info("Running %s", VLC_ARGS)
- args = VLC_ARGS.split()
- self.__pid = os.spawnvp(os.P_NOWAIT, args[0], args)
+ self.logger.info("Running %s %s", conf.VLC_EXE, conf.VLC_ARGS)
+ args = conf.VLC_ARGS.split()
+ args.insert(0, conf.VLC_EXE)
+ self.__pid = os.spawnvp(os.P_NOWAIT, conf.VLC_EXE, args)
exit_status = os.waitpid(self.__pid, 0)[1]
self.logger.info("VLC exited with return code %d", exit_status)
self.logger.info("VLC has been stopped")
@@ -74,6 +59,32 @@ class VLC:
def stop(self):
self.runner.stop()
+ def getVersion(self):
+ out = os.popen2("%s --version" %conf.VLC_EXE)[1]
+ return out.readline()
+
+ def getLogTail(self, sep="\n", lines=50):
+ try:
+ f = open(conf.VLC_LOGS)
+ except:
+ return ""
+ avgCharsPerLine = 70
+ while True:
+ beginningOfFileReached = False
+ try:
+ pos = - avgCharsPerLine * lines
+ f.seek(pos, 2)
+ except IOError:
+ beginningOfFileReached = True
+ f.seek(0)
+ logs = f.read().split("\n")[-lines:]
+ if(len(logs) < lines and not beginningOfFileReached):
+ avgcharsperline *= 1.3
+ else:
+ break
+ f.close()
+ return sep.join(logs)
+
def restart(self):
self.stop()
time.sleep(1)
@@ -91,24 +102,24 @@ class Monitor(threading.Thread):
while(True):
time.sleep(1)
cpuLoad = self.getCpuLoad()
- if(cpuLoad >= CPU_LOAD_THRESHOLD):
+ if(cpuLoad >= conf.CPU_LOAD_THRESHOLD):
logger.warn("CPU load is %f, VLC restart triggered", cpuLoad)
self.vlc.stop()
# Because load won't go down in one millisecond
logger.info("Waiting for the load to decrease")
while(True):
time.sleep(5)
- if(self.getCpuLoad() < CPU_LOAD_THRESHOLD):
+ if(self.getCpuLoad() < conf.CPU_LOAD_THRESHOLD):
break
self.vlc.start()
continue
vlcCpu = self.getVlcCpu()
- if(vlcCpu >= VLC_CPU_THRESHOLD):
+ if(vlcCpu >= conf.VLC_CPU_THRESHOLD):
logger.warn("CPU usage of VLC is %f, VLC restart triggered", vlcCpu)
self.vlc.restart()
continue
vlcMem = self.getVlcMem()
- if(vlcMem >= VLC_MEMORY_THRESHOLD):
+ if(vlcMem >= conf.VLC_MEMORY_THRESHOLD):
logger.warn("Memory usage is %f, VLC restart triggered", vlcMem)
self.vlc.restart()
continue
@@ -144,7 +155,7 @@ class Monitor(threading.Thread):
if not line:
return 0.
else:
- if NETWORK_INTERFACE in line:
+ if conf.NETWORK_INTERFACE in line:
return float(line.replace(":", " ").split()[param])
except Exception, e:
logger.error(e)
@@ -168,7 +179,7 @@ if __name__ == '__main__':
logger = logging.getLogger('')
console = logging.StreamHandler()
logger.addHandler(console)
- logger.setLevel(LOG_LEVEL)
+ logger.setLevel(conf.LOG_LEVEL)
# Signal handling
signal.signal(signal.SIGINT, signalHandler)
signal.signal(signal.SIGHUP, signalHandler)
@@ -180,7 +191,7 @@ if __name__ == '__main__':
vlcInstance.start()
monitor.start()
# Start web server
- webserver = web.Server(monitor)
+ webserver = web.Server(vlcInstance, monitor)
webserver.start()
# Main thread
while(True):
diff --git a/vlma-watchdog/src/web.py b/vlma-watchdog/src/web.py
index 34721de..b08259b 100644
--- a/vlma-watchdog/src/web.py
+++ b/vlma-watchdog/src/web.py
@@ -1,34 +1,42 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
-import logging, threading
+import conf, logging, threading
from twisted.web import server, resource
from twisted.internet import reactor
from twisted.web import http
from data_pb2 import ServerState
-from vlc import SERVER_PORT, SERVER_LOGIN, SERVER_PASSWORD
+
class Server(threading.Thread):
- def __init__(self, monitor):
+ def __init__(self, vlc, monitor):
self.logger = logging.getLogger("Web Server")
threading.Thread.__init__(self)
+ self.vlc = vlc
self.monitor = monitor
self.setDaemon(True)
def run(self):
root = resource.Resource()
root.putChild("", IndexResource())
- root.putChild("monitor", ServerStateResource(self.monitor))
+ system = resource.Resource()
+ root.putChild("system", system)
+ system.putChild("monitor", SystemMonitorResource(self.monitor))
+ vlc = resource.Resource()
+ root.putChild("vlc", vlc)
+ vlc.putChild("restart", VlcRestart(self.vlc))
+ vlc.putChild("version", VlcVersion(self.vlc))
+ vlc.putChild("logTail", VlcLogTail(self.vlc))
site = server.Site(root)
- reactor.listenTCP(SERVER_PORT, site)
+ reactor.listenTCP(conf.SERVER_PORT, site)
reactor.run(installSignalHandlers=False)
class AuthenticationRequiredResource(resource.Resource):
def isAuthenticated(self, request):
- if(request.getUser() != SERVER_LOGIN or request.getPassword() != SERVER_PASSWORD):
+ if(request.getUser() != conf.SERVER_LOGIN or request.getPassword() != conf.SERVER_PASSWORD):
request.setResponseCode(http.UNAUTHORIZED)
request.setHeader('WWW-authenticate', 'basic realm="VLC monitor HTTP interface"')
request.write("Authentication required")
@@ -39,14 +47,70 @@ class AuthenticationRequiredResource(resource.Resource):
class IndexResource(AuthenticationRequiredResource):
isLeaf = True
+ def __init__(self):
+ self.index = {
+ "/vlc/restart/" : "Restart VLC",
+ "/vlc/logTail/" : "Get VLC logs",
+ "/vlc/version/" : "Get VLC version",
+ "/system/monitor/": "Monitor system"
+ }
+
+ def render_GET(self, request):
+ if(not self.isAuthenticated(request)):
+ return ""
+ html = []
+ for (link, descr) in self.index.items():
+ html.append("<a href=\"%s\">%s</a>" %(link, descr))
+ return "<br />\n".join(html)
+
+
+class VlcRestart(AuthenticationRequiredResource):
+ isLeaf = True
+
+ def __init__(self, vlc):
+ self.logger = logging.getLogger("Web Server")
+ self.vlc = vlc
+
+ def render_GET(self, request):
+ if(not self.isAuthenticated(request)):
+ return ""
+ try:
+ self.vlc.restart()
+ return "Done."
+ except Exception, e:
+ self.logger.error(e)
+ return "Error: %s" %e
+
+
+class VlcLogTail(AuthenticationRequiredResource):
+ isLeaf = True
+
+ def __init__(self, vlc):
+ self.vlc = vlc
+
+ def render_GET(self, request):
+ if(not self.isAuthenticated(request)):
+ return ""
+ if request.args.has_key("nbLines"):
+ logs = self.vlc.getLogTail(lines=int(request.args["nbLines"][0]), sep="<br />\n")
+ else:
+ logs = self.vlc.getLogTail(sep="<br />\n")
+ return logs
+
+
+class VlcVersion(AuthenticationRequiredResource):
+ isLeaf = True
+
+ def __init__(self, vlc):
+ self.vlc = vlc
+
def render_GET(self, request):
if(not self.isAuthenticated(request)):
return ""
- html = "<a href=\"monitor/\">VLC state</a>"
- return html
+ return self.vlc.getVersion()
-class ServerStateResource(AuthenticationRequiredResource):
+class SystemMonitorResource(AuthenticationRequiredResource):
isLeaf = True
def __init__(self, monitor):
More information about the vlma-devel
mailing list