提交 05c675d7 编写于 作者: N Nicolas Hennion

Add Uptime and Now plugin. First step for processes plugins. Had to be optimezd

上级 311ba7ce
......@@ -176,7 +176,8 @@ class GlancesInstance():
# Check if the attribute starts with 'get'
if (item.startswith(header)):
try:
# !!! Update the stat
# Update the stat
# !!! All the stat are updated before one grab (not optimized)
self.stats.update()
# Return the attribute
return getattr(self.stats, item)
......
......@@ -26,25 +26,25 @@ from psutil import NUM_CPUS
from glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):
"""
Glances' Core Plugin
Get stats about CPU core number
"""
Glances' Core Plugin
Get stats about CPU core number
stats is integer (number of core)
"""
stats is integer (number of core)
"""
def __init__(self):
GlancesPlugin.__init__(self)
def __init__(self):
GlancesPlugin.__init__(self)
def update(self):
"""
Update core stats
"""
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
# !!! 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
......@@ -19,7 +19,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from psutil import disk_partitions, disk_usage
from glances_plugin import GlancesPlugin, getTimeSinceLastUpdate
from glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Glances - An eye on your system
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# 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 <http://www.gnu.org/licenses/>.
# Import system libs
from datetime import datetime
# from ..plugins.glances_plugin import GlancesPlugin
from glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):
"""
Glances' Core Plugin
Get current date/time
stats is date (string)
"""
def __init__(self):
GlancesPlugin.__init__(self)
def update(self):
"""
Update current date/time
"""
# Now
self.stats = datetime.now()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Glances - An eye on your system
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# 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 <http://www.gnu.org/licenses/>.
# !!! Not optimized because both processcount and processlist
# !!! grab all processes.
# !!! Action: create a new file globalprocesses.py with a global
# !!! variable instance of GlancesGrabProcesses classes and share
# !!! it between the two plugins
from psutil import process_iter, AccessDenied, NoSuchProcess
from glances.core.glances_globals import is_BSD, is_Mac, is_Windows
from glances.core.glances_timer import Timer
from glances_plugin import GlancesPlugin, getTimeSinceLastUpdate
class Plugin(GlancesPlugin):
"""
Glances's processes Plugin
stats is a list
"""
def __init__(self):
GlancesPlugin.__init__(self)
# Init the process class
self.glancesgrabprocesses = GlancesGrabProcesses()
def update(self):
"""
Update processes stats
"""
self.glancesgrabprocesses.update()
self.stats = self.glancesgrabprocesses.getcount()
# processcount = self.glancesgrabprocesses.getcount()
# process = self.glancesgrabprocesses.getlist()
# if not hasattr(self, 'process'):
# self.processcount = {}
# self.process = []
# else:
# self.processcount = processcount
# self.process = process
def get_stats(self):
# Return the stats object for the RPC API
# Sort it by mount name
# Convert it to string
return str(self.stats)
class GlancesGrabProcesses:
"""
Get processed stats using the PsUtil lib
"""
def __init__(self, cache_timeout = 60):
"""
Init the class to collect stats about processes
"""
# Add internals caches because PSUtil do not cache all the stats
# See: https://code.google.com/p/psutil/issues/detail?id=462
self.username_cache = {}
self.cmdline_cache = {}
# The internals caches will be cleaned each 'cache_timeout' seconds
self.cache_timeout = cache_timeout
self.cache_timer = Timer(self.cache_timeout)
# Init the io dict
# key = pid
# value = [ read_bytes_old, write_bytes_old ]
self.io_old = {}
def __get_process_stats(self, proc):
"""
Get process statistics
"""
procstat = {}
# Process ID
procstat['pid'] = proc.pid
# Process name (cached by PSUtil)
procstat['name'] = proc.name
# Process username (cached with internal cache)
try:
self.username_cache[procstat['pid']]
except:
try:
self.username_cache[procstat['pid']] = proc.username
except KeyError:
try:
self.username_cache[procstat['pid']] = proc.uids.real
except KeyError:
self.username_cache[procstat['pid']] = "?"
procstat['username'] = self.username_cache[procstat['pid']]
# Process command line (cached with internal cache)
try:
self.cmdline_cache[procstat['pid']]
except:
self.cmdline_cache[procstat['pid']] = ' '.join(proc.cmdline)
procstat['cmdline'] = self.cmdline_cache[procstat['pid']]
# Process status
procstat['status'] = str(proc.status)[:1].upper()
# Process nice
procstat['nice'] = proc.get_nice()
# Process memory
procstat['memory_info'] = proc.get_memory_info()
procstat['memory_percent'] = proc.get_memory_percent()
# Process CPU
procstat['cpu_times'] = proc.get_cpu_times()
procstat['cpu_percent'] = proc.get_cpu_percent(interval=0)
# Process network connections (TCP and UDP) (Experimental)
# !!! High CPU consumption
# try:
# procstat['tcp'] = len(proc.get_connections(kind="tcp"))
# procstat['udp'] = len(proc.get_connections(kind="udp"))
# except:
# procstat['tcp'] = 0
# procstat['udp'] = 0
# Process IO
# procstat['io_counters'] is a list:
# [read_bytes, write_bytes, read_bytes_old, write_bytes_old, io_tag]
# If io_tag = 0 > Access denied (display "?")
# If io_tag = 1 > No access denied (display the IO rate)
# Note Disk IO stat not available on Mac OS
if not is_Mac:
try:
# Get the process IO counters
proc_io = proc.get_io_counters()
io_new = [proc_io.read_bytes, proc_io.write_bytes]
except AccessDenied:
# Access denied to process IO (no root account)
# Put 0 in all values (for sort) and io_tag = 0 (for display)
procstat['io_counters'] = [0, 0] + [0, 0]
io_tag = 0
else:
# For IO rate computation
# Append saved IO r/w bytes
try:
procstat['io_counters'] = io_new + self.io_old[procstat['pid']]
except KeyError:
procstat['io_counters'] = io_new + [0, 0]
# then save the IO r/w bytes
self.io_old[procstat['pid']] = io_new
io_tag = 1
# Append the IO tag (for display)
procstat['io_counters'] += [io_tag]
return procstat
def update(self):
self.processlist = []
self.processcount = {'total': 0, 'running': 0, 'sleeping': 0, 'thread': 0}
# Get the time since last update
time_since_update = getTimeSinceLastUpdate('process_disk')
# For each existing process...
for proc in process_iter():
try:
# Get stats using the PSUtil
procstat = self.__get_process_stats(proc)
# Add a specific time_since_update stats for bitrate
procstat['time_since_update'] = time_since_update
# ignore the 'idle' process on Windows and *BSD
# ignore the 'kernel_task' process on OS X
# waiting for upstream patch from psutil
if (is_BSD and procstat['name'] == 'idle' or
is_Windows and procstat['name'] == 'System Idle Process' or
is_Mac and procstat['name'] == 'kernel_task'):
continue
# Update processcount (global statistics)
try:
self.processcount[str(proc.status)] += 1
except KeyError:
# Key did not exist, create it
self.processcount[str(proc.status)] = 1
else:
self.processcount['total'] += 1
# Update thread number (global statistics)
try:
self.processcount['thread'] += proc.get_num_threads()
except:
pass
except (NoSuchProcess, AccessDenied):
continue
else:
# Update processlist
self.processlist.append(procstat)
# Clean internals caches if timeout is reached
if (self.cache_timer.finished()):
self.username_cache = {}
self.cmdline_cache = {}
# Restart the timer
self.cache_timer.reset()
def getcount(self):
return self.processcount
def getlist(self):
return self.processlist
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Glances - An eye on your system
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# 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 <http://www.gnu.org/licenses/>.
# !!! Not optimized because both processcount and processlist
# !!! grab all processes.
# !!! Action: create a new file globalprocesses.py with a global
# !!! variable instance of GlancesGrabProcesses classes and share
# !!! it between the two plugins
from psutil import process_iter, AccessDenied, NoSuchProcess
from glances.core.glances_globals import is_BSD, is_Mac, is_Windows
from glances.core.glances_timer import Timer
from glances_plugin import GlancesPlugin, getTimeSinceLastUpdate
from glances_processcount import GlancesGrabProcesses
class Plugin(GlancesPlugin):
"""
Glances's processes Plugin
stats is a list
"""
def __init__(self):
GlancesPlugin.__init__(self)
# Init the process class
self.glancesgrabprocesses = GlancesGrabProcesses()
def update(self):
"""
Update processes stats
"""
self.glancesgrabprocesses.update()
self.stats = self.glancesgrabprocesses.getlist()
# processcount = self.glancesgrabprocesses.getcount()
# process = self.glancesgrabprocesses.getlist()
# if not hasattr(self, 'process'):
# self.processcount = {}
# self.process = []
# else:
# self.processcount = processcount
# self.process = process
def get_stats(self):
# Return the stats object for the RPC API
# Convert it to string
return str(self.stats)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Glances - An eye on your system
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# 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 <http://www.gnu.org/licenses/>.
# Import system libs
# Check for PSUtil already done in the glances_core script
try:
from psutil import get_boot_time
except:
from putil import BOOT_TIME
from datetime import datetime
# from ..plugins.glances_plugin import GlancesPlugin
from glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):
"""
Glances' Core Plugin
Get stats about uptime
stats is date (string)
"""
def __init__(self):
GlancesPlugin.__init__(self)
def update(self):
"""
Update uptime stat
"""
# Uptime
try:
# For PsUtil >= 0.7.0
self.uptime = datetime.now() - datetime.fromtimestamp(get_boot_time())
except:
self.uptime = datetime.now() - datetime.fromtimestamp(BOOT_TIME)
# Convert uptime to string (because datetime is not JSONifi)
self.stats = str(self.uptime).split('.')[0]
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册