[root@BMS001 opt]# cat /opt/monitor/bms_monitor.sh
#!/bin/bash
DATE=$(date +%Y%m%d)
SYS=BMS
DIR=/opt/monitor/
IP=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
cd $DIR
echo `date '+%Y-%m-%d %H:%M:%S'`'#####################网络连接数检查###########################' >> ${IP}_${DATE}_bms_monitor.txt
netstat -an | grep -E "^(tcp)" | cut -c 70- | sort | uniq -c | sort -n > tcp
cat tcp >> ${IP}_${DATE}_bms_monitor.txt
echo `date '+%Y-%m-%d %H:%M:%S'`'################## TIME WAIT 数 ###############################' >> ${IP}_${DATE}_bms_monitor.txt
netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20 > timewait
cat timewait >> ${IP}_${DATE}_bms_monitor.txt
echo `date '+%Y-%m-%d %H:%M:%S'`'################## IP 请求数及 ##########################' >> ${IP}_${DATE}_bms_monitor.txt
netstat -anlp|grep 7001|grep tcp|awk '{print $5}'|awk -F: '{print $4}'|sort|uniq -c|sort -nr|head -n20 > cc
cat cc >> ${IP}_${DATE}_bms_monitor.txt
echo `date '+%Y-%m-%d %H:%M:%S'`'#####################磁盘使用空间检查#########################' >> ${IP}_${DATE}_bms_monitor.txt
df -h > df
cat df >> ${IP}_${DATE}_bms_monitor.txt
echo `date '+%Y-%m-%d %H:%M:%S'`'######################应用监听的端口###########################' >> ${IP}_${DATE}_bms_monitor.txt
netstat -tunl|grep -E ":7001|:7002|:7021|:7022|:7061|:7062">端口 #修改要监控的端口
cat 端口 >>${IP}_${DATE}_bms_monitor.txt
echo '---------------------------total:------------------------------'`wc -l 端口`>> ${IP}_${DATE}_bms_monitor.txt
echo `date '+%Y-%m-%d %H:%M:%S'`'#####################配置文件巡检##############################' >> ${IP}_${DATE}_bms_monitor.txt
find /etc ! -name 'adjtime' ! -name 'prelink.cache' -type f -mtime -2 2>/dev/null 1>修改文件 #etc为检查的文件夹 2是2天内改的
cat 修改文件 >>${IP}_${DATE}_bms_monitor.txt
xiugaiwenjianshu=$(wc -l 修改文件|cut -d ' ' -f1)
echo '修改文件数:'$xiugaiwenjianshu >>${IP}_${DATE}_bms_monitor.txt
echo `date '+%Y-%m-%d %H:%M:%S'`'#####################磁盘状态汇总#############################' >> ${IP}_${DATE}_bms_monitor.txt
sed -i 's/%//g' df
for i in $(awk '{print $5}' df|sed -n /^[0-9]/p)
do
[ $i -gt 80 ] && echo 'no ' >>df || echo 'yes ' >>df #修改阀值
done
grep "no[[:space:]]" df && echo ${IP}'磁盘状况:no' >> ${IP}_${DATE}_bms_monitor.txt||echo ${IP}'磁盘状况:yes' >> ${IP}_${DATE}_bms_monitor.txt
echo `date '+%Y-%m-%d %H:%M:%S'`'#####################端口汇总##################################' >> ${IP}_${DATE}_bms_monitor.txt
if [ $(wc -l 端口|cut -d ' ' -f1) -ne 6 ]; then #端口总数阀值
echo '----------------------------------------'${IP}'端口不正常:no' >>${IP}_${DATE}_bms_monitor.txt
else
echo '----------------------------------------'${IP}'端口正常:yes'>>${IP}_${DATE}_bms_monitor.txt
fi
echo '#####################配置文件状态汇总###########################' >> ${IP}_${DATE}_bms_monitor.txt
if [ ${xiugaiwenjianshu} -eq 0 ]; then
echo '---------------------------------------'${IP}"配置文件无修改:yes" >>${IP}_${DATE}_bms_monitor.txt
else
echo '----------------------------------------'${IP}"配置文件有修改:no" >>${IP}_${DATE}_bms_monitor.txt
fi
echo `date '+%Y-%m-%d %H:%M:%S'`'######################## tcp state ################' >> ${IP}_${DATE}_bms_monitor.txt
port1=`netstat -an|grep LISTEN|egrep "0.0.0.0|:::"|awk '/^tcp/ {print $4}'|awk -F: '{print $2$4}'|sort -n`
echo "PORT PID COMMAND" >> ${IP}_${DATE}_bms_monitor.txt
for a in $port1
do
b=`lsof -n -i:$a|grep TCP|grep LISTEN|grep IPv4|awk '{printf("%d %s
"),$2,$1}'`
echo "$a $b" >> ${IP}_${DATE}_bms_monitor.txt
done
###################上传文件#####################
#ftp -n 172.16.0.81 <<EOF
#user bms_monitor tysxtv189_bms
#cd pub
#mkdir $DATE
#cd $DATE
#put ${IP}_${DATE}_bms_monitor.txt
#bye
#exit
#EOF
[root@BMS001 opt]#