shell脚本:nginx读取IP次数,防火墙过滤;检查mysql是启动,若没有则重启

1.读取nginx日志最后N行数,超过一定数目就将此ip在防火墙中过滤

#!/bin/sh 
#########################################################
####功能介绍:读取nginx日志最后N行数,获取IP和访问数######
####超过一定数目就将此ip在防火墙中过滤			   ######
####每天凌晨重启防火墙,清除规则   				   ######
#########################################################
#根据:https://bbs.aliyun.com/read/50817.html  思路改编


#vi /data/shell/iptables_fangshua.sh
#chmod +x /data/shell/iptables_fangshua.sh
#echo "* * * * * root /data/shell/iptables_fangshua.sh" >> /etc/crontab
#service crond restart


#每天凌晨重启防火墙
#echo "01 1 * * * root /etc/init.d/iptables restart" >> /etc/crontab
#service crond restart


status=`tail -n1000 /data/log/www.funet8.com-access.log |awk '{print $3}'| awk -F":" '{print $NF}' |sort|uniq -c|sort -nr|head -1` 
NUM=`echo $status|awk '{print $1}'` 	#最多IP次数
IP=`echo $status|awk '{print $2}'` 		#最多IP地址
result=`echo "$NUM > 200" | bc` 
if [ $result = 1 ] 
then 
echo IP:$IP is over $NUM, NoAccess IT! >> /data/log/Iptables_NoAccess_IP.log
/sbin/iptables -I INPUT -s $IP -j DROP
mv /data/log/www.funet8.com-access.log /data/log/www.funet8.com-access_$(date "+%Y-%m-%d-%H-%M-%S").log
/etc/init.d/nginx reload
fi

2.检查mysql服务是启动,若没有则重启

#!/bin/bash

#######################################
##功能介绍:检查mysql是否启动     #####
##如果没有启动就重启mysql         #####
#######################################


#vi /data/shell/check_mysql_and_restart.sh
#chmod +x /data/shell/check_mysql_and_restart.sh
#echo "*/5 * * * * root /data/shell/check_mysql_and_restart.sh" >> /etc/crontab
#service crond restart

DATE=$(date +%Y%m%d-%H:%M:%S);
#service mysql status &>/dev/null

ps -le |grep mysqld	&>/dev/null
if [ $? -ne 0 ];
then
echo "At time: '$DATE':MYSQL Server is down,and restart. " >> /data/mysqldata56/mysql_restart.log
#service mysql restart
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/my.cnf &
fi

未经允许不得转载:好玩吧 » shell脚本:nginx读取IP次数,防火墙过滤;检查mysql是启动,若没有则重启