#!/usr/bin/env python # -*- coding: utf-8 -*- import curses import sys import time def drawBar(stdscr, y, x0, physLength, length, data, emptyColor): stdscr.addstr(y, x0, '[') offset = 1 length = float(length) physLength -= 2 for i in data: l = int(i[0]/(length/physLength)) stdscr.addstr(y, x0+offset, '#'*l, curses.color_pair(i[1])) offset += l stdscr.addstr(y, x0+offset, ' '*(physLength-offset), curses.color_pair(emptyColor)) stdscr.addstr(y, x0+physLength, ']') #stdscr.refresh() def getCpuTimes(cpu=''): stat = open('/proc/stat').read().split('\n') t = map(int, filter(lambda x: x.startswith('cpu%s '%cpu), stat)[0].split(' ')[-9:]) return ( t[0], # user mode t[1], # low nice user mode t[2], # system mode t[4], # waiting for I/O to complete t[5]+t[6], # servicing IRQs and SoftIRQs t[7]+t[8], # time spent in other OS running in virtualized environment # and time spent running a virtual CPU for guest operating systems under the control of the Linux kernel t[3], # idle ) def main(stdscr): curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK) curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(5, curses.COLOR_BLUE, curses.COLOR_BLACK) curses.init_pair(6, curses.COLOR_CYAN, curses.COLOR_BLACK) curses.init_pair(7, curses.COLOR_MAGENTA, curses.COLOR_BLACK) curses.init_pair(8, curses.COLOR_YELLOW, curses.COLOR_BLACK) stdscr.nodelay(1) cpus = map(lambda x: int(x.split(': ')[1]), filter(lambda x: x.startswith('processor\t'), open('/proc/cpuinfo').read().split('\n'))) oldcput = {} for i in cpus: oldcput[i] = getCpuTimes(i) while 1: meminfo = open('/proc/meminfo').read().split('\n') for i in meminfo: if i.startswith('MemTotal'): total = float(i.split(' ')[-2]) if i.startswith('MemFree'): free = float(i.split(' ')[-2]) if i.startswith('Cached'): cached = float(i.split(' ')[-2]) if i.startswith('Buffers'): bufs = float(i.split(' ')[-2]) if i.startswith('SwapTotal'): stotal = float(i.split(' ')[-2]) if i.startswith('SwapFree'): sfree = float(i.split(' ')[-2]) width = stdscr.getmaxyx()[1] stdscr.addstr(1, 0, 'Mem: ', curses.color_pair(1)) drawBar(stdscr, 1, 5, width-5, total, ( ((total-free-cached-bufs), 3), (cached, 4), (bufs, 5)), 2) stdscr.addstr(2, 0, ' Used: %.3f MiB'%((total-free-cached-bufs)/1024), curses.color_pair(3)) stdscr.addstr(3, 0, ' Cached: %.3f MiB'%(cached/1024), curses.color_pair(4)) stdscr.addstr(4, 0, 'Buffers: %.3f MiB'%(bufs/1024), curses.color_pair(5)) stdscr.addstr(5, 0, ' Total: %.3f MiB'%(total/1024), curses.color_pair(1)) stdscr.addstr(7, 0, 'Swap: ', curses.color_pair(1)) drawBar(stdscr, 7, 6, width-6, stotal, ( ((stotal-sfree), 3),), 2) stdscr.addstr(8, 0, ' Used: %.3f MiB'%((stotal-sfree)/1024), curses.color_pair(3)) stdscr.addstr(9, 0, 'Total: %.3f MiB'%(stotal/1024), curses.color_pair(1)) y = 11 for i in cpus: newcput = getCpuTimes(i) deltacput = map(lambda x, y: x-y, newcput, oldcput[i]) oldcput[i] = newcput sdelta = sum(deltacput) or 1 stdscr.addstr(y, 0, 'CPU%.3d: '%(i), curses.color_pair(1)) drawBar(stdscr, y, 8, width-8, sdelta, map(lambda x: (x[1], (x[0]+3)), enumerate(deltacput[:-1])), 2) y += 1 sdelta = float(sdelta) for j in enumerate(( 'User mode', 'Low nice user mode', 'System mode', 'Waiting for I/O', 'IRQs and SoftIRQs', 'Virtualization', 'Idle')): stdscr.addstr(y, 0, '%18s: %3d%%'%(j[1], 100*deltacput[j[0]]/sdelta), curses.color_pair((j[0]+3)%9)) y += 1 y += 1 stdscr.refresh() time.sleep(.5) c = stdscr.getch() if c==ord('q'): break curses.wrapper(main)