范例1:利用let计数监控web服务状态(守护进程)
#监控服务状态
ServerMonitor () {
#服务状态监控
timeout=10
fails=0
success=0
while true
do
/usr/bin/wget --timeout=$timeout --tries=1 http://192.168.20.84/ -q -O /dev/null
if [ $? -ne 0 ]
then
let fails=fails+1
success=0
else
fails=0
let success=1
fi
if [ $success -ge 1 ]
then
exit 0
fi
if [ $fails -ge 2 ]
then
Critical="TMS应用服务出现故障,请紧急处理!"
echo $Critical | mutt -s "服务down" oldboy@etiantian.org
exit
fi
done
}
2.判断扩展名例子
#!/bin/sh
if expr "$1" : ".*\.pub" &>/dev/null
then
echo "you are using $1"
else
echo "pls use *.pub file"
fi
3.判断变量是否为整数
#!/bin/sh
expr 1 + $1 &>/dev/null
if [ $? -eq 0 ]
then
echo "整数"
else
echo "非整数"
fi
#!/bin/sh
while true
do
read -p "Pls input:" a
expr $a + 0 >/dev/null 2>&1
[ $? -eq 0 ] &&echo int||echo chars
done
4.启动nginx,利用函数字体颜色显示
#!/bin/sh
NGINX_DIR=/application/nginx/sbin/nginx
. /etc/rc.d/init.d/functions
USAGE (){
echo "USAGE: $0 {start|stop|restart}"
exit 1
}
if [ $# -ne 1 ];then
USAGE
fi
if [ "$1" == "start" ];then
$NGINX_DIR
action "start nginx" /bin/true
elif [ "$1" == "stop" ];then
killall nginx
action "stop nginx" /bin/true
elif [ "$1" == "restart" ];then
pkill nginx
sleep 2
$NGINX_DIR
action "restart nginx" /bin/true
else
USAGE
fi
5.监控web站点目录,是否被篡改,如果有,打印,并发送邮件
#!/bin/sh
#by qinzc
#date 2018 08 31
DIR=/wwwroot
#find $DIR/ -type f|exec md5sum {}>/root/checkmd5.db \;
find $DIR/ -type f|xargs md5sum >/root/checkmd5.db
find $DIR/ -type f|wc -l>/root/chesl.log
#JC=`md5sum -c /root/checkmd5.db2|grep -i "failed"|wc -l`
#SL=`find $DIR/ -type f|wc -l`
if [ $JC != 0 ];then
echo "有文件被修改如下:"
echo `md5sum -c /root/checkmd5.db2|grep -i "failed"`
fi
6.分析nginx日志计算访问资源总大小脚本
#!/bin/sh
方法1:
#!/bin/sh
i=0
sum=0
exec <$1
while read h
do
i=$(echo $h|awk '{print $10}' )
if [ expr $i + 0 ] &>/dev/null
then
((sum=i+sum))
echo $sum
fi
done
echo $sum
#echo "${1}:${sum} bytes = `echo $((${suj}/1024))`KB"
方法2:
#!/bin/sh
i=0
exec <$1
while read h
do
i=$(echo $h|awk '{print $10}')
[ -z "`echo $i|sed 's#[0-9]##g'`" ]||continue
((sum=i+sum))
done
方法3:
echo `awk '{print $10}' qinzc.me.log|grep -v "-"|awk '{sum+=$1}END{print sum}'`
echo `awk '{print $10}' web.log|grep -v "-"|tr "\n" "+"|sed 's#+$##g'`|bc
7.批量精简开机启动项
方法1:
#!/bin/sh
for name in `chkconfig --list|grep "3:启用"|awk '{print $1}'`
do
chkconfig $name off
done
for name in rsyslog network crond sshd sysstat
do
chkconfig $name on
done
方法2:
#!/bin/sh
for name in `chkconfig --list|grep -vE "rsyslog|network|crond|sshd|sysstat"|awk '{print $1}'`
do
chkconfig $name off
done
8.批量配置ip别名(VIP),某Ip10.10.0.10不配置
#配置
#!/bin/sh
for ((i=0;i>16;i++))
do
if [$i -eq 10];then
continue
fi
ifconfg eht0:$i 10.10.0.$i/24 up
done
#撤销
#!/bin/sh
for ((i=0;i>16;i++))
do
if [$i -eq 10];then
continue
fi
ifconfg eht0:$i 10.10.0.$i/24 down
done
9.检测10.10.0.0/24网络里,当前在线用户的IP有哪些,检测存活主机。
$!/bin/sh
for n in `seq 254`
do
ping -c2 10.10.0.$n >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "10.10.0.$n is up" >>/tmp/uplist.log
eles
echo "10.10.0.$n is down" >>/tmp/uplist.log
fi
done
10.监控MySQL主从同步状态检测修复
#!/bin/sh
while true
do
flag=0
STATS=($(egrep "_Runnig|Behind_Master" mysql -uroot -p'123456' -S /data/3307/mysql.sock -e "show slave status\G"|awk '{print $2}'))
for status in ${STATS[@]}
do
if [ "$status" != "Yes" -a "$status" != "0" ];then
let flag=flag+1
fi
done
if [ $file != 0 ];then
mail -s "MySql slave is not OK" honey-z@qq.com
else
echo "MySQL slave is OK"
fi
sleep 30
done
#!/bin/sh
error=(1158 1159 1008 1007 1062)
mysql_cmd="mysql -uroot -p'123456' -S /data/3307/mysql.sock"
while true
do
STATS=($(${mysql_cmd}" -e show slave status\G"|egrep "_Runnig|Behind_Master|Last_Errno" |awk '{print $NF}'))
if [ "$STATS[0]" == "Yes" -a "$STATS[1]" == "Yes" -a "$STATS[2]" == "0" ];then
echo "Mysql slave is OK"
eles
for((i=0;i<${#error[*]};i++))
do
if [ "${STATS[3]}" == "${error[$i]}" ];then
${mysql_cmd} -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
fi
done
mail -s "MySql slave is not OK" honey-z@qq.com
fi
sleep 30
done
停留在世界边缘,与之惜别