modularize and make loading psutil dependant code check if psutil is tehre first. Add more license boilerplate

This commit is contained in:
Dan Ballard 2012-04-07 20:36:32 -07:00
parent 63c8706039
commit 523e57e238
5 changed files with 108 additions and 25 deletions

View File

@ -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=())

View File

@ -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 <http://www.gnu.org/licenses/>.
##
# 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())

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
##
import psutil

View File

@ -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 <http://www.gnu.org/licenses/>.
##
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