1. 程式人生 > 實用技巧 >Linux系統資源及賬戶安全監測指令碼

Linux系統資源及賬戶安全監測指令碼

#!/bin/bash
#Createdbyliteron2014/7/11

PATH=/opt/jdk1.6.0_45/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
exportPATH

#CPUINFO

id_cpu=`top-b-d2-n10|grepCpu|grep-vgrep|cut-d','-f4|tr-d-c'0-9.\n'|sort-n|head-n1`
use_cpu=`echo"100-$id_cpu"|bc`
echo-e"\e[32m*******************************************************\e[0m"
echo-e"\e[1;42mCPUINFO\e[0m"
echo-e"if($id_cpu<=20)print\"CPUhasbeenusedin\e[31m$use_cpu%,morethan80%\e[0m\n\"elseprint\"CPUhasbeenusedin\e[31m$use_cpu%,lessthan80%\e[0m\n\""|bc
echo-e"\e[32m*******************************************************\e[0m"


#MEMINFO

total_mem=`free-m|grepMem|tr-d-c'0-9\n'|tr-s''|cut-d''-f2`
used_mem=`free-m|grepMem|tr-d-c'0-9\n'|tr-s''|cut-d''-f3`
free_mem=`free-m|grepMem|tr-d-c'0-9\n'|tr-s''|cut-d''-f4`
buffers_mem=`free-m|grepMem|tr-d-c'0-9\n'|tr-s''|cut-d''-f6`
cached_mem=`free-m|grepMem|tr-d-c'0-9\n'|tr-s''|cut-d''-f7`
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;42mMEMINFO\e[0m"
echo"total_memis$total_memM"
echo"used_memis$used_memM"
echo"true_used_memis$numeratorM"
echo"buffers_memis$buffers_memM"
echo"cached_memis$cached_memM"
echo-e"if($int_float>=80)print\"MEMhasbeenusedin\e[31m$int_float%,morethan80%\e[0m\n\"elseprint\"MEMhasbeenusedin\e[31m$int_float%,lessthan80%\e[0m\n\""|bc
echo-e"\e[32m*******************************************************\e[0m"


#SWAPINFO

echo-e"\e[1;42mSWAPINFO\e[0m"
total_swap=`free-m|grepSwap|tr-d-c'0-9\n'|tr-s''|cut-d''-f2`
if[$total_swap-eq0];then
	echo-e"\e[31mswapisunavailable\e[0m"
else
	used_swap=`free-m|grepSwap|tr-d-c'0-9\n'|tr-s''|cut-d''-f3`
	free_swap=`free-m|grepSwap|tr-d-c'0-9\n'|tr-s''|cut-d''-f4`
	float_swap=`echo"scale=2;$used_swap/$total_swap"|bc`
	echo"total_swapis$total_swapM"
	echo"used_swapis$used_swapM"
	percentage_swap=`echo"float_swap*100"|bc`
	echo-e"SWAPhasbeenusedin\e[31m$percentage_swap%\e[0m"
fi
echo-e"\e[32m*******************************************************\e[0m"


#DISKINFO

echo-e"\e[1;42mDISKINFO\e[0m"
df-Th
echo-e"\e[32m*******************************************************\e[0m"


#PASSWDTABLE

echo-e"\e[1;42mPASSWORDTABLEINFO\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[$?-eq0];then
	echo-e"\e[31m/etc/passwdhavenochange\e[0m"
else
	echo-e"\e[31m/etc/passwdmaybechanged\e[0m"
fi
echo-e"\e[32m*******************************************************\e[0m"

執行示例(cpu,記憶體,swap,磁碟,密碼錶資訊):

wKioL1U3sQ6wAB-nAAQJ0PLBlzs569.jpg

轉載於:https://blog.51cto.com/liter0015/1637362