其他分享
首页 > 其他分享> > 04 一键查看服务器资源利用率

04 一键查看服务器资源利用率

作者:互联网

#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

function cpu() {
    util=`vmstat | awk 'NR==3 {print (100-$15) "%"}'`
    iowait=`vmstat | awk 'NR==3 {print $16 "%"}'`
    echo "CPU - 使用率: $util , 等待磁盘IO响应使用率: $iowait"
}

function memory() {
    total=`free -m | awk 'NR==2 {printf "%.1f", $2/1024}'`
    used=`free -m | awk 'NR==2 {printf "%.1f", $3/1024}'`
    available=`free -m | awk 'NR==2 {printf "%.1f", ($2-$3)/1024}'`
    echo "内存 - 总大小: ${total}G , 使用: ${used}G , 剩余: ${available}G"
}

function disk() {
    fs=$(df -h | awk '/^\/dev/ {print $1}')
    for p in $fs; do
        mounted=$(df -h | awk '$1=="'$p'" {print $NF}')
        size=$(df -h | awk '$1=="'$p'" {print $2}')
        used=$(df -h | awk '$1=="'$p'" {print $3}')
        used_percent=$(df -h | awk '$1=="'$p'" {print $5}')
        echo "硬盘 - 挂载点: $mounted , 总大小: $size , 使用: $used , 使用率: $used_percent"
    done
}

function tcp_status() {
    summary=$(ss -antp | awk 'NR>1 {status[$1]++}END{for(i in status) printf i ":" status[i] " , "}')
    echo "TCP连接状态 - $summary"
}

echo "-------------------------------------------CPU----------------------------------------------"
cpu
echo "-------------------------------------------MEMORY-------------------------------------------"
memory
echo "-------------------------------------------DISK---------------------------------------------"
disk
echo "-------------------------------------------TCP_STATUS---------------------------------------"
tcp_status

 

标签:used,04,服务器资源,一键,echo,-------------------------------------------,awk,print,NR
来源: https://www.cnblogs.com/LiuChang-blog/p/14697930.html