ó
ÍÿŠTc           @   sÊ   d  d l  Z  d  d l Z d  d l m Z m Z d  d l m Z d  d l m Z d  d l	 m
 Z
 m Z d  d l m Z d  d l m Z d  d l m Z d	 Z d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   inlineCallbackst   returnValue(   t   fetch_async(   t	   read_file(   t   LSB_RELEASE_FILENAMEt   parse_lsb_release(   t   fetch_ec2_meta_data(   t   get_fqdn(   t   MonitorPlugini   t   DistributionInfoErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyR	      s   t   ComputerInfoc           B   s¶   e  Z d  Z d Z d Z e d e d e d „ Z d „  Z	 e
 d „ Z e
 d „ Z e e
 d	 „ ƒ Z e
 d
 „ Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „  ƒ Z d „  Z RS(   s7   Plugin captures and reports basic computer information.s   computer-infot   computers   /proc/meminfot   /c         C   sC   | |  _  | |  _ | |  _ | |  _ d  |  _ d |  _ | |  _ d  S(   Ni    (   t	   _get_fqdnt   _meminfo_filenamet   _lsb_release_filenamet
   _root_patht   Nonet   _cloud_instance_metadatat   _cloud_retriest   _fetch_async(   t   selfR   t   meminfo_filenamet   lsb_release_filenamet	   root_pathR   (    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyt   __init__   s    						c         C   sk   t  t |  ƒ j | ƒ | j j |  _ |  j d |  j t ƒ |  j d |  j	 t ƒ |  j d |  j
 t ƒ d  S(   Ns   computer-infos   distribution-infos   cloud-instance-metadata(   t   superR   t   registert   configt   annotations_patht   _annotations_patht   call_on_acceptedt   send_computer_messaget   Truet   send_distribution_messaget$   send_cloud_instance_metadata_message(   R   t   registry(    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyR   %   s    			c         C   sO   |  j  ƒ  } | rK d | d <t j d ƒ |  j j j | |  j d | ƒn  d  S(   Ns   computer-infot   types,   Queueing message with updated computer info.t   urgent(   t   _create_computer_info_messaget   loggingt   infoR&   t   brokert   send_messaget   _session_id(   R   R(   t   message(    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyR"   /   s    
c         C   sO   |  j  ƒ  } | rK d | d <t j d ƒ |  j j j | |  j d | ƒn  d  S(   Ns   distribution-infoR'   s0   Queueing message with updated distribution info.R(   (   t!   _create_distribution_info_messageR*   R+   R&   R,   R-   R.   (   R   R(   R/   (    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyR$   7   s    
c         c   sP   |  j  ƒ  V} | rL d | d <t j d ƒ |  j j j | |  j d | ƒn  d  S(   Ns   cloud-instance-metadataR'   s6   Queueing message with updated cloud instance metadata.R(   (   t'   _create_cloud_instance_metadata_messageR*   R+   R&   R,   R-   R.   (   R   R(   R/   (    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyR%   ?   s    
c         C   sR   |  j  j } | j d |  j | ƒ | j d |  j | ƒ | j d |  j | ƒ d  S(   Ns   computer-infos   distribution-infos   cloud-instance-metadata(   R&   R,   t   call_if_acceptedR"   R$   R%   (   R   R(   R,   (    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyt   exchangeI   s    			c         C   sÔ   i  } |  j  | d |  j ƒ  ƒ |  j ƒ  \ } } |  j  | d | ƒ |  j  | d | ƒ i  } t j j |  j ƒ r´ x? t j |  j ƒ D]( } t t j j	 |  j | ƒ ƒ | | <q… Wn  | rÐ |  j  | d | ƒ n  | S(   Nt   hostnames   total-memorys
   total-swapt   annotations(
   t   _add_if_newR   t   _get_memory_infot   ost   patht   existsR    t   listdirR   t   join(   R   R/   t   total_memoryt
   total_swapR5   t   key(    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyR)   S   s    &c         C   s<   | |  j  j | ƒ k r8 |  j  j | | ƒ | | | <n  d  S(   N(   t   _persistt   gett   set(   R   R/   R?   t   value(    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyR6   c   s    c         C   s?   |  j  ƒ  } | |  j j d ƒ k r; |  j j d | ƒ | Sd  S(   Ns   distribution-info(   t   _get_distribution_infoR@   RA   RB   R   (   R   R/   (    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyR0   h   s
    c         C   s®   i  } t  |  j ƒ } xr | D]j } | d k r | j d ƒ } | d } | d	 k r† t | d j ƒ  j d ƒ d ƒ } | | | <q† q q W| j ƒ  | d d | d d f S(
   s>   Get details in megabytes and return a C{(memory, swap)} tuple.s   
t   :i    t   MemTotalt	   SwapTotali   t    i   (   RF   RG   (   t   openR   t   splitt   intt   stript   close(   R   R/   t   filet   linet   partsR?   RC   (    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyR7   o   s    
#
c         C   s    i  } | j  t |  j ƒ ƒ | S(   s#   Get details about the distribution.(   t   updateR   R   (   R   R/   (    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyRD   }   s    c         c   sN   d } |  j d k r@ |  j t k  r@ |  j ƒ  V|  _ |  j } n  t | ƒ d S(   s0   Fetch cloud metadata and insert it in a message.N(   R   R   R   t   METADATA_RETRY_MAXt   _fetch_ec2_meta_dataR   (   R   R/   (    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyR1   ƒ   s    c            sd   ˆ  j  d k r t j d ƒ n  t ˆ  j ƒ } ‡  f d †  } d „  } | j | ƒ | j | ƒ | S(   s+   Fetch information about the cloud instance.i    s   Querying cloud meta-data.c            s<   ˆ  j  d 7_  ˆ  j  t k r8 t j d |  j ƒ  ƒ n  d  S(   Ni   s    No cloud meta-data available. %s(   R   RR   R*   R+   t   getErrorMessage(   t   error(   R   (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyt   log_no_meta_data_found“   s    	c         S   s   t  j d ƒ |  S(   Ns   Acquired cloud meta-data.(   R*   R+   (   t   result(    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyt   log_success™   s    (   R   R*   R+   R   R   t   addCallbackt
   addErrback(   R   t   deferredRV   RX   (    (   R   sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyRS      s    	(   R
   R   t   __doc__t   persist_namet   scopeR   R   R   R   R   t   FalseR"   R$   R    R%   R3   R)   R6   R0   R7   RD   R1   RS   (    (    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyR      s(   		
	
					
(   R8   R*   t   twisted.internet.deferR    R   t   landscape.lib.fetchR   t   landscape.lib.fsR   t   landscape.lib.lsb_releaseR   R   t   landscape.lib.cloudR   t   landscape.lib.networkR   t   landscape.monitor.pluginR   RR   t	   ExceptionR	   R   (    (    (    sB   /usr/lib/python2.7/dist-packages/landscape/monitor/computerinfo.pyt   <module>   s   