o
    vba;`                     @   s   d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZmZ G d	d
 d
eZdS )zGlances main class.    N)__version__psutil_version)inputdisableenable)Config)WINDOWS)sort_processes_key_list)loggerLOG_FILENAMEc                   @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd%d d!Z		d&d"d#Zd$S )'GlancesMainz&Main class to manage Glances instance.      Fi  i  glances a  
Examples of use:
  Monitor local machine (standalone mode):
    $ glances

  Display all Glances modules (plugins and exporters) and exit:
    $ glances --module-list

  Monitor local machine with the Web interface and start RESTful server:
    $ glances -w
    Glances web server started on http://0.0.0.0:61208/

  Only start RESTful API (without the WebUI):
    $ glances -w --disable-webui
    Glances API available on http://0.0.0.0:61208/api/

  Monitor local machine and export stats to a CSV file (standalone mode):
    $ glances --export csv --export-csv-file /tmp/glances.csv

  Monitor local machine and export stats to a InfluxDB server with 5s refresh rate (standalone mode):
    $ glances -t 5 --export influxdb

  Start a Glances XML-RPC server (server mode):
    $ glances -s

  Connect Glances to a Glances XML-RPC server (client mode):
    $ glances -c <ip_server>

  Connect Glances to a Glances server and export stats to a StatsD server (client mode):
    $ glances -c <ip_server> --export statsd

  Start the client browser (browser mode):
    $ glances --browser

  Display stats to stdout (one stat per line):
    $ glances --stdout now,cpu.user,mem.used,load

  Display CSV stats to stdout (all stats in one line):
    $ glances --stdout-csv now,cpu.user,mem.used,load

  Disable some plugins (comma separated list):
    $ glances --disable-plugin network,ports

  Enable some plugins (comma separated list):
    $ glances --enable-plugin sensors
c                 C   s   |   | _dS )z"Manage the command line arguments.N)
parse_argsargsself r   ./usr/lib/python3/dist-packages/glances/main.py__init__c   s   zGlancesMain.__init__c              	   C   sV  d ttt}tjddtj| jd}|jddd|d |jd	d
ddddd |jddddd |jddddddd |jddddd |jdddd d |jd!ddd"d#d |jd$ddd%d&d |jd'd(ddd)d*d |jd+d,ddd-d.d |jd/d0ddd1d2d |jd3d4ddd5d6d |jd7d8ddd9d:d |jd;d<ddd=d>d |jd?d@dddAdBd |jdCdDdddEdFd |jdGdddHdId |jdJdddKdLd |jdMdddNdOd |jdPdddQdRdf |jdSdddTdUd |jdVdWt	dX dY
t	dZ |jd[d\d]d |jd^d_d`dadb |jdcddddded |jdfdgdhdidb |jdjt dkdldb |jdmdndodpd |jdqdrdddsdtd |jdudddvdwd |jdxdddydzd |jd{d|d}td~d | jd |jddddddb |jdddddd |jdddddd |jdddd |jdddddb |jddtddd |jdddddb |jdddddb |jdddddb |jdddddd |jdd| jtdd | jd |jddddddd |jd| jtdd | jd |jdddddd |jddddddd |jddd}tddd |jdddddd |jdddddd |jdd}dddb |jdd}dddb |jdd}dddd |jdd}dddd tsO|jdddddd |jddddddd |jdddddd |jdddddd |jdddddd |jdddddd |jdddddd |jdddddd |jdddddd |jddddd |S )z$Init all the command line arguments.z(Glances v{} with PsUtil v{}
Log file: {}r   Zresolve)progconflict_handlerformatter_classepilogz-Vz	--versionversion)actionr   z-dz--debug
store_trueFdebugzenable debug mode)r   defaultdesthelpz-Cz--config	conf_filezpath to the configuration file)r!   r"   z--modules-listz--module-listZmodules_listz1display modules (plugins & exports) list and exitz--disable-pluginz--disable-pluginsdisable_pluginz#disable plugin (comma separed list)z--enable-pluginz--enable-pluginsenable_pluginz"enable plugin (comma separed list)z--disable-processZdisable_processzdisable process modulez--disable-webuiZdisable_webuizdisable the Web Interfacez--lightz--enable-lightenable_lightz3light mode for Curses UI (disable all but top menu)z-0z--disable-irixZdisable_irixz<task's cpu usage will be divided by the total number of CPUsz-1z--percpuZpercpuzstart Glances in per CPU modez-2z--disable-left-sidebardisable_left_sidebarz1disable network, disk I/O, FS and sensors modulesz-3z--disable-quicklookZdisable_quicklookzdisable quick look modulez-4z--full-quicklookfull_quicklookz#disable all but quick look and loadz-5z--disable-topdisable_topz.disable top menu (QL, CPU, MEM, SWAP and LOAD)z-6z	--meangpuZmeangpuzstart Glances in mean GPU modez--disable-historyZdisable_historyzdisable stats historyz--disable-boldZdisable_boldz!disable bold mode in the terminalz--disable-bgZ
disable_bgz)disable background colors in the terminalz--enable-irqZ
enable_irqzenable IRQ modulez--enable-process-extendedZenable_process_extendedz$enable extended stats on top processz--sort-processesZsort_processes_keyzSort processes by: {}z, )r!   choicesr"   z--exportexportz)enable export module (comma separed list)z--export-csv-filez./glances.csvZexport_csv_filezfile path for CSV exporter)r    r!   r"   z--export-csv-overwriteZexport_csv_overwritezoverwrite existing CSV filez--export-json-filez./glances.jsonZexport_json_filezfile path for JSON exporterz--export-graph-pathZexport_graph_pathzFolder for Graph exporterz-cz--clientclientz<connect to a Glances server by IPv4/IPv6 address or hostnamez-sz--serverserverzrun Glances in server modez	--browserbrowserz*start the client browser (list of servers)z--disable-autodiscoverdisable_autodiscoverzdisable autodiscover featurez-pz--portNportz/define the client/server TCP port [default: {}])r    typer!   r"   z-Bz--bindz0.0.0.0Zbind_addressz6bind server to the given IPv4/IPv6 address or hostnamez
--usernameusername_promptzdefine a client/server usernamez
--passwordpassword_promptzdefine a client/server passwordz-uusername_usedz$use the given client/server usernamez--snmp-communityZpublicZsnmp_communityzSNMP communityz--snmp-port   Z	snmp_portz	SNMP portz--snmp-versionZ2cZsnmp_versionzSNMP version (1, 2c or 3)z--snmp-userZprivateZ	snmp_userzSNMP username (only for SNMPv3)z--snmp-authpasswordZ	snmp_authz)SNMP authentication key (only for SNMPv3)z--snmp-forceZ
snmp_forcezforce SNMP modez-tz--timetimez5set minumum refresh rate in seconds [default: {} sec]z-wz--webserver	webserverzQrun Glances in web server mode (python3-bottle needed, static files not included)z--cached-timecached_timez+set the server cache time [default: {} sec]z--open-web-browserZopen_web_browserz1try to open the Web UI in the default Web browserz-qz--quietquietz#do not display the curses interface)r    r   r!   r"   z-fz--process-filterprocess_filterz3set the process filter pattern (regular expression)z--process-short-nameTprocess_short_namez#force short name for processes namez--process-long-namestore_falsez"force long name for processes namez--stdoutstdoutz^display stats to stdout, one stat per line (comma separated list of plugins/plugins.attribute)z--stdout-csvZ
stdout_csvzWdisplay stats to stdout, csv format (comma separated list of plugins/plugins.attribute)z--issueZstdout_issuezMtest all plugins and exit (please copy/paste the output if you open an issue)z	--api-docZstdout_apidoczdisplay fields descriptionsz--hide-kernel-threadsZno_kernel_threadsz>hide kernel threads in process list (not available on Windows)z-bz--byteZbytez'display network rate in byte per secondz--diskio-show-ramfsZdiskio_show_ramfsz show RAM Fs in the DiskIO pluginz--diskio-iopsZdiskio_iopsz'show IO per second in the DiskIO pluginz--fahrenheitZ
fahrenheitz6display temperature in Fahrenheit (default is Celsius)z--fs-free-spaceZfs_free_spacez%display FS free space instead of usedz--sparklineZ	sparklinez9display sparklines instead of bar in the curses interfacez--theme-whiteZtheme_whitez,optimize display colors for white backgroundz--disable-check-updateZdisable_check_updatez$disable online Glances version ckeckz
--strftimeZstrftime_formatr   zEstrftime format string for displaying current date in standalone mode)r!   r    r"   )formatr   r   r   argparseArgumentParserRawDescriptionHelpFormatterexample_of_useadd_argumentr	   jointempfileZ
gettempdirintserver_portDEFAULT_REFRESH_TIMEfloatr9   strr   )r   r   parserr   r   r   	init_argsh   s  





































zGlancesMain.init_argsc                 C   s  |    }t|j| _|jrddlm} t	| n
ddl
m} |d | jdr5| jjdd| jd}|j| jkr>||_td|j | j D ]}| j|ri| j|d	d
rit|| td| qL|jdur}|jdD ]}t|| qu|jdur|jdD ]}t|| q|jdur|jdD ]
}t|d| d q|jdu r|jr| j|_n| j|_|jdurdd t|j dddd |j|jfD \|_|_|j!rt"d |jrd|_#|j$rd|_%|j&r| j'dd|_(n%|jr| j'dd|_(n|jr| j'dd|_(n|j)r|j)|_(n| j(|_(|j%s$|j)r]|j&r7| j*d|j(d|j(d|_+n*|jrJ| j*d|j(d|j(d|_+n|jr\| j*d|j(d|j(d|_+n| j+|_+d
|_,d
|_-d
|_.|j/rt"d d|_0t|d t|d  t|d! t|d" |j1rt"d# t|d$ t|d% t|d& t|d' t|d( |j2rt"d) t|d$ t|d% t|d& t|d' t|d( d
|_3|| _4| j4jduot5| j4j}t6r|rt"d* d| j4_7d
| j4_n| 8 s| 9 s|rt:d+ t;<d |j=dur!| 8 s!t:d, t;<d t>|d-d
r2t|d. td/ | 8 | j4_8| 9 | j4_9| ? | j4_?| @ | j4_@| A | j4_A|S )0zParse command line arguments.r   )DEBUG)simplefilterignoreglobalZrefresh)r    z(Global refresh rate is set to {} secondsr   Fz%{} disabled by the configuration fileN,Zexport_Tc                 s   s     | ]\}}|r
|n|V  qd S )Nr   ).0xyr   r   r   	<genexpr>?  s    z)GlancesMain.parse_args.<locals>.<genexpr>:r   zAuto discover mode is disabledz$Define the Glances server username: )descriptionz'Define the Glances webserver username: z#Enter the Glances server username: z2Define the Glances server password ({} username): )rX   confirmusernamez5Define the Glances webserver password ({} username): z1Enter the Glances server password ({} username): )rX   clearrZ   zLight mode is onZprocessZalertZampsZdockerzFull quicklook modeZ	quicklookZcpuZmemZmemswaploadzDisable top menuz/On Windows OS, export disable the Web interfacez5Export is only available in standalone or client modez3Process filter is only available in standalone modeZdisable_sensorsZhddtempz Sensors and HDDTemp are disabled)BrM   r   r   r#   configr   ZloggingrN   r
   ZsetLevelwarningsrO   Zhas_sectionZget_float_valuerI   r7   r?   ZsectionsZget_bool_valuer   r$   splitr%   r   r+   setattrr0   r8   web_server_portrH   r,   zip	partitionr/   infor<   r2   r3   r-   _GlancesMain__get_usernamerZ   r4   _GlancesMain__get_passwordr6   Zhelp_tagZnetwork_sumZnetwork_cumulr&   r'   r(   r)   Zgenerate_graphr   anyr   r:   is_standalone	is_clientZcriticalsysexitr;   getattris_client_browser	is_serveris_webserver)r   r   rN   rO   Zglobal_refreshspZ
export_tagr   r   r   r   	  s  






6





























zGlancesMain.parse_argsc                 C   s(   | j j o| j j o| j j o| j j S )z5Return True if Glances is running in standalone mode.)r   r,   r.   r-   r8   r   r   r   r   rh     s   
zGlancesMain.is_standalonec                 C   s   | j js| j jo| j j S )z1Return True if Glances is running in client mode.)r   r,   r.   r-   r   r   r   r   ri     s   zGlancesMain.is_clientc                 C   s   | j jo| j j S )z9Return True if Glances is running in client browser mode.)r   r.   r-   r   r   r   r   rm        zGlancesMain.is_client_browserc                 C      | j j o| j jS )z1Return True if Glances is running in server mode.)r   r,   r-   r   r   r   r   rn     rr   zGlancesMain.is_serverc                 C   rs   )z5Return True if Glances is running in Web server mode.)r   r,   r8   r   r   r   r   ro     rr   zGlancesMain.is_webserverc                 C      | j S )z!Return configuration file object.)r]   r   r   r   r   
get_config     zGlancesMain.get_configc                 C   rt   )zReturn the arguments.)r   r   r   r   r   get_args  rv   zGlancesMain.get_argsc                 C   rt   )zReturn the mode.)moder   r   r   r   get_mode  rv   zGlancesMain.get_modec                 C   s   t |S )z'Read an username from the command line.)r   )r   rX   r   r   r   Z__get_username  s   zGlancesMain.__get_usernamec                 C   s$   ddl m} ||d}||||S )zRead a password from the command line.

        - if confirm = True, with confirmation
        - if clear = True, plain (clear password)
        r   )GlancesPassword)rZ   )Zglances.passwordrz   Zget_password)r   rX   rY   r[   rZ   rz   r6   r   r   r   Z__get_password  s   
zGlancesMain.__get_passwordN)r   )r   FFr   )__name__
__module____qualname____doc__rI   r9   Z
client_tagrH   ra   rZ   r6   rC   r   rM   r   rh   ri   rm   rn   ro   ru   rw   ry   re   rf   r   r   r   r   r   "   s6    / " 8
r   )r~   r@   rj   rF   r   r   r   Zglances.compatr   r   r   Zglances.configr   Zglances.globalsr   Zglances.processesr	   Zglances.loggerr
   r   objectr   r   r   r   r   <module>   s   