From 60c4ca2e2f170ebcdac7f2c270783c1d3fdd539a Mon Sep 17 00:00:00 2001 From: afabiani Date: Mon, 14 Jan 2019 10:49:25 +0100 Subject: [PATCH] - Resource Monitor: minor refactoring to the data collection main cycle. Trying to take into account runtime deltas. --- src/wpsremote/resource_monitor.py | 24 ++++++++++++++---------- src/wpsremote/servicebot.py | 23 ++++++++++++++++------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/wpsremote/resource_monitor.py b/src/wpsremote/resource_monitor.py index 69f3d93..486668e 100644 --- a/src/wpsremote/resource_monitor.py +++ b/src/wpsremote/resource_monitor.py @@ -4,9 +4,10 @@ # This code is licensed under the GPL 2.0 license, available at the root # application directory. -import threading +import time import psutil import logging +import threading __author__ = "Alessio Fabiani" __copyright__ = "Copyright 2016 Open Source Geospatial Foundation - all rights reserved" @@ -71,15 +72,18 @@ def proc_is_running(self, proc_defs): print(tb) return False - def run(self): - while True: - ResourceMonitor.lock.acquire() + def update_stats(self): + ResourceMonitor.lock.acquire() - ResourceMonitor.vmem_perc[1] = (ResourceMonitor.vmem_perc[0] + ResourceMonitor.vmem_perc[1]) / 2.0 - ResourceMonitor.vmem_perc[0] = (ResourceMonitor.vmem_perc[1] + psutil.virtual_memory().percent) / 2.0 + ResourceMonitor.vmem_perc[1] = (ResourceMonitor.vmem_perc[0] + ResourceMonitor.vmem_perc[1]) / 2.0 + ResourceMonitor.vmem_perc[0] = (ResourceMonitor.vmem_perc[1] + psutil.virtual_memory().percent) / 2.0 - ResourceMonitor.cpu_perc[1] = ResourceMonitor.cpu_perc[0] - ResourceMonitor.cpu_perc[0] = psutil.cpu_percent( - interval=(ResourceMonitor.load_average_scan_minutes*60), percpu=False) + ResourceMonitor.cpu_perc[1] = ResourceMonitor.cpu_perc[0] + ResourceMonitor.cpu_perc[0] = psutil.cpu_percent( + interval=(ResourceMonitor.load_average_scan_minutes*60), percpu=False) - ResourceMonitor.lock.release() + ResourceMonitor.lock.release() + + def run(self): + while True: + self.update_stats() diff --git a/src/wpsremote/servicebot.py b/src/wpsremote/servicebot.py index 73afa11..0226f5d 100644 --- a/src/wpsremote/servicebot.py +++ b/src/wpsremote/servicebot.py @@ -4,14 +4,16 @@ # This code is licensed under the GPL 2.0 license, available at the root # application directory. -import introspection +import re +import psutil import thread -from collections import OrderedDict +import logging +import datetime import tempfile import subprocess -import datetime -import logging -import re +import introspection + +from collections import OrderedDict import busIndipendentMessages @@ -201,8 +203,15 @@ def handle_getloadavg(self, getloadavg_message): # Collect current Machine Load Average and Available Memory info try: - loadavg = self._resource_monitor.cpu_perc[0] - vmem = self._resource_monitor.vmem_perc[0] + logger.info("Fetching updated status from Resource Monitor...") + + vmem = psutil.virtual_memory().percent + if self._resource_monitor.vmem_perc[0] > 0: + vmem = (vmem + self._resource_monitor.vmem_perc[0]) / 2.0 + + loadavg = psutil.cpu_percent(interval=0, percpu=False) + if self._resource_monitor.cpu_perc[0] > 0: + loadavg = (loadavg + self._resource_monitor.cpu_perc[0]) / 2.0 logger.info("Scanning Running Process. Declared Black List: %s" % self._process_blacklist) if self._resource_monitor.proc_is_running(self._process_blacklist):