Glances：多功能 Linux 系统监控工具
Glances
![](/data/attachment/album/202008/30/212820tgdi7iou6dg6qqq2.jpg)


Linux 上最常用的[命令行进程监控工具](https://itsfoss.com/linux-system-monitoring-tools/)是 `top` 和它那色彩斑斓、功能丰富的表弟 [htop](https://hisham.hm/htop/)。


要[监控 Linux 上的温度](https://itsfoss.com/monitor-cpu-gpu-temp-linux/)，可以使用 [lm-sensors](https://github.com/lm-sensors/lm-sensors)。同样，还有很多实用工具可以监控其他实时指标，如磁盘 I/O、网络统计等。


[Glances](https://nicolargo.github.io/glances/) 是一个系统监控工具，它把这些都联系在一起，并提供了更多的功能。我最喜欢的是，你可以在远程 Linux 服务器上运行 Glances 来监控本地系统的系统资源，也可以通过 Web 浏览器监控。


下面是它的外观。下面截图中的终端已经[用 Pywal 工具美化过，可以根据壁纸自动改变颜色](https://itsfoss.com/pywal/)。


![](/data/attachment/album/202008/30/212845k997iv9vzu88hlnl.png)


你也可以将它集成到像 [Grafana](https://grafana.com/) 这样的工具中，在一个直观的仪表盘中监控统计数据。


它是用 Python 编写的，这意味着它的绝大多数功能都可以在大多数平台上使用。


### Glances 的功能


![Glances Data In Grafana Dashboard](/data/attachment/album/202008/30/213033x6rjhqsgjbk7cyks.jpg)


让我们快速浏览一下 Glances 提供的主要功能：


* 可以监控系统上的 15 个之多的指标（包括 Docker 容器）。
* 灵活的使用模式：单机模式、客户端-服务器模式、通过 SSH 和 Web 模式。
* 可用于集成的各种 REST API 和 XML-RPC API。
* 支持将数据轻松导出到不同的服务和数据库。
* 高度的可配置性和适应不同的需求。
* 非常全面的文档。


### 在 Ubuntu 和其他 Linux 发行版上安装 Glances


Glances 在许多 Linux 发行版的官方软件库中都有。这意味着你可以使用你的发行版的软件包管理器来轻松安装它。


在基于 Debian/Ubuntu 的发行版上，你可以使用以下命令：



```
sudo apt install glances

```

你也可以使用 snap 包安装最新的 Glances：



```
sudo snap install glances

```

由于 Glances 是基于 Python 的，你也可以使用 PIP 在大多数 Linux 发行版上安装它。先[安装 PIP](https://itsfoss.com/install-pip-ubuntu/)，然后用它来安装 Glances：



```
sudo pip3 install glances

```

如果没有别的办法，你还可以使用 Glances 开发者提供的自动安装脚本。虽然我们不建议直接在你的系统上随便运行脚本，但这完全取决于你自己：



```
curl -L https://bit.ly/glances | /bin/bash

```

你可以从他们的[文档](https://github.com/nicolargo/glances/blob/master/README.rst#installation)中查看其他安装 Glances 的方法，甚至你还可以把它作为一个 Docker 容器来安装。


### 使用 Glances 监控本地系统上的 Linux 系统资源（独立模式）


你可以通过在终端上运行这个命令，轻松启动 Glances 来监控你的本地机器：



```
glances

```

你可以立即观察到，它将很多不同的信息整合在一个屏幕上。我喜欢它在顶部显示电脑的公共和私人 IP：


![](/data/attachment/album/202008/30/212937p4xg8xuwr9dcoxgo.png)


Glances 也是交互式的，这意味着你可以在它运行时使用命令与它互动。你可以按 `s` 将传感器显示在屏幕上；按 `k` 将 TCP 连接列表显示在屏幕上；按 `1` 将 CPU 统计扩展到显示单个线程。


你也可以使用方向键在进程列表中移动，并按不同的指标对表格进行排序。


你可以通过各种命令行选项来启动 Glances。此外，它还有很多交互式命令。你可以在他们的[丰富的文档](https://glances.readthedocs.io/en/latest/cmds.html)中找到完整的列表。


按 `Ctrl+C` 键退出 Glances。


### 使用 Glances 监控远程 Linux 系统（客户端-服务器模式）


要监控远程计算机，你可以在客户端-服务器模式下使用 Glances。你需要在两个系统上都安装 Glances。


在远程 Linux 系统上，使用 `-s` 选项在服务器模式下启动 Glances：



```
glances -s

```

在客户端系统中，使用下面的命令在客户端模式下启动 Glances 并连接到服务器：



```
glances -c server_ip_address

```

你也可以通过 SSH 进入任何一台电脑，然后启动 Glances，它可以完美地工作。更多关于客户端-服务器模式的信息请看[这里](https://glances.readthedocs.io/en/latest/quickstart.html#central-client)。


### 使用 Glances 在 Web 浏览器中监控 Linux 系统资源（Web 模式）


Glances 也可以在 Web 模式下运行。这意味着你可以使用 Web 浏览器来访问 Glances。与之前的客户端-服务器模式不同，你不需要在客户端系统上安装 Glances。


要在 Web 模式下启动 Glances，请使用 `-w` 选项：



```
glances -w

```

请注意，即使在 Linux 服务器上，它也可能显示 “Glances Web User Interface started on <http://0.0.0.0:61208>”，而实际上它使用的是服务器的 IP 地址。


最主要的是它使用的是 61208 端口号，你可以用它来通过网络浏览器访问 Glances。只要在服务器的 IP 地址后面输入端口号，比如 <http://123.123.123.123:61208>。


你也可以在本地系统中使用 <http://0.0.0.0:61208/> 或 <https://localhost:61208/> 访问。


![](/data/attachment/album/202008/30/212952u5bph462djb6dkph.png)


Web 模式也模仿终端的样子。网页版是根据响应式设计原则打造的，即使在手机上也很好看。


你可能想用密码来保护 Web 模式，这样只有授权的人才能使用它。默认的用户名是 `glances`。



```
root@localhost:~# glances -w --password
Define the Glances webserver password (glances username):
Password (confirm):
Do you want to save the password? [Yes/No]: n
Glances Web User Interface started on http://0.0.0.0:61208/

```

你可以在[快速入门指南](https://glances.readthedocs.io/en/stable/quickstart.html)中找到关于配置密码的更多信息。


### 导出 Glances 数据到不同的服务


使用 Glances 最大的优势之一就是开箱即用，它支持将数据导出到各种数据库、服务，并无缝集成到各种数据管道中。


你可以在监控的同时用这个命令导出到 CSV：



```
glances --export csv --export-csv-file /tmp/glances.csv

```

`/tmp/glances.csv` 是文件的位置。数据以时间序列的形式整齐地填入。


![](/data/attachment/album/202008/30/213004w4wy5y0zn500jsif.png)


你也可以导出到其它大型应用程序，如 [Prometheus](https://prometheus.io/)，以启用条件触发器和通知。


它可以直接插入到消息服务（如 RabbitMQ、MQTT）、流媒体平台（如 Kafka），并将时间序列数据导出到数据库（如 InfluxDB），并使用 Grafana 进行可视化。


你可以在[这里](https://glances.readthedocs.io/en/latest/gw/index.html)查看服务和导出选项的整个列表。


### 使用 REST API 将 Glances 与其他服务进行整合


这是整个栈中我最喜欢的功能。Glances 不仅可以将各种指标汇集在一起，还可以通过 API 将它们暴露出来。


这个简单而强大的功能使得为任何特定的用例构建自定义应用程序、服务和中间件应用程序变得非常容易。


当你在 Web 模式下启动 Glances 时，REST API 服务器会自动启动。要在 API 服务器模式下启动它，你可以使用以下命令：



```
glances -w --disable-webui

```

[REST API](https://github.com/nicolargo/glances/wiki/The-Glances-RESTFULL-JSON-API) 的文档很全面，其响应也很容易与 Web 应用集成。这使得使用类似 [Node-RED](https://nodered.org/) 这样的工具可以很容易地构建一个统一的仪表盘来监控多个服务器。


![](/data/attachment/album/202008/30/213021golczzzzssogpvgi.png)


Glances 也提供了一个 XML-RPC 服务器，你可以在[这里](https://github.com/nicolargo/glances/wiki)查看文档。


### 关于 Glances 的结束语


Glances 使用 [psutil](https://pypi.org/project/psutil/) Python 库来访问不同的系统统计数据。早在 2017 年，我就曾使用相同的库构建了一个简单的 API 服务器来检索 CPU 的使用情况。我能够使用 Node-RED 构建的仪表盘监控一个集群中的所有树莓派。


Glances 可以为我节省一些时间，同时提供更多的功能，可惜我当时并不知道它。


在写这篇文章的时候，我确实尝试着在我的树莓派上安装 Glances，可惜所有的安装方法都出现了一些错误，失败了。当我成功后，我会更新文章，或者可能再写一篇文章，介绍在树莓派上安装的步骤。


我希望 Glances 能提供一种顶替 `top` 或 `htop` 等的方法。让我们希望在即将到来的版本中得到它。


我希望这能给你提供大量关于 Glances 的信息。你们使用什么系统监控工具呢，请在评论中告诉我。




---


via: <https://itsfoss.com/glances/>


作者：[Chinmay](https://itsfoss.com/author/chinmay/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
The most commonly used command line [tools for process monitoring on Linux](https://itsfoss.com/linux-system-monitoring-tools/) are top and its colorful, feature rich cousin [htop](https://hisham.hm/htop/) .

To [monitor temperature on Linux](https://itsfoss.com/monitor-cpu-gpu-temp-linux/), you can use [lm-sensors](https://github.com/lm-sensors/lm-sensors). Similarly, there are many utilities to monitor other real-time metrics such as Disk I/O, Network Stats and others.

[Glances](https://nicolargo.github.io/glances/) is a system monitoring tool which ties them all together and offers a lot more features. What I like the most is that you can use run Glances on a remote Linux server and monitor the system resources on your local system or monitor it through your web browser.

Here’s what it looks like. The terminal in the below screenshot has been [beautified with Pywal tool that automatically changes the color based on wallpaper](https://itsfoss.com/pywal/).

![Glances Linux](https://itsfoss.com/content/images/wordpress/2020/08/glances-linux-800x510.png)

You can also integrate it to a tool like [Grafana](https://grafana.com/) to monitor the stats in an intuitive dashboard.

It is written in Python which means that it can be used on most platforms with almost all features.

## Features of Glances

![Glances Data In Grafana Dashboard](https://itsfoss.com/content/images/wordpress/2020/08/glances-data-in-grafana-dashboard.jpg)

Let’s have a quick glance over the main feature Glances provides (pun intended).

- Can monitor 15+ metrics on a system (including docker containers).
- Flexible usage modes: standalone, client-server, over SSH and web mode.
- Versatile REST API and XML-RPC available for integrations.
- Readily supports exporting data to different services and databases.
- Highly configurable and adaptable to different needs.
- Very comprehensive Documentation.

## Installing Glances on Ubuntu and other Linux distributions

Glances is available in the official repositories of many Linux distributions. This means you can use your distribution’s package manager to install it easily.

On Debian/Ubuntu based distributions, you can use the following command:

`sudo apt install glances`


You can also install the latest Glances using snap package:

`sudo snap install glances `


Since Glances is based on Python, you can also use PIP to install it on most Linux distributions. [Install PIP](https://itsfoss.com/install-pip-ubuntu/) first and then use it to install Glances:

`sudo pip3 install glances`


If nothing else, you can always use the auto install script provided by Glances developer. Though we at It’s FOSS don’t recommend running random scripts directly on your system. It’s entirely your decision.

`curl -L https://bit.ly/glances | /bin/bash`


You can check out other ways to install Glances from their [documentation](https://github.com/nicolargo/glances/blob/master/README.rst#installation), you can also install it as a docker container.

## Using Glances to monitor Linux system resources on local system (standalone mode)

You can easily launch Glances to monitor your local machine by running this command n the terminal.

`glances`


You can instantly observe that it integrates a lot of different information in one single screen. I like that it shows the public and private IP’s of the computer right at the top.

![Screenshot From 2020 08 13 11 54 18](https://itsfoss.com/content/images/wordpress/2020/08/Screenshot-from-2020-08-13-11-54-18-800x503.png)

Glances is also interactive, meaning you can use commands to interact with it while it is running. You can press **“s”** to bring up the sensors onto the screen; **“k”** to bring up the TCP connections list; **“1”** to expand the CPU stats to show individual threads.

You can also use the arrow keys to move around the list of processes and sort the table by different metrics.

You can launch Glances with various command line options. There are plenty more interactive commands as well. You can find the complete list in their [comprehensive documentation](https://glances.readthedocs.io/en/latest/cmds.html).

Press Ctrl+C to exit Glances.

## Using Glances to monitor remote Linux systems (Client – Server mode)

To monitor a remote computer you can use Glances in client-server mode. You need to have Glances installed on both systems.

On the remote Linux system, you’ll have to launch glances in server mode using the -s option.

`glances -s`


On the client system, use this command to launch Glances in client mode and connect to the server.

`glances -c server_ip_address`


You can also SSH into any computer and launch Glances, which works flawlessly. More information about client-server mode [here](https://glances.readthedocs.io/en/latest/quickstart.html#central-client).

## Monitor Linux system resources in web browser using Glances (Web Mode)

Glances can also run in web mode. This means that you can use a web browser to access Glances. Unlike the previous client-server mode, you don’t need to install Glances on the client system.

To start Glances in web mode, use the -w option.

`glances -w`


Keep in mind that it may display “Glances Web User Interface started on http://0.0.0.0:61208” even on a Linux server when it is actually using the server’s IP address.

The main part is that it uses 61208 port number and you can use it to access Glances via web browser. Just type the server’s IP address followed by the port number like http://123.123.123.123:61208.

You can also use it on the local system using http://0.0.0.0:61208/ or [https://localhost:61208/](https://localhost:61208/).

![Glances in web mode](https://itsfoss.com/content/images/wordpress/2020/08/Screenshot-from-2020-08-13-16-49-11-800x471.png)

The web mode also emulates the terminal look. The web version is built with responsive design principles in mind and looks good even on phones.

You may want to protect the web mode with a password so that only authorized people could use it. The default username is glances.

```
root@localhost:~# glances -w --password
Define the Glances webserver password (glances username):
Password (confirm):
Do you want to save the password? [Yes/No]: n
Glances Web User Interface started on http://0.0.0.0:61208/
```


You can find more information on configuring password in the [quickstart guide](https://glances.readthedocs.io/en/stable/quickstart.html).

## Export Glances data to different services

One of the biggest advantage of using Glances is the out of the box support to export data to various databases, services and integration into various data pipelines seamlessly.

You can export to CSV while monitoring with this command.

`glances --export csv --export-csv-file /tmp/glances.csv`


‘/tmp/glances.csv’ is the location of the file. The data is filled in neatly as time series.

![export Glances data](https://itsfoss.com/content/images/wordpress/2020/08/Screenshot-from-2020-08-13-12-25-40-800x448.png)

You can export to powerful applications like [Prometheus](https://prometheus.io/) to enable conditional triggers and notifications.

It can directly plug into messaging services like RabbitMQ, MQTT, streaming platforms like Kafka and export time series data to databases like InfluxDB and visualize using Grafana.

You can check out the whole list of services and export options [here](https://glances.readthedocs.io/en/latest/gw/index.html).

## Use REST API to integrate Glances with other services

This is my favorite feature in the whole stack. Glances not only brings together various metrics together, it also exposes them via APIs.

This simple yet powerful feature makes it very easy to build custom applications, services and middleware applications for any specific use cases.

REST API server stars automatically when you launch Glances in web mode. To launch it in API server only mode you can use the following command.

`glances -w --disable-webui`


The [REST API](https://github.com/nicolargo/glances/wiki/The-Glances-RESTFULL-JSON-API) documentation is comprehensive and the responses are easy to integrate with a web application. This makes it easy to build a unified dashboard to monitor multiple servers with a tool like [Node-RED](https://nodered.org/).

![Glances running with NODE-RED using REST API](https://itsfoss.com/content/images/wordpress/2020/08/Screenshot-from-2020-08-13-17-49-41-800x468.png)

Glances also provides an XML-RPC server, you can check out the documentation [here](https://github.com/nicolargo/glances/wiki).

## Closing thoughts on Glances

Glances uses the [psutil](https://pypi.org/project/psutil/) python library to access different system stats. I had built a simple API server using the same library to retrieve CPU usage back in 2017. I was able to monitor all the Raspberry Pi’s in a cluster using a dashboard built with Node-RED.

Glances could have saved me some time while offering way more features, unfortunately I didn’t know about it back then.

I did try to install Glances on my Raspberry Pi’s while writing this article, unfortunately it failed with some errors with all the installation methods. I’ll update the article when I succeed or maybe write another article with steps to install on a Raspberry Pi.

I wish Glances offered a way to kill processes like top or htop. Let’s hope that we get it in upcoming releases.

I hope this gave you a good deal of information on Glances. What system monitoring tools do you guys use? let me know in the comments.