[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"
}
##安全資訊##
sec_check(){
#方法1:通過md5sum 生成校驗碼,判斷檔案內容是否有被改動!
[ -f /opt/md5sum ] || md5sum /var/www/html/index.html > /opt/md5sum
md5sum -c /opt/md5sum > /dev/null
if [ $? -eq 0 ]; then
echo "web 檔案內容 沒有被篡改!"
else
echo "小心!!被黑了!!"
fi
#方法2:判斷web目錄下的檔案數是否有變動
[ -f /opt/countfile_sec ] || find /var/www/html/ -type f > /opt/countfile_sec
[ -f /opt/countfile ] || find /var/www/html/ -type f > /opt/countfile
diff /opt/countfile /opt/countfile_sec > /dev/null
if [ $? -eq 0 ]; then
echo "web 目錄下的檔案數沒有發生變化"
else
echo "web 目錄發生了變化!!"
fi
}
#while :
#do
sys_check
net_check
cpu_check
mem_check
disk_check
sec_check
#done