[tuxdroid-svn] r5010 - software_suite_v3/software/plugin/plugin-system/trunk/plugin-system/executab
Status: Beta
Brought to you by:
ks156
|
From: remi <c2m...@c2...> - 2009-07-02 09:46:30
|
Author: remi
Date: 2009-07-02 11:45:18 +0200 (Thu, 02 Jul 2009)
New Revision: 5010
Modified:
software_suite_v3/software/plugin/plugin-system/trunk/plugin-system/executables/plugin-system.py
Log:
* Attempt to fixes alert mode (not yet functional ...)
Modified: software_suite_v3/software/plugin/plugin-system/trunk/plugin-system/executables/plugin-system.py
===================================================================
--- software_suite_v3/software/plugin/plugin-system/trunk/plugin-system/executables/plugin-system.py 2009-07-02 09:44:34 UTC (rev 5009)
+++ software_suite_v3/software/plugin/plugin-system/trunk/plugin-system/executables/plugin-system.py 2009-07-02 09:45:18 UTC (rev 5010)
@@ -55,18 +55,18 @@
def setMemTresholdParam(self, stringParam):
self.__memTresholdParam = stringParam
-
+
def getCheckMemParam(self):
return self.__checkMemParam
def setCheckMemParam(self, booleanParam):
- self.__checkMemParam = stringParam
-
+ self.__checkMemParam = booleanParam
+
def getCheckCpuParam(self):
return self.__checkCpuParam
def setCheckCpuParam(self, booleanParam):
- self.__checkCpuParam = stringParam
+ self.__checkCpuParam = booleanParam
@@ -74,7 +74,7 @@
"""
This class contain utilities to retrieve system informations.
"""
-
+
def __getPlatform(self):
'''
Return the plateform name of this current computer.
@@ -89,9 +89,9 @@
'''
platform = self.__getPlatform()
return (platform == 'Windows' ) or (platform == 'windows') or (platform == "Microsoft")
-
-
-
+
+
+
def getLinuxCPULoad(self):
'''
Return the current cpu load for linux systems.
@@ -111,10 +111,10 @@
total = (u2-u1) + (n2 - n1) + (s2 - s1) + (i2 -i1)
lo = int((usage/total)* 100)
return str(lo)
-
-
-
-
+
+
+
+
def getWindowsCPULoad(self):
'''
Return the cpu load for windows operating systems.
@@ -122,14 +122,14 @@
from win32com.client import GetObject
wmi = GetObject('winmgmts:')
cpu = wmi.InstancesOf('Win32_Processor')
-
+
x = cpu[0]
return x.Properties_('LoadPercentage').value
-
+
def getLinuxMemoryUsage(self):
'''
Return the current memory usage for linux systems.
@@ -142,8 +142,8 @@
buffers = commands.getoutput("""cat /proc/meminfo | grep Buffers | awk 'BEGIN {FS=":"} {print $2}' | awk '{print $1, $9}'""")
free = commands.getoutput("""cat /proc/meminfo | grep MemFree | awk 'BEGIN {FS=":"} {print $2}' | awk '{print $1, $9}'""")
return str(int(cached.split()[0])/1024 + int(buffers)/1024 + int(free)/1024)
-
-
+
+
def get_usedmem():
"""
Get used memory
@@ -153,14 +153,14 @@
buffers = commands.getoutput("""cat /proc/meminfo | grep Buffers | awk 'BEGIN {FS=":"} {print $2}' | awk '{print $1, $9}'""")
free = commands.getoutput("""cat /proc/meminfo | grep MemFree | awk 'BEGIN {FS=":"} {print $2}' | awk '{print $1, $9}'""")
return str(int(total)/1024 - int(cached.split()[0])/1024 - int(buffers)/1024 - int(free)/1024)
-
+
free = get_freemem()
used = get_usedmem()
return str(int((float(used) / (float(used) + float(free))) * 100.0))
-
-
-
+
+
+
def getWindowsMemoryUsage(self):
''''
Return the windows memory usage.
@@ -171,26 +171,26 @@
totalAvailable = result.find("Available Physical Memory")
totalMem = result.find("MB", totalIndex, totalAvailable)
totalMem = result[ totalIndex + len("Total Physical Memory:") : totalMem ]
-
+
freeMem = result.find("MB", totalAvailable)
freeMem = result[ totalAvailable + len("Available Physical Memory:") : freeMem ]
-
+
totalMem = string.replace(totalMem.strip(), ".", "")
freeMem = string.replace(freeMem.strip(), ".", "")
-
+
totalMem = string.replace(totalMem.strip(), ",", "")
freeMem = string.replace(freeMem.strip(), ",", "")
-
+
totalMem = string.replace(totalMem.strip(), "'", "")
freeMem = string.replace(freeMem.strip(), "'", "")
-
+
value = str(float( float(int(totalMem) - int(freeMem)) / float(totalMem) ) * 100)
if value.find("."):
value = value[ : value.find(".")]
return value
-
+
class SystemPlugin(SimplePlugin):
"""
This class override the SimplePlugin class to make easy the plugin coding.
@@ -205,64 +205,66 @@
self.__daemonRun = False
self.__daemonMutex = threading.Lock()
-
+
def start(self):
"""Plugin entry point.
This method should be used to dispatch commands.
"""
if self.getCommand() == "run":
- self.throwTrace("run command retrieved")
self.run(False)
elif self.getCommand() == "daemon_start":
- self.throwTrace("runDaemon command retrieved")
self.runDaemon()
-
+
def run(self, notif):
"""
Plugin entry point for the "run" command.
"""
- cpu = ""
- memory = ""
-
+ cpu = "0.0"
+ memory = "0.0"
+
+ self.throwTrace("Run 01")
sutils = SystemUtils()
-
+
+ self.throwTrace("Run 02")
if self.configuration().getCheckCpuParam():
- '''
- Getting cpu load.
- '''
-
- if not self.isWindows():
- #linux system.
- cpu = sutils.getLinuxCPULoad()
- if not notif:
- self.throwMessage("Your c p u load is {0} percent", cpu)
-
+
+ self.throwTrace("OK 2")
+ if os.name != "nt":
+ self.throwTrace("LINUX")
+ #linux system.
+ cpu = sutils.getLinuxCPULoad()
+ if not notif:
+ self.throwMessage("Your c p u load is {0} percent", cpu)
else:
- #windows system.
- cpu = sutils.getWindowsCPULoad()
- if not notif:
- self.throwMessage("Your c p u load is {0} percent", cpu)
-
-
+ self.throwTrace("WINDOWS")
+ #windows system.
+ self.throwTrace("Get CPU")
+ cpu = sutils.getWindowsCPULoad()
+ self.throwTrace("CPU retrieved")
+ if not notif:
+ self.throwMessage("Your c p u load is {0} percent", cpu)
+
+ self.throwTrace("Run 03")
if self.configuration().getCheckMemParam():
- '''
- Getting Memory load.
- '''
- if not sutils.isWindows():
+
+ if not self.isWindows():
#linux system.
memory = sutils.getLinuxMemoryUsage()
if not notif :
self.throwMessage("Your used memory is {0} percent", memory)
else:
+ self.throwTrace("Get MEMORY")
memory = sutils.getWindowsMemoryUsage()
+ self.throwTrace("Memory retrieved")
if not notif :
self.throwMessage("Your used memory is {0} percent", memory)
-
+
+ self.throwTrace("Run 04")
if notif :
return cpu, memory
-
+
def __getDaemonRun(self):
"""
"""
@@ -271,45 +273,50 @@
self.__daemonMutex.release()
return result
-
-
+
+
def __setDaemonRun(self, daemonRun):
"""
"""
self.__daemonMutex.acquire()
self.__daemonRun = daemonRun
self.__daemonMutex.release()
-
-
-
+
+
+
def runDaemon(self):
"""
Plugin entry point for the "run_daemon" command.
"""
self.__setDaemonRun(True)
while self.__getDaemonRun():
-
+ self.throwTrace("Cycle step 1")
cpu, memory = self.run(True)
-
+ self.throwTrace("Cycle step 2")
+
if (float(cpu) > float(self.configuration().getCpuTresholdParam())):
self.throwNotification("start")
self.throwMessage("c p u meltdown")
self.throwNotification("stop")
-
+
if (float(memory) > float(self.configuration().getMemTresholdParam())):
self.throwNotification("start")
self.throwMessage("memory is up")
self.throwNotification("stop")
-
- sleep(4.0)
+ for i in range(40):
+ if not self.__getDaemonRun():
+ break
+ sleep(0.1)
+
def onPluginStop(self):
"""Callback on plugin stop.
"""
- pass
+ self.__setDaemonRun(False)
+ sys.exit(0)
-
+
def onPluginEvent(self, eventName, eventValues):
"""
Callback on plugin event.
|