ó
~`c           @   sð   d  d l  Z  d d l m Z d d l Td  d l Z d  d l Td  d l m Z d  d l	 j
 Z
 d  d l Z d  d l Z e j j ƒ  Z d d g Z d d	 g Z d
 e j f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z d S(   iÿÿÿÿNi   (   t   base(   t   *(   t   commandst   base_reachable_timet   retrans_times   /run/sysctl.ds   /etc/sysctl.dt   SysctlPluginc           B   sG   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z	 RS(   s.   
	Plugin for applying custom sysctl options.
	c         O   s2   t  t |  ƒ j | | Ž  t |  _ t ƒ  |  _ d  S(   N(   t   superR   t   __init__t   Truet   _has_dynamic_optionsR   t   _cmd(   t   selft   argst   kwargs(    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyR      s    	c         C   s—   t  | _ t | _ |  j | j ƒ } |  j j | i  ƒ | _ t	 | j ƒ d k r‡ t
 j d ƒ |  j | ƒ i  | _ |  j j | ƒ n  | j | _ d  S(   Ni    s0   recovering old sysctl settings from previous run(   t   Falset   _has_dynamic_tuningR   t   _has_static_tuningt   _storage_keyt   namet   _storaget   gett   _sysctl_originalt   lent   logt   infot   _instance_unapply_statict   unsett   optionst   _sysctl(   R   t   instancet   storage_key(    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyt   _instance_init   s    			c         C   s&   |  j  | j ƒ } |  j j | ƒ d  S(   N(   R   R   R   R   (   R   R   R   (    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyt   _instance_cleanup+   s    c         C   s	  x¨ t  | j j ƒ  ƒ D]‘ \ } } t | ƒ } | d  k rN t j d | ƒ q |  j j |  j	 j
 | ƒ ƒ } |  j | | ƒ } | d  k	 r | | j | <t | | ƒ q q W|  j | j ƒ } |  j j | | j ƒ |  j j t j t j ƒ rt j d ƒ t ƒ  n  d  S(   NsD   sysctl option %s will not be set, failed to read the original value.s   reapplying system sysctl(   t   listR   t   itemst   _read_sysctlt   NoneR   t   errort
   _variablest   expandR
   t   unquotet   _process_assignment_modifiersR   t   _write_sysctlR   R   R   t   sett   _global_cfgt   get_boolt   constst   CFG_REAPPLY_SYSCTLt   CFG_DEF_REAPPLY_SYSCTLR   t   _apply_system_sysctl(   R   R   t   optiont   valuet   original_valuet	   new_valueR   (    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyt   _instance_apply_static/   s"    "		c         C   s®   t  } t  } x› t | j j ƒ  ƒ D]„ \ } } t | ƒ } |  j |  j j | ƒ | ƒ } | d  k	 r" |  j	 | |  j
 j | ƒ |  j
 j | ƒ | ƒ t k r¦ t } q¦ q" q" W| S(   N(   R   R!   R   R"   R#   R)   R&   R'   R$   t   _verify_valueR
   t	   remove_wsR   (   R   R   t   ignore_missingt   devicest   retR2   R3   t   curr_val(    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyt   _instance_verify_staticE   s    "6c         C   s7   x0 t  | j j ƒ  ƒ D] \ } } t | | ƒ q Wd  S(   N(   R!   R   R"   R*   (   R   R   t   full_rollbackR2   R3   (    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyR   Q   s    "(
   t   __name__t
   __module__t   __doc__R   R   R    R6   R=   R   R   (    (    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyR      s   					c          C   sÅ   i  }  xm t  D]e } y t j | ƒ } Wn
 q n Xx< | D]4 } | j d ƒ sU q: n  | |  k r: | |  | <q: q: Wq Wx> t |  j ƒ  ƒ D]* } |  | } d | | f } t | ƒ q‰ Wt d ƒ d  S(   Ns   .confs   %s/%ss   /etc/sysctl.conf(   t   SYSCTL_CONFIG_DIRSt   ost   listdirt   endswitht   sortedt   keyst   _apply_sysctl_config_file(   t   filest   dt   flistt   fnamet   path(    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyR1   V   s     
c      	   C   sÀ   t  j d |  ƒ y] t |  d ƒ 7 } x- t | d ƒ D] \ } } t |  | | ƒ q6 WWd  QXt  j d |  ƒ WnK t t f k
 r» } | j t j k r¼ t  j	 d |  t
 | ƒ f ƒ q¼ n Xd  S(   Ns%   Applying sysctl settings from file %st   ri   s.   Finished applying sysctl settings from file %ss.   Error reading sysctl settings from file %s: %s(   R   t   debugt   opent	   enumeratet   _apply_sysctl_config_linet   OSErrort   IOErrort   errnot   ENOENTR%   t   str(   RM   t   ft   linenot   linet   e(    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyRH   i   s    		c         C   sé   | j  ƒ  } t | ƒ d k s> | d d k s> | d d k rB d  S| j d d ƒ } t | ƒ d k r t j d |  | f ƒ d  S| \ } } | j  ƒ  } t | ƒ d k rÆ t j d |  | f ƒ d  S| j  ƒ  } t | | d t ƒd  S(	   Ni    t   #t   ;t   =i   i   s    Syntax error in file %s, line %dR9   (   t   stripR   t   splitR   R%   R*   R   (   RM   RY   RZ   t   tmpR2   R3   (    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyRR   v   s     2		c         C   s   d |  j  d d ƒ S(   Ns   /proc/sys/%st   .t   /(   t   replace(   R2   (    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyt   _get_sysctl_pathˆ   s    c         C   s÷   t  |  ƒ } y‡ t | d ƒ W } d } x; t | ƒ D]- \ } } | d k r4 t j d |  ƒ d  Sq4 W| j ƒ  } Wd  QXt j d |  | f ƒ | SWn] t t	 f k
 rò } | j
 t
 j k rÑ t j d |  ƒ n t j d |  t | ƒ f ƒ d  SXd  S(   NRN   t    i    sG   Failed to read sysctl parameter '%s', multi-line values are unsupporteds&   Value of sysctl parameter '%s' is '%s'sB   Failed to read sysctl parameter '%s', the parameter does not exists(   Failed to read sysctl parameter '%s': %s(   Re   RP   RQ   R   R%   R$   R_   RO   RS   RT   RU   RV   RW   (   R2   RM   RX   RZ   t   iR3   R[   (    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyR#   ‹   s(    				c         C   sÿ   t  |  ƒ } t j j | ƒ t k r9 t j d |  ƒ t SyD t j d |  | f ƒ t	 | d ƒ  } | j
 | ƒ Wd  QXt SWn{ t t f k
 rú } | j t j k rÖ | r¶ t j n t j } | d |  | f ƒ n  t j d |  | t | ƒ f ƒ t SXd  S(   Ns+   Refusing to set deprecated sysctl option %ss%   Setting sysctl parameter '%s' to '%s't   wsI   Failed to set sysctl parameter '%s' to '%s', the parameter does not exists/   Failed to set sysctl parameter '%s' to '%s': %s(   Re   RC   RM   t   basenamet   DEPRECATED_SYSCTL_OPTIONSR   R%   R   RO   RP   t   writeR   RS   RT   RU   RV   RW   (   R2   R3   R9   RM   RX   R[   t   log_func(    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyR*   ¢   s&    			(   t   reRf   R    t
   decoratorst
   tuned.logst   tunedt
   subprocesst   tuned.utils.commandsR   t   tuned.constsR.   RU   RC   t   logsR   R   Rj   RB   t   PluginR   R1   RH   RR   Re   R#   R   R*   (    (    (    s?   /usr/lib/python2.7/site-packages/tuned/plugins/plugin_sysctl.pyt   <module>   s&   

	E					