Linux系统资源及账户安全监测脚本
#!/bin/bash #Created by liter on 2014/7/11 PATH=/opt/jdk1.6.0_45/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin export PATH #CPU INFO id_cpu=`top -b -d 2 -n 10 | grep Cpu | grep -v grep | cut -d ‘,‘ -f 4 | tr -d -c ‘0-9.\n‘ | sort -n | head -n 1` use_cpu=`echo "100-$id_cpu" | bc` echo -e "\e[32m******************************************************* \e[0m" echo -e "\e[1;42m CPU INFO \e[0m" echo -e "if($id_cpu<=20) print \"CPU has been used in \e[31m$use_cpu%, more than 80%\e[0m\n\" else print \"CPU has been used in \e[31m$use_cpu%, less than 80%\e[0m\n\"" | bc echo -e "\e[32m******************************************************* \e[0m" #MEM INFO total_mem=`free -m | grep Mem | tr -d -c ‘0-9 \n‘ | tr -s ‘ ‘ | cut -d ‘ ‘ -f 2` used_mem=`free -m | grep Mem | tr -d -c ‘0-9 \n‘ | tr -s ‘ ‘ | cut -d ‘ ‘ -f 3` free_mem=`free -m | grep Mem | tr -d -c ‘0-9 \n‘ | tr -s ‘ ‘ | cut -d ‘ ‘ -f 4` buffers_mem=`free -m | grep Mem | tr -d -c ‘0-9 \n‘ | tr -s ‘ ‘ | cut -d ‘ ‘ -f 6` cached_mem=`free -m | grep Mem | tr -d -c ‘0-9 \n‘ | tr -s ‘ ‘ | cut -d ‘ ‘ -f 7` numerator=$[ $used_mem - $buffers_mem - $cached_mem ] float=`echo "scale=2;$numerator/$total_mem" | bc` int_float=`echo "$float * 100" | bc` echo -e "\e[1;42m MEM INFO \e[0m" echo "total_mem is $total_mem M" echo "used_mem is $used_mem M" echo "true_used_mem is $numerator M" echo "buffers_mem is $buffers_mem M" echo "cached_mem is $cached_mem M" echo -e "if($int_float>=80) print \"MEM has been used in \e[31m$int_float%, more than 80%\e[0m\n\" else print \"MEM has been used in \e[31m$int_float%, less than 80%\e[0m\n\"" | bc echo -e "\e[32m******************************************************* \e[0m" #SWAP INFO echo -e "\e[1;42m SWAP INFO \e[0m" total_swap=`free -m | grep Swap | tr -d -c ‘0-9 \n‘ | tr -s ‘ ‘ | cut -d ‘ ‘ -f 2` if [ $total_swap -eq 0 ];then echo -e "\e[31mswap is unavailable\e[0m" else used_swap=`free -m | grep Swap | tr -d -c ‘0-9 \n‘ | tr -s ‘ ‘ | cut -d ‘ ‘ -f 3` free_swap=`free -m | grep Swap | tr -d -c ‘0-9 \n‘ | tr -s ‘ ‘ | cut -d ‘ ‘ -f 4` float_swap=`echo "scale=2;$used_swap/$total_swap" | bc` echo "total_swap is $total_swap M" echo "used_swap is $used_swap M" percentage_swap=`echo "float_swap * 100" | bc` echo -e "SWAP has been used in \e[31m$percentage_swap%\e[0m" fi echo -e "\e[32m******************************************************* \e[0m" #DISK INFO echo -e "\e[1;42m DISK INFO \e[0m" df -Th echo -e "\e[32m******************************************************* \e[0m" #PASSWD TABLE echo -e "\e[1;42m PASSWORD TABLE INFO \e[0m" passwd="/root/.passwd" if [ ! -f "$passwd" ];then cp /etc/passwd /root/.passwd chattr +i /root/.passwd fi diff /root/.passwd /etc/passwd if [ $? -eq 0 ];then echo -e "\e[31m/etc/passwd have no change\e[0m" else echo -e "\e[31m/etc/passwd maybe changed\e[0m" fi echo -e "\e[32m******************************************************* \e[0m"
运行示例(cpu,内存,swap,磁盘,密码表信息):
本文出自 “稻花香里说丰年” 博客,请务必保留此出处http://liter0015.blog.51cto.com/8746750/1637362
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。