> 文章列表 > 一个简单的监控web服务的shell脚本

一个简单的监控web服务的shell脚本

一个简单的监控web服务的shell脚本

  • 监控说明:

    • 应用场景:监控web服务器状态,异常时邮件报警。

    • 脚本说明:通过wget(也可以用curl)监控服务器状态,如果不能正常访问,ping检测网络,网络正

    • 常通知管理员检查服务,ping不通邮件通知管理员。

    • 服务器列表使用数组,服务器状态函数使用返回值判断服务器是否异常。

  • 创建:

    #! /bin/bash
    ​
    web_hosts=(192.168.178.52:1000 192.168.178.53:2000)
    today_time=`date '+%F %H:%M:%S_week-%w'`
    to_email=1095322098@qq.com
    ​
    systemctl restart sendmail
    if [ `echo $?` -ne 0 ];thenecho "无sendmail服务"exit
    fi
    ​
    for host_port in ${web_hosts[*]};doecho ${host_port}host=`echo ${host_port} | awk -F':' '{print $1}'`ping -c 5 ${host} &> /dev/nullif [ `echo $?` -ne 0 ];thenecho "${host} 服务器网络异常"echo "${today_time}发现: ${host}服务器网络异常" | mail -s "网络警告" ${to_email} &> /dev/nullficurl --connect-timeout 30 ${host_port} &> /dev/nullif [ `echo $?` -ne 0 ];thenecho "${host_port} web服务异常"echo "${today_time}发现: ${host_port}web服务异常" | mail -s "服务警告" ${to_email} &> /dev/nullfi
    done