ó
˝-'Nc           @   sź   d  d l  Z  d  d l m Z d  d l Z d  d l Z d  d l m Z d  d l m Z d d  Z d e	 f d     YZ
 d	 e	 f d
     YZ d e	 f d     YZ d e f d     YZ d S(   i˙˙˙˙N(   t   datetime(   t   to_timestamp(   t   MonitorPluginu   /proc/uptimec         C   s1   t  |  d  j   } | j   \ } } t |  S(   s   
    This parses a file in /proc/uptime format and returns a floating point
    version of the first value (the actual uptime).
    t   r(   t   filet   readlinet   splitt   float(   t   uptime_filet   datat   upt   idle(    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyt
   get_uptime
   s    t	   LoginInfoc           B   s   e  Z d  Z d Z d   Z RS(   s=   Information about a login session gathered from wtmp or utmp.t   hi32s4s32s256shhiiiiiii20sc         C   sŐ   t  j |  j |  } | d |  _ | d |  _ | d j d  |  _ | d j d  |  _ | d j d  |  _ | d j d  |  _	 | d |  _
 | d	 |  _ | d
 |  _ t j | d  |  _ | d |  _ d  S(   Ni    i   i   t    i   i   i   i   i   i   i	   i   (   t   structt   unpackt
   RAW_FORMATt
   login_typet   pidt   stript
   tty_devicet   idt   usernamet   hostnamet   termination_statust   exit_statust
   session_idR    t   utcfromtimestampt
   entry_timet   remote_ip_address(   t   selft   raw_datat   info(    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyt   __init__   s    (   t   __name__t
   __module__t   __doc__R   R#   (    (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyR      s   t   LoginInfoReaderc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s=   Reader parses C{/var/log/wtmp} and/or C{/var/run/utmp} files.c         C   s"   | |  _  t j t j  |  _ d S(   s(   Initialize the reader with an open file.N(   t   _fileR   t   calcsizeR   R   t   _struct_length(   R    R   (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyR#   1   s    	c         c   s,   x% t  r' |  j   } | s Pn  | Vq Wd S(   s2   Returns a generator that yields LoginInfo objects.N(   t   Truet	   read_next(   R    R"   (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyt
   login_info6   s
    	c         C   s>   |  j  j |  j  } | r: t |  |  j k r: t |  Sd S(   s9   Returns login data or None if no login data is available.N(   R(   t   readR*   t   lenR   t   None(   R    R	   (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyR,   @   s    
(   R$   R%   R&   R#   R-   R,   (    (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyR'   .   s   		
t	   BootTimesc           B   s8   e  Z d Z d Z d  d d d  Z d   Z d   Z RS(   s   /var/log/wtmpi    c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t	   _filenamet   _boots_newer_thant   _shutdowns_newer_than(   R    t   filenamet   boots_newer_thant   shutdowns_newer_than(    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyR#   N   s    		c         C   sě   g  } g  } t  t |  j   } |  j |  _ |  j |  _ xŚ | j   D] } | j j	 d  rF t
 | j  } | j d k r¤ | |  j k r¤ | j |  | |  _ qŢ | j d k rŢ | |  j k rŢ | j |  | |  _ qŢ qF qF W| | f S(   Nt   ~t   reboott   shutdown(   R'   R   R2   R3   t
   _last_bootR4   t   _last_shutdownR-   R   t
   startswithR   R   R   t   append(   R    t   reboot_timest   shutdown_timest   readerR"   t	   timestamp(    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyt	   get_timesT   s"    c         C   s5   |  j  d  k r. t t j   t    |  _  n  |  j  S(   N(   R;   R0   t   intt   timeR   (   R    (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyt   get_last_boot_timeh   s    N(   R$   R%   R0   R;   R<   R#   RC   RF   (    (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyR1   J   s   	t   ComputerUptimec           B   sJ   e  Z d  Z d Z d d  Z d   Z e d  Z e d  Z d   Z	 RS(   s1   Plugin reports information about computer uptime.s   computer-uptimes   /var/log/wtmpc         C   s   t  |  _ | |  _ d S(   s   Initialize the plugin.N(   R+   t
   _first_runt
   _wtmp_file(   R    t	   wtmp_file(    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyR#   s   s    	c         C   sF   t  t |   j |  | j j d |  j  |  j d |  j t  d S(   s7   Register this plugin with the specified plugin manager.t   runs   computer-uptimeN(   t   superRG   t   registert   reactort   call_onRK   t   call_on_acceptedR+   (   R    t   registry(    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyRM   x   s    c         C   s   |  j  j } |  j rS |  j d } t j j |  rS | j d |  j | |  qS n  t j j |  j  r | j d |  j |  j |  n  d S(   sJ  Create a message and put it on the message queue.

        The last logrotated file, if it exists, will be checked the
        first time the plugin runs.  This behaviour ensures we don't
        accidentally miss a reboot/shutdown event if the machine is
        rebooted and wtmp is logrotated before the client starts.
        s   .1s   computer-uptimeN(	   RQ   t   brokerRH   RI   t   ost   patht   isfilet   call_if_acceptedt   send_message(   R    t   urgentRR   R5   (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyRK   ~   s    		c         C   sQ   |  j  |  } d | k s' d | k rM d | d <|  j j j | d | n  d  S(   Ns   shutdown-timess   startup-timess   computer-uptimet   typeRX   (   t   _create_messageRQ   RR   RW   (   R    R5   RX   t   message(    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyRW      s    
c         C   sž   i  } g  } g  } |  j  j d d  } |  j  j d d  } t | d | d | } | j   \ } } | r |  j  j d | d  | | d <n  | rş |  j  j d | d  | | d <n  | S(	   s7   Generate a message with new startup and shutdown times.s   last-startup-timei    s   last-shutdown-timeR6   R7   i˙˙˙˙s   startup-timess   shutdown-times(   t   _persistt   getR1   RC   t   set(   R    R5   R[   t   startup_timesR@   t   last_startup_timet   last_shutdown_timet   times(    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyRZ      s     		(
   R$   R%   R&   t   persist_nameR#   RM   t   FalseRK   RW   RZ   (    (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyRG   n   s   	(   RE   R    RS   R   t   landscape.lib.timestampR   t   landscape.monitor.pluginR   R   t   objectR   R'   R1   RG   (    (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyt   <module>   s   
$