ó
½-'Nc           @   sÔ   d  d l  Z  d  d l Z d  d l Z d  d l m Z d  d l m Z d e f d „  ƒ  YZ d d d „  ƒ  YZ d d d	 „  ƒ  YZ	 d
 d d „  ƒ  YZ
 d d d „  ƒ  YZ d e	 f d e f d e
 f d e f g Z d S(   iÿÿÿÿN(   t   PluginConfigError(   t   MonitorPlugint   ProcessorInfoc           B   sq   e  Z d  Z d Z d Z d d d d „ Z d „  Z d „  Z d „  Z	 e
 d „ Z e
 d	 „ Z d
 „  Z d „  Z RS(   sú  Plugin captures information about the processor(s) in this machine.

    This plugin runs once per client session.  When processor
    information is retrieved it's compared against the last known
    processor information, which is saved in persistent storage.  A
    message is only put on the message queue if the latest processor
    information differs from the last known processor information.

    The information available from /proc/cpuinfo varies per platform.
    For example, an Apple PowerMac Dual G5 doesn't contain a vendor ID
    and provides the processor name in the 'cpu' field, as opposed to
    the 'model name' field used on x86-based hardware.  For reasons
    such as this, the schema of the data reported by this plugin is
    flexible.  Only 'processor-id' and 'model' are guaranteed to be
    present.

    In order to deal with the vagaries of parsing /proc/cpu
    information on the various platforms we support, message
    generation is deferred to per-platform message factories.
    s   processor-infoi   s   /proc/cpuinfoc         C   sJ   | |  _  | |  _ | d k r1 t j ƒ  d } n  |  j | | ƒ |  _ d S(   s:   Initialize plugin with starting delay and source filename.i   N(   t   _delayt   _source_filenamet   Nonet   ost   unamet   _create_cpu_info_readert   _cpu_info_reader(   t   selft   delayt   machine_namet   source_filename(    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyt   __init__#   s    			c         C   sU   x> t  D]6 } t j | d ƒ } | j | ƒ r | d | ƒ Sq Wt d | ƒ ‚ d S(   sA   Return a message factory suitable for the specified machine name.i    i   s2   A processor info reader for '%s' is not available.N(   t   message_factoriest   ret   compilet   matchR    (   R
   R   R   t   pairt   regexp(    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR   /   s    c         C   sL   t  t |  ƒ j | ƒ |  j j j |  j |  j ƒ |  j d |  j	 t
 ƒ d S(   s8   Register this plugin with the specified plugin registry.s   processor-infoN(   t   superR   t   registert   registryt   reactort
   call_laterR   t   runt   call_on_acceptedt   send_messaget   True(   R
   R   (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR   :   s    c         C   s   i d d 6|  j  j ƒ  d 6S(   s6   Retrieve processor information and generate a message.s   processor-infot   typet
   processors(   R	   t   create_message(   R
   (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR    @   s    
c         C   sØ   t  } |  j ƒ  } x | d D]„ } d t | d ƒ f } |  j j | ƒ } | d  k rv i  } |  j | | ƒ t } q |  j | | ƒ r |  j | | ƒ t } q q W| rÔ t	 j
 d ƒ |  j j j | d | ƒn  d  S(   NR   t	   processors   processor-ids-   Queueing message with updated processor info.t   urgent(   t   FalseR    t   strt   _persistt   getR   t   _updateR   t   _has_changedt   loggingt   infoR   t   brokerR   (   R
   R"   t   dirtyt   messageR!   t   keyt   cached_processor(    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR   E   s    	c         C   s    |  j  j j d |  j | ƒ d S(   s1   Create a message and put it on the message queue.s   processor-infoN(   R   R+   t   call_if_acceptedR   (   R
   R"   (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR   Y   s    c         C   s\   | d | d k r t  S| d | j d d ƒ k r8 t  S| d | j d d ƒ k rX t  St S(   s>   Returns true if processor details changed since the last read.t   modelt   vendort    t
   cache_sizes
   cache-sizeiÿÿÿÿ(   R   R&   R#   (   R
   R!   R-   (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR(   ^   s    c         C   so   | d | d <| d | d <| j  d d ƒ | d <| j  d d ƒ | d <|  j j d	 t | d ƒ f | ƒ d
 S(   s1   Update the processor details with current values.s   processor-idt   idR1   s
   cache-sizeiÿÿÿÿR4   R2   R3   R!   N(   R&   R%   t   setR$   (   R
   R!   R-   (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR'   k   s    N(   t   __name__t
   __module__t   __doc__t   persist_nameR   t   run_intervalR   R   R   R    R#   R   R   R(   R'   (    (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR   	   s   				t   PowerPCMessageFactoryc           B   s    e  Z d  Z d „  Z d „  Z RS(   s@   Factory for ppc-based processors provides processor information.c         C   s   | |  _  d S(   s/   Initialize reader with filename of data source.N(   R   (   R
   R   (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR   x   s    c         C   sÀ   g  } t  |  j ƒ } z™ d } xŒ | D]„ } | j d d ƒ } | d j ƒ  } | d k r† i t | d j ƒ  ƒ d 6} | j | ƒ q% | d k r% | d j ƒ  | d <q% q% WWd | j ƒ  X| S(	   s;   Returns a list containing information about each processor.t   :i   i    R!   s   processor-idt   cpuR1   N(   t   openR   R   t   splitt   stript   intt   appendt   close(   R
   R   t   filet   currentt   linet   partsR.   (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR    |   s    (   R7   R8   R9   R   R    (    (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR<   u   s   	t   ARMMessageFactoryc           B   s    e  Z d  Z d „  Z d „  Z RS(   s@   Factory for arm-based processors provides processor information.c         C   s   | |  _  d S(   s/   Initialize reader with filename of data source.N(   R   (   R
   R   (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR   –   s    c   	      C   så   g  } t  |  j ƒ } z¾ t j d ƒ } i  } xŒ | D]„ } | j | j ƒ  ƒ } | r4 | j d ƒ } | j d ƒ } | d k r– d | d <| | d <q¸ | d k r¸ t | ƒ | d	 <q¸ q4 q4 W| rÒ | j | ƒ n  Wd
 | j	 ƒ  X| S(   s;   Returns a list containing information about each processor.s    (?P<key>.*?)\s*:\s*(?P<value>.*)R.   t   valuet	   Processori    s   processor-idR1   s
   Cache sizes
   cache-sizeN(
   R?   R   R   R   R   RA   t   groupRB   RC   RD   (	   R
   R   RE   R   RF   RG   R   R.   RJ   (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR    š   s&    
(   R7   R8   R9   R   R    (    (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyRI   “   s   	t   SparcMessageFactoryc           B   s    e  Z d  Z d „  Z d „  Z RS(   sB   Factory for sparc-based processors provides processor information.c         C   s   | |  _  d S(   s/   Initialize reader with filename of data source.N(   R   (   R
   R   (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR   ¼   s    c         C   sö   g  } d	 } t |  j ƒ } zÉ t j d ƒ } x³ | D]« } | j d d ƒ } | d j ƒ  } | d k r{ | d j ƒ  } q4 | j | ƒ r4 t j d ƒ j | ƒ j	 ƒ  \ } }	 i t
 | | |	 !ƒ d 6| d 6}
 | j |
 ƒ q4 q4 WWd	 | j ƒ  X| S(
   s;   Returns a list containing information about each processor.s   CPU(\d{1})+R=   i   i    R>   s   \d+s   processor-idR1   N(   R   R?   R   R   R   R@   RA   R   t   searcht   spanRB   RC   RD   (   R
   R   R1   RE   R   RG   RH   R.   t   startt   endR-   (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR    À   s"    $
(   R7   R8   R9   R   R    (    (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyRM   ¹   s   	t   X86MessageFactoryc           B   s    e  Z d  Z d „  Z d „  Z RS(   s@   Factory for x86-based processors provides processor information.c         C   s   | |  _  d S(   s/   Initialize reader with filename of data source.N(   R   (   R
   R   (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR   Ý   s    c         C   s  g  } t  |  j ƒ } zõ d } xè | D]à } | j d d ƒ } | d j ƒ  } | d k r† i t | d j ƒ  ƒ d 6} | j | ƒ q% | d k r© | d j ƒ  | d <q% | d k rÌ | d j ƒ  | d	 <q% | d
 k r% | d j ƒ  } t | d j ƒ  ƒ | d <q% q% WWd | j ƒ  X| S(   s;   Returns a list containing information about each processor.R=   i   i    R!   s   processor-idt	   vendor_idR2   s
   model nameR1   s
   cache sizes
   cache-sizeN(   R?   R   R   R@   RA   RB   RC   RD   (   R
   R   RE   RF   RG   RH   R.   t   value_parts(    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyR    á   s&    %(   R7   R8   R9   R   R    (    (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyRR   Ú   s   	s   arm*s   ppc(64)?s	   sparc[64]s   i[3-7]86|x86_64(    (    (    (    (   R)   R   R   t   landscape.pluginR    t   landscape.monitor.pluginR   R   R<   RI   RM   RR   R   (    (    (    sC   /usr/lib/python2.7/dist-packages/landscape/monitor/processorinfo.pyt   <module>   s   l&!#			