o
    va@                     @   s   d Z ddlm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 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	ZdddddiZdddd d!d"dd gZG d#d$ d$eZdS )%zNetwork plugin.    )unicode_literalsN)getTimeSinceLastUpdate)GlancesPlugin)n)loggerzInterface name.string)descriptionunitz Interface alias name (optional).z,The received/input rate (in bit per second).Zbpsz)The sent/output rate (in bit per second).z@The number of bytes received through the interface (cumulative).bytesz<The number of bytes sent through the interface (cumulative).zSMaximum interface speed (in bit per second). Can return 0 on some operating-system.zIs the interface up ?boolz$Number of seconds since last update.Zseconds)	interface_namealiasrxtxcumulative_rxcumulative_txspeedis_uptime_since_updatedefaultz1.3.6.1.2.1.2.2.1.2z1.3.6.1.2.1.2.2.1.10z1.3.6.1.2.1.2.2.1.16)r   r   r   r   zDownload rate per secondzbit/s)namer   Zy_unitr   zUpload rate per secondc                       sN   e Zd ZdZd fdd	Zdd Zejdd Z fd	d
Z	dddZ
  ZS )Pluginz1Glances network plugin.

    stats is a list
    Nc                    sf   t t| j||ttg d d| _|dur|j| jddd| _nd| _ddg| _	| 
  | jd	 dS )
zInit the plugin.)argsconfigitems_history_listfields_descriptionZstats_init_valueTN	hide_zeroF)r   r   r   r   )superr   __init__r   r   Zdisplay_curseZget_bool_valueZplugin_namer   hide_zero_fieldsupdateZrefresh_timerset)selfr   r   	__class__ A/usr/lib/python3/dist-packages/glances/plugins/glances_network.pyr   T   s   

zPlugin.__init__c                 C   s   dS )zReturn the key of the list.r   r%   )r"   r%   r%   r&   get_keyk   s   zPlugin.get_keyc                 C   sX  |   }| jdkrztjdd}W n ty. } ztd| | jW  Y d}~S d}~ww i }zt	 }W n t
yQ } ztd| W Y d}~nd}~ww t| dslz|| _W | jS  ttfyk   Y | jS w td}|}|D ]_}| |s||vrqtzD|| j}|| j}	||	 }
|| j| j }|	| j| j }|| }t|| t|||||	||
||| j|| jd	 |  d
}W n	 ty   Y qtw || qt|| _n| jdkrz| jt| j dd}W n ty   | jtd dd}Y nw t| dsz|| _W n ttfy   Y nw td}|}|D ]}| |r'qzc| jdkrJztt|dd   }W n t!yI   |}Y nw |}t"|| d }t"|| d }	||	 }
|t"| j| d  }|	t"| j| d  }|| }|| |||||	||
|d	}W n ty   Y qw |  |d< || q|| _|| _| jS )zoUpdate network stats using the input method.

        Stats is a list of dict (one dict per interface)
        ZlocalT)Zpernicz+Can not get network interface counters ({})Nz)Can not get network interface status ({})network_oldneti   )r   r   r   r   r   r   r   cumulative_cxcxr   r   keyZsnmp)snmp_oidZbulkr   Zwindows   r   r   )	r   r   r   r   r   r   r   r*   r+   r,   )#Zget_init_valueZinput_methodpsutilZnet_io_countersUnicodeDecodeErrorr   debugformatstatsZnet_if_statsOSErrorhasattrr(   IOErrorUnboundLocalErrorr   Zis_hideZ
bytes_recvZ
bytes_sentr   Z	has_aliasZisupr   r'   KeyErrorappendZget_stats_snmpr-   Zshort_system_namestrbase64Z	b16decodeupper	TypeErrorfloat)r"   r4   ZnetiocounterseZ	netstatusr   Znetwork_newr)   r   r   r*   r   r   r+   Znetstatr   r%   r%   r&   r    p   s   










zPlugin.updatec                    s"  t t|   |   |  D ]}|d dd }t|d |d  d }t|d |d  d }| j||d d	}| j||d
 d	}|dkr\d|v r\|d dkr\| j||d dd}|dkrtd|v rt|d dkrt| j||d dd}|| j|| 	   d d< || j|| 	   d d< qdS )zUpdate stats views.r   :r   r   r      r   Z_rx)headerZ_txZDEFAULTr   )ZcurrentZmaximumrC   
decorationN)
r   r   update_viewsZupdate_views_hiddenZget_rawsplitintZ	get_alertZviewsr'   )r"   iZ
ifrealnameZbps_rxZbps_txZalert_rxZalert_txr#   r%   r&   rE   	  s*   zPlugin.update_viewsc                    s.  g }j r	 r|S |d }djd|d}||d |jrN|jr3dd}|| nFdd	}|| dd
}|| n+|jr_dd}|| ndd}|| dd}||  D ] d v r d du rq}t fddj	D rq} d du r d 
dd }n d }t||krd|| d d  }|jrd}d}nd}d}|jrt d | | }	t d | | }
t d | t d |  | }n?t d  d  | | }	t d   d  | | }
t d  d  | t d   d  |  | }|  dj||d}|| |jrdd|}|| q}d|	}||j   dd!d" d|
}||j   d d!d" q}|S )#z2Return the dict to display in the curse interface.   z
{:{width}}ZNETWORK)widthZTITLEz{:>14}zRx+Txz{:>7}ZRxZTxzRx+Tx/szRx/szTx/sr   Fc                    s$   g | ]}j    |d dqS )Zhiddenitemr,   option)	get_viewsr'   ).0frH   r"   r%   r&   
<listcomp>V  s   $ z$Plugin.msg_curse.<locals>.<listcomp>r   Nr   rA   r   _    rB   br   r   r   r   r   rD   rK   )r4   Zis_disabledr3   r:   Zcurse_add_lineZnetwork_cumulZnetwork_sumZsorted_statsallr   rF   lenZbyteZ	auto_unitrG   Zcurse_new_linerN   r'   )r"   r   Z	max_widthretZname_max_widthmsgZifnameZto_bitr	   r   r   Zsxr%   rQ   r&   	msg_curse*  s   





""


zPlugin.msg_curse)NN)__name__
__module____qualname____doc__r   r'   r   Z_log_result_decoratorr    rE   r[   __classcell__r%   r%   r#   r&   r   N   s    
 !r   )r_   Z
__future__r   r<   Zglances.timerr   Zglances.plugins.glances_pluginr   Zglances.compatr   Zglances.loggerr   r0   r   r-   r   r   r%   r%   r%   r&   <module>   sd   