Linux服务器那么多参数该如何监控,掌握这些Linux监控命令可以早点下班! 您所在的位置:网站首页 prevent怎么读 Linux服务器那么多参数该如何监控,掌握这些Linux监控命令可以早点下班!

Linux服务器那么多参数该如何监控,掌握这些Linux监控命令可以早点下班!

2023-03-13 03:53| 来源: 网络整理| 查看: 265

来源:网络技术联盟站 链接:https://www.wljslmz.cn/19901.html1. CPUcat /proc/cpuinfo # 物理 CPU 个数 cat /proc/cpuinfo | grep 'physical id' | sort | uniq | wc -l # 每个 CPU 核心数 cat /proc/cpuinfo | grep 'core id' | sort | uniq | wc -l # 逻辑 CPU cat /proc/cpuinfo | grep 'processor' | sort | uniq | wc -l # mpstat mpstat mpstat 2 10 2. 内存cat /proc/meminfo free -gt df -hT du -csh ./*

操作系统 IPC 共享内存/队列:

ipcs #(shmems, queues, semaphores)

平时我们经常需要监控内存的使用状态,常用的命令有free、vmstat、top、dstat -m等。

2.1 free> free -h total used free shared buffers cached Mem: 7.7G 6.2G 1.5G 17M 33M 184M -/+ buffers/cache: 6.0G 1.7G Swap: 24G 581M 23G 各行数据含义

第一行Mem:

total:内存总数7.7G,物理内存大小,就是机器实际的内存used:已使用内存6.2G,这个值包括了cached和应用程序实际使用的内存free:空闲的内存1.5G,未被使用的内存大小shared:共享内存的大小,17Mbuffers:被缓冲区占用的内存大小,33Mcached:被缓存占用的内存大小,184M

其中有:

total = used + free

第二行-/+ buffers/cache,代表应用程序实际使用的内存:

前一个值表示used - buffers/cached,表示应用程序实际使用的内存后一个值表示free + buffers/cached,表示理论上都可以被使用的内存

可以看到,这两个值加起来也是total

第三行swap,代表交换分区的使用情况:总量、使用的和未使用的

缓存 cache

cache代表缓存,当系统读取文件时,会先把数据从硬盘读到内存里,因为硬盘比内存慢很多,所以这个过程会很耗时。

为了提高效率,Linux 会把读进来的文件在内存中缓存下来(局部性原理),即使程序结束,cache 也不会被自动释放。因此,当有程序进行大量的读文件操作时,就会发现内存使用率升高了。

当其他程序需要使用内存时,Linux 会根据自己的缓存策略(例如 LRU)将这些没人使用的 cache 释放掉,给其他程序使用,当然也可以手动释放缓存:

echo 1 > /proc/sys/vm/drop_caches 缓冲区 buffer

考虑内存写文件到硬盘的场景,因为硬盘太慢了,如果内存要等待数据写完了之后才继续后面的操作,效率会非常低,也会影响程序的运行速度,所以就有了缓冲区buffer。

当内存需要写数据到硬盘中时会先放到 buffer 里面,内存很快把数据写到 buffer 中,可以继续其他工作,而硬盘可以在后台慢慢读出 buffer 中的数据并保存起来,这样就提高了读写的效率。

例如把电脑中的文件拷贝到 U 盘时,如果文件特别大,有时会出现这样的情况:明明看到文件已经拷贝完,但系统还是会提示 U 盘正在使用中。这就是 buffer 的原因:拷贝程序虽然已经把数据放到 buffer 中,但是还没有全部写入到 U 盘中

同样的,可以使用sync命令来手动flush buffer中的内容:

> sync --help Usage: sync [OPTION] [FILE]... Synchronize cached writes to persistent storage If one or more files are specified, sync only them, or their containing file systems. -d, --data sync only file data, no unneeded metadata -f, --file-system sync the file systems that contain the files --help display this help and exit --version output version information and exit GNU coreutils online help: Full documentation at: or available locally via: info '(coreutils) sync invocation' 交换分区 swap

交换分区swap是实现虚拟内存的重要概念。swap就是把硬盘上的一部分空间当作内存来使用,正在运行的程序会使用物理内存,把未使用的内存放到硬盘,叫做swap out。而把硬盘交换分区中的内存重新放到物理内存中,叫做swap in。

交换分区可以在逻辑上扩大内存空间,但是也会拖慢系统速度,因为硬盘的读写速度很慢。Linux 系统会将不经常使用的内存放到交换分区中。

cache 和 buffer 的区别cache:作为page cache的内存,是文件系统的缓存,在文件层面上的数据会缓存到page cache中buffer:作为buffer cache的内存,是磁盘块的缓存,直接对磁盘进行操作的数据会缓存到 buffer cache 中

简单来说:page cache用来缓存文件数据,buffer cache用来缓存磁盘数据。在有文件系统的情况下,对文件操作,那么数据会缓存到page cache中。如果直接采用dd等工具对磁盘进行读写,那么数据会缓存到buffer cache中。

2.2 vmstat

vmstat (Virtual Memory Statics,虚拟内存统计) 是对系统的整体情况进行统计,包括内核进程、虚拟内存、磁盘、中断和 CPU 活动的统计信息:

> vmstat --help Usage: vmstat [options] [delay [count]] Options: -a, --active active/inactive memory -f, --forks number of forks since boot -m, --slabs slabinfo -n, --one-header do not redisplay header -s, --stats event counter statistics -d, --disk disk statistics -D, --disk-sum summarize disk statistics -p, --partition partition specific statistics -S, --unit define display unit -w, --wide wide output -t, --timestamp show timestamp -h, --help display this help and exit -V, --version output version information and exit 来源 | 公众号:网络技术干货圈 For more details see vmstat(8). > vmstat -SM 1 100 # 1 表示刷新间隔(秒),100 表示打印次数,单位 MB procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 470 188 1154 0 0 0 4 3 0 0 0 99 0 0 0 0 0 470 188 1154 0 0 0 0 112 231 1 1 98 0 0 0 0 0 470 188 1154 0 0 0 0 91 176 0 0 100 0 0 0 0 0 470 188 1154 0 0 0 0 118 229 1 0 99 0 0 0 0 0 470 188 1154 0 0 0 0 78 156 0 0 100 0 0 0 0 0 470 188 1154 0 0 0 64 84 186 0 1 97 2 0 procsr列:表示运行和等待 CPU 时间片的进程数,这个值如果长期大于 CPU 个数,就说明 CPU 资源不足,可以考虑增加 CPUb列:表示在等待资源的进程数,例如正在等待 I/O 或者内存交换memoryswpn列:表示切换到交换分区的内存大小,如果swpd的值不为 0 或者比较大,且si、so的值长期为 0,那么这种情况暂时不会影响系统性能free列:当前空闲的物理内存大小buff列:表示buffers cache的内存大小,一般对块设备的读写才需要缓冲cache列:表示page cache的内存大小,一般作为文件系统的缓存,频繁访问的文件都会被 cached。如果 cache 值比较大,就说明 cached 文件数量较多。如果此时 I/O 中的bi比较小,就说明文件系统效率比较好swapsi列:表示swap in,即内存由交换分区放入物理内存中so列:表示swap out,即将未使用的内存放到硬盘的交换分区中iobi列:表示从块设备读取的数据总量,即读磁盘,单位KB/sbo列:表示写入块设备的数据总量,即写磁盘,单位KB/s

这里设置的bi+bo参考值为1000,如果超过1000,且wa值比较大,则表示系统磁盘 I/O 性能瓶颈

systemin列:表示在某一时间间隔中观察到的每秒设备中断数cs列:表示每秒产生的上下文切换次数

上面这两个值越大,内核消耗的 CPU 时间就越多

cpuus列:表示用户进程消耗 CPU 的时间百分比。us值比较高时,说明用户进程消耗的 CPU 时间多,如果长期大于 50%,可以考虑优化程序sy列:表示内核进程消耗 CPU 的时间百分比。sy值比较高时,说明内核消耗的 CPU 时间多,如果us+sy超过 80%,就说明 CPU 资源存在不足id列:表示 CPU 处在空闲状态的时间百分比wa列:表示 I/O Wait 所占 CPU 的时间百分比。wa值越高,说明 I/O Wait 越严重。如果wa值超过 20%,说明 I/O Wait 严重st列:表示 CPU Steal Time,针对虚拟机3. 网络3.1 接口ifconfig iftop ethtool 3.2 端口# 端口 netstat -ntlp # TCP netstat -nulp # UDP netstat -nxlp # UNIX netstat -nalp # 不仅展示监听端口,还展示其他阶段的连接 lsof -p -P lsof -i :5900 sar -n DEV 1 # 网络流量 ss ss -s 3.3 tcpdumpsudo tcpdump -i any udp port 20112 and ip[0x1f:02]=0x4e91 -XNnvvv sudo tcpdump -i any -XNnvvv sudo tcpdump -i any udp -XNnvvv sudo tcpdump -i any udp port 20112 -XNnvvv sudo tcpdump -i any udp port 20112 and ip[0x1f:02]=0x4e91 -XNnvvv 3.4 nethogs

监控各进程的网络流量

nethogs 4. I/O 性能iotop iostat iostat -kx 2 vmstat -SM vmstat 2 10 dstat dstat --top-io --top-bio 5. 进程top top -H htop ps auxf ps -eLf # 展示线程 ls /proc//task 5.1 top

例如最常用的top命令:

Help for Interactive Commands - procps version 3.2.8 Window 1:Def: Cumulative mode Off. System: Delay 3.0 secs; Secure mode Off. Z,B Global: 'Z' change color mappings; 'B' disable/enable bold l,t,m Toggle Summaries: 'l' load avg; 't' task/cpu stats; 'm' mem info 1,I Toggle SMP view: '1' single/separate states; 'I' Irix/Solaris mode f,o . Fields/Columns: 'f' add or remove; 'o' change display order F or O . Select sort field . Move sort field: '' next col right R,H . Toggle: 'R' normal/reverse sort; 'H' show threads c,i,S . Toggle: 'c' cmd name/line; 'i' idle tasks; 'S' cumulative time x,y . Toggle highlights: 'x' sort field; 'y' running tasks z,b . Toggle: 'z' color/mono; 'b' bold/reverse (only if 'x' or 'y') u . Show specific user only n or # . Set maximum tasks displayed k,r Manipulate tasks: 'k' kill; 'r' renice d or s Set update interval W Write configuration file q Quit ( commands shown with '.' require a visible task display window ) Press 'h' or '?' for help with Windows, any other key to continue 1: 显示各个 CPU 的使用情况c: 显示进程完整路径H: 显示线程P: 排序 - CPU 使用率M: 排序 - 内存使用率R: 倒序Z: Change color mappingsB: Disable/enable boldl: Toggle load avgt: Toggle task/cpu statsm: Toggle mem infous - Time spent in user space sy - Time spent in kernel space ni - Time spent running niced user processes (User defined priority) id - Time spent in idle operations wa - Time spent on waiting on IO peripherals (eg. disk) hi - Time spent handling hardware interrupt routines. (Whenever a peripheral unit want attention form the CPU, it literally pulls a line, to signal the CPU to service it) 来源 | 公众号:网络技术干货圈 si - Time spent handling software interrupt routines. (a piece of code, calls an interrupt routine...) st - Time spent on involuntary waits by virtual cpu while hypervisor is servicing another processor (stolen from a virtual machine) 5.2 lsoflsof -P -p 123 6. 性能测试stress --cpu 8 \ --io 4 \ --vm 2 \ --vm-bytes 128M \ --timeout 60s

time命令

7. 用户w whoami 8. 系统状态uptime htop vmstat mpstat dstat 9. 硬件设备lspci lscpu lsblk lsblk -fm # 显示文件系统、权限 lshw -c display dmidecode 10. 文件系统# 挂载 mount umount cat /etc/fstab # LVM pvdisplay pvs lvdisplay lvs vgdisplay vgs df -hT lsof 11. 内核、中断cat /proc/modules sysctl -a | grep ... cat /proc/interrupts 12. 系统日志、内核日志dmesg less /var/log/messages less /var/log/secure less /var/log/auth 13. cron 定时任务crontab -l crontab -l -u nobody # 查看所有用户的cron sudo find /var/spool/cron/ | sudo xargs cat 14. 调试工具14.1 perf14.2 strace

strace命令用于打印系统调用、信号:

strace -p strace -p 5191 -f strace -e trace=signal -p 5191 -e trace=open -e trace=file -e trace=process -e trace=network -e trace=signal -e trace=ipc -e trace=desc -e trace=memory 14.3 ltrace

ltrace命令用于打印动态链接库访问:

ltrace -p ltrace -S # syscall 15. 场景案例场景 1:连上服务器之后w # 显示当前登录的用户、登录 IP、正在执行的进程等 last # 看看最近谁登录了服务器、服务器重启时间 uptime # 开机时间、登录用户、平均负载 history # 查看历史命令 场景 2:/proc 目录有哪些信息cat /proc/... cgroups cmdline cpuinfo crypto devices diskstats filesystems iomem ioports kallsyms meminfo modules partitions uptime version vmstat 场景 3:后台执行命令nohup &>[some.log] & 一些命令# 综合 top htop glances dstat & sar mpstat # 性能分析 perf # 进程 ps pstree -p pgrep pkill pidof Ctrl+z & jobs & fg # 网络 ip ifconfig dig ping traceroute iftop pingtop nload netstat vnstat slurm scp tcpdump # 磁盘 I/O iotop iostat # 虚拟机 virt-top # 用户 w whoami # 运行时间 uptime # 磁盘 du df lsblk # 权限 chown chmod # 服务 systemctl list-unit-files # 定位 find locate # 性能测试 time


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有