#NEWLINE# def master( master = None , connected = True ) : #NEWLINE# #INDENT# "\n    .. versionadded:: 2015.5.0\n\n    Fire an event if the minion gets disconnected from its master. This\n    function is meant to be run via a scheduled job from the minion. If\n    master_ip is an FQDN/Hostname, is must be resolvable to a valid IPv4\n    address.\n\n    CLI Example:\n\n    .. code-block:: bash\n\n        salt '*' status.master\n    " #NEWLINE# def _win_remotes_on( port ) : #NEWLINE# #INDENT# "\n        Windows specific helper function.\n        Returns set of ipv4 host addresses of remote established connections\n        on local or remote tcp port.\n\n        Parses output of shell 'netstat' to get connections\n\n        PS C:> netstat -n -p TCP\n\n        Active Connections\n\n          Proto  Local Address          Foreign Address        State\n          TCP    10.1.1.26:3389         10.1.1.1:4505          ESTABLISHED\n          TCP    10.1.1.26:56862        10.1.1.10:49155        TIME_WAIT\n          TCP    10.1.1.26:56868        169.254.169.254:80     CLOSE_WAIT\n          TCP    127.0.0.1:49197        127.0.0.1:49198        ESTABLISHED\n          TCP    127.0.0.1:49198        127.0.0.1:49197        ESTABLISHED\n        " #NEWLINE# remotes = set ( ) #NEWLINE# try : #NEWLINE# #INDENT# data = subprocess . check_output ( [ 'netstat' , '-n' , '-p' , 'TCP' ] ) #NEWLINE# #UNINDENT# except subprocess . CalledProcessError : #NEWLINE# #INDENT# log . error ( 'Failed netstat' ) #NEWLINE# raise #NEWLINE# #UNINDENT# lines = salt . utils . to_str ( data ) . split ( '\n' ) #NEWLINE# for line in lines : #NEWLINE# #INDENT# if ( 'ESTABLISHED' not in line ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# chunks = line . split ( ) #NEWLINE# ( remote_host , remote_port ) = chunks [ 2 ] . rsplit ( ':' , 1 ) #NEWLINE# if ( int ( remote_port ) != port ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# remotes . add ( remote_host ) #NEWLINE# #UNINDENT# return remotes #NEWLINE# #UNINDENT# port = 4505 #NEWLINE# master_ip = None #NEWLINE# if ( __salt__ [ 'config.get' ] ( 'publish_port' ) != '' ) : #NEWLINE# #INDENT# port = int ( __salt__ [ 'config.get' ] ( 'publish_port' ) ) #NEWLINE# #UNINDENT# if ( master is not None ) : #NEWLINE# #INDENT# tmp_ip = _host_to_ip ( master ) #NEWLINE# if ( tmp_ip is not None ) : #NEWLINE# #INDENT# master_ip = tmp_ip #NEWLINE# #UNINDENT# #UNINDENT# ips = _win_remotes_on ( port ) #NEWLINE# if connected : #NEWLINE# #INDENT# if ( master_ip not in ips ) : #NEWLINE# #INDENT# event = salt . utils . event . get_event ( 'minion' , opts = __opts__ , listen = False ) #NEWLINE# event . fire_event ( { #NEWLINE# #INDENT# 'master' : master , #NEWLINE# #UNINDENT# } , '__master_disconnected' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( master_ip in ips ) : #NEWLINE# #INDENT# event = salt . utils . event . get_event ( 'minion' , opts = __opts__ , listen = False ) #NEWLINE# event . fire_event ( { #NEWLINE# #INDENT# 'master' : master , #NEWLINE# #UNINDENT# } , '__master_connected' )