From 25c9a4cfb5ef10d6f8c7b02311ee0fa8f38fc672 Mon Sep 17 00:00:00 2001 From: Sylvain Maziere Date: Tue, 12 May 2015 09:30:15 +0200 Subject: [PATCH 1/2] Add macosx support for worker --- src/octopus/worker/worker.py | 96 ++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/src/octopus/worker/worker.py b/src/octopus/worker/worker.py index add39f83..c1eb9740 100644 --- a/src/octopus/worker/worker.py +++ b/src/octopus/worker/worker.py @@ -153,49 +153,41 @@ def getNbCores(self): return multiprocessing.cpu_count() def getTotalMemory(self): - memTotal = 1024 - if os.path.isfile('/proc/meminfo'): - try: - # get total memory - f = open('/proc/meminfo', 'r') - for line in f.readlines(): - if line.split()[0] == 'MemTotal:': - memTotal = line.split()[1] - f.close() - break - except: - pass - return int(memTotal) / 1024 + """ + | Use psutil module to retrieve total memory available. The value is transmitted as a int. + :return: An integer representing the amount of total memory available on the system + """ + memTotal = 0 + try: + memTotal = psutil.virtual_memory().total + except psutil.Error: + LOGGER.warning("An error occured when retrieving total memory.") + except NameError, e: + LOGGER.debug("Impossible to use psutil module on this host: %r" % e) + except Exception, e: + LOGGER.warning("An unexpected error occured: %r" % e) + return int(memTotal) / ( 1024 ** 2 ) def getFreeMem(self, pUnit=MEGABYTES): """ - | Starts a shell process to retrieve amount of free memory on the worker's system. - | The amount of memory is transmitted in MEGABYTES, but can be specified to another unit - | To estimate this, we retrieve specific values in /proc/meminfo: - | Result = MemFree + Buffers + Cached - + | Use psutil module to retrieve free memory. The value is transmitted as a int. :param pUnit: An integer representing the unit to which the value is converted (DEFAULT is MEGABYTES). :return: An integer representing the amount of FREE memory on the system - :raise: OSError if subprocess fails. Returns "-1" if no correct value can be retrieved. """ - + freeMem = 0 try: - freeMemStr = subprocess.Popen(["awk", - "/MemFree|Buffers|^Cached/ {free+=$2} END {print free}", - "/proc/meminfo"], stdout=PIPE).communicate()[0] - except OSError, e: - LOGGER.warning("Error when retrievieng free memory: %r", e) - - if freeMemStr == '': - return -1 - - freeMem = int(freeMemStr) + freeMem = psutil.virtual_memory().available + except psutil.Error: + LOGGER.warning("An error occured when retrieving free memory.") + except NameError, e: + LOGGER.debug("Impossible to use psutil module on this host: %r" % e) + except Exception, e: + LOGGER.warning("An unexpected error occured: %r" % e) if pUnit is MEGABYTES: - freeMem = int(freeMem / 1024) + freeMem = int(freeMem / (1024 ** 2)) elif pUnit is GIGABYTES: - freeMem = int(freeMem / (1024 * 1024)) - + freeMem = int(freeMem / (1024 ** 3)) return freeMem def getSwapUsage(self): @@ -213,11 +205,10 @@ def getSwapUsage(self): LOGGER.debug("Impossible to use psutil module on this host: %r" % e) except Exception, e: LOGGER.warning("An unexpected error occured: %r" % e) - return swapUsage def getCpuInfo(self): - if os.path.isfile('/proc/cpuinfo'): + if platform.system() == "Linux": try: # get cpu speed f = open('/proc/cpuinfo', 'r') @@ -230,6 +221,14 @@ def getCpuInfo(self): f.close() except: pass + elif platform.system() == "Darwin": + command = '/usr/sbin/sysctl -n machdep.cpu.brand_string' + result = subprocess.check_output(command, shell=True) + self.cpuName = result.split('@')[0].strip() + self.speed = result.split('@')[1].split('GHz')[0].strip() + else: + self.cpuName = 'unknown' + self.speed = 'unknown' def getDistribName(self): ''' @@ -274,18 +273,21 @@ def getDistribName(self): self.distrib = "unknown" def getOpenglVersion(self): - import subprocess - import re - p = subprocess.Popen("glxinfo", stdout=subprocess.PIPE, stderr=subprocess.PIPE) - output, errors = p.communicate() - outputList = output.split("\n") - for line in outputList: - if "OpenGL version string" in line: - LOGGER.info("found : %s" % line) - oglpattern = re.compile("(\d.\d.\d)") - res = oglpattern.search(line) - self.openglversion = res.group() - break + if platform.system() == "Linux": + import subprocess + import re + p = subprocess.Popen("glxinfo", stdout=subprocess.PIPE, stderr=subprocess.PIPE) + output, errors = p.communicate() + outputList = output.split("\n") + for line in outputList: + if "OpenGL version string" in line: + LOGGER.info("found : %s" % line) + oglpattern = re.compile("(\d.\d.\d)") + res = oglpattern.search(line) + self.openglversion = res.group() + break + elif platform.system() == 'Darwin': + self.openglversion = 'unknown' def updateSysInfos(self, ticket): self.updateSys = True From 1e889740d2ce4200f46f24241cd80dcf3d7e1a7d Mon Sep 17 00:00:00 2001 From: Sylvain Maziere Date: Tue, 12 May 2015 09:33:31 +0200 Subject: [PATCH 2/2] Use more generic code to get uptime (macosx compatible) --- .../webservice/webservicedispatcher.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/octopus/dispatcher/webservice/webservicedispatcher.py b/src/octopus/dispatcher/webservice/webservicedispatcher.py index b6371fb6..f31e3065 100644 --- a/src/octopus/dispatcher/webservice/webservicedispatcher.py +++ b/src/octopus/dispatcher/webservice/webservicedispatcher.py @@ -246,10 +246,18 @@ def get(self): uptime = 0 load = (0, 0, 0) try: - with open('/proc/uptime', 'r') as f: - uptime_seconds = float(f.readline().split()[0]) - uptime = timedelta(seconds=uptime_seconds) - load = os.getloadavg() + # code stolen from https://gist.github.com/carschar/1019870 + # don't work on Windows + raw = subprocess.check_output('uptime').replace(',','') + days = int(raw.split()[2]) + if 'min' in raw: + hours = 0 + minutes = int(raw[4]) + else: + hours, minutes = map(int, raw.split()[4].split(':')) + uptime_seconds = days*24*60*60 + hours*60*60 + minutes*60 + uptime = timedelta(seconds=uptime_seconds) + load = os.getloadavg() except Exception, e: logging.getLogger('main').warning("Impossible to retrieve server uptime or load average: %s" % e)