python:programmes:swap
Afficher la swap consommée par les processus
- swap.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # Ghislain LE MEUR import os, operator, sys, pwd procPath = '/proc' def bytes2human(n): symbols = (' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB') prefix = {} for i, s in enumerate(symbols): prefix[s] = 1 << (i+1)*10 for s in reversed(symbols): if n >= prefix[s]: value = float(n) / prefix[s] return '%.2f%s' % (value, s) return "%s KB" % n def lsPid(): return [int(_x) for _x in os.listdir(procPath) if _x.isdigit()] class pid: def __init__(self, pid): self.pid = int(pid) self.path = os.path.join(procPath, str(self.pid)) @property def status(self): _x = {} for _i in [_i.replace(':', '').split() for _i in open(os.path.join(self.path, 'status'), 'r').readlines()]: _x[_i[0]] = _i[1:] return _x if os.getuid() != 0: print('Vous devez être root !') sys.exit(1) _x = [] print('%6s %20s %15s (%20s) -> %9s %9s %10s' %('PID', 'NAME', 'ID', 'GECOS', 'RSS', 'SWAP', 'TOTAL')) print('-' * 102) for myPid in lsPid(): p = pid(myPid) try: _x.append((int(p.status['Pid'][0]), p.status['Name'][0], int(p.status['Uid'][0]), int(p.status['VmRSS'][0]), int(p.status['VmSwap'][0]))) except: pass _x.sort(key = operator.itemgetter(4), reverse = True) totalRss = 0 totalSwap = 0 for _x in _x: totalRss += _x[3] totalSwap += _x[4] print('%6s (%20s) %15s (%20s) -> %9s %9s %10s' % (_x[0], _x[1], pwd.getpwuid(_x[2])[0], pwd.getpwuid(_x[2])[4], bytes2human(_x[3]), bytes2human(_x[4]), bytes2human(_x[3] + _x[4]))) print('-' * 102) print('%81s %9s %10s' %(bytes2human(totalRss), bytes2human(totalSwap), bytes2human(totalRss + totalSwap)))
python/programmes/swap.txt · Dernière modification : 2013/03/16 17:50 de root