提交 3366b421 编写于 作者: N Nicolas Hennion

Add network plugin

上级 9613d6aa
......@@ -56,14 +56,6 @@ if psutil_version < (0, 5, 0):
print('PsUtil 0.5.1 or higher is needed. Glances cannot start.')
sys.exit(1)
try:
# psutil.virtual_memory() only available from psutil >= 0.6
psutil.virtual_memory()
except Exception:
psutil_mem_vm = False
else:
psutil_mem_vm = True
try:
# psutil.net_io_counters() only available from psutil >= 1.0.0
psutil.net_io_counters()
......
......@@ -26,68 +26,68 @@ from psutil import cpu_times
from glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):
"""
Glances' Cpu Plugin
"""
Glances' Cpu Plugin
stats is a dict
"""
stats is a dict
"""
def __init__(self):
GlancesPlugin.__init__(self)
def __init__(self):
GlancesPlugin.__init__(self)
def update(self):
"""
Update CPU stats
"""
def update(self):
"""
Update CPU stats
"""
# Grab CPU using the PSUtil cpu_times method
cputime = cpu_times(percpu=False)
cputime_total = cputime.user + cputime.system + cputime.idle
# Grab CPU using the PSUtil cpu_times method
cputime = cpu_times(percpu=False)
cputime_total = cputime.user + cputime.system + cputime.idle
# Only available on some OS
if hasattr(cputime, 'nice'):
cputime_total += cputime.nice
if hasattr(cputime, 'iowait'):
cputime_total += cputime.iowait
if hasattr(cputime, 'irq'):
cputime_total += cputime.irq
if hasattr(cputime, 'softirq'):
cputime_total += cputime.softirq
if hasattr(cputime, 'steal'):
cputime_total += cputime.steal
if not hasattr(self, 'cputime_old'):
self.cputime_old = cputime
self.cputime_total_old = cputime_total
self.stats = {}
else:
self.cputime_new = cputime
self.cputime_total_new = cputime_total
try:
percent = 100 / (self.cputime_total_new -
self.cputime_total_old)
self.stats = {'user': (self.cputime_new.user -
self.cputime_old.user) * percent,
'system': (self.cputime_new.system -
self.cputime_old.system) * percent,
'idle': (self.cputime_new.idle -
self.cputime_old.idle) * percent}
if hasattr(self.cputime_new, 'nice'):
self.stats['nice'] = (self.cputime_new.nice -
self.cputime_old.nice) * percent
if hasattr(self.cputime_new, 'iowait'):
self.stats['iowait'] = (self.cputime_new.iowait -
self.cputime_old.iowait) * percent
if hasattr(self.cputime_new, 'irq'):
self.stats['irq'] = (self.cputime_new.irq -
self.cputime_old.irq) * percent
if hasattr(self.cputime_new, 'softirq'):
self.stats['softirq'] = (self.cputime_new.softirq -
self.cputime_old.softirq) * percent
if hasattr(self.cputime_new, 'steal'):
self.stats['steal'] = (self.cputime_new.steal -
self.cputime_old.steal) * percent
self.cputime_old = self.cputime_new
self.cputime_total_old = self.cputime_total_new
except Exception, err:
self.stats = {}
# Only available on some OS
if hasattr(cputime, 'nice'):
cputime_total += cputime.nice
if hasattr(cputime, 'iowait'):
cputime_total += cputime.iowait
if hasattr(cputime, 'irq'):
cputime_total += cputime.irq
if hasattr(cputime, 'softirq'):
cputime_total += cputime.softirq
if hasattr(cputime, 'steal'):
cputime_total += cputime.steal
if not hasattr(self, 'cputime_old'):
self.cputime_old = cputime
self.cputime_total_old = cputime_total
self.stats = {}
else:
self.cputime_new = cputime
self.cputime_total_new = cputime_total
try:
percent = 100 / (self.cputime_total_new -
self.cputime_total_old)
self.stats = {'user': (self.cputime_new.user -
self.cputime_old.user) * percent,
'system': (self.cputime_new.system -
self.cputime_old.system) * percent,
'idle': (self.cputime_new.idle -
self.cputime_old.idle) * percent}
if hasattr(self.cputime_new, 'nice'):
self.stats['nice'] = (self.cputime_new.nice -
self.cputime_old.nice) * percent
if hasattr(self.cputime_new, 'iowait'):
self.stats['iowait'] = (self.cputime_new.iowait -
self.cputime_old.iowait) * percent
if hasattr(self.cputime_new, 'irq'):
self.stats['irq'] = (self.cputime_new.irq -
self.cputime_old.irq) * percent
if hasattr(self.cputime_new, 'softirq'):
self.stats['softirq'] = (self.cputime_new.softirq -
self.cputime_old.softirq) * percent
if hasattr(self.cputime_new, 'steal'):
self.stats['steal'] = (self.cputime_new.steal -
self.cputime_old.steal) * percent
self.cputime_old = self.cputime_new
self.cputime_total_old = self.cputime_total_new
except Exception, err:
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
import psutil
# from ..plugins.glances_plugin import GlancesPlugin
from glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):
"""
Glances's memory Plugin
stats is a dict
"""
def __init__(self):
GlancesPlugin.__init__(self)
def update(self):
"""
Update MEM (RAM) stats
"""
# RAM
# psutil >= 0.6
if hasattr(psutil, 'virtual_memory'):
phymem = psutil.virtual_memory()
# buffers and cached (Linux, BSD)
buffers = getattr(phymem, 'buffers', 0)
cached = getattr(phymem, 'cached', 0)
# active and inactive not available on Windows
active = getattr(phymem, 'active', 0)
inactive = getattr(phymem, 'inactive', 0)
# phymem free and usage
total = phymem.total
free = phymem.available # phymem.free + buffers + cached
used = total - free
self.stats = {'total': total,
'percent': phymem.percent,
'used': used,
'free': free,
'active': active,
'inactive': inactive,
'buffers': buffers,
'cached': cached}
# psutil < 0.6
elif hasattr(psutil, 'phymem_usage'):
phymem = psutil.phymem_usage()
# buffers and cached (Linux, BSD)
buffers = getattr(psutil, 'phymem_buffers', 0)()
cached = getattr(psutil, 'cached_phymem', 0)()
# phymem free and usage
total = phymem.total
free = phymem.free + buffers + cached
used = total - free
# active and inactive not available for psutil < 0.6
self.stats = {'total': total,
'percent': phymem.percent,
'used': used,
'free': free,
'buffers': buffers,
'cached': cached}
else:
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
import psutil
# from ..plugins.glances_plugin import GlancesPlugin
from glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):
"""
Glances's swap memory Plugin
stats is a dict
"""
def __init__(self):
GlancesPlugin.__init__(self)
def update(self):
"""
Update MEM (SWAP) stats
"""
# SWAP
# psutil >= 0.6
if hasattr(psutil, 'swap_memory'):
# Try... is an hack for issue #152
try:
virtmem = psutil.swap_memory()
except Exception:
self.stats = {}
else:
self.stats = {'total': virtmem.total,
'used': virtmem.used,
'free': virtmem.free,
'percent': virtmem.percent}
# psutil < 0.6
elif hasattr(psutil, 'virtmem_usage'):
virtmem = psutil.virtmem_usage()
self.stats = {'total': virtmem.total,
'used': virtmem.used,
'free': virtmem.free,
'percent': virtmem.percent}
else:
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
import psutil
try:
# psutil.net_io_counters() only available from psutil >= 1.0.0
psutil.net_io_counters()
except Exception:
psutil_net_io_counters = False
else:
psutil_net_io_counters = True
# from ..plugins.glances_plugin import GlancesPlugin
from glances_plugin import GlancesPlugin, getTimeSinceLastUpdate
class Plugin(GlancesPlugin):
"""
Glances's network Plugin
stats is a list
"""
def __init__(self):
GlancesPlugin.__init__(self)
def update(self):
"""
Update network stats
"""
# By storing time data we enable Rx/s and Tx/s calculations in the
# XML/RPC API, which would otherwise be overly difficult work
# for users of the API
time_since_update = getTimeSinceLastUpdate('net')
if psutil_net_io_counters:
# psutil >= 1.0.0
try:
get_net_io_counters = psutil.net_io_counters(pernic=True)
except IOError:
pass
else:
# psutil < 1.0.0
try:
get_net_io_counters = psutil.network_io_counters(pernic=True)
except IOError:
pass
network = []
# Previous network interface stats are stored in the network_old variable
if not hasattr(self, 'network_old'):
# First call, we init the network_old var
try:
self.network_old = get_net_io_counters
except (IOError, UnboundLocalError):
pass
else:
network_new = get_net_io_counters
for net in network_new:
try:
# Try necessary to manage dynamic network interface
netstat = {}
netstat['time_since_update'] = time_since_update
netstat['interface_name'] = net
netstat['cumulative_rx'] = network_new[net].bytes_recv
netstat['rx'] = (network_new[net].bytes_recv -
self.network_old[net].bytes_recv)
netstat['cumulative_tx'] = network_new[net].bytes_sent
netstat['tx'] = (network_new[net].bytes_sent -
self.network_old[net].bytes_sent)
netstat['cumulative_cx'] = (netstat['cumulative_rx'] +
netstat['cumulative_tx'])
netstat['cx'] = netstat['rx'] + netstat['tx']
except Exception:
continue
else:
network.append(netstat)
self.network_old = network_new
self.stats = network
......@@ -26,82 +26,82 @@ from psutil import cpu_times
from glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):
"""
Glances' PerCpu Plugin
"""
Glances' PerCpu Plugin
stats is a list
"""
stats is a list
"""
def __init__(self):
GlancesPlugin.__init__(self)
def __init__(self):
GlancesPlugin.__init__(self)
def update(self):
"""
Update Per CPU stats
"""
def update(self):
"""
Update Per CPU stats
"""
# Grab CPU using the PSUtil cpu_times method
# Per-CPU
percputime = cpu_times(percpu=True)
percputime_total = []
for i in range(len(percputime)):
percputime_total.append(percputime[i].user +
percputime[i].system +
percputime[i].idle)
# Only available on some OS
for i in range(len(percputime)):
if hasattr(percputime[i], 'nice'):
percputime_total[i] += percputime[i].nice
for i in range(len(percputime)):
if hasattr(percputime[i], 'iowait'):
percputime_total[i] += percputime[i].iowait
for i in range(len(percputime)):
if hasattr(percputime[i], 'irq'):
percputime_total[i] += percputime[i].irq
for i in range(len(percputime)):
if hasattr(percputime[i], 'softirq'):
percputime_total[i] += percputime[i].softirq
for i in range(len(percputime)):
if hasattr(percputime[i], 'steal'):
percputime_total[i] += percputime[i].steal
if not hasattr(self, 'percputime_old'):
self.percputime_old = percputime
self.percputime_total_old = percputime_total
self.stats = []
else:
self.percputime_new = percputime
self.percputime_total_new = percputime_total
perpercent = []
self.stats = []
try:
for i in range(len(self.percputime_new)):
perpercent.append(100 / (self.percputime_total_new[i] -
self.percputime_total_old[i]))
cpu = {'user': (self.percputime_new[i].user -
self.percputime_old[i].user) * perpercent[i],
'system': (self.percputime_new[i].system -
self.percputime_old[i].system) * perpercent[i],
'idle': (self.percputime_new[i].idle -
self.percputime_old[i].idle) * perpercent[i]}
if hasattr(self.percputime_new[i], 'nice'):
cpu['nice'] = (self.percputime_new[i].nice -
self.percputime_old[i].nice) * perpercent[i]
if hasattr(self.percputime_new[i], 'iowait'):
cpu['iowait'] = (self.percputime_new[i].iowait -
self.percputime_old[i].iowait) * perpercent[i]
if hasattr(self.percputime_new[i], 'irq'):
cpu['irq'] = (self.percputime_new[i].irq -
self.percputime_old[i].irq) * perpercent[i]
if hasattr(self.percputime_new[i], 'softirq'):
cpu['softirq'] = (self.percputime_new[i].softirq -
self.percputime_old[i].softirq) * perpercent[i]
if hasattr(self.percputime_new[i], 'steal'):
cpu['steal'] = (self.percputime_new[i].steal -
self.percputime_old[i].steal) * perpercent[i]
self.stats.append(cpu)
self.percputime_old = self.percputime_new
self.percputime_total_old = self.percputime_total_new
except Exception, err:
self.stats = []
# Grab CPU using the PSUtil cpu_times method
# Per-CPU
percputime = cpu_times(percpu=True)
percputime_total = []
for i in range(len(percputime)):
percputime_total.append(percputime[i].user +
percputime[i].system +
percputime[i].idle)
# Only available on some OS
for i in range(len(percputime)):
if hasattr(percputime[i], 'nice'):
percputime_total[i] += percputime[i].nice
for i in range(len(percputime)):
if hasattr(percputime[i], 'iowait'):
percputime_total[i] += percputime[i].iowait
for i in range(len(percputime)):
if hasattr(percputime[i], 'irq'):
percputime_total[i] += percputime[i].irq
for i in range(len(percputime)):
if hasattr(percputime[i], 'softirq'):
percputime_total[i] += percputime[i].softirq
for i in range(len(percputime)):
if hasattr(percputime[i], 'steal'):
percputime_total[i] += percputime[i].steal
if not hasattr(self, 'percputime_old'):
self.percputime_old = percputime
self.percputime_total_old = percputime_total
self.stats = []
else:
self.percputime_new = percputime
self.percputime_total_new = percputime_total
perpercent = []
self.stats = []
try:
for i in range(len(self.percputime_new)):
perpercent.append(100 / (self.percputime_total_new[i] -
self.percputime_total_old[i]))
cpu = {'user': (self.percputime_new[i].user -
self.percputime_old[i].user) * perpercent[i],
'system': (self.percputime_new[i].system -
self.percputime_old[i].system) * perpercent[i],
'idle': (self.percputime_new[i].idle -
self.percputime_old[i].idle) * perpercent[i]}
if hasattr(self.percputime_new[i], 'nice'):
cpu['nice'] = (self.percputime_new[i].nice -
self.percputime_old[i].nice) * perpercent[i]
if hasattr(self.percputime_new[i], 'iowait'):
cpu['iowait'] = (self.percputime_new[i].iowait -
self.percputime_old[i].iowait) * perpercent[i]
if hasattr(self.percputime_new[i], 'irq'):
cpu['irq'] = (self.percputime_new[i].irq -
self.percputime_old[i].irq) * perpercent[i]
if hasattr(self.percputime_new[i], 'softirq'):
cpu['softirq'] = (self.percputime_new[i].softirq -
self.percputime_old[i].softirq) * perpercent[i]
if hasattr(self.percputime_new[i], 'steal'):
cpu['steal'] = (self.percputime_new[i].steal -
self.percputime_old[i].steal) * perpercent[i]
self.stats.append(cpu)
self.percputime_old = self.percputime_new
self.percputime_total_old = self.percputime_total_new
except Exception, err:
self.stats = []
......@@ -18,6 +18,23 @@
# 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/>.
from time import time
last_update_times = {}
def getTimeSinceLastUpdate(IOType):
global last_update_times
# assert(IOType in ['net', 'disk', 'process_disk'])
current_time = time()
last_time = last_update_times.get(IOType)
if not last_time:
time_since_update = 1
else:
time_since_update = current_time - last_time
last_update_times[IOType] = current_time
return time_since_update
class GlancesPlugin(object):
"""
Main class for Glances' plugin
......@@ -32,5 +49,6 @@ class GlancesPlugin(object):
return str(self.stats)
def get_stats(self):
# Return the stats object
return self.stats
# Return the stats object for the RPC API
# Had to convert it to string
return str(self.stats)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册