[root@localhost ~]# vim sys_check.sh
#!/bin/bash
sys_check(){
##系统信息##
os_type=$(uname)
echo "操作系统的类型:${os_type}"
os_ver=$(cat /etc/redhat-release)
echo "操作系统的版本号:${os_ver}"
os_ker=$(uname -r)
echo "系统内核版本号:${os_ker}"
os_time=$(date +%F-%T)
echo "服务器当前运行时间:${os_time}"
os_last_reboot=$(uptime |awk '{print $3}'|awk -F ',' '{print $1}')
echo "服务器最后重启时间:${os_last_reboot}"
os_hostname=$(hostname)
echo "服务器主机名称:${os_hostname}"
}
net_check(){
##网络信息##
ip_address=$(ifconfig ens33|awk '/netmask/{print $2}')
echo "服务器的ip地址:${ip_address}"
#验证服务器是否可以连通外网
ping -c 2 baidu.com > /dev/null
if [ $? -eq 0 ]; then
echo "服务器的网络是ok的"
else
echo "请检查你的网络"
fi
#获取指定网卡的流量
##流入的量
RX=$(ifconfig ens33|grep RX|sed -n '1p'|awk '{print $(NF-1)}'|awk -F '(' '{print $2}')
echo "流入的量:${RX}MiB"
##流出的量
TX=$(ifconfig ens33|grep TX|sed -n '1p'|awk '{print $(NF-1)}'|awk -F '(' '{print $2}')
echo "流出额量:${TX}MiB"
}
##硬件信息##
cpu_check(){
#cpu
cpu_num=$(cat /proc/cpuinfo |grep "physical id"|sort |uniq |wc -l)
echo "cpu的物理个数:${cpu_num}"
cpu_core=$(cat /proc/cpuinfo |grep "cpu cores"|sort |uniq |awk -F ':' '{print $2}')
echo "cpu的核心数:${cpu_core}"
cpu_model=$(cat /proc/cpuinfo |grep "model name"|sort |uniq |awk -F ':' '{print $2}')
echo "cpu的型号:${cpu_model}"
}
mem_check(){
#mem
mem_total=$(free |grep Mem|awk '{print $2}')
echo "内存总大小:${mem_total}"
mem_used=$(free |grep Mem|awk '{print $3}')
echo "已用内存大小:${mem_used}"
mem_free=$(free |grep Mem|awk '{print $4}')
echo "剩余内存大小:${mem_free}"
#已用内存百分比
#${mem_used}/${mem_total}
Percent_mem_used=$(echo "scale=2;${mem_used}/${mem_total}*100"|bc)
echo "已用内存百分比:${Percent_mem_used}%"
#剩余内存百分比
#${mem_free}/${mem_total}
Percent_mem_free=$(echo "scale=2;${mem_free}/${mem_total}*100"|bc)
echo "剩余内存百分比:${Percent_mem_free}%"
}
disk_check(){
#disk
disk_total=$(lsblk |grep -w sda|awk '{print $4}')
echo "磁盘的总量:${disk_total}"
#剩余磁盘总量
a=($(df -T|egrep -v "tmpfs|文件系统"|awk '{print $5}'))
sum=0
for i in ${a[@]}
do
let sum=sum+$i
done
#kb(1024)-mb(1024)-Gb
diskfree=$[$sum/1024/1024]
echo "剩余磁盘总量:${diskfree}GB"
}
#while :
#do
sys_check
net_check
cpu_check
mem_check
disk_check
#done
运行后截图: