日常运维

知识点

  • w ,vmstat ,top ,sar ,nload ,iostst ,free ,ps ,netstat ,tcpdump ,ifdown ,ifup

w命令 可以查看系统负载

[root@localhost ~]# w
 20:58:32 up 13 min,  1 user,  load average: 0.00, 0.02, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.139.1    20:47    0.00s  0.04s  0.00s w

主要关注load average这个点,其中后面的数字分别代表1分钟、5分钟、15分钟的负载。 那负载的值为多少才合适呢!通过cat /proc/cpuinfo查看

[root@localhost ~]# cat /proc/cpuinfo
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 58
model name	: Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz

如果你的processor为0,代表只有一颗cpu,所以最佳的负载值就是1。 如果是7则最佳的负载值就是8。

vmstat命令

用w查看发现load高于processor,就可以使用vmstat找到瓶颈在哪里。

[root@localhost ~]# vmstat 1 5  //1代表1秒 5代表5次 
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 768784   2076 133844    0    0    19     3   55   56  0  0 99  0  0
 0  0      0 768768   2076 133844    0    0     0     0   43   39  0  1 99  0  0
 0  0      0 768768   2076 133844    0    0     0     0   50   42  0  0 100  0  0
 0  0      0 768768   2076 133844    0    0     0     0   40   34  0  0 100  0  0
 0  0      0 768768   2076 133844    0    0     0     0   48   39  0  0 100  0  0

其中r列表示有多少个进程处于run的状态。b列表示进程被卡死等待的状态。swpd表示使用虚拟内存的数值,内存不够时会变大,所以0为最佳。free是空闲的内存,buff是缓冲(cpu通过内存写数据到磁盘上),cache是缓存(cpu通过内存从磁盘读取数据)。。。。

top命令

top - display Linux processes

使用vmstat查看之后使用top命令可以显示linux的进程。查看具体是哪个进程有问题!top是动态显示系统进程的。像windows里的任务管理器

[root@localhost ~]# top
top - 22:37:59 up  1:52,  1 user,  load average: 0.00, 0.01, 0.05
Tasks:  83 total,   1 running,  82 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   999696 total,   762352 free,    95532 used,   141812 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   741948 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                      
   520 root      20   0  305296   6248   4872 S  0.7  0.6   0:22.25 vmtoolsd                                     
   948 root      20   0       0      0      0 S  0.3  0.0   0:21.03 kworker/0:0                                  
     1 root      20   0  128164   6808   4044 S  0.0  0.7   0:03.01 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.17 ksoftirqd/0                                  
     5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                                 
.....

  • 按M 按内存排行
  • 按P 按cpu排行
  • 按1 互相切换cpu
  • -bn1 静态显示
  • -c 显示具体的命令

通过观察发现top命令基本包含了w、vmstat的基本内容。==懒人神器==

然而使用top -c可以查看具体哪个命令。

[root@localhost ~]# top -c
top - 22:39:49 up  1:54,  1 user,  load average: 0.00, 0.01, 0.05
Tasks:  83 total,   1 running,  82 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   999696 total,   762336 free,    95532 used,   141828 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   741932 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                      
   520 root      20   0  305296   6248   4872 S  0.3  0.6   0:22.61 /usr/bin/vmtoolsd                            
   948 root      20   0       0      0      0 S  0.3  0.0   0:21.38 [kworker/0:0]                                
     1 root      20   0  128164   6808   4044 S  0.0  0.7   0:03.01 /usr/lib/systemd/systemd --switched-root --s+
     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.17 [ksoftirqd/0]                                
     5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 [kworker/0:0H]                               
     6 root      20   0       0      0      0 S  0.0  0.0   0:00.05 [kworker/u256:0]                             
     7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 [migration/0]                                

sar命令

查看网卡流量,先安装yum install -y sysstat

[root@localhost ~]# sar -n DEV 1 3
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 	2018年11月29日 	_x86_64_	(1 CPU)

23时00分16秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
23时00分17秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23时00分17秒     ens33      1.02      1.02      0.06      0.20      0.00      0.00      0.00

23时00分17秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
23时00分18秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23时00分18秒     ens33      1.00      1.00      0.06      0.40      0.00      0.00      0.00

23时00分18秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
23时00分19秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23时00分19秒     ens33      1.02      1.02      0.06      0.41      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      1.01      1.01      0.06      0.34      0.00      0.00      0.00

个人感觉sar最大的用处在于他会监控网卡流量,然后将数据保存到/var/log/sa/saXX。 每天保存一份,最多保存一个月。如果哪天怀疑服务器流量不正常的时候,可以调出历史数据进行比对。使用命令 sar -f /var/log/sa/saXX 如果是sarXX则可以直接cat。

nload命令

nload也是查看网卡流量的命令,个人感觉用nload看网卡流量比sar要舒服。首先也许要安装 yum install -y nload

使用左右按钮可以切换网卡!

iostat命令

cpu和内存查看完了,现在来看看磁盘。iostat查看磁盘使用命令。

关键的注意点在%util,该参数暗示了设备的繁忙程度 。一般地,如果该参数是100%表示设备已经接近满负荷运行了。所以越小越好!

iotop也可以查看磁盘的使用情况,而且他可以具体到进程!

free命令

查看内存使用情况

[root@localhost ~]# free
              total        used        free      shared  buff/cache   available
Mem:         999696       97424      587652        6808      314620      716584
Swap:       2097148           0     2097148

哇靠 top里基本上都有的。

ps命令

ps - report a snapshot of the current processes 静态显示任务管理器。

[root@localhost ~]# ps -aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.6 128164  6816 ?        Ss   20:45   0:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root          2  0.0  0.0      0     0 ?        S    20:45   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    20:45   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   20:45   0:00 [kworker/0:0H]
root          6  0.0  0.0      0     0 ?        S    20:45   0:00 [kworker/u256:0]
root          7  0.0  0.0      0     0 ?        S    20:45   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    20:45   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        R    20:45   0:01 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S    20:45   0:00 [watchdog/0]
root         12  0.0  0.0      0     0 ?        S    20:45   0:00 [kdevtmpfs]
root         13  0.0  0.0      0     0 ?        S<   20:45   0:00 [netns]
root         14  0.0  0.0      0     0 ?        S    20:45   0:00 [khungtaskd]
root         15  0.0  0.0      0     0 ?        S<   20:45   0:00 [writeback]
root         16  0.0  0.0      0     0 ?        S<   20:45   0:00 [kintegrityd]
root         17  0.0  0.0      0     0 ?        S<   20:45   0:00 [bioset]
root         18  0.0  0.0      0     0 ?        S<   20:45   0:00 [kblockd]
root         19  0.0  0.0      0     0 ?        S<   20:45   0:00 [md]
root         25  0.0  0.0      0     0 ?        S    20:45   0:00 [kswapd0]
....

然后看到哪个进程不爽,可以使用kill xx(PID号)将其结束掉。也可以配合管道符 ‘\’使用。比如 ==ps aux | grep ngix== 查看ngix是否有启动!最后是STAT列的含义了

  • D 表示不能中断的进程
  • R 表示run状态的进程
  • S sleep的进程
  • T 暂停
  • Z 僵尸
  • < 高优先级
  • N 低优先级
  • L 内存中被锁了内存分页
  • s 主进程
  • L 多线程进程
    • 前台进程

netstat命令

查看网络状态。个人感觉用到最多的就是可以查看哪些端口有开放。

  • -lnp 查看监听端口
  • -an 查看网络连接状况
  • -lntp 只查看tcp的连接
[root@localhost ~]# netstat -ltnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      807/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      890/master          
tcp6       0      0 :::22                   :::*                    LISTEN      807/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      890/master          

统计各个状态的连接数: ==netstat -an | awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}'==

tcpdump命令

抓包命令,一个很实用的命令。先安装 yum install -y tcpdump。举例2个用法,自己举一反三。

++tcpdump -nn -i ens33++

++tcpdump -nn not port 22 and host 192.168.0.100++

++tcpdump -nn -c 100 -w 1.cap++ //这个要配合 tcpdump -r 1.cap使用!

还有一个在windows里被广泛使用的抓包工具wireshark。yum install -y wireshark。

==tshark -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"==


ifdown ens33 && ifup ens33 //重启网卡,down掉马上再up。

mii-tool ens33 || ethtool ens33 //查看网口是否有网线连接

hostnamectl set-hostname xxoo //设置hostname,配置文件 /etc/hostname

DNS的配置文件 /etc/resolv.conf

本地host文件 /etc/hosts


w、vmstat、free 记top就好

sar 主要做监控

nload 很棒的查看网络流量工具

iostst 上面几个要么看cpu要么看内存MEM,这个就看磁盘

ps 有很多STAT

netstat 主要查看端口

tcpdump 抓包工具

ifdown ens33 && ifup ens33 重启网卡神命令

mii-tool、ethtool 查看是否有连网线detect link

hostnamectl set-hostname xxoo 设置hostname

转载于:https://my.oschina.net/u/3997678/blog/2977247

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值