Replace FsGrab by PsUtil

This commit is contained in:
nicolargo 2012-02-07 11:50:05 +01:00
parent 89b5ebc8b4
commit e1e8a99276

View File

@ -218,7 +218,7 @@ class glancesLogs():
class glancesGrabFs(): class glancesGrabFs():
""" """
Get FS stats: idem as structure http://www.i-scream.org/libstatgrab/docs/sg_get_fs_stats.3.html Get FS stats
""" """
def __init__(self): def __init__(self):
@ -229,45 +229,28 @@ class glancesGrabFs():
""" """
Update the stats Update the stats
""" """
# Ignore the following fs
ignore_fsname = ('', 'none', 'gvfs-fuse-daemon', 'fusectl', 'cgroup')
ignore_fstype = ('binfmt_misc', 'devpts', 'iso9660', 'none', 'proc', 'sysfs', 'usbfs')
# Reset the list # Reset the list
self.fs_list = [] self.fs_list = []
# Ignore the following fs
ignore_fsname = ('none', 'gvfs-fuse-daemon', 'fusectl', 'cgroup')
ignore_fstype = ('binfmt_misc', 'devpts', 'iso9660', 'none', 'proc', 'sysfs', 'usbfs')
# Open the current mounted FS # Open the current mounted FS
mtab = open("/etc/mtab", "r") fs_stat = psutil.disk_partitions(True)
for line in mtab.readlines(): for fs in range(len(fs_stat)):
if line.split()[0] in ignore_fsname: continue
if line.split()[2] in ignore_fstype: continue
# Get FS stats
fs_current = {} fs_current = {}
fs_name = self.__getmount__(line.split()[1]) fs_current['device_name'] = fs_stat[fs].device
fs_stats = os.statvfs(fs_name) if fs_current['device_name'] in ignore_fsname: continue
# Build the list fs_current['fs_type'] = fs_stat[fs].fstype
fs_current['device_name'] = str(line.split()[0]) if fs_current['fs_type'] in ignore_fstype: continue
fs_current['fs_type'] = str(line.split()[2]) fs_current['mnt_point'] = fs_stat[fs].mountpoint
fs_current['mnt_point'] = str(fs_name) fs_usage = psutil.disk_usage(fs_current['mnt_point'])
fs_current['size'] = float(fs_stats.f_blocks) * long(fs_stats.f_frsize) fs_current['size'] = fs_usage.total
fs_current['used'] = float(fs_stats.f_blocks - fs_stats.f_bfree) * long(fs_stats.f_frsize) fs_current['used'] = fs_usage.used
fs_current['avail'] = float(fs_stats.f_bfree) * long(fs_stats.f_frsize) fs_current['avail'] = fs_usage.free
self.fs_list.append(fs_current) self.fs_list.append(fs_current)
mtab.close()
def __getmount__(self, path):
"""
Return the real root path of a file
Exemple: /home/nicolargo can return /home or /
"""
path = os.path.realpath(os.path.abspath(path))
while path != os.path.sep:
if os.path.ismount(path):
return path
path = os.path.abspath(os.path.join(path, os.pardir))
return path
def get(self): def get(self):
@ -366,13 +349,23 @@ class glancesStats():
# NET # NET
try: try:
self.networkinterface = statgrab.sg_get_network_iface_stats() self.network_old
except: except:
self.networkinterface = {} self.network_old = psutil.network_io_counters(True)
try: self.network = []
self.network = statgrab.sg_get_network_io_stats_diff() else:
except: try:
self.network = {} self.network_new = psutil.network_io_counters(True)
self.network = []
for net in self.network_new:
netstat = {}
netstat['interface_name'] = net
netstat['rx'] = self.network_new[net].bytes_recv - self.network_old[net].bytes_recv
netstat['tx'] = self.network_new[net].bytes_sent - self.network_old[net].bytes_sent
self.network.append(netstat)
self.network_old = self.network_new
except:
self.network = []
# DISK IO # DISK IO
try: try:
@ -454,11 +447,7 @@ class glancesStats():
def getMemSwap(self): def getMemSwap(self):
return self.memswap return self.memswap
def getNetworkInterface(self):
return self.networkinterface
def getNetwork(self): def getNetwork(self):
return self.network return self.network
@ -626,14 +615,23 @@ class glancesScreen():
560745673 -> 561M 560745673 -> 561M
... ...
""" """
if val >= 1073741824L: symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
return "%.1fG" % (val / 1073741824L) prefix = {
elif val >= 1048576L: 'Y': 1208925819614629174706176L,
return "%.1fM" % (val / 1048576L) 'Z': 1180591620717411303424L,
elif val >= 1024: 'E': 1152921504606846976L,
return "%.1fK" % (val / 1024) 'P': 1125899906842624L,
else: 'T': 1099511627776L,
return str(int(val)) 'G': 1073741824,
'M': 1048576,
'K': 1024
}
for key in reversed(symbols):
if val >= prefix[key]:
value = float(val) / prefix[key]
return '%.1f%s' % (value, key)
return "%s" % val
def __getAlert(self, current = 0, max = 100): def __getAlert(self, current = 0, max = 100):
# If current < CAREFUL of max then alert = OK # If current < CAREFUL of max then alert = OK
@ -760,7 +758,7 @@ class glancesScreen():
self.displayCpu(stats.getCpu()) self.displayCpu(stats.getCpu())
self.displayLoad(stats.getLoad(), stats.getCore()) self.displayLoad(stats.getLoad(), stats.getCore())
self.displayMem(stats.getMem(), stats.getMemSwap()) self.displayMem(stats.getMem(), stats.getMemSwap())
network_count = self.displayNetwork(stats.getNetwork(), stats.getNetworkInterface()) network_count = self.displayNetwork(stats.getNetwork())
diskio_count = self.displayDiskIO(stats.getDiskIO(), self.network_y + network_count) diskio_count = self.displayDiskIO(stats.getDiskIO(), self.network_y + network_count)
fs_count = self.displayFs(stats.getFs(), self.network_y + network_count + diskio_count) fs_count = self.displayFs(stats.getFs(), self.network_y + network_count + diskio_count)
log_count = self.displayLog(self.network_y + network_count + diskio_count + fs_count) log_count = self.displayLog(self.network_y + network_count + diskio_count + fs_count)
@ -950,27 +948,18 @@ class glancesScreen():
self.term_window.addnstr(self.mem_y+3, self.mem_x+30, str((mem['free']+mem['cache'])/1048576), 8) self.term_window.addnstr(self.mem_y+3, self.mem_x+30, str((mem['free']+mem['cache'])/1048576), 8)
def displayNetwork(self, network, networkinterface): def displayNetwork(self, network):
""" """
Display the network interface bitrate Display the network interface bitrate
Return the number of interfaces Return the number of interfaces
""" """
# Network interfaces bitrate # Network interfaces bitrate
if (not network or not networkinterface or not self.network_tag): if (not network or not self.network_tag):
return 0 return 0
screen_x = self.screen.getmaxyx()[1] screen_x = self.screen.getmaxyx()[1]
screen_y = self.screen.getmaxyx()[0] screen_y = self.screen.getmaxyx()[0]
if ((screen_y > self.network_y+3) if ((screen_y > self.network_y+3)
and (screen_x > self.network_x+28)): and (screen_x > self.network_x+28)):
# Get the speed of the network interface
# TODO: optimize...
speed = {}
for i in range(0, len(networkinterface)):
# Strange think, on Ubuntu, libstatgrab return 65525 for my ethernet card...
if networkinterface[i]['speed'] == 65535:
speed[networkinterface[i]['interface_name']] = 0
else:
speed[networkinterface[i]['interface_name']] = networkinterface[i]['speed']*1000000
# Network interfaces bitrate # Network interfaces bitrate
self.term_window.addnstr(self.network_y, self.network_x, _("Net rate"), 8, self.title_color if self.hascolors else curses.A_UNDERLINE) self.term_window.addnstr(self.network_y, self.network_x, _("Net rate"), 8, self.title_color if self.hascolors else curses.A_UNDERLINE)
self.term_window.addnstr(self.network_y, self.network_x+10, _("Rx/ps"), 8) self.term_window.addnstr(self.network_y, self.network_x+10, _("Rx/ps"), 8)
@ -978,14 +967,10 @@ class glancesScreen():
# Adapt the maximum interface to the screen # Adapt the maximum interface to the screen
ret = 2 ret = 2
for i in range(0, min(screen_y-self.network_y-3, len(network))): for i in range(0, min(screen_y-self.network_y-3, len(network))):
try: elapsed_time = max (1, self.__refresh_time)
speed[network[i]['interface_name']]
except:
break
elapsed_time = max (1, network[i]['systime'])
self.term_window.addnstr(self.network_y+1+i, self.network_x, network[i]['interface_name']+':', 8) self.term_window.addnstr(self.network_y+1+i, self.network_x, network[i]['interface_name']+':', 8)
self.term_window.addnstr(self.network_y+1+i, self.network_x+10, self.__autoUnit(network[i]['rx']/elapsed_time*8) + "b", 8, self.__getNetColor(network[i]['rx']/elapsed_time*8, speed[network[i]['interface_name']])) self.term_window.addnstr(self.network_y+1+i, self.network_x+10, self.__autoUnit(network[i]['rx']/elapsed_time*8) + "b", 8)
self.term_window.addnstr(self.network_y+1+i, self.network_x+20, self.__autoUnit(network[i]['tx']/elapsed_time*8) + "b", 8, self.__getNetColor(network[i]['tx']/elapsed_time*8, speed[network[i]['interface_name']])) self.term_window.addnstr(self.network_y+1+i, self.network_x+20, self.__autoUnit(network[i]['tx']/elapsed_time*8) + "b", 8)
ret = ret + 1 ret = ret + 1
return ret return ret
return 0 return 0