在維護服務過程當中,要時刻了解機器資源的使用狀況,這是服務運行正常的基本條件。經常使用的須要瞭解系統信息、cpu使用率、內存使用狀況、磁盤使用狀況、網絡、端口鏈接、進程查詢等等。node
uname : 查看系統信息。es6
先看說明書:api
> 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 : 查看 / 設置系統環境變量。 說明書:bash
[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
...
複製代碼
說明書:app
經過 cat /proc/cpuinfo
來查看CPU硬件的相關信息。socket
示例:tcp
[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:
...
...
複製代碼
變相示例:ide
// 查看邏輯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.
複製代碼
未完待續 ...