ó
ÍÿŠTc           @   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(   sw   Reader parses C{/var/log/wtmp} and/or C{/var/run/utmp} files.

    @file: Initialize the reader with an open file.
    c         C   s"   | |  _  t j t j ƒ |  _ d  S(   N(   t   _fileR   t   calcsizeR   R   t   _struct_length(   R    R   (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyR#   4   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_info8   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,   B   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#   P   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_timesV   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_timej   s    N(   R$   R%   R0   R;   R<   R#   RC   RF   (    (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyR1   L   s   	t   ComputerUptimec           B   sP   e  Z d  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-uptimet   computers   /var/log/wtmpc         C   s   t  |  _ | |  _ d  S(   N(   R+   t
   _first_runt
   _wtmp_file(   R    t	   wtmp_file(    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyR#   v   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_onRL   t   call_on_acceptedR+   (   R    t   registry(    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyRN   z   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(	   RR   t   brokerRI   RJ   t   ost   patht   isfilet   call_if_acceptedt   send_message(   R    t   urgentRS   R5   (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyRL   €   s    		c         C   sW   |  j  | ƒ } d | k s' d | k rS d | d <|  j j j | |  j d | ƒn  d  S(   Ns   shutdown-timess   startup-timess   computer-uptimet   typeRY   (   t   _create_messageRR   RS   RX   t   _session_id(   R    R5   RY   t   message(    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyRX   •   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.pyR[   œ   s     		(   R$   R%   R&   t   persist_namet   scopeR#   RN   t   FalseRL   RX   R[   (    (    (    sD   /usr/lib/python2.7/dist-packages/landscape/monitor/computeruptime.pyRG   p   s   	(   RE   R    RT   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   
$