Refactor the diskIO plugin (based on the network one)

This commit is contained in:
Nicolas Hennion 2014-03-13 21:34:21 +01:00
parent db4b9ae147
commit 1286515f62
5 changed files with 63 additions and 26 deletions

View File

@ -17,13 +17,16 @@
# #
# You should have received a copy of the GNU Lesser General Public License # 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/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Glances CPU plugin
"""
# Import system lib # Import system lib
from psutil import disk_io_counters from psutil import disk_io_counters
# Import Glances lib # Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
from glances.core.glances_globals import is_Mac from glances.core.glances_globals import is_Mac
from glances_plugin import GlancesPlugin
from glances.core.glances_timer import getTimeSinceLastUpdate from glances.core.glances_timer import getTimeSinceLastUpdate
@ -46,57 +49,76 @@ class Plugin(GlancesPlugin):
# Enter -1 to diplay bottom # Enter -1 to diplay bottom
self.line_curse = 3 self.line_curse = 3
# Init stats
self.diskio_old = []
def update(self): def update(self):
""" """
Update disk IO stats Update disk IO stats
""" """
self.diskio = [] # Grab the stat using the PsUtil disk_io_counters method
# read_count: number of reads
# Disk IO stat not available on Mac OS # write_count: number of writes
if is_Mac: # read_bytes: number of bytes read
self.stats = self.diskio # write_bytes: number of bytes written
return self.stats # read_time: time spent reading from disk (in milliseconds)
# write_time: time spent writing to disk (in milliseconds)
diskiocounters = disk_io_counters(perdisk=True)
# Previous disk IO stats are stored in the diskio_old variable
diskio = []
if (self.diskio_old == []):
# First call, we init the network_old var
try:
self.diskio_old = diskiocounters
except (IOError, UnboundLocalError):
pass
else:
# By storing time data we enable Rx/s and Tx/s calculations in the # By storing time data we enable Rx/s and Tx/s calculations in the
# XML/RPC API, which would otherwise be overly difficult work # XML/RPC API, which would otherwise be overly difficult work
# for users of the API # for users of the API
time_since_update = getTimeSinceLastUpdate('disk') time_since_update = getTimeSinceLastUpdate('disk')
if not hasattr(self, 'diskio_old'): diskio_new = diskiocounters
try: for disk in diskio_new:
self.diskio_old = disk_io_counters(perdisk=True)
except IOError:
self.diskio_error_tag = True
else:
self.diskio_new = disk_io_counters(perdisk=True)
for disk in self.diskio_new:
try: try:
# Try necessary to manage dynamic disk creation/del # Try necessary to manage dynamic disk creation/del
diskstat = {} diskstat = {}
diskstat['time_since_update'] = time_since_update diskstat['time_since_update'] = time_since_update
diskstat['disk_name'] = disk diskstat['disk_name'] = disk
diskstat['read_bytes'] = ( diskstat['read_bytes'] = (
self.diskio_new[disk].read_bytes - diskio_new[disk].read_bytes -
self.diskio_old[disk].read_bytes) self.diskio_old[disk].read_bytes)
diskstat['write_bytes'] = ( diskstat['write_bytes'] = (
self.diskio_new[disk].write_bytes - diskio_new[disk].write_bytes -
self.diskio_old[disk].write_bytes) self.diskio_old[disk].write_bytes)
except Exception: except KeyError:
continue continue
else: else:
self.diskio.append(diskstat) diskio.append(diskstat)
self.diskio_old = self.diskio_new self.diskio_old = diskio_new
self.stats = self.diskio self.stats = diskio
return self.stats
def msg_curse(self, args=None): def msg_curse(self, args=None):
""" """
Return the dict to display in the curse interface Return the dict to display in the curse interface
""" """
#!!! TODO: Add alert on disk IO
#!!! TODO: manage the hide tag for disk IO list
# Init the return message # Init the return message
ret = [] ret = []
# Only process if stats exist...
if (self.stats == []):
return ret
# Build the string message # Build the string message
# Header # Header
msg = "{0:8}".format(_("DISK I/O")) msg = "{0:8}".format(_("DISK I/O"))
@ -107,7 +129,6 @@ class Plugin(GlancesPlugin):
ret.append(self.curse_add_line(msg)) ret.append(self.curse_add_line(msg))
# Disk list (sorted by name) # Disk list (sorted by name)
for i in sorted(self.stats, key=lambda diskio: diskio['disk_name']): for i in sorted(self.stats, key=lambda diskio: diskio['disk_name']):
# !!! TODO: manage the hide tag
# New line # New line
ret.append(self.curse_new_line()) ret.append(self.curse_new_line())
msg = "{0:8}".format(i['disk_name']) msg = "{0:8}".format(i['disk_name'])

View File

@ -115,6 +115,10 @@ class Plugin(GlancesPlugin):
# Init the return message # Init the return message
ret = [] ret = []
# Only process if stats exist...
if (self.stats == []):
return ret
# Build the string message # Build the string message
# Header # Header
msg = "{0:8}".format(_("MOUNT")) msg = "{0:8}".format(_("MOUNT"))

View File

@ -98,6 +98,10 @@ class Plugin(GlancesPlugin):
# Init the return message # Init the return message
ret = [] ret = []
# Only process if stats exist...
if (self.stats == {}):
return ret
# Build the string message # Build the string message
# Header # Header
msg = "{0:5} ".format(_("MEM")) msg = "{0:5} ".format(_("MEM"))

View File

@ -81,6 +81,10 @@ class Plugin(GlancesPlugin):
# Init the return message # Init the return message
ret = [] ret = []
# Only process if stats exist...
if (self.stats == {}):
return ret
# Build the string message # Build the string message
# Header # Header
msg = "{0:5} ".format(_("SWAP")) msg = "{0:5} ".format(_("SWAP"))

View File

@ -113,6 +113,10 @@ class Plugin(GlancesPlugin):
# Init the return message # Init the return message
ret = [] ret = []
# Only process if stats exist...
if (self.stats == []):
return ret
# Build the string message # Build the string message
# Header # Header
msg = "{0:8}".format(_("NETWORK")) msg = "{0:8}".format(_("NETWORK"))