From 9613d6aaa74136eee8cbd9bf05a63aa033f313de Mon Sep 17 00:00:00 2001 From: Nicolas Hennion Date: Wed, 22 Jan 2014 10:49:35 +0100 Subject: Server starts working :) --- glances/README.txt | 2 +- glances/core/glances_server.py | 209 ++++++++++++++++++++++------------------ glances/plugins/glances_core.py | 50 ++++++++++ glances/plugins/glances_host.py | 1 - glances/plugins/glances_load.py | 50 ++++++++++ 5 files changed, 217 insertions(+), 95 deletions(-) create mode 100644 glances/plugins/glances_core.py create mode 100644 glances/plugins/glances_load.py diff --git a/glances/README.txt b/glances/README.txt index c7e36ad3..4664b204 100644 --- a/glances/README.txt +++ b/glances/README.txt @@ -25,4 +25,4 @@ outputs/ glances_curse.py The Curse (console) interface glances_csv.py The CSV interface glances_html.py The HTML interface - ... \ No newline at end of file + ... diff --git a/glances/core/glances_server.py b/glances/core/glances_server.py index 55e26a61..934499f5 100644 --- a/glances/core/glances_server.py +++ b/glances/core/glances_server.py @@ -166,99 +166,122 @@ class GlancesInstance(): # Return the processes monitored list return json.dumps(monitors.getAll()) - def getSystem(self): - # Return operating system info - # No need to update... - #~ self.__update__() - return json.dumps(self.stats.getSystem()) - - def getCore(self): - # Update and return number of Core - self.__update__() - return json.dumps(self.stats.getCore()) - - def getCpu(self): - # Update and return CPU stats - self.__update__() - return json.dumps(self.stats.getCpu()) - - def getLoad(self): - # Update and return LOAD stats - self.__update__() - return json.dumps(self.stats.getLoad()) - - def getMem(self): - # Update and return MEM stats - self.__update__() - return json.dumps(self.stats.getMem()) - - def getMemSwap(self): - # Update and return MEMSWAP stats - self.__update__() - return json.dumps(self.stats.getMemSwap()) - - def getSensors(self): - # Update and return SENSORS stats - self.__update__() - return json.dumps(self.stats.getSensors()) - - def getHDDTemp(self): - # Update and return HDDTEMP stats - self.__update__() - return json.dumps(self.stats.getHDDTemp()) - - def getNetwork(self): - # Update and return NET stats - self.__update__() - return json.dumps(self.stats.getNetwork()) - - def getDiskIO(self): - # Update and return DISK IO stats - self.__update__() - return json.dumps(self.stats.getDiskIO()) - - def getFs(self): - # Update and return FS stats - self.__update__() - return json.dumps(self.stats.getFs()) - - def getProcessCount(self): - # Update and return ProcessCount stats - self.__update__() - return json.dumps(self.stats.getProcessCount()) - - def getProcessList(self): - # Update and return ProcessList stats - self.__update__() - return json.dumps(self.stats.getProcessList()) - - def getBatPercent(self): - # Update and return total batteries percent stats - self.__update__() - return json.dumps(self.stats.getBatPercent()) - - def getNow(self): - # Update and return current date/hour - self.__update__() - return json.dumps(self.stats.getNow().strftime(_("%Y-%m-%d %H:%M:%S"))) - - def getUptime(self): - # Update and return system uptime - self.__update__() - return json.dumps(self.stats.getUptime().strftime(_("%Y-%m-%d %H:%M:%S"))) - - def __getTimeSinceLastUpdate(self, IOType): - assert(IOType in ['net', 'disk', 'process_disk']) - return getTimeSinceLastUpdate(IOType) - - def getNetTimeSinceLastUpdate(self): - return getTimeSinceLastUpdate('net') - - def getDiskTimeSinceLastUpdate(self): - return getTimeSinceLastUpdate('net') - - def getProcessDiskTimeSinceLastUpdate(self): - return getTimeSinceLastUpdate('process_disk') + def __getattr__(self, item): + """ + Overwrite the getattr in case of attribute is not found + The goal is to dynamicaly generate the API get'Stats'() methods + """ + + header = 'get' + # Check if the attribute starts with 'get' + if (item.startswith(header)): + try: + # !!! Update the stat + self.stats.update() + # Return the attribute + return getattr(self.stats, item) + except Exception, e: + # The method is not found for the plugin + raise AttributeError(item) + else: + # Default behavior + raise AttributeError(item) + + # !!! Check the 2.0 cover before deleted the following comments + + # def getSystem(self): + # # Return operating system info + # # No need to update... + # #~ self.__update__() + # return json.dumps(self.stats.getSystem()) + + # def getCore(self): + # # Update and return number of Core + # self.__update__() + # return json.dumps(self.stats.getCore()) + + # def getCpu(self): + # # Update and return CPU stats + # self.__update__() + # return json.dumps(self.stats.getCpu()) + + # def getLoad(self): + # # Update and return LOAD stats + # self.__update__() + # return json.dumps(self.stats.getLoad()) + + # def getMem(self): + # # Update and return MEM stats + # self.__update__() + # return json.dumps(self.stats.getMem()) + + # def getMemSwap(self): + # # Update and return MEMSWAP stats + # self.__update__() + # return json.dumps(self.stats.getMemSwap()) + + # def getSensors(self): + # # Update and return SENSORS stats + # self.__update__() + # return json.dumps(self.stats.getSensors()) + + # def getHDDTemp(self): + # # Update and return HDDTEMP stats + # self.__update__() + # return json.dumps(self.stats.getHDDTemp()) + + # def getNetwork(self): + # # Update and return NET stats + # self.__update__() + # return json.dumps(self.stats.getNetwork()) + + # def getDiskIO(self): + # # Update and return DISK IO stats + # self.__update__() + # return json.dumps(self.stats.getDiskIO()) + + # def getFs(self): + # # Update and return FS stats + # self.__update__() + # return json.dumps(self.stats.getFs()) + + # def getProcessCount(self): + # # Update and return ProcessCount stats + # self.__update__() + # return json.dumps(self.stats.getProcessCount()) + + # def getProcessList(self): + # # Update and return ProcessList stats + # self.__update__() + # return json.dumps(self.stats.getProcessList()) + + # def getBatPercent(self): + # # Update and return total batteries percent stats + # self.__update__() + # return json.dumps(self.stats.getBatPercent()) + + # def getNow(self): + # # Update and return current date/hour + # self.__update__() + # return json.dumps(self.stats.getNow().strftime(_("%Y-%m-%d %H:%M:%S"))) + + # def getUptime(self): + # # Update and return system uptime + # self.__update__() + # return json.dumps(self.stats.getUptime().strftime(_("%Y-%m-%d %H:%M:%S"))) + + # def __getTimeSinceLastUpdate(self, IOType): + # assert(IOType in ['net', 'disk', 'process_disk']) + # return getTimeSinceLastUpdate(IOType) + + # def getNetTimeSinceLastUpdate(self): + # return getTimeSinceLastUpdate('net') + + # def getDiskTimeSinceLastUpdate(self): + # return getTimeSinceLastUpdate('net') + + # def getProcessDiskTimeSinceLastUpdate(self): + # return getTimeSinceLastUpdate('process_disk') class GlancesServer(): diff --git a/glances/plugins/glances_core.py b/glances/plugins/glances_core.py new file mode 100644 index 00000000..6986748a --- /dev/null +++ b/glances/plugins/glances_core.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Glances - An eye on your system +# +# Copyright (C) 2014 Nicolargo +# +# Glances is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Glances 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . + +# Import system libs +# Check for PSUtil already done in the glances_core script +from psutil import NUM_CPUS + +# from ..plugins.glances_plugin import GlancesPlugin +from glances_plugin import GlancesPlugin + +class Plugin(GlancesPlugin): + """ + Glances' Core Plugin + Get stats about CPU core number + + stats is integer (number of core) + """ + + def __init__(self): + GlancesPlugin.__init__(self) + + + def update(self): + """ + Update core stats + """ + + # !!! Note: The PSUtil 2.0 include psutil.cpu_count() and psutil.cpu_count(logical=False) + # !!! TODO: We had to return a dict (with both hys and log cpu number) instead of a integer + try: + self.stats = NUM_CPUS + except Exception, e: + self.stats = None diff --git a/glances/plugins/glances_host.py b/glances/plugins/glances_host.py index 5cf7e98e..b0ca1220 100644 --- a/glances/plugins/glances_host.py +++ b/glances/plugins/glances_host.py @@ -58,4 +58,3 @@ class Plugin(GlancesPlugin): self.stats['os_version'] = ' '.join(os_version[::2]) else: self.stats['os_version'] = "" - diff --git a/glances/plugins/glances_load.py b/glances/plugins/glances_load.py new file mode 100644 index 00000000..a963488f --- /dev/null +++ b/glances/plugins/glances_load.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Glances - An eye on your system +# +# Copyright (C) 2014 Nicolargo +# +# Glances is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Glances 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . + +# Import system libs +# Check for PSUtil already done in the glances_core script +from os import getloadavg + +# from ..plugins.glances_plugin import GlancesPlugin +from glances_plugin import GlancesPlugin + +class Plugin(GlancesPlugin): + """ + Glances's Load Plugin + + stats is a dict + """ + + def __init__(self): + GlancesPlugin.__init__(self) + + + def update(self): + """ + Update load stats + """ + try: + load = getloadavg() + + self.stats = {'min1': load[0], + 'min5': load[1], + 'min15': load[2]} + except Exception, err: + self.stats = {} -- cgit v1.2.3