shell实战之查看服务器利用率

shell实战之查看服务器利用率_第1张图片

#!/bin/bash
# 查看服务器利用率
green='\033[0;32m'
red='\033[0;31m'
blue='\033[0;36m'
pink='\033[0;35m'
NC='\033[0m'
# 获取CPU使用率
cpu() {
   unit=`vmstat | awk 'NR==3{print $14+$13}'`
   iowait=`vmstat | awk 'NR==3{print $16}'`
   echo -e "CPU 使用率: ${green} ${unit}% ${NC} 等待磁盘IO响应使用率: ${green} ${iowait}% ${NC}"
}

memory() {
   total=`free -m | awk 'NR==2{printf"%.1f\n",$2/1024}'`
   used=`free -m | awk 'NR==2{printf"%.1f\n",($2-$NF)/1024}'`
   available=`free -m | awk 'NR==2{printf"%.1f\n",$NF/1024}'`
   echo -e "总内存:${blue}${total}G${NC} 已用内存:${red}${used}G${NC} 可用内存:${green}${available}G${NC}"
}

disk() {
   fs=`df -h | awk '/^\/dev/{print $1}'`
   for n in $fs
   do
      size=`df -h | awk -v n=$n 'n==$1{print $2}'`
      mounted=`df -h | awk -v n=$n 'n==$1{print $NF}'`
      used=`df -h | awk -v n=$n 'n==$1{print $3}'`
      used_percent=`df -h | awk -v n=$n 'n==$1{print $5}'`
      echo -e "硬盘 总大小:${blue}${size}${NC} 已用:${green}${used}${NC} 使用率:${green}${used_percent}${NC} 挂载点:${pink}${mounted}${NC}"
   done
}

tcp_status() {
   echo -e "TCP连接状态 ${green}`ss -anlp | awk '{a[$2]++}END{for (i in a)printf i": "a[i]" "}'`${NC}"
}

cpu
memory
disk
tcp_status

你可能感兴趣的:(服务器,linux)