
    Vhr.              	           d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
mZ d dlmZ d dlmZ dd	d
dddZg dZe G d d edd                   Z G d de      Z G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: monit
short_description: Manage the state of a program monitored using Monit
description:
  - Manage the state of a program monitored using Monit.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    description:
      - The name of the C(monit) program/process to manage.
    required: true
    type: str
  state:
    description:
      - The state of service.
    required: true
    choices: ["present", "started", "stopped", "restarted", "monitored", "unmonitored", "reloaded"]
    type: str
  timeout:
    description:
      - If there are pending actions for the service monitored by monit, then Ansible will check for up to this many seconds
        to verify the requested action has been performed. Ansible will sleep for five seconds between each check.
    default: 300
    type: int
author:
  - Darryl Stoflet (@dstoflet)
  - Simon Kelly (@snopoke)
z
- name: Manage the state of program httpd to be in started state
  community.general.monit:
    name: httpd
    state: started
N)
namedtuple)AnsibleModule)python_2_unicode_compatiblestopstartmonitor	unmonitorrestart)stoppedstarted	monitoredunmonitored	restarted)	ProcessFileFifo
Filesystem	DirectoryzRemote hostSystemProgramNetworkc                   Z     e Zd ZdZdZdZdZdZdZeeeeeegZ	d fd	Z
d Zd	 Zd
 Z xZS )StatusValuemissingoknot_monitoredinitializingdoes_not_existexecution_failedc                 .    t         t        |   | ||      S N)superr   __new__)clsvalue
is_pending	__class__s      k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/monit.pyr&   zStatusValue.__new__W   s    [#.sE:FF    c                 .    t        | j                  d      S )NT)r   r(   selfs    r+   pendingzStatusValue.pendingZ   s    4::t,,r,   c                     |d | j                   D        v r*| j                  t        | |dd  j                               k(  S t	        |      )Nc              3   &   K   | ]	  }d |z    yw)zis_%sN ).0statuss     r+   	<genexpr>z*StatusValue.__getattr__.<locals>.<genexpr>^   s     CGf$Cs      )
ALL_STATUSr(   getattrupperAttributeError)r/   items     r+   __getattr__zStatusValue.__getattr__]   sB    C4??CC::tABx~~/?!@@@T""r,   c                 B    | j                   | j                  rdS dS )Nz
 (pending) )r(   r)   r.   s    r+   __str__zStatusValue.__str__b   s%    T__\%LMM"%LMMr,   F)__name__
__module____qualname__MISSINGOKNOT_MONITOREDINITIALIZINGDOES_NOT_EXISTEXECUTION_FAILEDr8   r&   r0   r=   r@   __classcell__)r*   s   @r+   r   r   K   sM    G	B#M!L%N)]L.BRJG-#
Nr,   r   Statuszvalue, is_pendingc                      e Zd Z eej                        Z eej
                        Z eej
                        Z eej                        Z eej                        Z eej                        Z	 eej                        Z
y)rL   N)rB   rC   rD   r   rE   rF   RUNNINGrG   rH   rI   rJ   r3   r,   r+   rL   rL   f   so    +--.G	[^^	$B+..)G 9 9:M{778L !;!;<N";#?#?@r,   c                       e Zd Zd Zd Zd ZddZd Zed        Z	ddZ
d	 Zd
 Zd Zd Zd ZddZd Zd ZddZd Zd Zd Zd Zd Zy)Monitc                 f    || _         || _        || _        || _        d | _        d | _        d| _        y )N   )modulemonit_bin_pathprocess_nametimeout_monit_version_raw_version_status_change_retry_count)r/   rS   rT   service_namerV   s        r+   __init__zMonit.__init__q   s8    ,(" *+'r,   c                     | j                   '| j                         \  | _        }|d   |d   f| _         | j                   S )Nr      )rW   _get_monit_versionrX   )r/   versions     r+   monit_versionzMonit.monit_version{   sE    &)-)@)@)B&Dw")!*gaj"8D"""r,   c           	          | j                   j                  | j                  dgd      \  }}}|j                  d      d   }t	        j
                  d|      j                         }|t        t        t        |j                  d                  fS )Nz-VTcheck_rc
r   z([0-9]+\.){1,2}([0-9]+)?.)
rS   run_commandrT   splitresearchgrouptuplemapint)r/   rcouterrversion_lineraw_versions         r+   r^   zMonit._get_monit_version   s{    {{..0C0CT/JUY.ZCyyq)ii ;\JPPRE#c;+<+<S+A"BCCCr,   Nc                     |j                  || j                  |rt        |      nd d        | j                  j                  di | y )N)msgr`   process_statusr3   )updaterX   strrS   	fail_json)r/   rt   r5   kwargss       r+   	exit_failzMonit.exit_fail   sC    !..-3c&k
 	
 	''r,   c                 T    | j                   j                  d| j                  |       y )NTchangednamestate)rS   	exit_jsonrU   )r/   r   s     r+   exit_successzMonit.exit_success   s!    d1B1B%Pr,   c                 2    | j                         dkD  rdgS g S )N)      z-B)r`   r.   s    r+   command_argszMonit.command_args   s    ++-7v?R?r,   c                     |rdnd}|rdnd}| j                   |g| j                  z   | j                  gz   }| j                  j	                  ||      \  }}}| j                  ||      S )zReturn the status of the process in monit.

        :@param validate: Force monit to re-check the status of the process
        validater5   FTrb   )rT   r   rU   rS   rf   _parse_status)r/   r   monit_commandrc   commandrn   ro   rp   s           r+   
get_statuszMonit.get_status   sq    
 '/
H$5$&&69J9JJdN_N_M``{{..w.JC!!#s++r,   c                 (   dj                  t        D cg c]  }t        j                  |       c}      }d|dt        j                  | j                        d}t        j
                  ||t        j                        st        j                  S t        j                  d|t        j                        }|s| j                  d||       |d   j                         j                         }d	|v r|j                  d	      d   }d
|vr"|j                  dd      }t!        t        |      S |j                  d
      \  }}|j                         \  }}	|dv rt        j"                  }
nt        j$                  }
|	dk(  r|
j'                         }
|
S c c}w )N|(z) ''z^\s*status\s*([\w\- ]+)zUnable to find process statusstdoutstderrr   z | z -  _)STARTrH   RESTARTMONITORr0   )joinMONIT_SERVICESrh   escaperU   ri   
IGNORECASErL   rE   findall	MULTILINErz   stripr:   rg   replacer9   rF   rG   r0   )r/   outputrp   xescaped_monit_servicespattern
status_val	substatusactionr   r5   s              r+   r   zMonit._parse_status   sI   !$*PA299Q<*P!Q!74CTCT9UVyy&"--8>>!ZZ :FBLLQ
NN:6RUNV]((*002
J#))%03J
"#++C5J6:..$.$4$4U$;!J	%OO-MFEHH--	!)M3 +Qs   Fc                     | j                   dg| j                  z   }| j                  j                  |d      \  }}}t	        t        j                  d| j                  z  |            S )NsummaryTrb   z\b%s\b)rT   r   rS   rf   boolrh   r   rU   )r/   r   rn   ro   rp   s        r+   is_process_presentzMonit.is_process_present   s]    &&	2T5F5FF{{..w.FCBJJy4+<+<<cBCCr,   c                 6    | j                         j                  S r$   )r   is_okr.   s    r+   is_process_runningzMonit.is_process_running   s     &&&r,   c                 j    | j                   j                  | j                  || j                  gd      S )z1Runs a monit command, and returns the new status.Trb   )rS   rf   rT   rU   )r/   r   s     r+   rf   zMonit.run_command   s0    {{&&(;(;WdFWFW'Xcg&hhr,   c                    | j                         }|j                  |j                  k7  s|j                  t        j                  k(  r|S d}|j                  |j                  k(  rn|| j                  k\  r| j                  d|       |dz  }t        j                  d       |dz  dk(  }| j                  |      }|j                  |j                  k(  rn|S )Nr   z)waited too long for monit to change stater]   g      ?   )r   r(   r   rJ   rY   rz   timesleep)r/   current_statusrunning_status
loop_countr   s        r+   wait_for_status_changezMonit.wait_for_status_change   s    *>#7#77>;O;OS^SoSo;o!!
""n&:&::T<<<JN[!OJJJsO!A~*H!__X6N ""n&:&:: r,   c                    t        j                          | j                  z   }|s| j                         }t        j                  t        j
                  t        j                  g}|j                  s|j                  |v rlt        j                          |k\  r| j                  d|       t        j                  d       | j                  d      }|j                  r]|j                  |v rl|S )zNFails this run if there is no status or it is pending/initializing for timeoutz@waited too long for "pending", or "initiating" status to go awayr   T)r   )r   rV   r   r   rE   rH   rI   r)   r(   rz   r   )r/   r   timeout_timewaiting_statuss       r+   wait_for_monit_to_stop_pendingz$Monit.wait_for_monit_to_stop_pending   s    yy{T\\1!__.N$$&&

 ''N,@,@N,Ryy{l*acqrJJqM!__d_;N ''N,@,@N,R r,   c                     | j                   j                  | j                  dg      \  }}}|dk7  r| j                  d||       | j	                  d       y )Nreloadr   zmonit reload failedr   reloadedr   )rS   rf   rT   rz   r   )r/   rn   ro   rp   s       r+   r   zMonit.reload   sR    {{..0C0CX/NOC7NN0SNI
+r,   c                 H   | j                  d       t        j                         | j                  z   }| j                         sNt        j                         |k\  r| j	                  d       t        j
                  d       | j                         sN| j                  d       y )Nr   z/waited too long for process to become "present"r   presentr   )rf   r   rV   r   rz   r   r   )r/   r   s     r+   r   zMonit.present   sw    "yy{T\\1))+yy{l*PQJJqM	 ))+ 		*r,   c                 >   | j                         }| j                  t        |          | j                  |      }| j	                  |      }|j
                  |j
                  k(  }|r| }|r| j                  |       | j                  | j                  d||       y )Nr   z process not )	r   rf   STATE_COMMAND_MAPr   r   r(   r   rz   rU   )r/   r   expected_statusinvert_expectedr   r5   status_matchs          r+   change_statezMonit.change_state   s    **512,,^<44V<||'<'<<++LE*d.?.?GPr,   c                 D    | j                  dt        j                         y )Nr   r   rL   rG   r.   s    r+   r	   z
Monit.stop  s    )V%9%9:r,   c                 D    | j                  dt        j                         y )Nr   r   r.   s    r+   r   zMonit.unmonitor  s    -)=)=>r,   c                 D    | j                  dt        j                         y )Nr   r   rL   rF   r.   s    r+   r   zMonit.restart  s    +vyy1r,   c                 D    | j                  dt        j                         y )Nr   r   r.   s    r+   r
   zMonit.start  s    )VYY/r,   c                 H    | j                  dt        j                  d       y )Nr   T)r   r   r.   s    r+   r   zMonit.monitor  s    +v';';TRr,   r$   rA   )rB   rC   rD   r[   r`   r^   rz   r   propertyr   r   r   r   r   rf   r   r   r   r   r   r	   r   r   r
   r   r3   r,   r+   rP   rP   p   s    ,#D(Q @ @	,8D
'i &,
+
Q;?20Sr,   rP   c            	         t        t        d      t        dd      t        dg d            } t        | d	      j                  d
   }j                  d   }j                  d   }t        j	                  dd      ||      }fd}|dk(  r |        |j                          |j                         }|s|dk(  sj                  d|z  |       |dk(  r-|rj                  d||        |        |j                          |j                          |j                         }|r|dv rj                  d||       |r|dk(  r |        |j                          |r|dk(  r |        |j                          nZ|dk(  r |        |j                          n=|s|dk(  r |        |j                          n|s|dk(  r |        |j!                          j                  d||       y )NT)requiredi,  rm   )defaulttype)r   r   r   r   r   r   r   )r   choices)r~   rV   r   )argument_specsupports_check_moder~   r   rV   monitc                  D     j                   r j                  d       y y )NT)r}   )
check_moder   )rS   s   r+   exit_if_check_modez main.<locals>.exit_if_check_mode*  s"    T* r,   r   r   z.%s process not presently configured with monit)rt   r~   Fr|   )r   r   r   r   r   r   r   )dictr   paramsrP   get_bin_pathr   r   rx   r   r   r   r   r	   r   r   r
   r   )	arg_specr~   r   rV   r   r   r   runningrS   s	           @r+   mainr     s   4 Su-D  +A  BH tLF== DMM'"EmmI&G&&--gt<dGLE+ 
&&(G5I-MPTT[_`	UUC	((*&&(G544T?5I%

5M)	+	)++-
UU;r,   __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESr   rh   collectionsr   ansible.module_utils.basicr   ansible.module_utils.sixr   r   r   r   objectrL   rP   r   rB   r3   r,   r+   <module>r      s    A @!F  	 " 4 @   N*X':; N N4AV AhSF hSV<<~ zF r,   