o
    va9;                     @   s@  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dlmZ ddlmZ dd	lZd
ddddddddddddddddddddddddddddddddddddddddddddddddddd d!dd"Zd#d$d%d&dd'idd'id(d)d*d+d,Zd-d.d/d0d1d2d/d0gZG d3d4 d4eZd	S )5zCPU plugin.    )logger)getTimeSinceLastUpdate)iterkeys)cpu_percent)LINUX)Plugin)GlancesPluginNz)Sum of all CPU percentages (except idle).percent)descriptionunitzrpercent time spent in kernel space. System CPU time is the time spent running code in the Operating System kernel.zCPU percent time spent in user space. User CPU time is the time spent on the processor running your program's code (or code in libraries).zP*(Linux)*: percent time spent by the CPU waiting for I/O operations to complete.zpercent of CPU used by any program. Every program or task that runs on a computer system occupies a certain amount of processing time on the CPU. If the CPU has completed all tasks it is idle.z*(Linux and BSD)*: percent time spent servicing/handling hardware/software interrupts. Time servicing interrupts (hardware + software).z*(Unix)*: percent time occupied by user level processes with a positive nice value. The time the CPU has spent running users' processes that have been *niced*.z}*(Linux)*: percentage of time a virtual CPU waits for a real CPU while the hypervisor is servicing another virtual processor.znumber of context switches (voluntary + involuntary) per second. A context switch is a procedure that a computer's CPU (central processing unit) follows to change from one task (or process) to another while ensuring that the tasks do not conflict.ZnumberTKZctx_sw)r
   r   ZrateZ
min_symbolZ
short_namez number of interrupts per second.ZinterzOnumber of software interrupts per second. Always set to 0 on Windows and SunOS.Zsw_intz8number of system calls per second. Always 0 on Linux OS.zTotal number of CPU core.z$Number of seconds since last update.Zseconds)totalsystemuseriowaitidleirqnicestealctx_switches
interruptssoft_interruptssyscallscpucoretime_since_updatez1.3.6.1.4.1.2021.11.9.0z1.3.6.1.4.1.2021.11.10.0z1.3.6.1.4.1.2021.11.11.0)r   r   r   z1.3.6.1.2.1.25.3.3.1.2z1.3.6.1.4.1.789.1.2.1.3.0z1.3.6.1.4.1.789.1.2.1.5.0z1.3.6.1.4.1.789.1.2.1.6.0)r   r   r   )defaultwindowsesxiZnetappr   zUser CPU usage%)namer
   Zy_unitr   zSystem CPU usagec                       s\   e Zd ZdZd fdd	Zejejdd Zdd Z	d	d
 Z
 fddZdddZ  ZS )r   zxGlances CPU plugin.

    'stats' is a dictionary that contains the system-wide CPU utilization as a
    percentage.
    Nc                    sV   t t| j||ttd d| _zt| jd d | _	W dS  t
y*   d| _	Y dS w )zInit the CPU plugin.)argsconfigitems_history_listfields_descriptionT)r    log   N)superr   __init__r"   r#   Zdisplay_curse
CorePluginr    updatenb_log_core	Exception)selfr    r!   	__class__ =/usr/lib/python3/dist-packages/glances/plugins/glances_cpu.pyr'   ~   s   zPlugin.__init__c                 C   s<   | j dkr
|  }n| j dkr|  }n|  }|| _| jS )z(Update CPU stats using the input method.ZlocalZsnmp)Zinput_methodupdate_localupdate_snmpget_init_valuestats)r,   r4   r/   r/   r0   r)      s   



zPlugin.updatec                 C   s   |   }t |d< tjdd}|jD ]	}t||||< qt }td|d< | j	|d< t
| ds<|jD ]}d||< q4n|jD ]}t||d	urUt||t| j| ||< q?|| _|S )
zUpdate CPU stats using psutil.r   g        )intervalZcpur   r   cpu_stats_oldr   N)r3   r   getpsutilcpu_times_percent_fieldsgetattr	cpu_statsr   r*   hasattrr6   )r,   r4   r9   statr<   r/   r/   r0   r1      s$   	





zPlugin.update_localc                 C   s\  |   }| jdv rkz| jt| j dd}W n ty"   |   Y nw d|d< d|d< |D ]}|drH|d  t|d 7  < |d  d	7  < q-|d dkrY|d |d  |d< d
|d  |d< d
|d  |d< |S z| jt| j d}W n ty   | jtd d}Y nw |d dkr|   | jS t	|D ]
}t|| ||< qd
|d  |d< |S )zUpdate CPU stats using SNMP.)r   r   T)snmp_oidZbulkr   r*   r   r	   z	percent.3r%   d   r   )r?   r    )
r3   Zshort_system_nameZget_stats_snmpr?   KeyErrorreset
startswithfloatr4   r   )r,   r4   r<   ckeyr/   r/   r0   r2      sJ   




zPlugin.update_snmpc                    s   t t|   dD ]}|| jv r| j| j| |d| j| d< q	dD ]}|| jv r8| j| j| |d| j| d< q"dD ]}|| jv rW| j| j| d| jd  |d| j| d< q;d	D ]}|| jv rhd
| j| d< qZdS )zUpdate stats views.)r   r   r   r   )header
decoration)r   )r   r@   r   )ZmaximumrH   )r   r   r   r   r   r   r   r   ToptionalN)r&   r   update_viewsr4   Zget_alert_logZviewsZ	get_alert)r,   rG   r-   r/   r0   rK     s$   


*
zPlugin.update_viewsc           	   	   C   sn  g }| j r| jjs|  r|S d| j v}dd}|| |d | d}| d}|du s4|du r7d}n|| }d| |}|| | d| j d	 }|| || j	d	d
d d| j v r|sdd}|| j|| j	dddd d| j d }|| j|| j	dddd |
| jdddd ||   |s|
| jddd nd| j v r|
| jddd |
| jdddd |
| jdddd ||   |s|
| jddd n
|
| jddd |
| jdddd |
| jdddd ||   |
| jddd |
| jdddd ts5|
| jdddd |S )z%Return the list to display in the UI.r   z{}ZCPUZTITLEr   Nz {:4}z{:5.1f}%r   rI   )rG   optionr   z  {:8}zidle:rJ   )rJ   r      z  )widthrH   )rN   r   r   Zcorer   r   r   r   r   )r4   r    ZpercpuZis_disabledformatappendZcurse_add_lineZ	get_trendZ	trend_msgZ	get_viewsextendZcurse_add_statZcurse_new_liner   )	r,   r    Z	max_widthretZidle_tagmsgZ
trend_userZtrend_systemZ	trend_cpur/   r/   r0   	msg_curse  s\   





zPlugin.msg_curse)NN)__name__
__module____qualname____doc__r'   r   Z_check_decoratorZ_log_result_decoratorr)   r1   r2   rK   rT   __classcell__r/   r/   r-   r0   r   w   s    21r   )rX   Zglances.loggerr   Zglances.timerr   Zglances.compatr   Zglances.cpu_percentr   Zglances.globalsr   Zglances.plugins.glances_corer   r(   Zglances.plugins.glances_pluginr   r8   r#   r?   r"   r/   r/   r/   r0   <module>   s   	=