Linux Shell经典面试题(其六)

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
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值