#NEWLINE# def _poll_switch( ip_addr , credentials , req_obj = 'mac' , oper = 'SCAN' ) : #NEWLINE# #INDENT# 'Poll switch by ip addr.\n\n\n    Args:\n       ip_addr: ip addr of the switch.\n       credentials: credentials of the switch.\n\n    Returns: switch attributes dict and list of machine attributes dict.\n    ' #NEWLINE# under_monitoring = 'under_monitoring' #NEWLINE# unreachable = 'unreachable' #NEWLINE# polling_error = 'error' #NEWLINE# hdmanager = HDManager ( ) #NEWLINE# ( vendor , state , err_msg ) = hdmanager . get_vendor ( ip_addr , credentials ) #NEWLINE# if ( not vendor ) : #NEWLINE# #INDENT# logging . info ( '*****error_msg: %s****' , err_msg ) #NEWLINE# logging . error ( 'no vendor found or match switch %s' , ip_addr ) #NEWLINE# return ( { #NEWLINE# #INDENT# 'vendor' : vendor , #NEWLINE# 'state' : state , #NEWLINE# 'err_msg' : err_msg , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# logging . debug ( 'hdmanager learn switch from %s' , ip_addr ) #NEWLINE# results = [ ] #NEWLINE# try : #NEWLINE# #INDENT# results = hdmanager . learn ( ip_addr , credentials , vendor , req_obj , oper ) #NEWLINE# #UNINDENT# except Exception as error : #NEWLINE# #INDENT# logging . exception ( error ) #NEWLINE# state = unreachable #NEWLINE# err_msg = 'SNMP walk for querying MAC addresses timedout' #NEWLINE# return ( { #NEWLINE# #INDENT# 'vendor' : vendor , #NEWLINE# 'state' : state , #NEWLINE# 'err_msg' : err_msg , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# logging . info ( 'pollswitch %s result: %s' , ip_addr , results ) #NEWLINE# if ( not results ) : #NEWLINE# #INDENT# logging . error ( 'no result learned from %s' , ip_addr ) #NEWLINE# state = polling_error #NEWLINE# err_msg = 'No result learned from SNMP walk' #NEWLINE# return ( { #NEWLINE# #INDENT# 'vendor' : vendor , #NEWLINE# 'state' : state , #NEWLINE# 'err_msg' : err_msg , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# logging . info ( ( 'poll switch result: %s' % str ( results ) ) ) #NEWLINE# machine_dicts = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for machine in results : #NEWLINE# #INDENT# mac = machine [ 'mac' ] #NEWLINE# port = machine [ 'port' ] #NEWLINE# vlan = int ( machine [ 'vlan' ] ) #NEWLINE# if vlan : #NEWLINE# #INDENT# vlans = [ vlan ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# vlans = [ ] #NEWLINE# #UNINDENT# if ( mac not in machine_dicts ) : #NEWLINE# #INDENT# machine_dicts [ mac ] = { #NEWLINE# #INDENT# 'mac' : mac , #NEWLINE# 'port' : port , #NEWLINE# 'vlans' : vlans , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# machine_dicts [ mac ] [ 'port' ] = port #NEWLINE# machine_dicts [ mac ] [ 'vlans' ] . extend ( vlans ) #NEWLINE# #UNINDENT# #UNINDENT# logging . debug ( 'update switch %s state to under monitoring' , ip_addr ) #NEWLINE# state = under_monitoring #NEWLINE# return ( { #NEWLINE# #INDENT# 'vendor' : vendor , #NEWLINE# 'state' : state , #NEWLINE# 'err_msg' : err_msg , #NEWLINE# #UNINDENT# } , machine_dicts . values ( ) )