1. 程式人生 > >2018-03-20 Linux學習

2018-03-20 Linux學習

Linux學習

10.6 監控 IO性能

iostat -x   磁盤使用
iotop  磁盤使用

free   查看內存使用情況
free -m / -g / -h
buffer/cache
公式:total=used+free+buff/cache
avaliable 包含free和buffer/cache

iostat -x

    [root@aming-01 ~]# iostat -x
    Linux 3.10.0-693.17.1.el7.x86_64 (aming-01)     2018年03月05日     _x86_64_    (1 CPU)

    avg-cpu:  %user   %nice %system %iowait  %steal   %idle
               0.06    0.00    0.13    0.00    0.00   99.80

    Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util

%util  io等待處理數據百分比,數字大,說明磁盤忙

iotop

    主要看IO選項

10.7 free

    [root@aming-01 ~]# free -h
                  total        used        free      shared  buff/cache   available
    Mem:           1.8G        142M        1.3G        8.6M        372M        1.5G
    Swap:          4.0G          0B        4.0G

預分配一定容量給 buff/cache
磁盤 --> 內存cache --> CPU
CPU --> 內存buffer --> 磁盤

10.8 ps

ps   查看系統進程
    用法: ps aux、ps -elf

stat 部分說明
    D   不能中斷的進程
    R   run狀態的進程
    S   sleep狀態進程
    T   暫停的進程
    Z   僵屍進程
    <   高優先級進程
    N   低優先級進程
    L   內存中被鎖了內存分頁
    s   主進程
    l   多線程進程
    +   前臺進程

    [root@aming-01 ~]# ps aux |grep apache
    root       2391  0.0  0.0 112676   980 pts/0    R+   06:42   0:00 grep --color=auto apache

殺掉某個進程,先找到這個進程的PID,及進程目錄

    [root@aming-01 ~]# ls -l /proc/566
    總用量 0
    ............................
    lrwxrwxrwx. 1 root root 0 3月   5 06:47 exe -> /usr/bin/vmtoolsd

10.9 查看網絡狀態

netstat   查看網絡狀態
netstat -lnp   查看監聽端口
netstat -an    查看系統的網絡連接狀況
netstat -lntp  只看出tcp的,不包含socket
ss -an 和 netstat 異曲同工
分享一個小技巧
netstat -an | awk ‘/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}‘

-a  :將目前系統上所有的連線、監聽、Socket 數據都列出來
-t  :列出 tcp 網絡封包的數據
-u  :列出 udp 網絡封包的數據
-n  :不以程序的服務名稱,以端口號 (port number) 來顯示;
-l  :列出目前正在網絡監聽 (listen) 的服務;
-p  :列出該網絡服務的程序 PID

    [root@aming-01 ~]# netstat -nltup
    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      898/sshd            
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1135/master         
    tcp6       0      0 :::22                   :::*                    LISTEN      898/sshd            
    tcp6       0      0 ::1:25                  :::*                    LISTEN      1135/master

    [root@aming-01 ~]# netstat -an | awk ‘/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}‘
    LISTEN   4
    ESTABLISHED      1

    ESTABLISHED   並發連接數,有多少個與服務器連接通信,1000以內服務器通常可以接受

10.10 Linux下抓包

監控系統狀態

    抓包工具 tcpdump   yum install -y tcpdump
    用法:tcpdump -nn
    tcpdump -nn -i ens33
    tcpdump -nn port 80
    tcpdump -nn not port 22 and host 192.168.0.100
    tcpdump -nn -c 100 -w 1.cap

    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"
    yum install -y wireshark

    tcpdump 看數據的流向
    udp 包是攻擊的可能性比較高

        [root@aming-01 ~]# tcpdump -nn -i ens33 port 22

        [root@aming-01 ~]# tcpdump -nn not port 22
        tcpdump: Bluetooth link-layer type filtering not implemented

        [root@aming-01 ~]# tcpdump -nn not port 22 and host 192.168.104.1
        tcpdump: Bluetooth link-layer type filtering not implemented

        [root@aming-01 ~]# tcpdump -nn -i ens33 -c 1
        tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
        listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
        06:34:33.980191 IP 192.168.104.160.22 > 192.168.104.1.49499: Flags [P.], seq 2353381568:2353381756, ack 1768454766, win 313, options [nop,nop,TS val 13350665 ecr 888596759], length 188
        1 packet captured
        2 packets received by filter
        0 packets dropped by kernel

        [root@aming-01 ~]# tcpdump -nn -i ens33 -c 10 -w /tmp/33.cap
        tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
        10 packets captured
        10 packets received by filter
        0 packets dropped by kernel
        [root@aming-01 ~]# file /tmp/33.cap 
        /tmp/33.cap: tcpdump capture file (little-endian) - version 2.4 (Ethernet, capture length 262144)

        [root@aming-01 ~]# tcpdump -r /tmp/33.cap 
        reading from file /tmp/33.cap, link-type EN10MB (Ethernet)
        06:41:05.765404 IP aming-01.ssh > 192.168.104.1.49499: Flags [P.], seq 2353411216:2353411340, ack 1768460730, win 313, options [nop,nop,TS val 13742450 ecr 888989950], length 124

2018-03-20 Linux學習