如何在Ubuntu上使用Glances监控系统

如何在Ubuntu上使用Glances监控系统,第1张

Glances 是一个用于监控系统的跨平台、基于文本模式的命令行工具。它是用 Python 编写的,使用 psutil 库从系统获取信息。你可以用它来监控 CPU、平均负载、内存、网络接口、磁盘 I/O,文件系统空间利用率、挂载的设备、所有活动进程以及消耗资源最多的进程。Glances 有很多有趣的选项。它的主要特性之一是可以在配置文件中设置阀值(careful小心、warning警告、critical致命),然后它会用不同颜色显示信息以表明系统的瓶颈。

Glances 的功能

◆CPU 平均负载

◆不同状态(如活动、休眠)进程的数量

◆所有内存信息,如物理内存、交换空间、空闲内存

◆CPU 信息

◆网络连接的上行/下行速度

◆磁盘 I/O 读/写速度详细信息

◆当前挂载设备的磁盘使用情况

◆消耗资源最多的进程和他们的 CPU/内存使用情况

安装 Glances

Glances 在 Ubuntu 的软件仓库中,所以安装很简单。执行下面的命令安装 Glances:

sudo apt-get install glances

若安装后无法正常使用,可考虑使用 pip 安装/升级 glances:

sudo pip install --upgrade glances

Glances 使用方法

安装完成后,可以执行下面的命令启动 Glances:

glances

你将看到类似下图的输出:

glances monitor system output

要退出 Glances 终端,按 ESC 键或 Ctrl + C。

默认情况下,时间间隔(显示数据刷新的时间间隔)是 1 秒,不过你可以在从终端启动 Glances 时自定义时间间隔。

要把时间间隔设为 5 秒,执行下面的命令:

glances -t 5

Glances 中不同颜色含义

Glances 中不同颜色的含义:

◆绿色:正常(OK)

◆蓝色:小心(careful)

◆紫色:警告(warning)

◆红色:致命(critical)

默认设置下,Glances 的阀值设置是:careful=50,warning=70,critical=90。你可以通过 “/etc/glances/” 目录下的默认配置文件 glancesconf 来自定义这些阀值。

Glances 的选项

Glances 提供了很多快捷键,可以在它运行时,用来查找输出信息。

下面是一些常用的热键列表:

◆m:按内存占用排序进程

◆p:按进程名称排序进程

◆c:按 CPU 占用率排序进程

◆i:按 I/O 频率排序进程

◆a:自动排序进程

◆d:显示/隐藏磁盘 I/O 统计信息

◆f:显示/隐藏文件系统统计信息

◆s:显示/隐藏传感器统计信息

◆y:显示/隐藏硬盘温度统计信息

◆l:显示/隐藏日志

◆n:显示/隐藏网络统计信息

◆x:删除警告和严重日志

◆h:显示/隐藏帮助界面

◆q:退出

◆w:删除警告记录

使用 Glances 监控远程系统

你也可以使用 Glances 监控远程系统。要在远程系统上使用它,使用下面的命令:

glances -s

你会看到类似下面的输出:

glances monitor remote system server

如你所见,Glances 运行在 61209 端口。

现在,到远程机器上执行下面的命令以连接到指定 IP 地址的 Glances 服务器上。假设 192168110 是你的 Glances 服务器 IP 地址。

glances -c -P 192168110

结论

对于每个 Linux 系统管理员来说,Glances 都是一个非常有用的工具。使用它,你可以轻松、高效地监控 Linux 系统。如果你有什么问题,自由地评论吧!

获取本机CPU温度、使用率、内存使用率、硬盘使用率等

在Python中获取系统信息的另一个好办法是使用psutil这个第三方模块。顾名思义,psutil = process and system utilities,它不仅可以通过一两行代码实现系统监控,还可以跨平台使用,支持Linux/UNIX/OSX/Windows等,是系统管理员和运维小伙伴不可或缺的必备模块。

需要安装psutil库

import sys   

import os    

    

import atexit   

import time    

import psutil   

    

#print "Welcome,current system is",osname," 3 seconds late start to get data"       

timesleep(3)   

    

line_num = 1   

    

#function of Get CPU State;   

def getCPUstate(interval=1):   

     return (" CPU: " + str(psutilcpu_percent(interval)) + "%")       

#function of Get Memory       

def getMemorystate():    

         phymem = psutilvirtual_memory()   

         line = "Memory: %5s%% %6s/%s"%(   

             phymempercent,   

             str(int(phymemused/1024/1024))+"M",   

             str(int(phymemtotal/1024/1024))+"M"   

             )   

         return line       

def bytes2human(n):       

         """     

         >>> bytes2human(10000)     

         '98 K'     

         >>> bytes2human(100001221)     

         '954 M'     

         """       

         symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')       

         prefix = {}       

         for i, s in enumerate(symbols):       

                 prefix[s] = 1 << (i+1)10       

         for s in reversed(symbols):       

                 if n >= prefix[s]:       

                         value = float(n) / prefix[s]       

                         return '%2f %s' % (value, s)       

         return '%2f B' % (n)       

          

          

def poll(interval):       

         """Retrieve raw stats within an interval window"""       

         tot_before = psutilnet_io_counters()       

         pnic_before = psutilnet_io_counters(pernic=True)       

         # sleep some time       

         timesleep(interval)       

         tot_after = psutilnet_io_counters()       

         pnic_after = psutilnet_io_counters(pernic=True)       

         # get cpu state       

         cpu_state = getCPUstate(interval)       

         # get memory       

         memory_state = getMemorystate()       

         return (tot_before, tot_after, pnic_before, pnic_after,cpu_state,memory_state)       

          

def refresh_window(tot_before, tot_after, pnic_before, pnic_after,cpu_state,memory_state):       

         if osname == 'nt':   

             ossystem("cls")       

         else:   

             ossystem("clear")       

         """Print stats on screen"""       

          

          

         #print current time #cpu state #memory       

         print(timeasctime()+" | "+cpu_state+" | "+memory_state)       

                  

         # totals       

         print(" NetStates:")       

         print("total bytes:                     sent: %-10s     received: %s" % (bytes2human(tot_afterbytes_sent),       

                                                                                                                                             bytes2human(tot_afterbytes_recv))       

         )       

         print("total packets:                 sent: %-10s     received: %s" % (tot_afterpackets_sent,      

                                                                                                                                             tot_afterpackets_recv)       

         )   

         # per-network interface details: let's sort network interfaces so       

         # that the ones which generated more traffic are shown first       

         print("")       

         nic_names = pnic_afterkeys()       

         #nic_namessort(key=lambda x: sum(pnic_after[x]), reverse=True)       

         for name in nic_names:       

                 stats_before = pnic_before[name]       

                 stats_after = pnic_after[name]       

                 templ = "%-15s %15s %15s"       

                 print(templ % (name, "TOTAL", "PER-SEC"))       

                 print(templ % (       

                         "bytes-sent",       

                         bytes2human(stats_afterbytes_sent),       

                         bytes2human(stats_afterbytes_sent - stats_beforebytes_sent) + '/s',       

                 ))       

                 print(templ % (       

                         "bytes-recv",       

                         bytes2human(stats_afterbytes_recv),       

                         bytes2human(stats_afterbytes_recv - stats_beforebytes_recv) + '/s',       

                 ))       

                 print(templ % (       

                         "pkts-sent",       

                         stats_afterpackets_sent,       

                         stats_afterpackets_sent - stats_beforepackets_sent,       

                 ))       

                 print(templ % (       

                         "pkts-recv",       

                         stats_afterpackets_recv,       

                         stats_afterpackets_recv - stats_beforepackets_recv,       

                 ))       

                 print("")       

          

try:       

         interval = 0       

         while 1:       

                 args = poll(interval)       

                 refresh_window(args)   

                 interval = 1       

except (KeyboardInterrupt, SystemExit):       

         pass

#!/usr/bin/env python

import os

import signal

# Change this to your process name

processname = 'aterm'

for line in ospopen("ps xa"):

fields = linesplit()

pid = fields[0]

process = fields[4]

if processfind(processname) > 0:

# Kill the Process Change signalSIGHUP to signalSIGKILL if you like

oskill(int(pid), signalSIGHUP)

# Do something else here

print "Doing something else here"

# Restart the process

ossystem(processname)

# Hop out of loop

break

以上就是关于如何在Ubuntu上使用Glances监控系统全部的内容,包括:如何在Ubuntu上使用Glances监控系统、python读取cpu温度和占用率、python有没有可以获取每个cpu内核空闲率的等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9614062.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存