OSDN Git Service

ignore inactive pools. (ticket#94)
[karesansui/karesansui.git] / bin / cputop.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*- 
3 #
4 # This file is part of Karesansui.
5 #
6 # Copyright (C) 2009-2010 HDE, Inc.
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13
14 import sys
15 import os
16 import curses
17 import time
18
19 import __cmd__
20
21 from karesansui.lib.virt.virt import KaresansuiVirtConnection
22
23 # initialize curses
24 stdscr = curses.initscr()
25 curses.noecho()
26 curses.cbreak()
27 stdscr.keypad(1)
28
29 #curses.start_color()
30 #curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
31
32 # timeout getch after 1s
33 stdscr.timeout(1000)
34
35 (maxy, maxx) = stdscr.getmaxyx()
36
37 vtop_start = time.time()
38 first_run = True
39
40 conn = KaresansuiVirtConnection()
41 shares = {}
42 while True:
43     guests = conn.search_guests()
44     idx = 1
45     cpusum=0
46     if first_run:
47         for guest in guests:
48             id = guest.ID()
49             if id > -1:
50                 name = guest.name()
51                 info = guest.info()
52                 shares[name] = info[4]
53         first_run = False
54     else:
55         stdscr.addstr(idx, 1, "Domain\t\tID\tVCPU\t%CPU\t%CPUSUM   ", curses.A_REVERSE)
56         idx += 1
57         for guest in guests:
58             id = guest.ID()
59             if id > -1:
60                 name = guest.name()
61                 info = guest.info()
62                 now = time.time()
63                 share = info[4] - shares[name]
64                 p = (share*100)/((now - vtop_start) * 10**9)
65                 cpusum+=p
66                 stdscr.addstr(idx, 1, "%s\t%d\t%d\t%.2f\t%.2f" % (name, id, info[3], p, cpusum))
67                 idx += 1
68         idx += 1
69         stdscr.addstr(idx, 1, "Press 'q' for quit.")
70         idx += 1
71     c = stdscr.getch()
72     if c == ord('q'):
73         curses.nocbreak()
74         stdscr.keypad(0)
75         curses.echo()
76         curses.endwin()
77         break
78     stdscr.erase()
79     stdscr.refresh()
80
81     curses.nocbreak()
82     stdscr.keypad(0)
83     curses.echo()
84 curses.endwin()
85 conn.close()
86