Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import time
- import sys
- def collect( ):
- result = { }
- fd = open( "/proc/stat" )
- while True:
- data = fd.readline( )
- if data == '': break
- if not data.startswith( "cpu" ): continue
- key, delimiter, data = data.partition( " " )
- result[ key ] = [ float( x ) for x in data.rstrip( ).split( ) ]
- fd.close( )
- return result
- def process( first, second ):
- result = { }
- for key in first:
- a = first[ key ]
- b = second[ key ]
- v1 = b[ 0 ] - a[ 0 ] + b[ 2 ] - a[ 2 ]
- v2 = v1 + b[ 3 ] - a[ 3 ]
- result[ key ] = 0
- if v2 > 0:
- result[ key ] = ( v1 * 100.0 ) / v2
- return result
- first = collect( )
- while True:
- time.sleep( 0.1 )
- second = collect( )
- result = process( first, second )
- for key in sorted( result.keys( ) ):
- print result[ key ],
- print
- sys.stdout.flush( )
- first = second
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement