Home  >  Article  >  System Tutorial  >  Glances: A powerful Linux system monitoring tool

Glances: A powerful Linux system monitoring tool

PHPz
PHPzforward
2024-02-11 15:18:17653browse

Linux system administrators need to frequently monitor various resources and performance indicators of the system, such as CPU, memory, disk, network, temperature, etc. While there are many command-line tools that can provide this information, they often only display a subset of the data, or require switching between different commands and windows. Is there a tool that can put all the important information on one screen and provide more functions and options? The answer is yes, it is Glances. Glances is an open source tool written in Python. It can display various real-time data of the Linux system on the terminal, browser or remote server, including process, load, memory, disk I/O, network traffic, file system, temperature, etc. . It also supports exporting data to different services and databases, as well as integration using REST API and XML-RPC API. In this article, we’ll cover how to install and use Glances on Ubuntu and other Linux distributions, as well as some of its key features and benefits

Here's what it looks like. The terminal in the screenshot below has been beautified with the Pywal tool and can automatically change colors according to the wallpaper.

Glances:一款强大的 Linux 系统监控工具

You can also integrate it into tools like Grafana to monitor statistics in an intuitive dashboard.

It is written in Python, which means the vast majority of its features are available on most platforms.

Function of Glances

Glances:一款强大的 Linux 系统监控工具

Glances Data In Grafana Dashboard

Let’s take a quick look at the main features Glances offers:

  • Can monitor up to 15 metrics on your system (including Docker containers).
  • Flexible usage modes: stand-alone, client-server, via SSH and web.
  • Various REST APIs and XML-RPC APIs available for integration.
  • Supports easy export of 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 are available in the official software repositories of many Linux distributions. This means you can easily install it using your distribution's package manager.

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

sudo apt install glances

You can also use the snap package to install the latest Glances:

sudo snap install glances

Since Glances is based on Python, you can also install it on most Linux distributions using PIP. Install PIP first, then use it to install Glances:

sudo pip3 install glances

If there is no other way, you can also use the automatic installation script provided by Glances developers. While we don't recommend running random scripts directly on your system, it's entirely up to you:

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

You can check out other ways to install Glances from their documentation, and you can even install it as a Docker container.

Use Glances to monitor Linux system resources on the local system (standalone mode)

You can easily start Glances to monitor your local machine by running this command in the terminal:

glances

You can immediately observe that it combines a lot of different information on one screen. I like that it shows the computer's public and private IP at the top:

Glances:一款强大的 Linux 系统监控工具

Glances is also interactive, meaning you can interact with it using commands while it is running. You can press s to bring the sensors to the screen; press k to bring the TCP connection list to the screen; press 1 to expand the CPU statistics to show individual threads. .

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

你可以通过各种命令行选项来启动 Glances。此外,它还有很多交互式命令。你可以在他们的丰富的文档中找到完整的列表。

Ctrl+C 键退出 Glances。

使用 Glances 监控远程 Linux 系统(客户端-服务器模式)

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

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

glances -s

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

glances -c server_ip_address

你也可以通过 SSH 进入任何一台电脑,然后启动 Glances,它可以完美地工作。更多关于客户端-服务器模式的信息请看这里。

使用 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/ 访问。

Glances:一款强大的 Linux 系统监控工具

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

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

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

你可以在快速入门指南中找到关于配置密码的更多信息。

导出 Glances 数据到不同的服务

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

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

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

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

Glances:一款强大的 Linux 系统监控工具

你也可以导出到其它大型应用程序,如 Prometheus,以启用条件触发器和通知。

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

你可以在这里查看服务和导出选项的整个列表。

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

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

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

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

glances -w --disable-webui

REST API 的文档很全面,其响应也很容易与 Web 应用集成。这使得使用类似 Node-RED 这样的工具可以很容易地构建一个统一的仪表盘来监控多个服务器。

Glances:一款强大的 Linux 系统监控工具

Glances 也提供了一个 XML-RPC 服务器,你可以在这里查看文档。

关于 Glances 的结束语

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

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

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

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

本文介绍了 Glances 这款多功能的 Linux 系统监控工具,它可以让你在一个屏幕上查看系统的各种重要信息,并且提供了灵活的使用模式和导出选项。Glances 是一个非常实用和强大的工具,它可以帮助你更好地了解和管理你的 Linux 系统。如果你想要更多地学习和探索 Glances 的功能和配置,你可以参考它的 官方文档 ,或者在 GitHub 上查看它的 源码 和 反馈 。希望本文对你有所帮助!

The above is the detailed content of Glances: A powerful Linux system monitoring tool. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete