美文网首页
简单的 liunx 性能监控(cpu、内存、io)

简单的 liunx 性能监控(cpu、内存、io)

作者: ling_1992 | 来源:发表于2018-08-15 10:10 被阅读0次
    monitor.sh
    #/bin/sh
    # 进入.sh所在目录
    cd /home/XXX/Documents/monitor
    #获取时间
    date=$(date '+%Y-%m-%d')
    _time=$(date '+%H:%M:%S')
    # 使用sar 获取 cpu io 情况
    cpu=$(sar -u 2 2 | sed -n '$p' | awk '{print $3}')
    io=$(sar -d -p 3 1 | sed -n '$p' | awk '{print $NF}')
    # 使用 free 获取内存情况
    totalMemory=$(free -m|awk  '{print $2}'|sed -n '2p')
    usedMemory=$(free -m|awk  '{print $3}'|sed -n '2p')
    freeMemory=$(free -m|awk  '{print $4}'|sed -n '2p')
    share=$(free -m | awk '{print $5}'| sed -n '2p')
    buff_cache=$(free -m | awk '{print $6}' | sed -n '2p')
    available=$(free -m | awk '{print $6}' | sed -n '2p')
    usedPerMemory=$(awk 'BEGIN{printf "%.0f",('$usedMemory'/'$totalMemory')*100}')
    freePerMemory=$(awk 'BEGIN{printf "%.0f",('$freeMemory'/'$totalMemory')*100}')
    sharePer=$(awk 'BEGIN{printf "%.0f",('$share'/'$totalMemory')*100}')
    buff_cachePer=$(awk 'BEGIN{printf "%.0f",('$buff_cache'/'$totalMemory')*100}')
    availablePer=$(awk 'BEGIN{printf "%.0f",('$available'/'$totalMemory')*100}')
    #导出日志
    echo "$_time => cpu: $cpu%,  io: $io%, memory: used $usedPerMemory%, free $freePerMemory%, share $sharePer%, buff/cache $buff_cachePer%, available $availablePer%" &>> monitor-${date}.log
    
    使用crontab 定时任务 20秒一次
    ## 监控 系统 性能
    * * * * * /bin/sh /home/ling/Documents/monitor/monitor.sh >/dev/null 2>&1
    * * * * * sleep 20; /bin/sh /home/ling/Documents/monitor/monitor.sh >/dev/null 2>&1
    * * * * * sleep 40; /bin/sh /home/ling/Documents/monitor/monitor.sh >/dev/null 2>&1
    

    shell 指令简单介绍

    sar -u 2 3 -u 参数 表示cpu数据; 2 1 间隔2秒 采样3次

    [root@XXX monitor]# sar -u 2 1 
    Linux 3.10.0-862.3.3.el7.x86_64 (XXX)   08/15/2018  _x86_64_    (1 CPU)
    
    09:51:12 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
    09:51:14 AM     all      7.54      0.00      1.01      0.00      0.00     91.46
    09:51:16 AM     all      0.50      0.00      0.00      0.00      0.00     99.50
    09:51:18 AM     all      0.00      0.00      0.50      0.00      0.00     99.50
    Average:        all      2.68      0.00      0.50      0.00      0.00     96.82
    
    [root@gongzhonghao monitor]# sar -d -p  2 3
    Linux 3.10.0-862.3.3.el7.x86_64 (gongzhonghao)  08/15/2018  _x86_64_    (1 CPU)
    
    10:00:05 AM       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
    10:00:07 AM       vda      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
    
    10:00:07 AM       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
    10:00:09 AM       vda      1.51      0.00    209.05    138.67      0.00      3.00      3.00      0.45
    
    10:00:09 AM       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
    10:00:11 AM       vda      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
    
    Average:          DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
    Average:          vda      0.50      0.00     69.45    138.67      0.00      3.00      3.00      0.15
    

    %util:I/O请求占CPU的百分比,比率越大,说明越饱和.

    我们取平均值Average 这一行数据
    sed -n '$p' 选择上面数据的最后一行
    awk '{print $3}' 选择第三列数据
    awk '{print $NF}' 选择最后一列数据

    free -m -m参数 表示以单位为M 显示数据

    [root@XXX monitor]# free -m
                  total        used        free      shared  buff/cache   available
    Mem:           1838         514          88           0        1235        1121
    Swap:             0           0           0
    

    相关文章

      网友评论

          本文标题:简单的 liunx 性能监控(cpu、内存、io)

          本文链接:https://www.haomeiwen.com/subject/bfsibftx.html