#!/bin/bash
#date: 2020.04.02
#description: show the rate of system resources
# cpu资源信息统计
function show_cpu
{
cpu_rate=$(vmstat | awk 'NR == 3 {print $(NF-4) + $(NF-3)"%"}')
wait_io=$(vmstat | awk 'NR == 3 {print $(NF-1)"%"}')
echo -e "CPU -> 占用率:${cpu_rate}\t等待磁盘io:${wait_io}"
}
# 内存资源信息统计
function show_memory
{
memory_total=$(free -m | awk 'NR == 2 {printf "%.2f", $2/1024}')
memory_used=$(free -m | awk 'NR == 2 {print $3}')
memory_available=$(free -m | awk 'NR == 2 {print $NF}')
echo -e "内存 -> 总数:${memory_total}G\t已用:${memory_used}M\t可用:${memory_available}M"
}
# 磁盘资源信息统计
function show_disk
{
df -h | awk '/^\/dev/ {printf "磁盘 -> 挂载点:%s\t总数:%s\t已用:%s\t可用:%s\t使用率:%s\n", $NF, $2, $3, $4, $5}'
}
# 网络资源信息统计
function show_net
{
netstat -atnp | awk 'BEGIN{printf "\n网络 -> \n%-16s%s\n", "状态", "总数"} NR > 2 { STATUS[$6]++ } END{ for(i in STATUS) printf "%-16s%4s\n", i, STATUS[i] }'
}
# 实现菜单显示
function menu
{
echo -e "\n\t\t欢迎使用系统资源监测系统\t\t"
echo
echo -e "\t\t\t0、退出系统\t\t\t"
echo -e "\t\t\t1、CPU资源监测\t\t\t"
echo -e "\t\t\t2、内存资源监测\t\t\t"
echo -e "\t\t\t3、磁盘资源监测\t\t\t"
echo -e "\t\t\t4、网络资源监测\t\t\t"
}
# 1.定义入口函数
function main
{
# 2.判断用户输入
# while为无限循环
while :
do
menu
read -p "请输入0~4之间的数字:" num
# 3.根据用户选择的选项, 调用相应的函数
case "${num}" in
0)
echo -e "欢迎再次光临!"
break
;;
1)
show_cpu
;;
2)
show_memory
;;
3)
show_disk
;;
4)
show_net
;;
*)
;;
esac
done
}
main