o
    va-                     @   s0  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 dg ddd	d
gddddgddddgddddgddddgddddgddddgddddgddg	Z
dddddiddididdididd
dd iddd!idd"idididdidd#iddidiZd$d% ZG d&d' d'e	Zd(S ))zAlert plugin.    )datetime)logger)glances_events)glances_thresholds)GlancesPluginz%No warning or critical alert detected)msg
thresholdsthresholds_minzHigh CPU user modeZcpu_user   zHigh CPU kernel usageZ
cpu_systemzHigh CPU I/O waitingZ
cpu_iowaitzALarge CPU stolen time. System running the hypervisor is too busy.Z	cpu_stealzHigh CPU niced valueZ	cpu_nicedz'System overloaded in the last 5 minutesloadzHigh swap (paging) usageZmemswapzHigh memory consumptionZmemZ_msgzMemory issuez
Swap issue)Z_yesZ_noz	I/O issueZ	cpu_totalzCPU issue with user process(es)zJCPU issue with stolen time. System running the hypervisor may be too busy.z!CPU issue with system process(es)zExternal issuec                     sd   t   tD ]} t fdd| d D | d< qttdd d}|d |d kr,|d	 S td
 d	 S )zwParse the decision tree and return the message.

    Note: message corresponding to the current threasholds values
    c                    s    g | ]}| v r |   qS  )value).0tZcurrent_thresholdsr   ?/usr/lib/python3/dist-packages/glances/plugins/glances_alert.py
<listcomp>   s     z"global_message.<locals>.<listcomp>r   weightc                 S   s   | d S )Nr   r   )dr   r   r   <lambda>   s    z global_message.<locals>.<lambda>)keyr	   r   r   )r   gettreesummax)iZthemaxr   r   r   global_message   s    r   c                       s>   e Zd ZdZd fdd	Zdd ZdddZdd
dZ  ZS )Pluginz1Glances alert plugin.

    Only for display.
    Nc                    s&   t t| j||g d d| _d| _dS )zInit the plugin.)argsconfigZstats_init_valueTZbottomN)superr   __init__Zdisplay_curseZalign)selfr   r   	__class__r   r   r!      s   
zPlugin.__init__c                 C   s   t  | _dS )z7Nothing to do here. Just return the global glances_log.N)r   r   stats)r"   r   r   r   update   s   zPlugin.updatec                 C   s  g }| j r	|  r|S || t d | j D ]}||   tt|d }|| | |d dkrJd	t|d t|d  }nd}|| | || d |d dkrud	|d |d	 }|| | nt|d	 }|| j||d d
 | j
|d |d ddrd	|d }nd	|d |d |d }|| | ddd |d D }|dkrd	|}|| | q|S )z2Return the dict to display in the curse interface.ZTITLEr      z ({})z
 (ongoing)z - z{} on {}r
      )Z
decoration      g?)	tolerancez	 ({:.1f})   z$ (Min:{:.1f} Mean:{:.1f} Max:{:.1f})z, c                 S   s   g | ]}|d  qS )namer   )r   pr   r   r   r      s    z$Plugin.msg_curse.<locals>.<listcomp>	    z: {})r%   Zis_disabledappendZcurse_add_liner   Zcurse_new_linestrr   Zfromtimestampformatapprox_equaljoin)r"   r   Z	max_widthretZalertr   Ztop_processr   r   r   	msg_curse   s@   

zPlugin.msg_curse        c                 C   sJ   t t| r!t t| r!t|| tt|t|| kS ||kS )z4Compare a with b using the tolerance (if numerical).)r2   intisdigitabsr   )r"   abr+   r   r   r   r4     s    "zPlugin.approx_equal)NN)r8   )	__name__
__module____qualname____doc__r!   r&   r7   r4   __classcell__r   r   r#   r   r      s    
	3r   N)rA   r   Zglances.loggerr   Zglances.eventsr   Zglances.thresholdsr   Zglances.plugins.glances_pluginr   r   Ztree_newr   r   r   r   r   r   <module>   s   
'
!W