Ñò
ýÉªWc        
   @   s¿   d  d k  Z  d  d k Z d  d k Z d  d k Z d  d k Z d  d k Z d  d k l Z d e i f d „  ƒ  YZ d d d „  ƒ  YZ d e f d „  ƒ  YZ	 d	 „  Z
 e d
 j o e
 ƒ  n d S(   iÿÿÿÿN(   t   SSHCommandert   AlertPolicyForCMS3Xc           B   sP   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   c         C   s   t  i  i |  ƒ d  S(   N(   t	   BaseClasst   __init__(   t   self(    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR      s    c         C   sA   | d } |  i  i d | ƒ t |  i  i ƒ  ƒ o t St Sd  S(   Ni    s3   SELECT IDENTIFIER FROM ALERTS WHERE IDENTIFIER='%s'(   t   cursort   executet   lent   fetchallt   Truet   False(   R   t   datat   alertIdentifier(    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyt   checkExistence   s
    
c         C   s   d S(   Ni   (    (   R   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyt	   getNextId   s    c         C   s  yé d |  i  | d d !ƒ } | GH|  i i | ƒ t i ƒ  } y: | d d  j	 o% | i d  | d d | d f ƒ n Wn t j
 o n Xy: | d d  j	 o% | i d  | d d | d f ƒ n Wn t j
 o n X|  i ƒ  SWn! t j
 o } | GH|  i	 ƒ  SXd  S(   Nsn   INSERT INTO ALERTS (IDENTIFIER, ALERT_NAME , THRESH_COUNT, THRESH_SECS, FREQ_COUNT, FREQ_SECS, TYPE) VALUES %si    i   i   i   (
   t
   tupleToStrR   R   t   AlertActionst   Nonet   insertt   KeyErrort   committ	   Exceptiont   rollback(   R   t   id_R   t	   statementt   aast   e(    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR      s(    ))c         C   s,  yd | d | d | d | d | d | d | d f } | GH|  i  i | ƒ t i ƒ  } y7 | d	 d  j	 o" | i | d d | d	 f ƒ n Wn t j
 o n Xy7 | d
 d  j	 o" | i | d d | d
 f ƒ n Wn t j
 o n X|  i ƒ  SWn! t j
 o } | GH|  i ƒ  SXd  S(   Nsˆ   UPDATE ALERTS SET ALERT_NAME='%s', THRESH_COUNT='%s', THRESH_SECS='%s', FREQ_COUNT='%s', FREQ_SECS='%s', TYPE='%s' WHERE IDENTIFIER='%s'i   i   i   i   i   i   i    i   i   (	   R   R   R   R   t   updateR   R   R   R   (   R   R   R   R   R   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR   2   s(    8&&c         C   s‹   | d } | d } t  i  ƒ  } yC | i | g ƒ d | | f } | GH|  i i | ƒ |  i ƒ  SWn! t j
 o } | GH|  i ƒ  SXd S(   s5   
		data[0]: alert identifier
		data[1]: alert name
		i    i   s>   DELETE FROM ALERTS WHERE IDENTIFIER = '%s' AND ALERT_NAME='%s'N(   R   t   deleteR   R   R   R   R   (   R   R   t   alertIdt	   alertNameR   R   R   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR   J   s    

c         C   sf  d } d } d d d d d g } | d j p t | ƒ o d Sg  } |  i i | | d	 ƒ |  i i ƒ  } xñ | D]é } h  } xO t t | ƒ ƒ D]; }	 | |	 d j o d
 | | |	 <q” | |	 | | |	 <q” W|  i i | | d	 ƒ |  i i ƒ  }
 xT |
 D]L } | d	 d	 j o | d | d <q| d	 d j o | d | d <qqW| i | ƒ qu W| S(   s¿   
		Input:
			args[0]: ALERT_NAME
		OutPut:
			[{"IDENTIFIER":value, "THRESH_COUNT":value, "THRESH_SECS":value, "FREQ_COUNT":value,"FREQ_SECS":value, "Email":value, "SNMP_TRAP":value}{...}]
		se   SELECT IDENTIFIER, THRESH_COUNT, THRESH_SECS, FREQ_COUNT, FREQ_SECS FROM ALERTS WHERE ALERT_NAME='%s'sF   SELECT  ACTION_TYPE, DETAIL FROM ALERT_ACTIONS WHERE IDENTIFIER = '%s't
   IDENTIFIERt   THRESH_COUNTt   THRESH_SECSt
   FREQ_COUNTt	   FREQ_SECSNi    t    i   t   Emailt	   SNMP_TRAP(   R   R   R   R   R   t   ranget   append(   R   t   argst   alertStatementt   alertActionStatmentt
   fieldArrayt   itemt	   alertRowst   alertRowt	   alertDictt   it   alertActionRowst   alertActionRow(    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyt   backUpAllExisting`   s4       c   
   	   C   se  d } d } d d d d d d d	 g } g  } x' t  t | ƒ ƒ D] } | i d
 ƒ q@ W| d j p t | ƒ o d S|  i i | | d ƒ |  i i ƒ  } x0 t  t | d ƒ ƒ D] } | d | | | <qµ W|  i i | | d ƒ |  i i ƒ  } xf | D]^ }	 |	 d d j o |	 d | | i d ƒ <qÿ |	 d d j o |	 d | | i d	 ƒ <qÿ qÿ W| S(   s¡   
		Input:
			args[0]: IDENTIFIER
		Output:
		 	A squence value of ("IDENTIFIER", "THRESH_COUNT", "THRESH_SECS", "FREQ_COUNT","FREQ_SECS", "SNMP_TRAP","EMAIL")
		se   SELECT IDENTIFIER, THRESH_COUNT, THRESH_SECS, FREQ_COUNT, FREQ_SECS FROM ALERTS WHERE IDENTIFIER='%s'sF   SELECT  ACTION_TYPE, DETAIL FROM ALERT_ACTIONS WHERE IDENTIFIER = '%s'R   R    R!   R"   R#   R&   t   EMAILR$   Ni    i   (   R'   R   R(   R   R   R   R   t   index(
   R   R)   R*   R+   R,   t
   valueArrayR1   R.   R2   R3   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyt   backUpExisting‡   s.       (
   t   __name__t
   __module__R   R   R   R   R   R   R4   R8   (    (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR   
   s   							't   AlertPolicyForCMS4Xc           B   sw   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z e d „  ƒ Z RS(   s   /etc/logstash/alerts.yamlc         C   s   |  i  | ƒ d  S(   N(   t   _loadConfig(   R   t   csNodes(    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR   ­   s    c         C   s½   | |  _  d t t i ƒ  ƒ |  _ |  i  d } |  i | d | d | d | d ƒ t |  i d ƒ i i ƒ  } z% | ~ } t	 i
 | i ƒ  ƒ |  _ Wd  QX|  i d  j o g  |  _ n d  S(   Ns   /tmp/alerts.%s.yamli    t   ipt   usert   passwordt
   rootPasswdt   r(   t   _hostst   strt   ost   getpidt   _localConfigt   syncConfigToLocalt   opent   __exit__t	   __enter__t   yamlt   loadt   readt   _allContentR   (   R   R=   t   host01t   _[1]t   f(    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR<   °   s    	&)c      
   C   s,  d | GHt  i i t i i d | i d d ƒ ƒ } d  } zÕ y¸ t | | | ƒ } d t i	 | f } | i
 | | ƒ \ } }	 |	 GH| d j oR d GHt  i i | ƒ }
 t  i i |
 ƒ p t  i |
 ƒ n t | d ƒ i ƒ  n t i | |  i ƒ Wn t j
 o
 ‚  n XWd  | o | i ƒ  n Xd  S(	   Ns4   Start to sync alert configuration from %s to LOCAL: s   alerts.%s.yamlt   .R$   s   su root -c "\cp -f %s %s"i    s/   Alert configuration on CS node may do not existt   a(   RE   t   patht   joint   Commont   COMMON_SHARED_FOLDERt   replaceR   R    R;   t   ALERT_CONF_FILEt   execute_interactivet   dirnamet   existst   makedirsRI   t   closet   shutilt   copyfileRG   R   t   logout(   R   t   serveript   usernameR@   t   rootpwdt   inter_filenamet   cmdt
   remote_cmdt   statust   outputt   basedir(    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyRH   ½   s(    	+ c   
   
   C   sñ   d | GHt  i i t i i d | i d d ƒ ƒ } t i |  i | ƒ d  } z‡ yj t
 | | | ƒ } d | t i | f } | i | | ƒ \ } }	 |	 GH| d j o t d | ƒ ‚ n Wn t j
 o
 ‚  n XWd  | o | i ƒ  n Xd  S(   Ns-   Start to sync alert configuration to REMOTE: s   alerts.%s.yamlRS   R$   s&   su root -c "/bin/cp -f %s %s;rm -f %s"i    s.   Failed to sync alert configuration to remote: (   RE   RU   RV   RW   RX   RY   R`   Ra   RG   R   R    R;   RZ   R[   R   Rb   (
   R   Rc   Rd   R@   Re   Rf   Rg   Rh   Ri   Rj   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyt   syncConfigToRemoteÖ   s     	+ c         C   s-   |  i  | d ƒ } | d  j o t St Sd  S(   NR   (   t   getAlertByIdentifierR   R	   R
   (   R   R   t   foundObj(    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR   í   s    c         C   sk   | } d  } xX |  i D]M } d | j o q n | d } d | j o | d | j o | } Pq q W| S(   Nt   alertt   name(   R   RO   (   R   t   idfR   t   objt   elemRo   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyRm   ô   s    
 
	c         C   só   d t  | ƒ GHh t | d ƒ d 6t i t | d ƒ ƒ d 6} h  } t  | d ƒ | d <t  | d ƒ | d	 <| | d
 <d | j o# | d o t | d ƒ | d <n d | j o# | d o t  | d ƒ | d <n |  i i h | d 6ƒ t S(   Ns   Adding alert: R"   t   countR#   t   timeR   Rp   t   PATTERNt   patternt	   frequencyR&   t   trapR5   t   emailRo   (   RD   t   intR;   t   formatSecondsRO   R(   R	   (   R   R   t   freqRr   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR     s    1
c         C   sç   d t  | ƒ GH|  i | d ƒ } | p d  St  | d ƒ | d <d | j o# | d o t | d ƒ | d <n d | j o# | d o t  | d ƒ | d <n h  } t | d	 ƒ | d
 <t i t | d ƒ ƒ | d <| | d <d  S(   Ns   Updating alert: R   Rv   Rw   R&   Ry   R5   Rz   R"   Rt   R#   Ru   Rx   (   RD   Rm   R{   R;   R|   (   R   R   Rr   R}   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR     s    c         C   s‚   d t  | ƒ GH| d } xb |  i D]W } d | j o q# n | d } d | j o& | d | j o |  i i | ƒ Pq# q# Wd  S(   Ns   Deleting alert: R   Ro   Rp   (   RD   RO   t   remove(   R   R   R   Rs   Ro   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR   ,  s    

 
c      
   C   s†   t  |  i d ƒ i i ƒ  } z& | ~ } t i |  i | d t ƒWd  QXx7 |  i D], } |  i	 | d | d | d | d ƒ qR Wd  S(   Nt   wt   default_flow_styleR>   R?   R@   RA   (
   RI   RG   RJ   RK   RL   t   dumpRO   R
   RC   Rl   (   R   RQ   RR   t   hst(    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyt
   saveToFile8  s
    )
 c         C   s9   |  d } |  d }  |  d } |  d }  d | | |  f S(   Ni  i<   s   %02d:%02d:%02d(    (   t   secst   hrt   min(    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR|   A  s
    



(   R9   R:   RZ   R   R<   RH   Rl   R   Rm   R   R   R   Rƒ   t   staticmethodR|   (    (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR;   ª   s   											t   AlertPolicyForCMS5Xc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s-   /opt/tandbergtv/cms/conf/logstash/alerts.yamlc         C   s   t  i |  d  ƒ d  S(   N(   R;   R   R   (   R   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR   K  s    c         C   s—   d t  t i ƒ  ƒ |  _ |  i d  d  d  d  ƒ t |  i d ƒ i i ƒ  } z% | ~ } t	 i
 | i ƒ  ƒ |  _ Wd  QX|  i d  j o g  |  _ n d  S(   Ns   /tmp/alerts.%s.yamlRB   (   RD   RE   RF   RG   RH   R   RI   RJ   RK   RL   RM   RN   RO   (   R   R=   RQ   RR   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyR<   N  s    )c         C   s/   d t  i |  i f GHt i t  i |  i ƒ d  S(   Ns/   Start to copy alert configuration file %s to %s(   Rˆ   RZ   RG   R`   Ra   (   R   Rc   Rd   R@   Re   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyRH   Y  s    c      	   C   sn   t  |  i d ƒ i i ƒ  } z& | ~ } t i |  i | d t ƒWd  QXt i	 |  i t
 i ƒ d t
 i GHd  S(   NR   R€   s#   Saved alerts to configuration file (   RI   RG   RJ   RK   RL   R   RO   R
   R`   Ra   Rˆ   RZ   (   R   RQ   RR   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyRƒ   ]  s    )(   R9   R:   RZ   R   R<   RH   Rƒ   (    (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyRˆ   I  s
   			c          C   sÉ   t  h d d 6d d 6d d 6d d 6g ƒ }  |  i h d d	 6d
 d 6d d 6d d 6d d 6d d 6d d 6ƒ |  i h d d	 6d
 d 6d d 6d d 6d d 6d d 6d d 6ƒ |  i h d d	 6ƒ |  i ƒ  d  S(   Ns	   127.0.0.1R>   t   rootR?   t   root1234R@   RA   s   Ingest Validation WarningR   s"   .*validationMessages.*= Warning:.*Rv   i   R    i<   R!   id   R"   R#   ic  R&   s   EPGManager Translation Alerts    EPGManager Validation Rule Abort(   R;   R   R   R   Rƒ   (   RT   (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyt   maind  s    +''t   __main__(    (   RE   R   R   R`   RL   RW   R    R   R;   Rˆ   R‹   R9   (    (    (    s`   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/Alerts.pyt   <module>   s    Ÿ	