<s> """<STR_LIT>""" <EOL> import diamond . collector <EOL> import diamond . convertor <EOL> import os <EOL> import re <EOL> try : <EOL> import psutil <EOL> except ImportError : <EOL> psutil = None <EOL> class DiskSpaceCollector ( diamond . collector . Collector ) : <EOL> def get_default_config_help ( self ) : <EOL> config_help = super ( DiskSpaceCollector , self ) . get_default_config_help ( ) <EOL> config_help . update ( { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> + "<STR_LIT>" <EOL> + "<STR_LIT>" , <EOL> } ) <EOL> return config_help <EOL> def get_default_config ( self ) : <EOL> """<STR_LIT>""" <EOL> config = super ( DiskSpaceCollector , self ) . get_default_config ( ) <EOL> config . update ( { <EOL> '<STR_LIT:path>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> + '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } ) <EOL> return config <EOL> def process_config ( self ) : <EOL> super ( DiskSpaceCollector , self ) . process_config ( ) <EOL> self . exclude_filters = self . config [ '<STR_LIT>' ] <EOL> if isinstance ( self . exclude_filters , basestring ) : <EOL> self . exclude_filters = [ self . exclude_filters ] <EOL> self . exclude_reg = re . compile ( '<STR_LIT:|>' . join ( self . exclude_filters ) ) <EOL> self . filesystems = [ ] <EOL> if isinstance ( self . config [ '<STR_LIT>' ] , basestring ) : <EOL> for filesystem in self . config [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) : <EOL> self . filesystems . append ( filesystem . strip ( ) ) <EOL> elif isinstance ( self . config [ '<STR_LIT>' ] , list ) : <EOL> self . filesystems = self . config [ '<STR_LIT>' ] <EOL> def get_disk_labels ( self ) : <EOL> """<STR_LIT>""" <EOL> path = '<STR_LIT>' <EOL> labels = { } <EOL> if not os . path . isdir ( path ) : <EOL> return labels <EOL> for label in os . listdir ( path ) : <EOL> label = label . replace ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> device = os . path . realpath ( path + '<STR_LIT:/>' + label ) <EOL> labels [ device ] = label <EOL> return labels <EOL> def get_file_systems ( self ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> if os . access ( '<STR_LIT>' , os . R_OK ) : <EOL> file = open ( '<STR_LIT>' ) <EOL> for line in file : <EOL> try : <EOL> mount = line . split ( ) <EOL> device = mount [ <NUM_LIT:0> ] <EOL> mount_point = mount [ <NUM_LIT:1> ] <EOL> fs_type = mount [ <NUM_LIT:2> ] <EOL> except ( IndexError , ValueError ) : <EOL> continue <EOL> if fs_type not in self . filesystems : <EOL> self . log . debug ( "<STR_LIT>" <EOL> + "<STR_LIT>" , <EOL> mount_point , fs_type ) <EOL> continue <EOL> if self . exclude_reg . search ( mount_point ) : <EOL> self . log . debug ( "<STR_LIT>" <EOL> + "<STR_LIT>" , mount_point ) <EOL> continue <EOL> if ( mount_point . startswith ( '<STR_LIT>' ) <EOL> or mount_point . startswith ( '<STR_LIT>' ) <EOL> or mount_point . startswith ( '<STR_LIT>' ) ) : <EOL> continue <EOL> if '<STR_LIT:/>' in device and mount_point . startswith ( '<STR_LIT:/>' ) : <EOL> try : <EOL> stat = os . stat ( mount_point ) <EOL> major = os . major ( stat . st_dev ) <EOL> minor = os . minor ( stat . st_dev ) <EOL> except OSError : <EOL> self . log . debug ( "<STR_LIT>" , <EOL> mount_point ) <EOL> continue <EOL> if ( major , minor ) in result : <EOL> continue <EOL> result [ ( major , minor ) ] = { <EOL> '<STR_LIT>' : os . path . realpath ( device ) , <EOL> '<STR_LIT>' : mount_point , <EOL> '<STR_LIT>' : fs_type <EOL> } <EOL> file . close ( ) <EOL> else : <EOL> if not psutil : <EOL> self . log . error ( '<STR_LIT>' ) <EOL> return None <EOL> partitions = psutil . disk_partitions ( False ) <EOL> for partition in partitions : <EOL> result [ ( <NUM_LIT:0> , len ( result ) ) ] = { <EOL> '<STR_LIT>' : os . path . realpath ( partition . device ) , <EOL> '<STR_LIT>' : partition . mountpoint , <EOL> '<STR_LIT>' : partition . fstype <EOL> } <EOL> pass <EOL> return result <EOL> def collect ( self ) : <EOL> labels = self . get_disk_labels ( ) <EOL> results = self . get_file_systems ( ) <EOL> if not results : <EOL> self . log . error ( '<STR_LIT>' ) <EOL> return None <EOL> for key , info in results . iteritems ( ) : <EOL> if info [ '<STR_LIT>' ] in labels : <EOL> name = labels [ info [ '<STR_LIT>' ] ] <EOL> else : <EOL> name = info [ '<STR_LIT>' ] . replace ( '<STR_LIT:/>' , '<STR_LIT:_>' ) <EOL> name = name . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) <EOL> if name == '<STR_LIT:_>' : <EOL> name = '<STR_LIT:root>' <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> try : <EOL> data = os . statvfs ( info [ '<STR_LIT>' ] ) <EOL> except OSError , e : <EOL> self . log . exception ( e ) <EOL> continue <EOL> block_size = data . f_bsize <EOL> blocks_total = data . f_blocks <EOL> blocks_free = data . f_bfree <EOL> blocks_avail = data . f_bavail <EOL> inodes_total = data . f_files <EOL> inodes_free = data . f_ffree <EOL> inodes_avail = data . f_favail <EOL> elif os . name == '<STR_LIT>' : <EOL> raw_data = psutil . disk_usage ( info [ '<STR_LIT>' ] ) <EOL> block_size = <NUM_LIT:1> <EOL> blocks_total = raw_data . total <EOL> blocks_free = raw_data . free <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> for unit in self . config [ '<STR_LIT>' ] : <EOL> metric_name = '<STR_LIT>' % ( name , unit ) <EOL> metric_value = float ( blocks_free ) / float ( <EOL> blocks_free + ( blocks_total - blocks_free ) ) * <NUM_LIT:100> <EOL> self . publish_gauge ( metric_name , metric_value , <NUM_LIT:2> ) <EOL> metric_name = '<STR_LIT>' % ( name , unit ) <EOL> metric_value = float ( block_size ) * float ( <EOL> blocks_total - blocks_free ) <EOL> metric_value = diamond . convertor . binary . convert ( <EOL> value = metric_value , oldUnit = '<STR_LIT>' , newUnit = unit ) <EOL> self . publish_gauge ( metric_name , metric_value , <NUM_LIT:2> ) <EOL> metric_name = '<STR_LIT>' % ( name , unit ) <EOL> metric_value = float ( block_size ) * float ( blocks_free ) <EOL> metric_value = diamond . convertor . binary . convert ( <EOL> value = metric_value , oldUnit = '<STR_LIT>' , newUnit = unit ) <EOL> self . publish_gauge ( metric_name , metric_value , <NUM_LIT:2> ) <EOL> if os . name != '<STR_LIT>' : <EOL> metric_name = '<STR_LIT>' % ( name , unit ) <EOL> metric_value = float ( block_size ) * float ( blocks_avail ) <EOL> metric_value = diamond . convertor . binary . convert ( <EOL> value = metric_value , oldUnit = '<STR_LIT>' , newUnit = unit ) <EOL> self . publish_gauge ( metric_name , metric_value , <NUM_LIT:2> ) <EOL> if os . name != '<STR_LIT>' : <EOL> if float ( inodes_total ) > <NUM_LIT:0> : <EOL> self . publish_gauge ( <EOL> '<STR_LIT>' % name , <EOL> float ( inodes_free ) / float ( inodes_total ) * <NUM_LIT:100> ) <EOL> self . publish_gauge ( '<STR_LIT>' % name , <EOL> inodes_total - inodes_free ) <EOL> self . publish_gauge ( '<STR_LIT>' % name , inodes_free ) <EOL> self . publish_gauge ( '<STR_LIT>' % name , inodes_avail ) </s>
