- Glances介绍
glances是一个基于python语言开发,可以为linux或者UNIX性能提供监视和分析性能数据的功能。glances在用户的终端上显示重要的系统信息,并动态的进行更新,让管理员实时掌握系统资源的使用情况,而动态监控并不会消耗大量的系统资源,比如CPU资源,通常消耗小于2%,glances默认每两秒更新一次数据。同时glances还可以将相同的数据捕获到一个文件,便于以后对报告进行分析和图形绘制,支持的文件格式有.csv电子表格格式和和html格式。
github地址:https://github.com/nicolargo/glances
glances工具的功能如下:
CPU使用率
内存使用率
内核统计信息和运行队列信息
磁盘I/O速度、传输和读/写比率
磁盘适配器
网络I/O速度、传输和读/写比率
页面监控
进程监控-消耗资源最多的进程
计算机信息和系统资源
如图:

2.glances 安装:
debian/ubuntu:
sudo apt-get install glances
Centos:
yum install epel* -y
yum -y install glances
当然也可以使用 Python 的包管理器(pip 命令)进行安装:
pip install glances
温度监控工具安装
lm_sensors 的软件可以帮助我们来监控主板、CPU 的工作电压、风扇转速、温度等数据。这些数据我们通常在主板的 BIOS 也可以看到。当我们可以在机器运行的时候通过 lm_sensors 随时来监测着 CPU 的温度变化,可以预防呵保护因为 CPU 过热而会烧掉。lm_sensors 软件监测到的数据可以被 glances 调用并且显示 。
yum -y install lm_sensors
3.glances的使用
glances 使用方法
glances 是一个命令行工具包括如下命令选项:
-b:显示网络连接速度 Byte/ 秒
-B @IP|host :绑定服务器端 IP 地址或者主机名称
-c @IP|host:连接 glances 服务器端
-C file:设置配置文件默认是 /etc/glances/glances.conf
-d:关闭磁盘 I/O 模块
-e:显示传感器温度
-f file:设置输出文件(格式是 HTML 或者 CSV)
-m:关闭挂载的磁盘模块
-n:关闭网络模块
-p PORT:设置运行端口默认是 61209
-P password:设置客户端 / 服务器密码
-s:设置 glances 运行模式为服务器
-t sec:设置屏幕刷新的时间间隔,单位为秒,默认值为 2 秒,数值范围:1~32767
-h : 显示帮助信息
-v : 显示版本信息
颜色级别定义:
绿色:正常(OK)
绿色表示性能良好;(此时 CPU 使用率、磁盘空间使用率和内存使用率低于 50%,系统负载低于 0.7)。
蓝色:小心(CAREFUL),需要注意
蓝色表示系统性能有一些小问题,用户应当开始关注系统性能;(此时 CPU 使用率、磁盘空间使用率和内存使用率在 50%-70% 之间,系统负载在 0.7-1 之间)。
紫色:警告(WARNING)
紫色表示性能报警,应当采取措施比如备份数据;(此时 CPU 使用率、磁盘空间使用率和内存使用率在 70%-90% 之间,,系统负载在 1-5 之间)。
红色:问题严重(CRITICAL)
红色表示性能问题严重,可能宕机;(此时 CPU 使用率、磁盘空间使用率和内存使用率在大于 90%,系统负载大于 5)。
glances 工作界面的说明 :
在上图中 的上部是 CPU 、Load(负载)、Mem(内存使用)、 Swap(交换分区)的使用情况。在图中的中上部是网络接口、Processes(进程)的使用情况。通常包括如下字段:
VIRT: 虚拟内存大小
RES: 进程占用的物理内存值
%CPU:该进程占用的 CPU 使用率
%MEM:该进程占用的物理内存和总内存的百分比
PID: 进程 ID 号
USER: 进程所有者的用户名
TIME+: 该进程启动后占用的总的 CPU 时间
IO_R 和 IO_W: 进程的读写 I/O 速率
NAME: 进程名称
NI: 进程优先级
S: 进程状态,其中 S 表示休眠,R 表示正在运行,Z 表示僵死状态。
另外 glances 可以使用交互式的方式运行该工具,用户可以使用如下快捷键:
h :显示帮助信息
q :离开程序退出
c :按照 CPU 实时负载对系统进程进行排序
m :按照内存使用状况对系统进程排序
i:按照 I/O 使用状况对系统进程排序
p:按照进程名称排序
d :显示磁盘读写状况
w :删除日志文件
l :显示日志
s:显示传感器信息
f :显示系统信息
1 :轮流显示每个 CPU 内核的使用情况
4.Glances的C/S模式
glances还支持C/S模式监控,被监控机运行服务端,监控端运行客户端既可以实现远程监控,两端都安装Glances服务即可。
服务端启动
服务端使用的端口默认是61209,启动命令如下:
glances -s -B 192.168.2.236
glances server is running on 192.168.2.236:61209
客户端访问:
glances -c 192.168.2.236 即可进入 glances 监控界面;
也可以使用用户名与密码进行访问,默认用户名是glances,如果想更改的话可以使用–username进行更换。
glances -s –username
Define the Glances server username: test
Define the Glances server password (test username):
Password (confirm):
Do you want to save the password? [Yes/No]: Yes
Glances XML-RPC server is running on 0.0.0.0:61209
glances -c 192.168.2.236 –username mytest
在 glances 的 WebServer 模式下,客户端只通过浏览器访问就可以获取远程服务器的运行状态。只需要安装 Python 的 Bottle 模块:
安装成功以后,glances -w –password 开启webserver模式:
浏览器输入192.168.2.236:61209 输入账号密码进入监控界面,也可以在手机页面浏览
安装 Python 的 Bottle 模块:
Bottle module not found. Glances cannot start in web server mode.
当你遇到“Bottle module not found. Glances cannot start in web server mode”这个错误时,通常意味着你的环境中缺少了Bottle模块,而Glances(一款用于监控系统的开源工具)依赖于这个模块来启动其Web服务器功能。
要解决这个问题,你可以按照以下步骤操作:
1).安装 Bottle 模块
首先,你需要确保你的Python环境中安装了Bottle模块。你可以通过pip(Python的包管理器)来安装它。打开你的命令行界面(例如终端或命令提示符),然后输入以下命令:
pip install bottle
如果你使用的是Python 3,并且系统同时安装了Python 2和Python 3,确保你使用的是Python 3的pip版本。你可以通过运行以下命令来确保使用正确的pip版本:
pip3 install bottle
2).验证安装
安装完成后,你可以通过运行以下命令来检查Bottle是否已正确安装:
pip show bottle
这个命令应该会显示Bottle模块的安装信息,包括版本号和位置等。
3).重新启动 Glances
安装完Bottle模块后,重新启动Glances。如果你是在命令行中启动的,可以直接重新运行启动命令。例如:
glances
如果你在图形界面或某些服务中运行Glances,确保重新加载或重启该服务。
4).检查依赖关系
确保你的环境中没有其他与Bottle相关的依赖问题。有时候,即使安装了Bottle,系统的其他部分(如虚拟环境配置)也可能导致问题。尝试在一个新的虚拟环境中重新安装所有依赖,看是否能解决问题:
python -m venv glances-venv # 创建一个新的虚拟环境
source glances-venv/bin/activate # 在Unix或MacOS上激活虚拟环境
Windows上使用: glances-venv\Scripts\activate
pip install glances bottle # 在虚拟环境中安装Glances和Bottle
glances # 启动Glances
[root@test236 /opt]$python3 -m venv glances-venv
[root@test236 /opt]$source glances-venv/bin/activate
(glances-venv) [root@test236 /opt]$pip install glances bottle
Collecting glances
Downloading https://files.pythonhosted.org/packages/4f/f8/5a2311db46f1133771b12f3203e9bb09b6f26ce3118b366ee613c9ff4112/Glances-3.4.0.5-py3-none-any.whl (698kB)
100% |████████████████████████████████| 706kB 18kB/s
Collecting bottle
Cache entry deserialization failed, entry ignored
Downloading https://files.pythonhosted.org/packages/83/f6/b55ec74cfe68c6584163faa311503c20b0da4c09883a41e8e00d6726c954/bottle-0.13.4-py2.py3-none-any.whl (103kB)
100% |████████████████████████████████| 112kB 27kB/s
……
Collecting ujson<5; python_version >= “3.6” and python_version < “3.7” (from glances)
Downloading https://files.pythonhosted.org/packages/21/93/ba928551a83251be01f673755819f95a568cda0bfb9e0859be80086dce93/ujson-4.3.0.tar.gz (7.1MB)
78% |█████████████████████████ | 5.6MB 26kB/s eta 0:00:59
100% |████████████████████████████████| 7.1MB 25kB/s
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/pip-build-q8lpvv7q/ujson/setup.py", line 38, in <module>
    "write_to_template": version_template,
  File "/opt/glances-venv/lib/python3.6/site-packages/setuptools/__init__.py", line 129, in setup
    return distutils.core.setup(**attrs)
......
  File "/tmp/pip-build-q8lpvv7q/ujson/.eggs/setuptools_scm-9.2.0-py3.6.egg/setuptools_scm/__init__.py", line 6
    from __future__ import annotations
    ^
SyntaxError: future feature annotations is not defined
----------------------------------------
Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-build-_pklb_ct/ujson/
You are using pip version 9.0.3, however version 25.2 is available.
You should consider upgrading via the ‘pip install –upgrade pip’ command.
pip install –upgrade pip
(glances-venv) [root@test236 /opt] pip install glances bottle
Collecting glances
Using cached Glances-3.4.0.5-py3-none-any.whl (698 kB)
Collecting bottle
Using cached bottle-0.13.4-py2.py3-none-any.whl (103 kB)
Collecting ujson<5
Downloading ujson-4.3.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (216 kB)
|████████████████████████████████| 216 kB 185 kB/s
Collecting psutil>=5.6.7
Downloading psutil-7.0.0-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (277 kB)
|████████████████████████████████| 277 kB 18 kB/s
Collecting defusedxml
Using cached defusedxml-0.7.1-py2.py3-none-any.whl (25 kB)
Collecting packaging
Using cached packaging-21.3-py3-none-any.whl (40 kB)
Collecting pyparsing!=3.0.5,>=2.0.2
Downloading pyparsing-3.0.7-py3-none-any.whl (98 kB)
|████████████████████████████████| 98 kB 19 kB/s
Installing collected packages: pyparsing, ujson, psutil, packaging, defusedxml, glances, bottle
Successfully installed bottle-0.13.4 defusedxml-0.7.1 glances-3.4.0.5 packaging-21.3 psutil-7.0.0 pyparsing-3.0.7 ujson-4.3.0
ps:
导出数据为CSV格式,命令如下:
glances –export-csv /tmp/1.csv,数据可以导出导入到其他平台,和其它监控软件一样,能形成实时化、图表化的数据显示。
 
 