Linux Shell经典面试题(其六)

时间:2022-12-01 21:45:07

Linux Shell经典面试题(其六)

参考:http://lilongzi.blog.51cto.com/5519072/1837858

1.监控web服务是否正常,不低于3种监控策略。要求间隔1分钟,持续监控。

############监控web服务的脚本编写##############

$ vi web-monitor.sh

#!/bin/bash
url=wwchengbi.cn
Port=80
check_web(){
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions
if [ "`nc -w 5 $url $Port &&echo ok`" = "ok" ];then
#if [ "`echo -e '\n'|telnet $url $Port|grep Connected|wc -l`" = "1" ];then
#if [ "`nmap $url -p $Port|grep open|wc -l`" = "1" ];then
#if [ "`curl -I http://$url 2>/dev/null|head -1|egrep "200|302|301"|wc -l`" ="1" ];then
#if [ "`curl -I -s -o /dev/null -w '%{http_code}\n' http://$url`" = "200" ];then
action "$url $Port" /bin/true
else
action "$url $Port /bin/false"
fi
}
main(){
while true
do
check_web
sleep 1m
done
}

main

2.监控db服务是否正常,不低于3种监控策略。要求间隔1分钟,持续监控。


############监控db服务的脚本编写##############

$ vi db-monitor.sh

#!bin/bash
Port=3306
check_db(){
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
#if [ `lsof -i:$Port|wc -l` -gt 1 ];then
#if [ "`netstat -tunlp|grep 3306|wc -l`" = "1" ];then
if [ `mysql -umha -pmha -h 172.16.2.10 -P $Port -e "show databases;"|wc -l` -gt 1 ];then
action "MySQL $Port online" /bin/true
else
action "MySQL $Port down" /bin/false
fi
}

main(){
while true
do
check_db
sleep 1m
done
}

main

3.监控web站点目录(/var/html/www)下所有文件是否被恶意篡改(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次(10分钟时间完成)。


############监控文件被恶意篡改的脚本编写##############

$ vi tampering-file.sh

#!/bin/bash
check_www(){
md5sum -c /tmp/md5_www.log > /tmp/result_www.log
[ ! -f /tmp/result_www.log ]&& echo "/tmp/result_www.log not exist."exit 2
exec < /tmp/result_www.log
while read line
do
file = `echo $line|awk -F ' ' 'printf $1'`
#echo $file
#echo $result
[ ! "$result" = "OK" ]&&action "$file" /bin/false
done
}

main(){
while true
do
LANF=en
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions
[ ! -f /tmp/md5_www.log ]&& echo "/tmp/md5_www.log not exist."&&exit 1
check_www
action "Alii check done." /bin/true
sleep 3m
done
}

main