00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 """ python vmstat for linux, requires linuxproc.py module """
00087 __ext_version__ = '1.0'
00088 __version__ = '$Revision: 1.2 $'
00089 import linuxproc
00090 import sys, time
00091 HEIGHT = 24
00092 DIGITS = {}
00093 for d in "1234567890":
00094 DIGITS[d] = 1
00095
00096 def showheader(timestamp):
00097 head="%8s%28s%8s%12s%11s%12s" % ('procs','memory','swap','io','system',
00098 'cpu')
00099 thead = head +"%12s" % 'time'
00100 print timestamp and thead or head
00101 s = "%2s %2s %2s %6s %6s %6s %6s %3s %3s %5s %5s %4s %5s %3s %3s %3s"
00102 items = (
00103 'r','b','w','swpd','free','buff','cache','si','so','bi','bo','in',
00104 'cs','us','sy','id'
00105 )
00106 print s % items
00107
00108 def meminfo(mem=None):
00109 if not mem: mem = linuxproc.meminfo()
00110 swpd = mem['swaptotal'] - mem['swapfree']
00111 return swpd, mem['memfree'], mem['buffers'], mem['cached']
00112
00113 def getcpu(stat=None):
00114 if not stat: stat = linuxproc.stat()
00115 cpu = stat['cpu']
00116 return cpu['user'], cpu['nice'], cpu['system'], cpu['idle']
00117
00118 def getswap(stat=None):
00119 if not stat: stat = linuxproc.stat()
00120 swap = stat['swap']
00121 return swap['in'], swap['out']
00122
00123 def getpage(stat=None):
00124 if not stat: stat = linuxproc.stat()
00125 page = stat['page']
00126 return page['in'], page['out']
00127
00128 def getall():
00129 stat = linuxproc.stat()
00130 d = {}
00131 lr, lb, ls = map(lambda x: len(x), linuxproc.getrunners())
00132 mswap, mfree, mbuf, mcache = meminfo()
00133 swapin, swapout = getswap(stat)
00134 pgin, pgout = getpage(stat)
00135 intr = stat['intr']['interrupts']
00136 ctxt = stat['ctxt']['context_switches']
00137 cuse, cnice, csys, cidl = getcpu(stat)
00138 loc = locals()
00139 for name in ('lr', 'lb', 'ls', 'mswap', 'mfree', 'mbuf', 'mcache',
00140 'swapin', 'swapout', 'pgin', 'pgout', 'intr', 'ctxt', 'cuse',
00141 'cnice', 'csys', 'cidl'):
00142 d[name] = loc[name]
00143 return d
00144
00145 def main(headers, count, delay, timestamp):
00146 i = 0
00147 HZ = 100L
00148 KB_PER_PAGE = 1
00149 showheader(timestamp)
00150 l = [None, None]
00151 stat = l[0] = getall()
00152 duse = stat['cuse'] + stat['cnice']
00153 dsys = stat['csys']
00154 didl = stat['cidl']
00155 Div = duse + dsys + didl
00156 divo2 = Div/2
00157 fswapin = (stat['swapin'] * KB_PER_PAGE * HZ + divo2)/Div
00158 fswapout = (stat['swapout'] * KB_PER_PAGE * HZ + divo2)/Div
00159 fpgin = (stat['pgin'] * HZ + divo2)/Div
00160 fpgout = (stat['pgout'] * HZ + divo2)/Div
00161 fintr = (stat['intr'] * HZ + divo2)/Div
00162 fctxt = (stat['ctxt'] * HZ + divo2)/Div
00163 fcusr = (100L * duse + divo2)/Div
00164 fcsys = (100L * dsys + divo2)/Div
00165 fcidle = (100L * didl + divo2)/Div
00166 flr = stat['lr'] - 1
00167 flb = stat['lb']
00168 fls = stat['ls']
00169 fmswap = stat['mswap']
00170 fmfree = stat['mfree']
00171 fmbuf = stat['mbuf']
00172 fmcache = stat['mcache']
00173 output(
00174 flr, flb, fls, fmswap, fmfree, fmbuf, fmcache, fswapin, fswapout,
00175 fpgin, fpgout, fintr, fctxt, fcusr, fcsys, fcidle,
00176 timestamp and time.asctime() or ''
00177 )
00178
00179
00180 tog = 0
00181 i = 0
00182 while 1:
00183 i = i + 1
00184 if not delay: break
00185 if count and i == count: break
00186 tog = not tog
00187 time.sleep(delay)
00188 if i % HEIGHT == 0 and headers:
00189 showheader(timestamp)
00190 stat = l[tog] = getall()
00191 old = l[not tog]
00192 duse = (stat['cuse']-old['cuse']) + (stat['cnice']-old['cnice'])
00193 dsys = (stat['csys']-old['csys'])
00194 didl = (stat['cidl']-old['cidl'])
00195
00196 if didl < 0:
00197 didl = 0
00198 Div = duse + dsys + didl
00199 divo2 = Div/2
00200 pero2 = delay/2
00201 flr = stat['lr'] - 1
00202 flb = stat['lb']
00203 fls = stat['ls']
00204 fmswap = stat['mswap']
00205 fmfree = stat['mfree']
00206 fmbuf = stat['mbuf']
00207 fmcache = stat['mcache']
00208 fswapin = ((stat['swapin']-old['swapin']) *KB_PER_PAGE+pero2)/delay
00209 fswapout = ((stat['swapout']-old['swapout']) *KB_PER_PAGE+pero2)/delay
00210 fpgin = ((stat['pgin']-old['pgin']) +pero2)/delay
00211 fpgout = ((stat['pgout']-old['pgout']) +pero2)/delay
00212 fintr = ((stat['intr']-old['intr']) +pero2)/delay
00213 fctxt = ((stat['ctxt']-old['ctxt']) +pero2)/delay
00214 fcusr = (100L * duse + divo2)/Div
00215 fcsys = (100L * dsys + divo2)/Div
00216 fcidle = (100L * didl + divo2)/Div
00217 output(
00218 flr, flb, fls, fmswap, fmfree, fmbuf, fmcache, fswapin, fswapout,
00219 fpgin, fpgout, fintr, fctxt, fcusr, fcsys, fcidle,
00220 timestamp and time.asctime() or ''
00221 )
00222 def output(*args):
00223 tfmt="%2u %2u %2u %6u %6u %6u %6u %3u %3u %5u %5u %4u %5u %3u %3u %3u %s"
00224 fmt = tfmt[:-3]
00225 if len(args) == 17:
00226 print tfmt % args
00227 elif len(args) == 16:
00228 print fmt % args
00229
00230 def write_tstamp():
00231 print time.asctime()
00232
00233 def usage():
00234 print "usage: %s [-V] [-n] [-t] [delay [count]]" % sys.argv[0]
00235 print " -V prints version."
00236 print " -n causes the headers not to be reprinted regularly."
00237 print " -t causes a timestamp to be printed."
00238 print " delay is the delay between updates in seconds."
00239 print " count is the number of updates."
00240
00241 if __name__ == '__main__':
00242 delay_seen = None
00243 headers = 1
00244 timestamps = 0
00245 count = 0
00246 delay = 0
00247 for arg in sys.argv[1:]:
00248 if arg[0] != '-':
00249 for char in arg:
00250 if not DIGITS.has_key(char):
00251 usage()
00252 sys.exit(1)
00253 if delay_seen:
00254 count = int(arg)
00255 else:
00256 delay = int(arg)
00257 delay_seen = 1
00258 elif arg[1:] and arg[1] == 'V':
00259 print sys.argv[0], 'version %s' % __version__
00260 sys.exit(0)
00261 elif arg[1:] and arg[1] == 'n':
00262 headers = 0
00263 elif arg[1:] and arg[1] == 't':
00264 timestamps = 1
00265 else:
00266 usage()
00267 sys.exit(1)
00268 main(headers, count, delay, timestamps)
00269