2019-02-25笔记—监控Linux系统状态
查看系统负载
- w命令
[[email protected] ~]# w
10:11:05 up 10 min, 1 user, load average: 0.00, 0.08, 0.11
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/0 192.168.85.1 10:01 1.00s 0.13s 0.01s w
- load average 分别表示1,5,15分钟的系统平均负载(负载:平均时间内有多少进程占用cpu)
- load average数据是每隔5秒钟检查一次活跃的进程数,然后按特定算法计算出的数值。如果这个数除以逻辑CPU的数量,结果高于5的时候就表明系统在超负荷运转了。
- uptime命令
[[email protected] ~]# uptime
10:13:19 up 12 min, 1 user, load average: 0.00, 0.05, 0.10
- top命令(默认按照cpu使用率从高到低排序,每隔3s刷新一次)
top - 10:28:27 up 27 min, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 109 total, 1 running, 108 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.3 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2028116 total, 1606156 free, 238916 used, 183044 buff/cache
KiB Swap: 4194300 total, 4194300 free, 0 used. 1612500 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
9 root 20 0 0 0 0 S 0.3 0.0 0:01.46 rcu_sched
30 root 20 0 0 0 0 S 0.3 0.0 0:03.60 kworker/0:1
6106 root 20 0 227204 6480 5116 S 0.3 0.3 0:04.23 vmtoolsd
7135 root 20 0 162004 2240 1564 R 0.3 0.1 0:00.11 top
1 root 20 0 190972 3772 2572 S 0.0 0.2 0:02.40 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.11 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root rt 0 0 0 0 S 0.0 0.0 0:00.05 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
11 root rt 0 0 0 0 S 0.0 0.0 0:00.02 watchdog/0
12 root rt 0 0 0 0 S 0.0 0.0 0:00.01 watchdog/1
13 root rt 0 0 0 0 S 0.0 0.0 0:00.03 migration/1
14 root 20 0 0 0 0 S 0.0 0.0 0:00.06 ksoftirqd/1
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1:0H
18 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kdevtmpfs
19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
20 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd
21 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeback
22 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrityd
23 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
24 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
25 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
26 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kblockd
27 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 md
28 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 edac-poller
29 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 watchdogd
35 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kswapd0
36 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd
37 root 39 19 0 0 0 S 0.0 0.0 0:00.29 khugepaged
38 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 crypto
3.1 系统运行时间和系统负载
- 当前时间
- 系统已运行的时间
- 当前登录用户的数量
- 相应最近5、10和15分钟内的平均负载。
3.2 任务
- Tasks — 任务(进程),系统现在共有108个进程,其中处于运行中的有2个,106个在休眠(sleep),stoped状态的有0个,zombie状态(僵尸)的有0个。
- zombie(出现僵尸进程的话需要关注一下)
3.3 cpu状态
- 0.2 us:用户进程占用cpu百分比
- 0.3 sy:内核/系统进程占用cpu百分比
- 0.0 ni:改变过优先级的进程占用的cpu百分比
- 99.5 id:空闲cpu百分比(一般shell脚本中这个数值用于计算cpu使用率)
- 0.0 wa:IO等待占用CPU的百分比
- 0.0 hi:硬中断(Hardware IRQ)占用CPU的百分比
- 0.0 si:软中断(Software Interrupts)占用CPU的百分比
3.4 内存状态
- 第一行是物理内存使用,第二行是虚拟内存使用
- 可用内存计算(不是特别准确):物理的free+物理的buffers+虚拟的cached
- 在top里我们要重点监控虚拟swap交换分区的used,如果这个数值在不断的变化,说明内核在不断进行内存和swap的数据交换,真正说明内存不够用
3.5 详细进程状态监控(重点关注RES/S/%CPU/&MEN)
- PID:进程ID,进程的唯一标识符
- USER:进程所有者的实际用户名。
- PR:进程的调度优先级。这个字段的一些值是’rt’。这意味这这些进程运行在实时态。
- NI:进程的优先级。越小的值意味着越高的优先级。负值表示高优先级,正值表示低优先级
- VIRT:进程使用的虚拟内存。单位kb。VIRT=SWAP+RES
- RES:驻留内存大小。驻留内存是任务使用的非交换物理内存大小。进程使用的、未被换出的物理内存大小,单位kb。RES=CODE+DATA
- SHR:SHR是进程使用的共享内存,单位kb
- S:这个是进程的状态。它有以下不同的值:
D - 不可中断的睡眠状态。
R – 运行状态
S – 睡眠状态
T – 被跟踪或已停止
Z – 僵尸状态
- %CPU:自从上一次更新时到现在任务所使用的CPU时间百分比。
- %MEM:进程使用的可用物理内存百分比。
- TIME+:任务启动后到现在所使用的全部CPU时间,精确到百分之一秒。
- COMMAND:运行进程所使用的命令。进程名称(命令名/命令行)
3.6 top命令补充
- top -bn1:静态显示所有进程的情况,也是按cpu百分比排序,方便在shell脚本中使用top命令
- top命令界面,按数字1 ,可以显示所有cpu使用详情
- top命令界面,按h,可以查看帮助
- top命令界面,x,y,b,打开/关闭加亮效果
- top命令界面,按M,按内存使用率排序
- top命令界面,按s,修改刷新频率
- top命令界面,按f,可自定义top界面字段显示
- cat /proc/cpuinfo |grep processor:查看cpu核数
- lscpu:查看cpu信息
- free命令
[[email protected] ~]# free
total used free shared buff/cache available
Mem: 2028116 243864 1547712 9736 236540 1603212
Swap: 4194300 0 4194300
[[email protected] ~]# free -m
total used free shared buff/cache available
Mem: 1980 238 1511 9 230 1565
Swap: 4095 0 4095
[[email protected] ~]# free -h
total used free shared buff/cache available
Mem: 1.9G 238M 1.5G 9.5M 230M 1.5G
Swap: 4.0G 0B 4.0G
- available:真正剩余物理内存
- buffer:是io缓存,用于cpu到磁盘之间的内存
- cache:用于磁盘到cpu之间使用的内存
手动增加swap
- /dev/zero(造零器,可以无限制产生空字符)
- /dev/null(空设备/黑洞),丢弃一切写入的数据
[[email protected] /]# time dd if=/dev/zero of=/bigfile bs=1M count=1000
记录了1000+0 的读入
记录了1000+0 的写出
1048576000字节(1.0 GB)已复制,5.25927 秒,199 MB/秒
real 0m5.262s
user 0m0.004s
sys 0m5.258s
[[email protected] /]# ll bigfile -h
-rw-r--r-- 1 root root 1000M 2月 21 14:52 bigfile
[[email protected] /]# mkswap /bigfile
正在设置交换空间版本 1,大小 = 1023996 KiB
无标签,UUID=b449c534-002b-48c5-91e4-7ed1bf79a82c
[[email protected] /]# chmod 600 /bigfile
[[email protected] /]# free -h
total used free shared buff/cache available
Mem: 1.9G 238M 483M 9.5M 1.2G 1.5G
Swap: 4.0G 0B 4.0G
[[email protected] /]# swapon /bigfile
[[email protected] /]# free -h
total used free shared buff/cache available
Mem: 1.9G 238M 483M 9.5M 1.2G 1.5G
Swap: 5.0G 0B 5.0G
[[email protected] /]# swapoff /bigfile
[[email protected] /]# free -h
total used free shared buff/cache available
Mem: 1.9G 238M 483M 9.5M 1.2G 1.5G
Swap: 4.0G 0B 4.0G
- 磁盘监控
5.1 iostat
[[email protected] ~]# yum install -y sysstat
[[email protected] ~]# iostat
Linux 3.10.0-957.1.3.el7.x86_64 (linux2019) 2019年02月22日 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
5.54 0.00 4.51 2.10 0.00 87.85
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 18.21 800.26 165.11 365784 75468
[[email protected] ~]# iostat -d #只查看磁盘状态
Linux 3.10.0-957.1.3.el7.x86_64 (linux2019) 2019年02月22日 _x86_64_ (2 CPU)
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 18.06 793.65 163.83 365784 75508
[[email protected] ~]# iostat -dx #显示信息更全
Linux 3.10.0-957.1.3.el7.x86_64 (linux2019) 2019年02月22日 _x86_64_ (2 CPU)
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 0.58 15.05 2.72 779.71 161.22 105.92 0.81 45.86 10.17 243.28 4.71 8.38
[[email protected] ~]# iostat -dx 1 #每隔1秒刷新一次
Linux 3.10.0-957.1.3.el7.x86_64 (linux2019) 2019年02月22日 _x86_64_ (2 CPU)
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 0.57 14.92 2.83 772.90 160.90 105.22 0.81 45.52 10.17 231.59 4.71 8.35
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 0.00 0.00 2.00 0.00 12.00 12.00 0.22 321.50 0.00 321.50 55.00 11.00
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
- 重点关注%util参数(磁盘使用百分比)
- iostat -dx 1 5 :每秒刷新一次,显示5次
5.2 iotop (默认按io百分百排序)
[[email protected] ~]# yum install -y iotop
5.3 vmstat 命令(综合监测,断定瓶颈点)
[[email protected] ~]# vmstat 1 3
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 940168 2076 475372 0 0 123 27 140 181 3 2 95 1 0
0 0 0 940300 2076 475372 0 0 0 0 200 309 1 0 99 0 0
0 0 0 940300 2076 475372 0 0 0 0 146 255 1 1 99 0 0
procs 进程
- r :等待运行的进程数量,是一个平均值,可以是非整数,相当于top中进程状态的running+waitting。
- b:被阻塞的进程数量。
memory 内存
- swpd:已使用的虚拟内存,非零状态下,可能是物理内存已经不够用了,要结合si、so参数状态判断
swap 交换内存
- si:从磁盘交换到swap虚拟内存的交换页数量,单位:KB/秒。如果这个值大于0,表示物理内存不够用或者内存泄露了
- so:从swap虚拟内存交换到磁盘的交换页数量,单位:KB/秒,如果这个值大于0,表示物理内存不够用或者内存泄露了
io
- bi:磁盘读的速度
- bo:磁盘写的速度
system
- in:中断数量,越小越好
- cs:上下文切换数量,越小越好
cpu
- id:cpu空闲百分比
- 网卡流量监控
[[email protected] ~]# sar -n DEV 1 2 #查看网卡流量(rxkB/s代表带宽)
Linux 3.10.0-957.1.3.el7.x86_64 (linux2019) 2019年02月25日 _x86_64_ (2 CPU)
15时47分15秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
15时47分16秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
15时47分16秒 ens33 0.00 0.00 0.00 0.00 0.00 0.00 0.00
15时47分16秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
15时47分17秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
15时47分17秒 ens33 1.00 2.00 0.10 0.44 0.00 0.00 0.00
平均时间: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
平均时间: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: ens33 0.50 1.00 0.05 0.22 0.00 0.00 0.00
[[email protected] ~]# sar 1 2 -b #磁盘读写
Linux 3.10.0-957.1.3.el7.x86_64 (linux2019) 2019年02月25日 _x86_64_ (2 CPU)
15时48分48秒 tps rtps wtps bread/s bwrtn/s
15时48分49秒 0.00 0.00 0.00 0.00 0.00
15时48分50秒 4.00 0.00 4.00 0.00 72.00
平均时间: 2.00 0.00 2.00 0.00 36.00
[[email protected] ~]# sar 1 2 #默认采集cpu
Linux 3.10.0-957.1.3.el7.x86_64 (linux2019) 2019年02月25日 _x86_64_ (2 CPU)
15时47分44秒 CPU %user %nice %system %iowait %steal %idle
15时47分45秒 all 0.51 0.00 0.00 0.00 0.00 99.49
15时47分46秒 all 0.00 0.00 0.51 0.00 0.00 99.49
平均时间: all 0.25 0.00 0.25 0.00 0.00 99.49
[[email protected] ~]# sar 1 2 -q #查看性能负载
Linux 3.10.0-957.1.3.el7.x86_64 (linux2019) 2019年02月25日 _x86_64_ (2 CPU)
15时57分51秒 runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked
15时57分52秒 0 230 0.02 0.02 0.06 0
15时57分53秒 0 230 0.02 0.02 0.06 0
平均时间: 0 230 0.02 0.02 0.06 0
[[email protected] ~]# mii-tool ens33 #查看网卡信息
ens33: negotiated 1000baseT-FD flow-control, link ok
[[email protected] ~]# ethtool ens33 #查看网卡信息
Settings for ens33:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Supported FEC modes: Not reported
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Advertised FEC modes: Not reported
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
MDI-X: off (auto)
Supports Wake-on: d
Wake-on: d
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes
- 系统进程监控
[[email protected] ~]# ps aux
[[email protected] ~]# ps -elf
[[email protected] ~]# ps -eLf #查看详细线程
STAT 状态说明
- S:Sleep
- R:Running
- s:父进程
- N:低优先级
- <:高优先级
- +:前台进程
- l:多线程进程
- Z:僵尸进程
[[email protected] ~]# ps aux|grep mysql
root 6773 0.0 0.0 115436 1768 ? S 15:30 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/linux2019.pid
mysql 6924 0.3 4.7 1717392 96424 ? Sl 15:30 0:08 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mariadb.log --pid-file=/data/mysql/linux2019.pid --socket=/tmp/mysql.sock
root 7379 0.0 0.0 112728 988 pts/0 S+ 16:14 0:00 grep --color=auto mysql
[[email protected] ~]# ls /proc/6924 #查看一个进程的详细信息
attr cgroup comm cwd fd io map_files mountinfo net oom_adj pagemap projid_map schedstat smaps statm task wchan
autogroup clear_refs coredump_filter environ fdinfo limits maps mounts ns oom_score patch_state root sessionid stack status timers
auxv cmdline cpuset exe gid_map loginuid mem mountstats numa_maps oom_score_adj personality sched setgroups stat syscall uid_map
- 查看网络连接状态
[[email protected] ~]# netstat -lnptu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 6792/php-fpm: maste
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6727/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6674/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 6827/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 6727/nginx: master
tcp6 0 0 :::3306 :::* LISTEN 6924/mysqld
tcp6 0 0 127.0.0.1:41934 :::* LISTEN 7174/java
tcp6 0 0 127.0.0.1:21934 :::* LISTEN 7174/java
tcp6 0 0 :::8080 :::* LISTEN 7153/java
tcp6 0 0 :::22 :::* LISTEN 6674/sshd
tcp6 0 0 ::1:25 :::* LISTEN 6827/master
tcp6 0 0 127.0.0.1:51934 :::* LISTEN 7174/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 7153/java
udp 0 0 127.0.0.1:323 0.0.0.0:* 6129/chronyd
udp6 0 0 ::1:323 :::* 6129/chronyd
[[email protected] ~]# ss -ano #功能用法类似netstat
- -l:LISTEN
- -t:tcp端口
- -u:udp端口
- -n:直接使用ip地址,而不通过域名服务器
- -p:显示正在使用Socket的程序识别码和程序名称
- 抓包工具
[[email protected] ~]# tcpdump -nn -i ens33 port 80 #i指定网卡,
[[email protected] ~]# tcpdump -nn -i ens33 -c 100 -w 1.cap #c指定抓包数量 ,w保存到文件
[[email protected] ~]# tcpdump -i ens33 -nn not port 22 and not port 80 and not host 192.168.222.1 #对流量包做限定
[[email protected] ~]# tcpdump -nn -r 1.cap #read
[[email protected] ~]# yum install -y wireshark
[[email protected] ~]# tshark -i ens33 -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri" #对显示结果进行自定义以便查看
tshark: -R without -2 is deprecated. For single-pass filtering use -Y.
Running as user "root" and group "root". This could be dangerous.
Capturing on 'ens33'
"Feb 25, 2019 16:38:35.171470487 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/background.png
"Feb 25, 2019 16:38:35.174159979 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/switch_width.png
"Feb 25, 2019 16:38:35.185432955 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/px.png
"Feb 25, 2019 16:38:35.188386686 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/newarow.gif
"Feb 25, 2019 16:38:35.188846341 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/pn.png
"Feb 25, 2019 16:38:35.197134210 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/nv.png
"Feb 25, 2019 16:38:35.204988926 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/qmenu.png
"Feb 25, 2019 16:38:35.205373180 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/nv_a.png
"Feb 25, 2019 16:38:35.205438229 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/search.png
"Feb 25, 2019 16:38:35.222168361 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/pt_item.png
"Feb 25, 2019 16:38:35.228242711 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/fav.gif
"Feb 25, 2019 16:38:35.230104005 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/feed.gif
"Feb 25, 2019 16:38:35.230132776 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/increase.png
"Feb 25, 2019 16:38:35.230144612 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/arw_l.gif
"Feb 25, 2019 16:38:35.234321635 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/atarget.png
"Feb 25, 2019 16:38:35.254423296 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/arrwd.gif
"Feb 25, 2019 16:38:35.262068234 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/refresh.png
"Feb 25, 2019 16:38:35.276178222 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/editor/editor.gif
"Feb 25, 2019 16:38:35.473982058 CST" 192.168.85.1 bbs.aibenwoniu.xyz GET /static/image/common/scrolltop.png