+
    *je                     l    R t ^ RIt^ RIt^ RIt^ RItRtRtRtR t	R	R lt
R t]R8X  d
   ]! 4        R# R# )
u  paas-cmk-sync — runs ON the Checkmk server (systemd timer).

Model: ONE Checkmk host per paas project, named after the project's MAIN
domain (uptime + cert via folder-scoped custom_checks rules on $HOSTNAME$).
Every OTHER configured domain of the project gets its own
"Certificate <domain>" service on that same host, via host-scoped
custom_checks rules that this script reconciles (marker in the rule
description). Changing a project's main domain simply renames the host:
old host deleted, new one created, secondary cert rules follow.

Config: /etc/paas-cmk-sync.conf  (KEY=VALUE)
  PANEL_URL=https://deploy.younex.de
  TOKEN=<CMK_AGENT_TOKEN>
  SITE=monitoring
  VIP=188.40.238.163
  EXTRA_DOMAINS=deploy.younex.de
Nz/etc/paas-cmk-sync.conf
paas_sitesz	paas-certc                      / p \        \        4       FW  pVP                  4       pV'       g   K  VP                  R 4      '       d   K6  RV9   g   K?  VP	                  R^4      w  r#W0V&   KY  	  V # )#=)openCONFstrip
startswithsplit)cfglinekvs       I/Users/younes/Documents/develop/phpstorm/dotnews-paas/infra/paas-cmk-sync	load_confr      sY    
CT
zz|4,,::c1%DAF	 
 J    c                    \         P                  P                  YVe%   \        P                  ! V4      P                  4       MR R7      pVP                  RR4       VP                  RR4       T;'       g    / P                  4        F  w  rgVP                  Wg4       K  	   \         P                  P                  WT^R7      ;_uu_ 4       pVP                  VP                  4       P                  4       3uuR R R 4       #   + '       g   i     R # ; i  \         P                  P                   d5   p	T	P                  T	P                  4       P                  4       3u R p	?	# R p	?	ii ; i)N)methoddatazContent-Typezapplication/jsonAccept)contexttimeout)urllibrequestRequestjsondumpsencode
add_headeritemsurlopenstatusreaddecodeerror	HTTPErrorcode)
r   urlbodyheadersctxreqr   r   res
   &&&&&     r   httpr.   '   s    
..
 
 CGCSdjj&6&=&=&?Y] ! _CNN>#56NN8/0B%%'q ()^^##Cb#AAQ88QVVX__.. BAAA<<!! )vvqvvx((()s<   #)D *D6
D D	D D E(4)E#E(#E(c                  l   \        4       p \        R V R,           R24      P                  4       P                  4       pRV R,           R2pRRV 2/p\	        RV R,           R	2RR
V R,           2/\
        P                  ! 4       R7      w  rEV^8w  d5   \        RV 2\        P                  R7       \        P                  ! ^4       / p\        P                  ! V4      P                  R. 4       F  pVP                  R. 4       Uu. uF8  qP                  4       '       g   K  VP                  4       P                  4       NK:  	  p	pV	'       g   Ka  \        VP                  R4      ;'       g
    V	^ ,          4      P                  4       P                  4       p
W9  d
   V	^ ,          p
\!        \#        V	4      V
0,
          4      Wj&   K  	  V P                  RR4      P%                  R4       F=  pVP                  4       P                  4       pV'       g   K+  VP'                  V. 4       K?  	  Rp\	        RV R\(         R2VR7      w  rEV^8w  d?   \        RV RVR,           2\        P                  R7       \        P                  ! ^4       \        P                  ! V4      P                  R. 4       Uu0 uF  qR,          kK  	  pp\!        \#        V4      V,
          4       FT  p\	        RV R2R R!\(         2R"VR#R$V R%,          R&R'R(R)V 2//VR*7      w  rE\        R+V R,V 24       T;'       g    V^8H  pKV  	  \!        V\#        V4      ,
          4       F8  p\	        R-V R.V 2VR7      w  rE\        R/V R,V 24       T;'       g    V^8H  pK:  	  \	        RV R02VR7      w  rEV^8w  d?   \        R1V RVR,           2\        P                  R7       \        P                  ! ^4       / p\        P                  ! V4      P                  R. 4       F  pVP                  R24      ;'       g    / P                  R34      ;'       g    / P                  R44      ;'       g    RpVP%                  4       p\+        V4      ^8X  g   Kr  V^ ,          \,        8X  g   K  VR,          VV^,          V^,          3&   K  	  VP/                  4        UUUu0 uF  w  ppV F  pVV3kK	  	  K  	  pppp\!        V\#        V4      ,
          4       Fn  w  ppR5V: R6V: R72p\	        RV R82R9R:R R!\(         2R3R4\,         R;V R;V 2/R<VR=R"R>V.R?R@///VR*7      w  rE\        RAV RBV R,V 24       T;'       g    V^8H  pKp  	  \!        \#        V4      V,
          4       FF  w  pp\	        R-V RCWV3,           2VR7      w  rE\        RDV RBV R,V 24       T;'       g    V^8H  pKH  	  V'       d9   \	        RV RE2RFRRV R,          .RGRH// VCRIRJ/CR*7      w  rE\        RKV 24       RM# \        RL4       RM# u upi u upi u upppi )Nz/omd/sites/SITEz./var/check_mk/web/automation/automation.secretzhttp://localhost/z/check_mk/api/1.0AuthorizationzBearer automation GET	PANEL_URLz
/cmk-siteszBearer TOKEN)r)   r*   zpanel unreachable: )filesitesdomainsmainEXTRA_DOMAINS ,Fz/objects/folder_config/~z/collections/hosts)r)   zcannot list folder hosts (z): :N   NvalueidPOSTz)/domain-types/host_config/collections/allfolder/	host_name
attributes	ipaddressVIP	tag_agentzno-agentaliaszpaas website )r(   r)   z	add host z: DELETEz/objects/host_config/zremove host z=/domain-types/rule/collections/all?ruleset_name=custom_checkszcannot list rules (
extensions
propertiesdescriptionz%{'service_description': 'Certificate z"', 'command_line': 'check_http -H z -S --sni -C 21,7'}z"/domain-types/rule/collections/allrulesetcustom_checks 	value_raw
conditionsmatch_onoperatorone_ofzadd cert rule z on z/objects/rule/zremove cert rule z</domain-types/activation_run/actions/activate-changes/invokeredirectforce_foreign_changesTzIf-Match*z
activate: zin syncN)r   r   r"   r   r.   sslcreate_default_contextprintsysstderrexitr   loadsgetlowerstrsortedsetr
   
setdefaultFOLDERlen	RULE_MARKr   )r   secretapiauthstr(   desiredsitedr7   r8   changedhcurrent_hostshostcurrent_rulesr,   descpartssecsdomdesired_rulesr=   s                          r   r8   r8   5   s   
+CKF},Z[\aaciikFc&k]*;
<C1&:;D Ec+./z:,G~.FG2246HB 
Sy#B4(szz:G

4 $$Wb1.2hhy".ES.E$1779??$.ES488F#11wqz288:@@B1:Ds7|tf45 2 WW_b)//4GGIOO1q"% 5
 G EcU":6(BTU_cdHB	Sy*2$c$t*>SZZP&*jj&6&:&:7B&GH&GtWW&GMHs7|m343%'P Q&l;E
K"mD6$:<Y

  		$r"&'&&R3Y 5 }s7|34SE)>tf"EtTTF"RD)*&&R3Y 5 EcU"_` "HB	Sy#B4s4:,7cjjIMZZ!!'2.%%%++00>DD"II-X^^\^

u:?uQx9423D'M58U1X./	 / 4;==?S?ZT4dsdC[d[?MSMC,>>?	cKNPSU3%'I J&l=YKqau*EF;dVZ(RSR
  	se4vRt45&&R3Y @ c-0=@AsSESk8R7S"T^bc!#d4&2$78&&R3Y B
 3%'c d(%3v;-I`bfg 94 9S 9; 	
2$ iS T& I: Ts   X%!"X%2X*	X/__main__)NNN)__doc__r   rW   rZ   urllib.requestr   r   rd   rf   r   r.   r8   __name__ r   r   <module>r}      sL   "  
 
  		)Xv zF r   