pystat.py

Go to the documentation of this file.
00001 #!/bin/python
00002 ##############################################################################
00003 # 
00004 # Zope Public License (ZPL) Version 1.0
00005 # -------------------------------------
00006 # 
00007 # Copyright (c) Digital Creations.  All rights reserved.
00008 # 
00009 # This license has been certified as Open Source(tm).
00010 # 
00011 # Redistribution and use in source and binary forms, with or without
00012 # modification, are permitted provided that the following conditions are
00013 # met:
00014 # 
00015 # 1. Redistributions in source code must retain the above copyright
00016 #    notice, this list of conditions, and the following disclaimer.
00017 # 
00018 # 2. Redistributions in binary form must reproduce the above copyright
00019 #    notice, this list of conditions, and the following disclaimer in
00020 #    the documentation and/or other materials provided with the
00021 #    distribution.
00022 # 
00023 # 3. Digital Creations requests that attribution be given to Zope
00024 #    in any manner possible. Zope includes a "Powered by Zope"
00025 #    button that is installed by default. While it is not a license
00026 #    violation to remove this button, it is requested that the
00027 #    attribution remain. A significant investment has been put
00028 #    into Zope, and this effort will continue if the Zope community
00029 #    continues to grow. This is one way to assure that growth.
00030 # 
00031 # 4. All advertising materials and documentation mentioning
00032 #    features derived from or use of this software must display
00033 #    the following acknowledgement:
00034 # 
00035 #      "This product includes software developed by Digital Creations
00036 #      for use in the Z Object Publishing Environment
00037 #      (http://www.zope.org/)."
00038 # 
00039 #    In the event that the product being advertised includes an
00040 #    intact Zope distribution (with copyright and license included)
00041 #    then this clause is waived.
00042 # 
00043 # 5. Names associated with Zope or Digital Creations must not be used to
00044 #    endorse or promote products derived from this software without
00045 #    prior written permission from Digital Creations.
00046 # 
00047 # 6. Modified redistributions of any form whatsoever must retain
00048 #    the following acknowledgment:
00049 # 
00050 #      "This product includes software developed by Digital Creations
00051 #      for use in the Z Object Publishing Environment
00052 #      (http://www.zope.org/)."
00053 # 
00054 #    Intact (re-)distributions of any official Zope release do not
00055 #    require an external acknowledgement.
00056 # 
00057 # 7. Modifications are encouraged but must be packaged separately as
00058 #    patches to official Zope releases.  Distributions that do not
00059 #    clearly separate the patches from the original work must be clearly
00060 #    labeled as unofficial distributions.  Modifications which do not
00061 #    carry the name Zope may be packaged in any form, as long as they
00062 #    conform to all of the clauses above.
00063 # 
00064 # 
00065 # Disclaimer
00066 # 
00067 #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
00068 #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00069 #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00070 #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
00071 #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00072 #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00073 #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00074 #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00075 #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00076 #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00077 #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00078 #   SUCH DAMAGE.
00079 # 
00080 # 
00081 # This software consists of contributions made by Digital Creations and
00082 # many individuals on behalf of Digital Creations.  Specific
00083 # attributions are listed in the accompanying credits file.
00084 # 
00085 ##############################################################################
00086 """ python vmstat for linux, requires linuxproc.py module """
00087 __ext_version__ = '1.0'          # External code revision
00088 __version__ = '$Revision: 1.2 $' # LICOS version
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 # minus ourselves
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     # if we're repeating, do it below
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         # idle may run backwards (kernel "feature")
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 # minus ourselves
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 

Generated on Thu Apr 27 20:52:42 2006 for LICOS L02-01-00 by doxygen 1.4.6-NO