<s> try : <EOL> import commands <EOL> except ImportError : <EOL> import subprocess as commands <EOL> import string <EOL> from swiftlm . utils . utility import get_swift_bind_ips , UtilityExeception <EOL> from swiftlm . utils . utility import ip_to_interface <EOL> from swiftlm . utils . metricdata import MetricData <EOL> from swiftlm . utils . values import Severity <EOL> BASE_RESULT = MetricData ( <EOL> name = __name__ , <EOL> messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ) <EOL> def str_to_num ( val ) : <EOL> try : <EOL> return int ( val ) <EOL> except ValueError : <EOL> try : <EOL> return float ( val ) <EOL> except : <EOL> return val <EOL> def get_interface_names ( ip_to_interface_map , bind_ip_list ) : <EOL> """<STR_LIT>""" <EOL> interface_name_list = set ( ) <EOL> for ip_address in bind_ip_list : <EOL> interface_name_list . add ( ip_to_interface_map [ ip_address ] ) <EOL> return interface_name_list <EOL> def get_interface_speed ( interface_name ) : <EOL> """<STR_LIT>""" <EOL> c = BASE_RESULT . child ( dimensions = { '<STR_LIT>' : interface_name } ) <EOL> command = "<STR_LIT>" + interface_name <EOL> interface_data = commands . getoutput ( command ) <EOL> speed = '<STR_LIT>' <EOL> lines = interface_data . split ( '<STR_LIT:\n>' ) <EOL> for line in lines : <EOL> if '<STR_LIT>' in line : <EOL> _ , speed = line . split ( '<STR_LIT::>' ) <EOL> speed = '<STR_LIT>' . join ( c for c in speed if c in string . digits ) <EOL> if speed : <EOL> c . value = str_to_num ( speed ) <EOL> else : <EOL> c . value = <NUM_LIT:0> <EOL> return c <EOL> def get_interface_data ( interface_name ) : <EOL> """<STR_LIT>""" <EOL> command = '<STR_LIT>' + interface_name + '<STR_LIT::>' <EOL> interface_data = commands . getoutput ( command ) <EOL> data = [ str_to_num ( val ) for val in interface_data . split ( ) ] <EOL> d = { <EOL> '<STR_LIT>' : interface_name , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:4> , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : <NUM_LIT:6> , <EOL> '<STR_LIT>' : <NUM_LIT:7> , <EOL> '<STR_LIT>' : <NUM_LIT:8> , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : <NUM_LIT:9> , <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : <NUM_LIT:11> , <EOL> '<STR_LIT>' : <NUM_LIT:12> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:15> , <EOL> '<STR_LIT>' : <NUM_LIT:16> , <EOL> } , <EOL> } <EOL> for x in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> for field_name , field_index in d [ x ] . items ( ) : <EOL> d [ x ] [ field_name ] = data [ field_index ] <EOL> return d <EOL> def run_network_interface_check ( ) : <EOL> results = [ ] <EOL> ip_to_interface_map = ip_to_interface ( ) <EOL> bind_ip_list = get_swift_bind_ips ( ) <EOL> try : <EOL> interface_names = get_interface_names ( ip_to_interface_map , <EOL> bind_ip_list ) <EOL> except UtilityExeception : <EOL> c = BASE_RESULT . child ( ) <EOL> c . value = Severity . fail <EOL> return [ c ] <EOL> for interface_name in interface_names : <EOL> results . append ( get_interface_speed ( interface_name ) ) <EOL> data = get_interface_data ( interface_name ) <EOL> for x in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> for stat_name , stat_count in data [ x ] . items ( ) : <EOL> c = BASE_RESULT . child ( <EOL> name = x + '<STR_LIT:.>' + stat_name , <EOL> dimensions = { <EOL> '<STR_LIT>' : interface_name , <EOL> } <EOL> ) <EOL> c . value = stat_count <EOL> results . append ( c ) <EOL> return results <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> return run_network_interface_check ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
