系统信息:
#!/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_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_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}"
Percent_mem_used=$(echo "scale=2;${mem_used}/${mem_total}*100"|bc)
echo "已用内存百分比:${Percent_mem_used}%"
Percent_mem_free=$(echo "scale=2;${mem_free}/${mem_total}*100"|bc)
echo "剩余内存百分比:${Percent_mem_free}%"
}
disk_check(){
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
diskfree=$[$sum/1024/1024]
echo "剩余磁盘总量:${diskfree}GB"
}
安全信息:
sec_check(){
[ -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
[ -f /opt/countfile_sec ] || find /var/www/html/ -type f > /opt/countfile_sec
rm -rf /opt/countfile
[ -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
sleep 60
done