ó
ÍÿŠTc           @   s»   d  d l  Z  d  d l Z d  d l Z d  d l m Z d  d l m Z d  d l m Z d  d l	 m
 Z
 y# d  d l m Z e e d ƒ Z Wn e k
 r  e Z n Xd e
 f d	 „  ƒ  YZ d S(
   iÿÿÿÿN(   t   threads(   t   Accumulator(   t   CoverageMonitor(   t   MonitorPlugin(   t   Radost   get_cluster_statst	   CephUsagec           B   sƒ   e  Z d  Z d Z d Z d Z d d e j d „ Z d „  Z	 d „  Z
 e d „ Z e d	 „ Z d
 „  Z d „  Z d „  Z d „  Z RS(   s  
    Plugin that captures Ceph usage information. This only works if the client
    runs on one of the Ceph monitor nodes, and noops otherwise.

    The plugin requires the 'python-ceph' package to be installed, which is the
    case on a standard "ceph" charm deployment.
    The landscape-client charm should join a ceph-client relation with the ceph
    charm, which will crete a keyring and config file for the landscape-client
    to consume in <data_path>/ceph-client/ceph.landscape-client.conf. It
    contains the following:

    [global]
    auth supported = cephx
    keyring = <keyring-file>
    mon host = <ip>:6789

    The configured keyring can be generated with:

    ceph-authtool <keyring-file> --create-keyring
        --name=client.landscape-client --add-key=<key>
    s
   ceph-usaget   storagei   i<   c         C   sL   t  |  _ t |  _ | |  _ | |  _ g  |  _ d  |  _ | |  _	 d  |  _
 d  S(   N(   t   Truet   activet	   has_radost
   _has_radost	   _intervalt   _monitor_intervalt   _ceph_usage_pointst   Nonet   _ceph_ring_idt   _create_timet   _ceph_config(   t   selft   intervalt   monitor_intervalt   create_time(    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/cephusage.pyt   __init__/   s    							c         C   sê   t  t |  ƒ j | ƒ t j j |  j j j d d ƒ |  _	 t
 |  j |  j ƒ |  _ t |  j d d d |  j ƒ|  _ |  j j j |  j |  j ƒ |  j j j |  j |  j j ƒ |  j j j d |  j j d d ƒ|  j d	 |  j t ƒ d  S(
   Ns   ceph-clients   ceph.landscape-client.confgš™™™™™é?s   Ceph usage snapshotR   t   stopt   priorityiÐ  s
   ceph-usage(   t   superR   t   registert   ost   patht   joint   registryt   configt	   data_pathR   R   t   _persistR   t   _accumulateR   R   t   _monitort   reactort
   call_everyt   runR   t   logt   call_ont   call_on_acceptedt   send_messageR   (   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/cephusage.pyR   :   s    	"c         C   s;   |  j  } |  j } g  |  _  i d d 6| d 6g  d 6| d 6S(   Ns
   ceph-usaget   types   ring-ids   ceph-usagess   data-points(   R   R   (   R   t   ceph_pointst   ring_id(    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/cephusage.pyt   create_messageK   s    			
c         C   sF   |  j  ƒ  } | d rB | d rB |  j j j | |  j d | ƒn  d  S(   Ns   ring-ids   data-pointst   urgent(   R/   R   t   brokerR+   t   _session_id(   R   R0   t   message(    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/cephusage.pyR+   T   s    c         C   s    |  j  j j d |  j | ƒ d  S(   Ns
   ceph-usage(   R   R1   t   call_if_acceptedR+   (   R   R0   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/cephusage.pyt   exchangeZ   s    c         C   sC   |  j  ƒ  s d  S|  j j ƒ  t j |  j ƒ } | j |  j ƒ | S(   N(   t   _should_runR$   t   pingR    t   deferToThreadt   _perform_rados_callt   addCallbackt   _handle_usage(   R   t   deferred(    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/cephusage.pyR'   ^   s    c         C   s]   |  j  s t S|  j s0 t j d ƒ t |  _  t S|  j d k sU t j j	 |  j ƒ rY t St
 S(   s.   Returns whether or not this plugin should run.sG   This machine does not appear to be a Ceph machine. Deactivating plugin.N(   R	   t   FalseR   t   loggingt   infoR   R   R   R   t   existsR   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/cephusage.pyR6   g   s    			%c         C   sa   t  d |  j d d ƒ C } | j ƒ  } |  j d k rW t | j ƒ  d ƒ } | |  _ n  Wd QX| S(   s   The actual Rados interaction.t   conffilet   rados_ids   landscape-clients   utf-8N(   R   R   R   R   R   t   unicodet   get_fsid(   R   t   clustert   cluster_statst   fsid(    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/cephusage.pyR9   y   s    c   
      C   sÀ   d d d g } t  |  j ƒ  ƒ } g  } xH | D]@ \ } } | | d } |  j | | d | ƒ } | j | ƒ q. Wt | ƒ s‚ d	 S| d
 g }	 |	 j d „  | Dƒ ƒ |  j j t |	 ƒ ƒ d	 S(   s†   A method to use as callback to the rados interaction.

        Parses the output and stores the usage data in an accumulator.
        t   totalt   kbt   availt   kb_availt   usedt   kb_usedi   s   usage.%sNi    c         s   s   |  ] } t  | d  ƒ Vq d S(   i   N(   t   int(   t   .0t
   step_value(    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/cephusage.pys	   <genexpr>˜   s    (   RH   RI   (   RJ   RK   (   RL   RM   (   RN   R   R#   t   appendt   allt   extendR   t   tuple(
   R   RF   t	   names_mapt	   timestampt   step_valuest   namet   keyt   valueRP   t   point(    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/cephusage.pyR;   …   s    Ni  (   t   __name__t
   __module__t   __doc__t   persist_namet   scopeR   t   run_intervalt   timeR   R   R/   R=   R+   R5   R'   R6   R9   R;   (    (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/cephusage.pyR      s   
							(   R>   Rb   R   t   twisted.internetR    t   landscape.accumulateR   t   landscape.lib.monitorR   t   landscape.monitor.pluginR   t   radosR   t   hasattrR
   t   ImportErrorR=   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/cephusage.pyt   <module>   s   
