<s> import os <EOL> import platform <EOL> import socket <EOL> import time <EOL> import json <EOL> import psutil <EOL> from pyformance import global_registry <EOL> class Collector ( object ) : <EOL> def __init__ ( self , registry = None ) : <EOL> if registry is None : <EOL> registry = global_registry ( ) <EOL> self . registry = registry <EOL> def collect_disk_io ( self , whitelist = [ ] ) : <EOL> stats = psutil . disk_io_counters ( perdisk = True ) <EOL> for entry , stat in stats . iteritems ( ) : <EOL> if not whitelist or entry in whitelist : <EOL> for k , v in stat . _asdict ( ) . iteritems ( ) : <EOL> self . registry . gauge ( "<STR_LIT>" % ( entry , k ) ) . set_value ( v ) <EOL> def collect_network_io ( self , whitelist = [ ] ) : <EOL> stats = psutil . network_io_counters ( pernic = True ) <EOL> for entry , stat in stats . iteritems ( ) : <EOL> if not whitelist or entry in whitelist : <EOL> for k , v in stat . _asdict ( ) . iteritems ( ) : <EOL> self . registry . gauge ( "<STR_LIT>" % ( entry . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:_>" ) , k ) ) . set_value ( v ) <EOL> def collect_cpu_times ( self , whitelist = [ ] ) : <EOL> stats = psutil . cpu_times ( percpu = True ) <EOL> for entry , stat in enumerate ( stats ) : <EOL> if not whitelist or entry in whitelist : <EOL> for k , v in stat . _asdict ( ) . iteritems ( ) : <EOL> self . registry . gauge ( "<STR_LIT>" % ( entry , k ) ) . set_value ( v ) <EOL> def collect_phymem_usage ( self ) : <EOL> stats = psutil . phymem_usage ( ) <EOL> for k , v in stats . _asdict ( ) . iteritems ( ) : <EOL> self . registry . gauge ( "<STR_LIT>" % k ) . set_value ( v ) <EOL> def collect_swap_usage ( self ) : <EOL> stats = psutil . swap_memory ( ) <EOL> for k , v in stats . _asdict ( ) . iteritems ( ) : <EOL> self . registry . gauge ( "<STR_LIT>" % k ) . set_value ( v ) <EOL> def collect_virtmem_usage ( self ) : <EOL> stats = psutil . virtmem_usage ( ) <EOL> for k , v in stats . _asdict ( ) . iteritems ( ) : <EOL> self . registry . gauge ( "<STR_LIT>" % k ) . set_value ( v ) <EOL> def collect_uptime ( self ) : <EOL> uptime = int ( time . time ( ) ) - int ( psutil . BOOT_TIME ) <EOL> self . registry . gauge ( "<STR_LIT>" ) . set_value ( uptime ) <EOL> def collect_disk_usage ( self , whitelist = [ ] ) : <EOL> for partition in psutil . disk_partitions ( ) : <EOL> if not whitelist or partition . mountpoint in whitelist or partition . device in whitelist : <EOL> usage = psutil . disk_usage ( partition . mountpoint ) <EOL> if platform . system ( ) == "<STR_LIT>" : <EOL> disk_name = "<STR_LIT:->" + partition . mountpoint . replace ( "<STR_LIT:\\>" , "<STR_LIT>" ) . replace ( "<STR_LIT::>" , "<STR_LIT>" ) <EOL> else : <EOL> disk_name = partition . mountpoint . replace ( "<STR_LIT:/>" , "<STR_LIT:->" ) <EOL> if disk_name == "<STR_LIT:->" : <EOL> disk_name = "<STR_LIT>" <EOL> self . registry . gauge ( "<STR_LIT>" % disk_name ) . set_value ( usage . total ) <EOL> self . registry . gauge ( "<STR_LIT>" % disk_name ) . set_value ( usage . used ) <EOL> self . registry . gauge ( "<STR_LIT>" % disk_name ) . set_value ( usage . free ) <EOL> def collect_loadavgs ( self ) : <EOL> loadavgs = os . getloadavg ( ) <EOL> self . registry . gauge ( '<STR_LIT>' ) . set_value ( loadavgs [ <NUM_LIT:0> ] ) <EOL> self . registry . gauge ( '<STR_LIT>' ) . set_value ( loadavgs [ <NUM_LIT:1> ] ) <EOL> self . registry . gauge ( '<STR_LIT>' ) . set_value ( loadavgs [ <NUM_LIT:2> ] ) <EOL> def collect ( self ) : <EOL> self . collect_disk_io ( ) <EOL> self . collect_cpu_times ( ) <EOL> self . collect_uptime ( ) <EOL> self . collect_network_io ( ) <EOL> self . collect_phymem_usage ( ) <EOL> self . collect_virtmem_usage ( ) <EOL> self . collect_swap_usage ( ) <EOL> self . collect_disk_usage ( ) <EOL> self . collect_loadavgs ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> from pyformance . reporters import ConsoleReporter <EOL> reporter = ConsoleReporter ( ) <EOL> col = Collector ( ) <EOL> col . collect ( ) <EOL> reporter . report_now ( ) </s>
