OSDN Git Service

Add validate
[karesansui/karesansui.git] / bin / get_memory_usage.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 os
15 import sys
16 import time
17 import logging
18 from optparse import OptionParser
19
20 from ksscommand import KssCommand, KssCommandException
21
22 import __cmd__
23
24 try:
25     import karesansui
26     from karesansui import __version__
27     from karesansui.lib.virt.virt import KaresansuiVirtConnection
28     from karesansui.lib.utils import gettimeofday
29     from karesansui.lib.utils import load_locale
30 except ImportError:
31     print >>sys.stderr, "[Error] karesansui package was not found."
32     sys.exit(1)
33
34 _ = load_locale()
35
36 class GetMemoryUsage(KssCommand):
37
38     def process(self):
39         conn = KaresansuiVirtConnection()
40         try:
41             guests = conn.search_guests()
42     
43             nodeinfo = conn.get_nodeinfo()
44             
45             infos = []
46             for guest in guests:
47                 id = guest.ID()
48                 if id > -1:
49                     name = guest.name()
50                     print name
51                     info = guest.info()
52                     maxMem = info[1]
53                     memory = info[2]
54                     
55                     pcentCurrMem = memory * 100.0 / (nodeinfo["memory"]*1024)
56                     pcentMaxMem  = maxMem * 100.0 / (nodeinfo["memory"]*1024)
57                     
58                     print "%.3f%%" % pcentCurrMem
59                     #print pcentMaxMem
60                     
61             return True
62         
63         finally:
64             conn.close()
65
66 if __name__ == "__main__":
67     target = GetMemoryUsage()
68     sys.exit(target.run())