Linux内存管理和性能工具

内存管理

free命令

基本用法
[root@template ~]# free
              total        used        free      shared  buff/cache   available
Mem:         995896      148304      694920        7872      152672      682308
Swap:       2097148           0     2097148

以MB为单位查询内存的使用情况
[root@template ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            972         143         679           7         149         667
Swap:          2047           0        2047

以更加人性化的方式显示
[root@template ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           972M        143M        679M        7.7M        149M        667M
Swap:          2.0G          0B        2.0G

每个3秒钟显示输出一次内存情况
[root@template ~]# free -h -s 3
              total        used        free      shared  buff/cache   available
Mem:           972M        143M        679M        7.7M        149M        667M
Swap:          2.0G          0B        2.0G

              total        used        free      shared  buff/cache   available
Mem:           972M        143M        679M        7.7M        149M        667M
Swap:          2.0G          0B        2.0G

显示总和列
[root@template ~]# free -h -t
              total        used        free      shared  buff/cache   available
Mem:           972M        143M        679M        7.7M        149M        667M
Swap:          2.0G          0B        2.0G
Total:         2.9G        143M        2.7G

显示free的版本[root@node2 ~]# free -V
free from procps-ng 3.3.10

swap交换内存

swap的使用

[root@server3 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           972M        346M        396M        7.7M        229M        443M
Swap:          2.0G          0B        2.0G

[root@server3 ~]# dd if=/dev/zero of=/dev/null bs=338M count=1
记录了1+0 的读入
记录了1+0 的写出
354418688字节(354 MB)已复制,0.18485 秒,1.9 GB/秒
[root@server3 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           972M        303M        482M        7.7M        186M        498M
Swap:          2.0G          0B        2.0G

[root@server3 ~]# dd if=/dev/zero of=/dev/null bs=550M count=1
记录了1+0 的读入
记录了1+0 的写出
576716800字节(577 MB)已复制,0.106914 秒,5.4 GB/秒
[root@server3 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           972M        304M        606M        2.0M         61M        552M
Swap:          2.0G        6.8M        2.0G

创建swap交换空间
[root@server3 /]# dd if=/dev/zero of=/data/swapfile bs=1M count=1024
记录了1024+0 的读入
记录了1024+0 的写出

root@server3 data]# mkswap swapfile
正在设置交换空间版本 1,大小 = 1048572 KiB
无标签,UUID=ccc4acd1-c03b-4645-a8c0-d47034bc2d0f

[root@server3 data]# swapon /data/swapfile
swapon: /data/swapfile:不安全的权限 0644,建议使用 0600。

root@server3 data]# chmod 600 /data/swapfile

设置开机自动挂载
[root@server3 data]# echo "/data/swapfile swap swap defaults 0 0">>/etc/fstab

查看swap使用情况
[root@template data]# swapon -s
文件名                          类型            大小    已用    权限
/dev/dm-1                               partition       2097148 0       -2
/data/swapfile                          file    1048572 0       -3
[root@template data]# swapon --show
NAME           TYPE       SIZE USED PRIO
/dev/dm-1      partition    2G 520K   -2
/data/swapfile file      1024M   0B   -3

swap的优化
临时调整
[root@server3 data]# sysctl vm.swappiness=10
永久调整
[root@server3 ~]# vim /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
# For more information, see sysctl.conf(5) and sysctl.d(5).
vm.swappiness=10

禁用swap
[root@server3 ~]# sed -i.bak '/swap/d' /etc/fstab
[root@server3 ~]# swapoff -a



vmstat命令

[root@node2 ~]# vmstat

-s:显示内存的统计数据
[root@template ~]# vmstat -s
       995896 K total memory
       138012 K used memory
        90928 K active memory
        73392 K inactive memory
       716500 K free memory
         2116 K buffer memory
       139268 K swap cache
      3145720 K total swap
            0 K used swap
      3145720 K free swap
          537 non-nice user cpu ticks
            0 nice user cpu ticks
          699 system cpu ticks
        62434 idle cpu ticks
            4 IO-wait cpu ticks
            0 IRQ cpu ticks
            8 softirq cpu ticks
            0 stolen cpu ticks
       115453 pages paged in
         7815 pages paged out
            0 pages swapped in
            0 pages swapped out
        56290 interrupts
       262101 CPU context switches
   1736694479 boot time
         7557 forks

查看磁盘情况

df查看磁盘

df命令用于显示系统上磁盘空间的使用情况
语法:df [参数] [对象磁盘/分区]
常见参数
 -a:显示所有系统文件
 -h:以更加人性化的方式显示文件系统
 -i:显示索引字节信息
 -l:只显示本地文件系统
 -T:输出时显示文件系统类型
 -t 文件系统类型: 输出指定文件系统类型的信息

iostat命令

[root@docker ~]# yum install -y sysstat

常见参数

 -c 显示CPU使用情况
 -d 显示磁盘使用情况
 -k 以 KB 为单位显示
 -m 以 M 为单位显示
 -N 显示磁盘阵列(LVM) 信息
 -n 显示NFS 使用情况
 -p [磁盘] 显示磁盘和分区的情况
 -t 显示终端和CPU的信息
 -x 显示详细信息
 -V 显示版本信息

dstat命令

[root@template ~]# yum insall dstat
[root@template ~]# dstat

监控磁盘的读操作
[root@template ~]# dd if=/dev/sda of=/dev/null

监控磁盘的写操作
[root@template ~]# dd if=/dev/zero of=/dev/sda

监控网络流量
[root@node2 ~]# ping -f -s 65506 192.168.253.129

iotop命令

iotop常用参数
-o, --only只显示正在产生I/O的进程或线程,除了传参,可以在运行过程中按o生效
-b, --batch非交互模式,一般用来记录日志
-n NUM, --iter=NUM设置监测的次数,默认无限。在非交互模式下很有用
-d SEC, --delay=SEC设置每次监测的间隔,默认1秒,接受非整形数据例如1.1
-p PID, --pid=PID指定监测的进程/线程
-u USER, --user=USER指定监测某个用户产生的I/O
-P, --processes仅显示进程,默认iotop显示所有线程
-a, --accumulated显示累积的I/O,而不是带宽
-k, --kilobytes使用kB单位,而不是对人友好的单位。在非交互模式下,脚本编程有用
-t, --time 加上时间戳,非交互非模式
-q, --quiet 禁止头几行,非交互模式,有三种指定方式
-q 只在第一次监测时显示列名
-qq 永远不显示列名
-qqq 永远不显示I/O汇总

交互按键
left和right方向键:改变排序
r:反向排序
o:切换至选项--only
p:切换至--processes选项
a:切换至--accumulated选项
q:退出
i:改变线程的优先级

监控网络状态

netstat命令

命令:netstat

语法:netstat [选项]

作用:查看网络连接状态

选项说明

  -an:按一定顺序排列输出

  -t:表示只列出tcp 协议的连接

  -n:表示将地址从字母组合转化成ip 地址,将协议转化成端口号来显示

  -l :表示过滤出"state(状态)"列中其值为LISTEN(监听)的连接

  -p:表示显示发起连接的进程pid 和进程名称

ss命令

ss命令的基本语法为:ss [选项]。常用的选项包括:

 -a 或 --all:显示所有套接字,包括监听和非监听的。
 -t 或 --tcp:仅显示TCP套接字。
 -u 或 --udp:仅显示UDP套接字。
 -l 或 --listening:仅显示处于监听状态的套接字。
 -n 或 --numeric:以数字格式显示地址和端口,避免解析为主机名。
 -p 或 --processes:显示与套接字关联的进程信息。
 -4:仅显示IPv4套接字。
 -6:仅显示IPv6套接字。

iftop显示网络带宽

[root@template ~]# yum install iftop -y
[root@template ~]# iftop 
#以数字(ip地址)的形式显示
[root@template ~]# iftop -n
#以数字(ip地址)的形式并指定具体的网络接口显示
[root@template ~]# iftop -ni ens33

[root@node2 ~]# ping -f -s 15507 192.168.253.129

[root@template ~]# iftop -n

firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="你要禁止的IP地址" reject'
firewall-cmd --permanent --remove-rich-rule="rule family='ipv4' source address='你要解绑的IP地址' reject"
firewall-cmd --reload

nload查看网络实时吞吐量

语法:nload [选项]

常见选项

 -a:全部数据的刷新时间周期,单位是秒,默认是300s
 -i:进入网卡的流量图的显示比例最大值设置,默认10240 kBit/s
 -m:不显示流量图,只显示统计数据
 -o:出去网卡的流量图的显示比例最大值设置,默认10240 kBit/s
 -t:显示数据的刷新时间间隔,单位是毫秒,默认500ms
 -u:设置右边Curr、Avg、Min、Max的数据单位,默认是h自动变的.注意大小写单位不同

iptraf-ng网络监视工具

[root@template ~]# yum install -y iptraf-ng

# 查看每一块网卡上的流量
[root@template ~]# iptraf-ng -g
# 显示指定网卡上的流量统计,总体流量、流入量、流出量、以及按协议分类的流量统计
[root@template ~]# iptraf-ng -d ens33
# 统计各port的流量
[root@template ~]# iptraf-ng -s ens33
# 查看远程主机端口及报文(含抓包信息)
[root@template ~]# iptraf-ng -i ens33

你可能感兴趣的:(云计算运维,linux,运维,服务器)