# -*- python -*-
from plugins.dentrystate import DentryState
from plugins.df import Discs
from plugins.discstats import DiscStats
from plugins.files import Files
from plugins.inodestate import InodeState
from plugins.locks import Locks
from plugins.random import Random
from common import *
class MyIO(Entry):
def __init__(self, *args, fs_name_map = None, fs_ignore = None, fs_type_ignore = None, **kwargs):
self.fs_name_map = [] if fs_name_map is None else fs_name_map
self.fs_ignore = set([] if fs_ignore is None else fs_ignore)
self.fs_type_ignore = set(MyIO.nodev_fs() if fs_type_ignore is None else fs_type_ignore)
self.show_value = 0
self.show_disc_value = 0
self.show_overall = True
self.show_disc = 0
self.disc_map = None
Entry.__init__(self, *args, **kwargs)
@staticmethod
def nodev_fs():
ret = []
with open('/proc/filesystems', 'rb') as file:
data = file.read()
data = data.decode('utf-8', 'strict')[:-1].replace('\t', ' ').split('\n')
for line in data:
cols = line.split(' ')
if 'nodev' in cols[:-1]:
ret.append(cols[-1])
ret += ['fuse.gvfsd-fuse']
return ret
def action(self, col, button, x, y):
if button == LEFT_BUTTON:
overall = self.show_overall
disc_map = self.disc_map
if overall and disc_map is not None:
x = 0
for text, disc in disc_map:
if col < x:
break
w = Bar.coloured_length(text)
if col < x + w:
if disc is not None:
self.show_disc = disc
break
x += w + 1
self.show_overall = not overall
elif button == MIDDLE_BUTTON and not self.show_overall:
self.show_disc_value = max(self.show_disc_value - 1, 0)
elif button == RIGHT_BUTTON and not self.show_overall:
self.show_disc_value = min(self.show_disc_value + 1, 4)
elif button == SCROLL_UP:
if self.show_overall:
self.show_value = min(self.show_value + 1, 13)
else:
show_disc = self.show_disc
if isinstance(show_disc, str):
try:
show_disc = [d.filesystem for d in self.get_discs()].index(show_disc)
except:
show_disc = -1
self.show_disc = show_disc + 1
elif button == SCROLL_DOWN:
if self.show_overall:
self.show_value = max(self.show_value - 1, 0)
else:
show_disc = self.show_disc
if isinstance(show_disc, str):
try:
show_disc = [d.filesystem for d in self.get_discs()].index(show_disc)
except:
show_disc = -1
self.show_disc = max(show_disc - 1, 0)
else:
return
self.invalidate()
def colour(self, percent):
colour = '39'
if percent < 25: colour = '32'
if percent > 50: colour = '33'
if percent > 90: colour = '31'
if percent > 95: colour = '39;41'
return colour
def unit(self, value):
units = ['', 'K', 'M', 'G', 'T', 'P', 'E']
unit = 0
while unit + 1 < len(units) and value >= 1024:
unit += 1
value /= 1024
return (value, units[unit])
def si(self, value, baseunit = 0):
unit = baseunit + 3
units = ['n', 'µ', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E']
while unit + 1 < len(units) and value >= 1000:
unit += 1
value /= 1000
return (value, units[unit])
def get_discs(self):
discs = Discs()
fs = [discs.filesystems[f] for f in sorted(discs.filesystems.keys()) if
f not in self.fs_ignore and
discs.filesystems[f].fstype not in self.fs_type_ignore and
f[0] == '/']
return fs
def function_overall(self):
df = []
if self.show_value == 0:
label = 'Df'
self.disc_map = [(label + ':', None)]
used = 0
available = 0
for disc in self.get_discs():
percent = disc.used * 100 / (disc.used + disc.available)
text = '\033[%sm%.0f\033[0m%%' % (self.colour(percent), percent)
df.append(text)
self.disc_map.append((text, disc.filesystem))
used += disc.used
available += disc.available
df.append(':')
percent = used * 100 / (used + available)
df.append('\033[%sm%.0f\033[0m%%' % (self.colour(percent), percent))
elif self.show_value == 1:
label = 'Df'
self.disc_map = [(label + ':', None)]
used = 0
available = 0
for disc in self.get_discs():
percent = disc.used * 100 / (disc.used + disc.available)
text = '\033[%sm%.1f\033[0m%s' % (self.colour(percent), *self.unit(disc.available))
df.append(text)
self.disc_map.append((text, disc.filesystem))
used += disc.used
available += disc.available
df.append(':')
percent = used * 100 / (used + available)
df.append('\033[%sm%.1f\033[0m%s' % (self.colour(percent), *self.unit(available)))
elif self.show_value == 2:
label = 'Df(inodes)'
self.disc_map = [(label + ':', None)]
used = 0
available = 0
for disc in self.get_discs():
percent = disc.iused * 100 / (disc.iused + disc.ifree)
text = '\033[%sm%.0f\033[0m%%' % (self.colour(percent), percent)
df.append(text)
self.disc_map.append((text, disc.filesystem))
used += disc.iused
available += disc.ifree
df.append(':')
percent = used * 100 / (used + available)
df.append('\033[%sm%.0f\033[0m%%' % (self.colour(percent), percent))
elif self.show_value == 3:
label = 'Dent'
dentry = DentryState()
percent = (dentry.nr_dentry - dentry.nr_unused) * 100 / dentry.nr_dentry
df.append('\033[%sm%.0f\033[0m%%' % (self.colour(percent), percent))
df.append('%is' % dentry.age_limit)
df.append('%ipages' % dentry.want_pages)
elif self.show_value == 4:
label = 'Inode'
inode = InodeState()
percent = (inode.nr_inodes - inode.nr_free_inodes) * 100 / inode.nr_inodes
df.append('\033[%sm%.0f\033[0m%%%s' % (self.colour(percent), percent,
'' if inode.preshrink == 0 else ' preshrink'))
elif self.show_value == 5:
label = 'File'
files = Files()
percent = (files.nr_files - files.nr_free_files) * 100 / files.file_max
df.append('\033[%sm%.0f\033[0m%%' % (self.colour(percent), percent))
df.append('%i/%i/%i' % (files.nr_files - files.nr_free_files, files.nr_files, files.file_max))
elif self.show_value == 6:
label = 'Lock'
locks = Locks().locks
ar, aw, mr, mw = 0, 0, 0, 0
for lock in locks:
if lock.mandatory:
if lock.shared:
mr += 1
else:
mw += 1
else:
if lock.shared:
ar += 1
else:
aw += 1
df.append('%iar' % ar)
df.append('%iaw' % aw)
df.append('%imr' % mr)
df.append('%imw' % mw)
df.append(':')
df.append('%ia' % (ar + aw))
df.append('%im' % (mr + mw))
df.append('%ir' % (ar + mr))
df.append('%iw' % (aw + mw))
df.append(':')
df.append('%i' % (ar + aw + mr + mw))
elif self.show_value == 7:
label = 'Rand'
random = Random()
df.append(str(random.poolsize))
elif self.show_value in (8, 11):
label = 'Disc(r %s)' % ('sum' if self.show_value == 8 else 'avg')
stats = DiscStats().devices.values()
r_complete, r_merge, r_sectors, r_time = 0, 0, 0, 0
for stat in stats:
r_complete += stat.r_complete
r_merge += stat.r_merge
r_sectors += stat.r_sectors
r_time += stat.r_time
n = 1 if self.show_value == 8 else len(stats)
r_complete /= n
r_merge /= n
r_sectors /= n
r_time /= n
df.append('%.1f%scompleted' % self.si(r_complete))
df.append('%.1f%smerge' % self.si(r_merge))
df.append('%.1f%ssectors' % self.si(r_sectors))
df.append('%.1f%ss' % self.si(r_time, -1))
elif self.show_value in (9, 12):
label = 'Disc(w %s)' % ('sum' if self.show_value == 9 else 'avg')
stats = DiscStats().devices.values()
w_complete, w_merge, w_sectors, w_time = 0, 0, 0, 0
for stat in stats:
w_complete += stat.w_complete
w_merge += stat.w_merge
w_sectors += stat.w_sectors
w_time += stat.w_time
n = 1 if self.show_value == 9 else len(stats)
w_complete /= n
w_merge /= n
w_sectors /= n
w_time /= n
df.append('%.1f%scomplete' % self.si(w_complete))
df.append('%.1f%smerge' % self.si(w_merge))
df.append('%.1f%ssectors' % self.si(w_sectors))
df.append('%.1f%ss' % self.si(w_time, -1))
elif self.show_value in (10, 13):
label = 'Disc(io %s)' % ('sum' if self.show_value == 10 else 'avg')
stats = DiscStats().devices.values()
io_current, io_time, io_weighted_time = 0, 0, 0
for stat in stats:
io_current += stat.io_current
io_time += stat.io_time
io_weighted_time += stat.io_weighted_time
n = 1 if self.show_value == 10 else len(stats)
io_current /= n
io_time /= n
io_weighted_time /= n
df.append('%.1f%s' % self.si(io_current))
df.append('%.1f%ss' % self.si(io_time, -1))
df.append('%.1f%ss(weighted)' % self.si(io_weighted_time, -1))
return '%s: %s' % (label, ' '.join(df))
def function_disc(self):
discs = self.get_discs()
disc = self.show_disc
if isinstance(disc, str):
for i, d in enumerate(discs):
if d.filesystem == disc:
disc = i
break
if isinstance(disc, str):
disc = 0
elif disc >= len(discs):
disc = len(discs) - 1
disc = discs[disc]
name = disc.filesystem
name = self.fs_name_map[name] if name in self.fs_name_map else name.split('/')[-1]
df = []
if self.show_disc_value > 1:
try:
devices = DiscStats().devices
device = None
fs = os.path.realpath(disc.filesystem)
stat = None
for dev in devices.keys():
if '/dev/' + dev == fs:
stat = devices[dev]
break
if stat is None:
self.show_disc_value = 0
except:
self.show_disc_value = 0
if self.show_disc_value == 0:
percent = disc.used * 100 / (disc.used + disc.available)
text = '\033[%sm%.0f\033[0m%%(%.1f%sB/%.f%sB)' % (self.colour(percent), percent,
*self.unit(disc.available),
*self.unit(disc.used + disc.available))
df.append(text)
percent = disc.iused * 100 / (disc.iused + disc.ifree)
text = '\033[%sm%.0f\033[0m%%(%.1f%s/%.1f%s)inodes' % (self.colour(percent), percent,
*self.si(disc.iused), *self.si(disc.inodes))
df.append(text)
elif self.show_disc_value == 1:
df.append(disc.fstype)
df.append(disc.mountpoint)
elif self.show_disc_value == 2:
name += '(r)'
df.append('%.1f%scompleted' % self.si(stat.r_complete))
df.append('%.1f%smerge' % self.si(stat.r_merge))
df.append('%.1f%ssectors' % self.si(stat.r_sectors))
df.append('%.1f%ss' % self.si(stat.r_time, -1))
elif self.show_disc_value == 3:
name += '(w)'
df.append('%.1f%scomplete' % self.si(stat.w_complete))
df.append('%.1f%smerge' % self.si(stat.w_merge))
df.append('%.1f%ssectors' % self.si(stat.w_sectors))
df.append('%.1f%ss' % self.si(stat.w_time, -1))
elif self.show_disc_value == 4:
name += '(io)'
df.append('%.1f%s' % self.si(stat.io_current))
df.append('%.1f%ss' % self.si(stat.io_time, -1))
df.append('%.1f%ss(weighted)' % self.si(stat.io_weighted_time, -1))
return '%s: %s' % (name, ' '.join(df))
def function(self):
try:
self.disc_map = None
if self.show_overall:
return self.function_overall()
else:
return self.function_disc()
except Exception as e:
return str(e)