机器资源
在维护服务过程中,要时刻了解机器资源的使用情况,这是服务运行正常的基本条件。常用的需要了解系统信息、cpu使用率、内存使用情况、磁盘使用情况、网络、端口连接、进程查询等等。
常用资源查看指令
系统信息
uname : 查看系统信息。
先看说明书:
> uname --help
Usage: uname [OPTION]... // 使用规则
Print certain system information. With no OPTION, same as -s. //默认是 uname -s
-a, --all print all information, in the following order,
except omit -p and -i if unknown:
// 按照下面顺序打印所有信息。最常用!
-s, --kernel-name print the kernel name // 输出内核名称
-n, --nodename print the network node hostname // 网络主机名
-r, --kernel-release print the kernel release // 发型号
-v, --kernel-version print the kernel version // 版本号
-m, --machine print the machine hardware name // 机器硬件名
-p, --processor print the processor type or "unknown" // 处理器类型
-i, --hardware-platform print the hardware platform or "unknown" // 硬件相关
-o, --operating-system print the operating system // 操作系统
--help display this help and exit
--version output version information and exit
示例:
[vip@test ~]$ uname -a
Linux test 3.10.0-862.9.1.el7.x86_64 #1 SMP Mon Jul 16 16:29:36 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[vip@test ~]$ uname -s
Linux
[vip@test ~]$ uname -n
test
[vip@test ~]$ uname -r
3.10.0-862.9.1.el7.x86_64
[vip@test ~]$ uname -v
#1 SMP Mon Jul 16 16:29:36 UTC 2018
[vip@test ~]$ uname -m
x86_64
[vip@test ~]$ uname -p
x86_64
[vip@test ~]$ uname -i
x86_64
[vip@test ~]$ uname -o
GNU/Linux
env : 查看 / 设置系统环境变量。 说明书:
[vip@test ~]$ env --help
Usage: env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
Set each NAME to VALUE in the environment and run COMMAND.
设置环境变量、执行 command
Mandatory arguments to long options are mandatory for short options too.
-i, --ignore-environment start with an empty environment // 提供干净环境
-0, --null end each output line with 0 byte rather than newline
-u, --unset=NAME remove variable from the environment
--help display this help and exit
--version output version information and exit
A mere - implies -i. If no COMMAND, print the resulting environment.
示例:
[vip@test ~]$ env
XDG_SESSION_ID=196756
HOSTNAME=test
TERM=xterm
SHELL=/bin/bash
HISTSIZE=2000
SSH_CLIENT=10.16.192.171 55438 22
USER=vip
PATH=/usr/local/jdk/bin:/usr/local/bin:/usr/bin:/usr/local/sbin
PWD=/home/vip
JAVA_HOME=/usr/local/jdk
...
CPU
说明书:
通过 cat /proc/cpuinfo
来查看CPU硬件的相关信息。
示例:
[vip@test ~]$ cat /proc/cpuinfo
processor : 0 // 该逻辑处理器的唯一标识符
vendor_id : GenuineIntel
cpu family : 6
model : 85
model name : Intel(R) Xeon(R) Silver 4110 CPU @ 2.10GHz
stepping : 4
microcode : 0x200004d
cpu MHz : 2100.000
cache size : 11264 KB
physical id : 0 // 每个物理封装的唯一标识符
siblings : 16 // 位于相同物理封装中的逻辑处理器的数量
core id : 0 // 每个内核的唯一标识符
cpu cores : 8 // 位于相同物理封装中的内核数量
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm (long mode, 可以根据该项判断是否支持 x64) constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke spec_ctrl intel_stibp
bogomips : 4200.00
clflush size : 64
cache_alignment : 64
address sizes : 46 bits physical, 48 bits virtual
power management:
...
...
变相示例:
// 查看逻辑CPU数量
[vip@test ~]$ cat /proc/cpuinfo | grep "processor" | wc -l
32
// 查看物理CPU数量
[vip@test ~]$ cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
2
// 查看机器核数
[vip@test ~]$ cat /proc/cpuinfo | grep "cpu cores" | wc -l
32
或许这些更优雅:
查看物理CPU的个数:grep 'physical id' /proc/cpuinfo | sort -u | wc -l
查看cpu cores:grep 'cpu cores' /proc/cpuinfo | sort -u
查看逻辑CPU的个数:grep 'processor' /proc/cpuinfo | sort -u | wc -l
具体每一项的含义可以参考:Linux系统cpuinfo详解-何格-CSDN.
磁盘
df 说明书:
[vip@test ~]$ df --help
Usage: df [OPTION]... [FILE]...
Show information about the file system on which each FILE resides,
or all file systems by default. // 查看文件系统信息。
Mandatory arguments to long options are mandatory for short options too.
-a, --all include dummy file systems
-B, --block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'-BM' prints sizes in units of 1,048,576 bytes;
see SIZE format below // -B 指定显示单位,常用。
--direct show statistics for a file instead of mount point
--total produce a grand total
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
// -h 懒人用法,常用。
-H, --si likewise, but use powers of 1000 not 1024 // 近似值,不建议使用
-i, --inodes list inode information instead of block usage
-k like --block-size=1K // 指定单位为kB
-l, --local limit listing to local file systems
--no-sync do not invoke sync before getting usage info (default)
--output[=FIELD_LIST] use the output format defined by FIELD_LIST,
or print all fields if FIELD_LIST is omitted.
-P, --portability use the POSIX output format
--sync invoke sync before getting usage info
-t, --type=TYPE limit listing to file systems of type TYPE
-T, --print-type print file system type // 查看文件系统格式
-x, --exclude-type=TYPE limit listing to file systems not of type TYPE
-v (ignored)
--help display this help and exit
--version output version information and exit
Display values are in units of the first available SIZE from --block-size,
and the DF_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).
SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).
FIELD_LIST is a comma-separated list of columns to be included. Valid
field names are: 'source', 'fstype', 'itotal', 'iused', 'iavail', 'ipcent',
'size', 'used', 'avail', 'pcent', 'file' and 'target' (see info page).
示例:
[vip@test ~]$ df -BG
Filesystem 1G-blocks Used Available Use% Mounted on
/dev/sde 3668G 1G 3481G 1% /data5
/dev/sdi 3668G 1G 3481G 1% /data9
/dev/sdc 3668G 1G 3481G 1% /data3
...
/dev/sdf 3668G 1G 3481G 1% /data6
/dev/sda 3668G 3G 3479G 1% /data1
/dev/sdg 3668G 1G 3481G 1% /data7
/dev/sdh 3668G 1G 3481G 1% /data8
/dev/sdd 3668G 1G 3481G 1% /data4
/dev/sdb 3668G 1G 3481G 1% /data2
/dev/sdk 3668G 201G 3281G 6% /data11
/dev/sdj 440G 4G 415G 1% /data13
/dev/sdl 3668G 299G 3183G 9% /data12
...
du
查看文件磁盘使用情况
说明书:
[vip@test ~]$ du --help
Usage: du [OPTION]... [FILE]...
or: du [OPTION]... --files0-from=F
Summarize disk usage of each FILE, recursively for directories. // 查看文件的磁盘使用情况,与 df 很相似。
Mandatory arguments to long options are mandatory for short options too.
-0, --null end each output line with 0 byte rather than newline
-a, --all write counts for all files, not just directories
--apparent-size print apparent sizes, rather than disk usage; although
the apparent size is usually smaller, it may be
larger due to holes in ('sparse') files, internal
fragmentation, indirect blocks, and the like
-B, --block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'-BM' prints sizes in units of 1,048,576 bytes;
see SIZE format below
// 常用
-b, --bytes equivalent to '--apparent-size --block-size=1'
-c, --total produce a grand total
-D, --dereference-args dereference only symlinks that are listed on the
command line
-d, --max-depth=N print the total for a directory (or file, with --all)
only if it is N or fewer levels below the command
line argument; --max-depth=0 is the same as
--summarize
--files0-from=F summarize disk usage of the
NUL-terminated file names specified in file F;
if F is -, then read names from standard input
-H equivalent to --dereference-args (-D)
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
--inodes list inode usage information instead of block usage
// 常用
-k like --block-size=1K
-L, --dereference dereference all symbolic links
-l, --count-links count sizes many times if hard linked
-m like --block-size=1M
-P, --no-dereference don't follow any symbolic links (this is the default)
-S, --separate-dirs for directories do not include size of subdirectories
--si like -h, but use powers of 1000 not 1024
-s, --summarize display only a total for each argument
-t, --threshold=SIZE exclude entries smaller than SIZE if positive,
or entries greater than SIZE if negative
--time show time of the last modification of any file in the
directory, or any of its subdirectories
--time=WORD show time as WORD instead of modification time:
atime, access, use, ctime or status
--time-style=STYLE show times using STYLE, which can be:
full-iso, long-iso, iso, or +FORMAT;
FORMAT is interpreted like in 'date'
-X, --exclude-from=FILE exclude files that match any pattern in FILE
--exclude=PATTERN exclude files that match PATTERN
-x, --one-file-system skip directories on different file systems
--help display this help and exit
--version output version information and exit
Display values are in units of the first available SIZE from --block-size,
and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).
SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).
示例 :
[hadoop@test /data1/test]$ du -h test/
8.0K test/test2
8.0K test/test1
20K test/
内存
- 查看内存硬件情况:
cat /proc/meminfo
,类似查看cpu情况.
示例:
[hadoop@g1-bdp-cdhtest-04 ~]$ cat /proc/meminfo
MemTotal: 131485632 kB
MemFree: 75146728 kB
MemAvailable: 96834688 kB
Buffers: 2344696 kB
Cached: 22600504 kB
...
...
- 查看内存使用情况:
free
。
说明书:
[hadoop@g1-bdp-cdhtest-04 ~]$ free --help
Usage:
free [options]
Options:
-b, --bytes show output in bytes // 指定单位
-k, --kilo show output in kilobytes
-m, --mega show output in megabytes
-g, --giga show output in gigabytes
--tera show output in terabytes
-h, --human show human-readable output // 易读性
--si use powers of 1000 not 1024
-l, --lohi show detailed low and high memory statistics
-t, --total show total for RAM + swap
-s N, --seconds N repeat printing every N seconds // 设置输出时间间隔,手动终止
-c N, --count N repeat printing N times, then exit // 设置输出次数
-w, --wide wide output
--help display this help and exit
-V, --version output version information and exit
示例:
// 分别输出:总内存、已使用内存、剩余内存、共享内存、缓存大小、可用内存大小
[hadoop@test ~]$ free -g
total used free shared buff/cache available
Mem: 125 25 71 4 27 92
Swap: 3 2 1
[hadoop@test ~]$ free -g -h
total used free shared buff/cache available
Mem: 125G 25G 71G 4.7G 27G 92G
Swap: 4.0G 2.4G 1.6G
[hadoop@test ~]$ free -g -h -s 3
total used free shared buff/cache available
Mem: 125G 25G 71G 4.7G 27G 92G
Swap: 4.0G 2.4G 1.6G
total used free shared buff/cache available
Mem: 125G 25G 71G 4.7G 27G 92G
Swap: 4.0G 2.4G 1.6G
^C // 手动终止
网络
查看本机 ip
// 都可用来查看ip信息和网络接口属性
[hadoop@test ~]$ ip a
[hadoop@test ~]$ ifconfig
netstat
说明书
[vip@test ~]$ netstat --help
usage: netstat [-vWeenNcCF] [<Af>] -r netstat {-V|--version|-h|--help}
netstat [-vWnNcaeol] [<Socket> ...]
netstat { [-vWeenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s [-6tuw] } [delay]
-r, --route display routing table
-I, --interfaces=<Iface> display interface table for <Iface>
-i, --interfaces display interface table
-g, --groups display multicast group memberships
-s, --statistics display networking statistics (like SNMP) // 查看统计信息
-M, --masquerade display masqueraded connections
-v, --verbose be verbose
-W, --wide don't truncate IP addresses
-n, --numeric don't resolve names
--numeric-hosts don't resolve host names
--numeric-ports don't resolve port names
--numeric-users don't resolve user names
-N, --symbolic resolve hardware names
-e, --extend display other/more information
-p, --programs display PID/Program name for sockets
-o, --timers display timers
-c, --continuous continuous listing
-l, --listening display listening server sockets // 查看监听的ss
-a, --all display all sockets (default: connected) // 查看已经建立的连接
-F, --fib display Forwarding Information Base (default)
-C, --cache display routing cache instead of FIB
-Z, --context display SELinux security context for sockets
<Socket>={-t|--tcp} {-u|--udp} {-U|--udplite} {-S|--sctp} {-w|--raw}
{-x|--unix} --ax25 --ipx --netrom // tcp udp之类的配置
<AF>=Use '-6|-4' or '-A <af>' or '--<af>'; default: inet
List of possible address families (which support routing):
inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25)
netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP)
x25 (CCITT X.25)
示例:
[vip@test ~]$ netstat -atup // 查看与本机建立的 tcp / udp 连接,并显示 pid
(No info could be read for "-p": geteuid()=3000 but you should be root.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:emc-pp-mgmtsvc 0.0.0.0:* LISTEN -
tcp 0 0 localhost:30235 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:25020 0.0.0.0:* LISTEN -
tcp 0 0 localhost:20477 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:60030 0.0.0.0:* LISTEN -
tcp 0 0 test.:8030 0.0.0.0:* LISTEN -
...
...
nslookup
可以根据 IP 解析 hostname
[vip@test ~]$ nslookup 10.16.208.174
Server: 10.17.1.252
Address: 10.17.1.252#53
174.208.16.10.in-addr.arpa name =test-01.
进程
ps
说明书:
// 可以根据下面 help 查看使用方法,例如 ps --help list
[vip@test ~]$ ps --help
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
示例:
[vip@test ~]$ ps -axu | grep psTest
vip 28640 0.0 0.0 112704 972 pts/2 S+ 22:49 0:00 grep --color=auto psTest
top
说明书:
[vip@test ~]$ top -help
procps-ng version 3.3.10
Usage:
top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]
说明:
top命令使用过程中,还可以使用一些交互的命令来完成其它参数的功能,如下:
<空格>:立刻刷新。
P:根据CPU使用大小进行排序。
T:根据时间、累计时间排序。
q:退出top命令。
m:切换显示内存信息。
t:切换显示进程和CPU状态信息。
c:切换显示命令名称和完整命令行。
M:根据使用内存大小进行排序。
W:将当前设置写入~/.toprc文件中。这是写top配置文件的推荐方法。
端口
查看端口是否打开
查看本地 :sudo lsof -i:port
查看远端 :telnet ip port
示例:
// 端口未打开则直接断开连接
[vip@test ~]$ telnet 10.16.208.174 10086
Trying 10.16.208.174...
telnet: connect to address 10.16.208.174: Connection refused
// 端口打开则会等待
[vip@test ~]$ telnet 10.16.208.174 22
Trying 10.16.208.174...
Connected to 10.16.208.174.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4
Protocol mismatch.
Connection closed by foreign host.
未完待续 ...