linux常用工具命令

时间:2021-10-28 16:24:48

linux常用工具命令

@(LINUX)[linux]

(一)dmidecode

1、硬件平台

dmidecode | grep -5 "Product"

System Information
Manufacturer: IBM
Product Name: BladeCenter HS22 -[7870RR5]-
Version: 07
Serial Number: 99T8221
UUID: 945421FC-6D31-11E0-8BAF-5CF3FC215704
Wake-up Type: Other
SKU Number: XxXxXxX
Family: System x

Base Board Information
Manufacturer: IBM
Product Name: 68Y8163
Version: (none)
Serial Number: 1423X5
Asset Tag: (none)
Features:
Board is a hosting board

可以看出,这是IBM的BladeCenter HS22机器。

2、内存

 dmidecode -t memory

查看内存的硬件信息

3、cpu

dmidecode -t coprocessor

其实这些都是dmidecode输出内容中的一部分布局,完整列表如下:

Valid type keywords are:
bios
system
baseboard
chassis
processor
memory
cache
connector
slot

(二)1、sar

怀疑CPU存在瓶颈,可用 sar -u 和 sar -q 等来查看
怀疑内存存在瓶颈,可用 sar -B、sar -r 和 sar -W 等来查看
怀疑I/O存在瓶颈,可用 sar -b、sar -u 和 sar -d 等来查看
怀疑网络存在瓶颈,可用sar -n DEV来查看

还可以查看一周内的情况,如:

sar -u -f  /var/log/sysstat/sa27

注意是sa27,不是sar27。

输出的各项参考:http://www.chinaz.com/server/2013/0401/297942.shtml

(三)iostat

1、iostat -xk 1

(四)vmstat

1、 vmstat -S M 5

$ vmstat -S M 5  


procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----

r b swpd free buff cache si so bi bo in cs us sy id wa st

0 0 0 566 232 239 0 0 65 824 59 39 4 1 93 1 0

0 3 0 366 232 432 0 0 0 25929 2638 2776 14 14 43 28 0

2 1 0 241 232 543 0 0 26 38110 2123 1316 75 11 0 14 0

3 0 0 78 232 543 0 0 0 11784 1558 1028 80 4 16 0 0

0 0 0 189 232 543 0 0 0 142 1052 933 70 3 27 1 0

0 0 0 185 232 543 0 0 0 30 500 589 15 1 84 0 0

2 0 0 180 232 544 0 0 0 3 502 595 12 1 87 0 0

0 0 0 508 232 293 0 0 0 74 1161 1036 77 5 18 0 0

0 0 0 626 233 175 0 0 0 150 385 447 2 1 97 0 0

以上各个字段的意思分别是:

好了,命令介绍完毕,现在开始实战讲解每个参数的意思。

r 表示运行队列(就是说多少个进程真的分配到CPU),当这个值超过了CPU数目,就会出现CPU瓶颈 了。这个也和top的负载有关系,一般负载超过了3就比较高,超过了5就高,超过了10就不正常了,服务器的状态很危险。top的负载类似每秒的运行队 列。如果运行队列过大,表示你的CPU很繁忙,一般会造成CPU使用率很高。

b 表示阻塞的进程,这个不多说,进程阻塞,大家懂的。

swpd 虚拟内存已使用的大小,如果大于0,表示你的机器物理内存不足了,如果不是程序内存泄露的原因,那么你该升级内存了或者把耗内存的任务迁移到其他机器。

free 空闲的物理内存的大小,我的机器内存总共8G,剩余3415M。

buff Linux/Unix系统是用来存储,目录里面有什么内容,权限等的缓存,我本机大概占用300多M

cache cache直接用来记忆我们打开的文件,给文件做缓冲,我本机大概占用300多M(这里是Linux/Unix的聪明之处,把空闲的物理内存的一部分拿来做文件和目录的缓存,是为了提高 程序执行的性能,当程序使用内存时,buffer/cached会很快地被使用。)

si 每秒从磁盘读入虚拟内存的大小,如果这个值大于0,表示物理内存不够用或者内存泄露了,要查找耗内存进程解决掉。我的机器内存充裕,一切正常。

so 每秒虚拟内存写入磁盘的大小,如果这个值大于0,同上。

bi 块设备每秒接收的块数量,这里的块设备是指系统上所有的磁盘和其他块设备,默认块大小是1024byte,我本机上没什么IO操作,所以一直是0,但是我曾在处理拷贝大量数据(2-3T)的机器上看过可以达到140000/s,磁盘写入速度差不多140M每秒

bo 块设备每秒发送的块数量,例如我们读取文件,bo就要大于0。bi和bo一般都要接近0,不然就是IO过于频繁,需要调整。

in 每秒CPU的中断次数,包括时间中断

cs 每秒上下文切换次数,例如我们调用系统函数,就要进行上下文切换,线程的切换,也要进程上下文切换,这个值要越小越好,太大了,要考虑调低线程或者进程的 数目,例如在apache和nginx这种web服务器中,我们一般做性能测试时会进行几千并发甚至几万并发的测试,选择web服务器的进程可以由进程或 者线程的峰值一直下调,压测,直到cs到一个比较小的值,这个进程和线程数就是比较合适的值了。系统调用也是,每次调用系统函数,我们的代码就会进入内核 空间,导致上下文切换,这个是很耗资源,也要尽量避免频繁调用系统函数。上下文切换次数过多表示你的CPU大部分浪费在上下文切换,导致CPU干正经事的 时间少了,CPU没有充分利用,是不可取的。

us 用户CPU时间,我曾经在一个做加密解密很频繁的服务器上,可以看到us接近100,r运行队列达到80(机器在做压力测试,性能表现不佳)。

sy 系统CPU时间,如果太高,表示系统调用时间长,例如是IO操作频繁。

id 空闲 CPU时间,一般来说,id + us + sy = 100,一般我认为id是空闲CPU使用率,us是用户CPU使用率,sy是系统CPU使用率。

wt 等待IO CPU时间。

(五)/proc/*

1、查看CPU信息

 cat /proc/cpuinfo中的信息

processor 逻辑处理器的id。
physical id 物理封装的处理器的id。
core id 每个核心的id。
cpu cores 位于相同物理封装的处理器中的内核数量。
siblings 位于相同物理封装的处理器中的逻辑处理器的数量。



1 查看物理CPU的个数
#cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc –l

2、 查看逻辑CPU的个数
#cat /proc/cpuinfo |grep "processor"|wc –l

3、 查看CPU是几核
#cat /proc/cpuinfo |grep "cores"|uniq

4、 查看CPU的主频
#cat /proc/cpuinfo |grep MHz|uniq

5、 # uname -a

6、 Linux euis1 2.6.9-55.ELsmp #1 SMP Fri Apr 20 17:03:35 EDT 2007 i686 i686 i386 GNU/Linux

(查看当前操作系统内核信息)

7、 # cat /etc/issue | grep Linux

8、 Red Hat Enterprise Linux AS release 4 (Nahant Update 5(查看当前操作系统发行版信息)

9、 # cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c

8 Intel(R) Xeon(R) CPU E5410 @ 2.33GHz

(看到有8个逻辑CPU, 也知道了CPU型号)

9 # cat /proc/cpuinfo | grep physical | uniq -c

4 physical id : 0

4 physical id : 1

(说明实际上是两颗4核的CPU)



10、# getconf LONG_BIT

32

(说明当前CPU运行在32bit模式下, 但不代表CPU不支持64bit)



11、# cat /proc/cpuinfo | grep flags | grep ' lm ' | wc –l

8(结果大于0, 说明支持64bit计算. lm指long mode, 支持lm则是64bit)



12、如何获得CPU的详细信息:

linux命令:cat /proc/cpuinfo

13、用命令判断几个物理CPU,几个核等:

逻辑CPU个数:
# cat /proc/cpuinfo | grep "processor" | wc -l

物理CPU个数:
# cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l

14、每个物理CPU中Core的个数:
# cat /proc/cpuinfo | grep "cpu cores" | wc -l

15、是否为超线程?如果有两个逻辑CPU具有相同的”core id”,那么超线程是打开的。每个物理CPU中逻辑CPU(可能是core, threads或both)的个数:
# cat /proc/cpuinfo | grep "siblings"



2.查看内存信息命令

cat /proc/meminfo

3.查看硬盘信息命令

fdisk -l

(六)mtr

Mtr是 Linux中有一个非常棒的网络连通性判断工具,它结合了ping, traceroute,nslookup 的相关特性。

最简单的使用办法:
mtr ip

使用report的模式输出,统计次数为10次

mtr -r 8.8.8.8 -c 10
HOST: gdc-storm01-storm.i.nease.n Loss% Snt Last Avg Best Wrst StDev
1.|-- 192.168.172.3 0.0% 10 0.5 0.5 0.4 0.6 0.0
2.|-- 115.238.118.181 0.0% 10 0.7 0.7 0.6 0.8 0.1
3.|-- 124.160.128.125 0.0% 10 0.5 0.4 0.4 0.5 0.0
4.|-- 124.160.81.173 50.0% 10 9.5 6.1 2.8 9.5 2.7
| `|-- 124.160.82.81
| |-- 124.160.81.177
| |-- 124.160.81.185
| |-- 124.160.82.133
5.|-- 219.158.6.157 50.0% 10 29.1 38.5 26.1 70.8 18.4
| `|-- 219.158.99.209
6.|-- 219.158.3.250 0.0% 10 36.2 29.9 27.3 36.2 2.5
| `|-- 219.158.96.226
7.|-- 219.158.19.89 0.0% 10 46.5 29.5 26.3 46.5 6.0
| `|-- 219.158.96.254
8.|-- 219.158.101.178 0.0% 10 48.4 38.0 34.3 48.4 3.8
| `|-- 219.158.101.166
9.|-- 72.14.215.130 70.0% 10 28.9 29.1 28.7 29.6 0.5
10.|-- 209.85.248.62 50.0% 10 35.9 36.7 32.7 44.0 4.6
11.|-- 209.85.142.185 60.0% 10 114.0 113.5 111.3 114.9 1.5
12.|-- 209.85.247.97 50.0% 10 78.7 78.0 76.0 79.7 1.7
13.|-- 216.239.50.85 50.0% 10 110.2 111.9 108.6 118.1 4.1
14.|-- 72.14.237.177 30.0% 10 116.6 117.9 113.9 133.0 6.8
15.|-- ??? 100.0 10 0.0 0.0 0.0 0.0 0.0
16.|-- google-public-dns-a.googl 20.0% 10 113.7 116.3 113.7 117.4 1.2

mtr -s:

用来指定ping数据包的大小

mtr -a:

来设置发送数据包的IP地址 这个对一个主机由多个IP地址是有用的

(七)iperf:检查2台机器间的网络带宽

其中一台充当服务器:

# iperf -s  

------------------------------------------------------------

Server listening on TCP port 5001

TCP window size: 85.3 KByte (default)

------------------------------------------------------------

[ 4] local 10.171.94.155 port 5001 connected with 10.171.29.191 port 46455

------------------------------------------------------------

Client connecting to 10.171.29.191, TCP port 5001

TCP window size: 143 KByte (default)

------------------------------------------------------------

[ 6] local 10.171.94.155 port 52215 connected with 10.171.29.191 port 5001

[ ID] Interval Transfer Bandwidth

[ 6] 0.0-10.0 sec 664 MBytes 557 Mbits/sec

[ 4] 0.0-10.0 sec 466 MBytes 390 Mbits/sec

另一台充当客户端:

# iperf -c 10.171.94.155 -f m -d  

------------------------------------------------------------

Server listening on TCP port 5001

TCP window size: 0.08 MByte (default)

------------------------------------------------------------

------------------------------------------------------------

Client connecting to 10.171.94.155, TCP port 5001

TCP window size: 0.10 MByte (default)

------------------------------------------------------------

[ 4] local 10.171.29.191 port 46455 connected with 10.171.94.155 port 5001

[ 5] local 10.171.29.191 port 5001 connected with 10.171.94.155 port 52215

[ ID] Interval Transfer Bandwidth

[ 4] 0.0-10.0 sec 466 MBytes 390 Mbits/sec

[ 5] 0.0-10.0 sec 664 MBytes 555 Mbits/sec

(八)tcpdump:检查数据包的传输情况

# tcpdump port 8649  

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

20:43:11.396729 IP master.38498 > slave1.8649: UDP, length 136

20:43:11.396746 IP master.38498 > slave1.8649: UDP, length 64

20:43:11.397101 IP master.38498 > slave1.8649: UDP, length 136

20:43:11.397105 IP master.38498 > slave1.8649: UDP, length 64

20:43:11.397107 IP master.38498 > slave1.8649: UDP, length 136

20:43:11.397108 IP master.38498 > slave1.8649: UDP, length 80

20:43:11.397109 IP master.38498 > slave1.8649: UDP, length 64

20:43:11.397110 IP master.38498 > slave1.8649: UDP, length 144

20:43:11.397111 IP master.38498 > slave1.8649: UDP, length 68

20:43:11.397112 IP master.38498 > slave1.8649: UDP, length 156

20:43:11.397114 IP master.38498 > slave1.8649: UDP, length 188

20:43:11.397115 IP master.38498 > slave1.8649: UDP, length 92

20:43:11.397116 IP master.38498 > slave1.8649: UDP, length 88

还可以使用host等参数。

(九)nslookup

(十)traceroute

(十一)strace