<s> """<STR_LIT>""" <EOL> import diamond . collector <EOL> import time <EOL> from diamond . metric import Metric <EOL> try : <EOL> import xml . etree . ElementTree as ET <EOL> except ImportError : <EOL> import cElementTree as ET <EOL> try : <EOL> from netappsdk . NaServer import * <EOL> from netappsdk . NaElement import * <EOL> netappsdk = <NUM_LIT:0> <EOL> except ImportError : <EOL> netappsdk = <NUM_LIT:1> <EOL> __author__ = '<STR_LIT>' <EOL> class netappDiskCol ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , device , ip , user , password , parent ) : <EOL> """<STR_LIT>""" <EOL> self . device = device <EOL> self . ip = ip <EOL> self . netapp_user = user <EOL> self . netapp_password = password <EOL> self . path_prefix = parent [ <NUM_LIT:0> ] <EOL> self . publish_metric = parent [ <NUM_LIT:1> ] <EOL> self . log = parent [ <NUM_LIT:2> ] <EOL> self . _netapp_login ( ) <EOL> disk_xml = self . get_netapp_elem ( <EOL> NaElement ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> storage_disk_xml = self . get_netapp_elem ( <EOL> NaElement ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . zero_disk ( disk_xml ) <EOL> self . spare_disk ( disk_xml ) <EOL> self . maintenance_center ( storage_disk_xml ) <EOL> self . consistency_point ( ) <EOL> self . agr_busy ( ) <EOL> def agr_busy ( self ) : <EOL> """<STR_LIT>""" <EOL> c1 = { } <EOL> c2 = { } <EOL> disk_results = { } <EOL> agr_results = { } <EOL> names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> netapp_api = NaElement ( '<STR_LIT>' ) <EOL> netapp_api . child_add_string ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> disk_1 = self . get_netapp_elem ( netapp_api , '<STR_LIT>' ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> disk_2 = self . get_netapp_elem ( netapp_api , '<STR_LIT>' ) <EOL> for instance_data in disk_1 : <EOL> temp = { } <EOL> for element in instance_data . findall ( "<STR_LIT>" ) : <EOL> if element . find ( '<STR_LIT:name>' ) . text in names : <EOL> temp [ element . find ( '<STR_LIT:name>' ) . text ] = element . find ( '<STR_LIT:value>' ) . text <EOL> agr_name = temp [ '<STR_LIT>' ] <EOL> agr_name = agr_name [ agr_name . find ( '<STR_LIT:/>' , <NUM_LIT:0> ) : agr_name . find ( '<STR_LIT:/>' , <NUM_LIT:1> ) ] <EOL> temp [ '<STR_LIT>' ] = agr_name . lstrip ( '<STR_LIT:/>' ) <EOL> c1 [ temp . pop ( '<STR_LIT>' ) ] = temp <EOL> for instance_data in disk_2 : <EOL> temp = { } <EOL> for element in instance_data . findall ( "<STR_LIT>" ) : <EOL> if element . find ( '<STR_LIT:name>' ) . text in names : <EOL> temp [ element . find ( '<STR_LIT:name>' ) . text ] = element . find ( '<STR_LIT:value>' ) . text <EOL> agr_name = temp [ '<STR_LIT>' ] <EOL> agr_name = agr_name [ agr_name . find ( '<STR_LIT:/>' , <NUM_LIT:0> ) : agr_name . find ( '<STR_LIT:/>' , <NUM_LIT:1> ) ] <EOL> temp [ '<STR_LIT>' ] = agr_name . lstrip ( '<STR_LIT:/>' ) <EOL> c2 [ temp . pop ( '<STR_LIT>' ) ] = temp <EOL> for item in c1 : <EOL> t_c1 = int ( c1 [ item ] [ '<STR_LIT>' ] ) <EOL> t_b1 = int ( c1 [ item ] [ '<STR_LIT>' ] ) <EOL> t_c2 = int ( c2 [ item ] [ '<STR_LIT>' ] ) <EOL> t_b2 = int ( c2 [ item ] [ '<STR_LIT>' ] ) <EOL> disk_busy = <NUM_LIT:100> * ( t_c2 - t_c1 ) / ( t_b2 - t_b1 ) <EOL> if c1 [ item ] [ '<STR_LIT>' ] in disk_results : <EOL> disk_results [ c1 [ item ] [ '<STR_LIT>' ] ] . append ( disk_busy ) <EOL> else : <EOL> disk_results [ c1 [ item ] [ '<STR_LIT>' ] ] = [ disk_busy ] <EOL> for aggregate in disk_results : <EOL> agr_results [ aggregate ] = sum ( disk_results [ aggregate ] ) / len ( disk_results [ aggregate ] ) <EOL> for aggregate in agr_results : <EOL> self . push ( '<STR_LIT>' , '<STR_LIT>' + aggregate , <EOL> agr_results [ aggregate ] ) <EOL> def consistency_point ( self ) : <EOL> """<STR_LIT>""" <EOL> cp_delta = { } <EOL> xml_path = '<STR_LIT>' <EOL> netapp_api = NaElement ( '<STR_LIT>' ) <EOL> netapp_api . child_add_string ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> instance = NaElement ( '<STR_LIT>' ) <EOL> instance . child_add_string ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> counter = NaElement ( '<STR_LIT>' ) <EOL> counter . child_add_string ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> netapp_api . child_add ( counter ) <EOL> netapp_api . child_add ( instance ) <EOL> cp_1 = self . get_netapp_elem ( netapp_api , xml_path ) <EOL> time . sleep ( <NUM_LIT:3> ) <EOL> cp_2 = self . get_netapp_elem ( netapp_api , xml_path ) <EOL> for element in cp_1 : <EOL> if element . find ( '<STR_LIT:name>' ) . text == '<STR_LIT>' : <EOL> cp_1 = element . find ( '<STR_LIT:value>' ) . text . rsplit ( '<STR_LIT:U+002C>' ) <EOL> break <EOL> for element in cp_2 : <EOL> if element . find ( '<STR_LIT:name>' ) . text == '<STR_LIT>' : <EOL> cp_2 = element . find ( '<STR_LIT:value>' ) . text . rsplit ( '<STR_LIT:U+002C>' ) <EOL> break <EOL> if not type ( cp_2 ) is list or not type ( cp_1 ) is list : <EOL> log . error ( "<STR_LIT>" <EOL> % self . device ) <EOL> return <EOL> cp_1 = { <EOL> '<STR_LIT>' : cp_1 [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : cp_1 [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : cp_1 [ <NUM_LIT:2> ] , <EOL> '<STR_LIT>' : cp_1 [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : cp_1 [ <NUM_LIT:4> ] , <EOL> '<STR_LIT>' : cp_1 [ <NUM_LIT:5> ] , <EOL> '<STR_LIT>' : cp_1 [ <NUM_LIT:6> ] , <EOL> '<STR_LIT>' : cp_1 [ <NUM_LIT:7> ] , <EOL> '<STR_LIT>' : cp_1 [ <NUM_LIT:8> ] , <EOL> '<STR_LIT>' : cp_1 [ <NUM_LIT:9> ] , <EOL> '<STR_LIT>' : cp_1 [ <NUM_LIT:10> ] , <EOL> '<STR_LIT>' : cp_1 [ <NUM_LIT:11> ] <EOL> } <EOL> cp_2 = { <EOL> '<STR_LIT>' : cp_2 [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : cp_2 [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : cp_2 [ <NUM_LIT:2> ] , <EOL> '<STR_LIT>' : cp_2 [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : cp_2 [ <NUM_LIT:4> ] , <EOL> '<STR_LIT>' : cp_2 [ <NUM_LIT:5> ] , <EOL> '<STR_LIT>' : cp_2 [ <NUM_LIT:6> ] , <EOL> '<STR_LIT>' : cp_2 [ <NUM_LIT:7> ] , <EOL> '<STR_LIT>' : cp_2 [ <NUM_LIT:8> ] , <EOL> '<STR_LIT>' : cp_2 [ <NUM_LIT:9> ] , <EOL> '<STR_LIT>' : cp_2 [ <NUM_LIT:10> ] , <EOL> '<STR_LIT>' : cp_2 [ <NUM_LIT:11> ] <EOL> } <EOL> for item in cp_1 : <EOL> c1 = int ( cp_1 [ item ] ) <EOL> c2 = int ( cp_2 [ item ] ) <EOL> cp_delta [ item ] = c2 - c1 <EOL> for item in cp_delta : <EOL> self . push ( item + '<STR_LIT>' , '<STR_LIT>' , cp_delta [ item ] ) <EOL> def maintenance_center ( self , storage_disk_xml = None ) : <EOL> """<STR_LIT>""" <EOL> disk_in_maintenance = <NUM_LIT:0> <EOL> for filer_disk in storage_disk_xml : <EOL> disk_status = filer_disk . find ( '<STR_LIT>' ) <EOL> if disk_status . text == '<STR_LIT>' : <EOL> disk_in_maintenance += <NUM_LIT:1> <EOL> self . push ( '<STR_LIT>' , '<STR_LIT>' , disk_in_maintenance ) <EOL> def zero_disk ( self , disk_xml = None ) : <EOL> """<STR_LIT>""" <EOL> troubled_disks = <NUM_LIT:0> <EOL> for filer_disk in disk_xml : <EOL> raid_state = filer_disk . find ( '<STR_LIT>' ) . text <EOL> if not raid_state == '<STR_LIT>' : <EOL> continue <EOL> is_zeroed = filer_disk . find ( '<STR_LIT>' ) . text <EOL> if is_zeroed == '<STR_LIT:false>' : <EOL> troubled_disks += <NUM_LIT:1> <EOL> self . push ( '<STR_LIT>' , '<STR_LIT>' , troubled_disks ) <EOL> def spare_disk ( self , disk_xml = None ) : <EOL> """<STR_LIT>""" <EOL> spare_disk = { } <EOL> disk_types = set ( ) <EOL> for filer_disk in disk_xml : <EOL> disk_types . add ( filer_disk . find ( '<STR_LIT>' ) . text ) <EOL> if not filer_disk . find ( '<STR_LIT>' ) . text == '<STR_LIT>' : <EOL> continue <EOL> disk_type = filer_disk . find ( '<STR_LIT>' ) . text <EOL> if disk_type in spare_disk : <EOL> spare_disk [ disk_type ] += <NUM_LIT:1> <EOL> else : <EOL> spare_disk [ disk_type ] = <NUM_LIT:1> <EOL> for disk_type in disk_types : <EOL> if disk_type in spare_disk : <EOL> self . push ( '<STR_LIT>' + disk_type , '<STR_LIT>' , spare_disk [ disk_type ] ) <EOL> else : <EOL> self . push ( '<STR_LIT>' + disk_type , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> def get_netapp_elem ( self , netapp_api = None , sub_element = None ) : <EOL> """<STR_LIT>""" <EOL> netapp_data = self . server . invoke_elem ( netapp_api ) <EOL> if netapp_data . results_status ( ) == '<STR_LIT>' : <EOL> self . log . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % self . device ) <EOL> print netapp_data . sprintf ( ) <EOL> return <EOL> netapp_xml = ET . fromstring ( netapp_data . sprintf ( ) ) . find ( sub_element ) <EOL> return netapp_xml <EOL> def _netapp_login ( self ) : <EOL> """<STR_LIT>""" <EOL> self . server = NaServer ( self . ip , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> self . server . set_transport_type ( '<STR_LIT>' ) <EOL> self . server . set_style ( '<STR_LIT>' ) <EOL> self . server . set_admin_user ( self . netapp_user , self . netapp_password ) <EOL> def push ( self , metric_name = None , type = None , metric_value = None ) : <EOL> """<STR_LIT>""" <EOL> graphite_path = self . path_prefix <EOL> graphite_path += '<STR_LIT:.>' + self . device + '<STR_LIT:.>' + type <EOL> graphite_path += '<STR_LIT:.>' + metric_name <EOL> metric = Metric ( <EOL> graphite_path , <EOL> metric_value , <EOL> precision = <NUM_LIT:4> , <EOL> host = self . device ) <EOL> self . publish_metric ( metric ) <EOL> class netappDisk ( diamond . collector . Collector ) : <EOL> """<STR_LIT>""" <EOL> running = set ( ) <EOL> def collect ( self , device , ip , user , password ) : <EOL> """<STR_LIT>""" <EOL> if netappsdk : <EOL> self . log . error ( <EOL> '<STR_LIT>' ) <EOL> return <EOL> if device in self . running : <EOL> return <EOL> self . running . add ( device ) <EOL> parent = ( self . config [ '<STR_LIT>' ] , self . publish_metric , self . log ) <EOL> netappDiskCol ( device , ip , user , password , parent ) <EOL> self . running . remove ( device ) </s>
