From 523e57e238aaccea42e213214257a6869fa03300 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Sat, 7 Apr 2012 20:36:32 -0700 Subject: [PATCH] modularize and make loading psutil dependant code check if psutil is tehre first. Add more license boilerplate --- monitor.py | 14 +++++----- sensors/__init__.py | 38 +++++++++++++++++++++++++++ sensors/memory.py | 18 ------------- sensors/{ => psutil_sensors}/cpu.py | 23 +++++++++++++++++ sensors/psutil_sensors/memory.py | 40 +++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 25 deletions(-) delete mode 100644 sensors/memory.py rename sensors/{ => psutil_sensors}/cpu.py (54%) create mode 100644 sensors/psutil_sensors/memory.py diff --git a/monitor.py b/monitor.py index 662de5b..795baa9 100755 --- a/monitor.py +++ b/monitor.py @@ -8,8 +8,7 @@ # # System-Monitoring-Daemon is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. +# the Free Software Foundation, version 3 of the License. # # System-Monitoring-Daemon is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -24,8 +23,9 @@ import threading from socket import * import time -from sensors.cpu import * -from sensors.memory import * +#from sensors.cpu import * +#from sensors.memory import * +import sensors HOST = '' PORT = 6000 @@ -39,9 +39,9 @@ class Stats(): self.read_lock = threading.Lock() self.write_lock = threading.Lock() - self.sensors = [] - self.sensors.append(cpu_monitor()) - self.sensors.append(mem_monitor()) + self.sensors = sensors.sensors #[] + #self.sensors.append(cpu_monitor()) + #self.sensors.append(mem_monitor()) self.stop = threading.Event() t = threading.Thread(target=self.update_loop, args=()) diff --git a/sensors/__init__.py b/sensors/__init__.py index e69de29..b3be01d 100644 --- a/sensors/__init__.py +++ b/sensors/__init__.py @@ -0,0 +1,38 @@ +# Sensors Module +# Load appropriate sensor sub module to monitor system with +# +# Copyright 2012 Dan Ballard and Robert Hausch +# +## License: +# +# This file is part of System-Monitoring-Daemon. +# +# System-Monitoring-Daemon is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# System-Monitoring-Daemon is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with System-Monitoring-Daemon. If not, see . +## + + +# Load appropriate sensor code + +has_psutil = False +try: + import psutil + has_psutil = True +except: + None + +sensors = [] + +if has_psutil: + from psutil_sensors import * + sensors.append(cpu.cpu_monitor()) + sensors.append(memory.mem_monitor()) diff --git a/sensors/memory.py b/sensors/memory.py deleted file mode 100644 index 6b9fbbf..0000000 --- a/sensors/memory.py +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/python - -import psutil - -class mem_monitor: - def __init__(self): - self.mem_usage = '' - self.virt_mem_usage = '' - - def update(self): - self.mem_usage = psutil.phymem_usage() - self.virt_mem_usage = psutil.virtmem_usage() - - def getFormatedData(self): - message = '{"type": "Memory", "value": "%.1f", "data": [{"name": "Physical", "value": "%.1f"},{"name": "Virtual", "value": "%.1f"}]}' % (self.mem_usage.percent, self.mem_usage.percent, self.virt_mem_usage.percent) - return message - - diff --git a/sensors/cpu.py b/sensors/psutil_sensors/cpu.py similarity index 54% rename from sensors/cpu.py rename to sensors/psutil_sensors/cpu.py index c523336..bb4b58d 100755 --- a/sensors/cpu.py +++ b/sensors/psutil_sensors/cpu.py @@ -1,4 +1,27 @@ #!/usr/bin/python +# +# CPU sensor ontop of psutil +# +# Copyright 2012 Dan Ballard and Robert Hausch +# +## License: +# +# This file is part of System-Monitoring-Daemon. +# +# System-Monitoring-Daemon is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# System-Monitoring-Daemon is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with System-Monitoring-Daemon. If not, see . +## + + import psutil diff --git a/sensors/psutil_sensors/memory.py b/sensors/psutil_sensors/memory.py new file mode 100644 index 0000000..a1de211 --- /dev/null +++ b/sensors/psutil_sensors/memory.py @@ -0,0 +1,40 @@ +#!/usr/bin/python +# +# Memory monitor ontop of psutil +# +# Copyright 2012 Dan Ballard and Robert Hausch +# +## License: +# +# This file is part of System-Monitoring-Daemon. +# +# System-Monitoring-Daemon is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# System-Monitoring-Daemon is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with System-Monitoring-Daemon. If not, see . +## + + +import psutil + +class mem_monitor: + def __init__(self): + self.mem_usage = '' + self.virt_mem_usage = '' + + def update(self): + self.mem_usage = psutil.phymem_usage() + self.virt_mem_usage = psutil.virtmem_usage() + + def getFormatedData(self): + message = '{"type": "Memory", "value": "%.1f", "data": [{"name": "Physical", "value": "%.1f"},{"name": "Virtual", "value": "%.1f"}]}' % (self.mem_usage.percent, self.mem_usage.percent, self.virt_mem_usage.percent) + return message + +