o
    id:                     @   s@   d dl Z d dlmZ d dlmZ d dlmZ G dd deZdS )    N)Cluster)parse_version)ElementTreec                   @   sF   e Zd ZdZd gZdZdZg dZdd Zdd Z	d	d
 Z
dd ZdS )	pacemakerz+Pacemaker High Availability Cluster Manager)r   T))onlineTzCollect nodes listed as online)offlineTzCollect nodes listed as offline)only-corosyncFz)Only use corosync.conf to enumerate nodesc              
   C   s   g | _ z.| ds)z|   W n ty( } z| d|  W Y d }~nd }~ww | j s0|   W n tyJ } z| d|  W Y d }~nd }~ww dd | j D }|r_| dd|  | j S )Nr   zJFalling back to sourcing corosync.conf. Could not parse crm_mon output: %sz*Could not determine nodes from cluster: %sc                 S   s   g | ]}d |vr|qS ). ).0nr
   r
   B/usr/lib/python3/dist-packages/sos/collector/clusters/pacemaker.py
<listcomp>/   s    z'pacemaker.get_nodes.<locals>.<listcomp>zWARNING: Node addresses '%s' may not resolve locally if you are not running on a node in the cluster. Try using option '-c pacemaker.only-corosync' if these connections fail.,)nodes
get_optionget_nodes_from_crm	Exceptionlog_warnget_nodes_from_corosync	log_errorjoin)selferr_shortsr
   r
   r   	get_nodes   s4   
zpacemaker.get_nodesc                 C   s   d}|  d}|d dkr%|d  d dd }t|tdks$d	}nd
S | j d| dd}|d dkr?| |d  d
S d
S )zG
        Try to parse crm_mon output for node list and status.
        z--output-as=xmlzcrm_mon --versionstatusr   output   -z2.0.3z--as-xmlNz crm_mon --one-shot --inactive %sT)	need_root)exec_primary_cmdsplitr   parse_crm_xml)r   xmlopt_vercver_outr
   r
   r   r   9   s   
zpacemaker.get_nodes_from_crmc                 C   sv   t |}|d}|D ],}|j}| dr%|d dkr%| j|d  q| dr8|d dkr8| j|d  qdS )zA
        Parse the xml output string provided by crm_mon
        r   r   truenamer   falseN)r   
fromstringfindattribr   r   append)r   	xmlstring_xmlr   node_noder
   r
   r   r#   M   s   

zpacemaker.parse_crm_xmlc                 C   sD   |  d | jd}td|}|D ]}| j|d   qdS )z
        As a fallback measure, read corosync.conf to get the node list. Note
        that this prevents us from separating online nodes from offline nodes.
        z_WARNING: unable to distinguish online nodes from offline nodes when sourcing from corosync.confz/etc/corosync/corosync.confz((\sring0_addr:)(.*))N)r   primary	read_filerefindallr   r.   strip)r   ccr   r1   r
   r
   r   r   Z   s   
z!pacemaker.get_nodes_from_corosyncN)__name__
__module____qualname__cluster_namesos_pluginspackagesstrict_node_listoption_listr   r   r#   r   r
   r
   r
   r   r      s    r   )r6   sos.collector.clustersr   sos.utilitiesr   	xml.etreer   r   r
   r
   r
   r   <module>   s
   
