linux 监控服务器脚本

本文介绍了一个使用bash编写的脚本,该脚本用于监测服务器列表中各网站的状态,并记录状态码及错误信息到日志文件。对于无法访问的网站,脚本还会发送邮件通知管理员。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/bash

ctime=`date +%x%T`
monitor_dir=/home/jk/
if [ ! -d $monitor_dir ]; then
    mkdir $monitor_dir
fi
cd $monitor_dir
web_stat_log=web.status
if [ ! -f $web_stat_log ]; then
   touch $web_stat_log
fi
server_list_file=server.list
if [ ! -f $server_list_file ]; then
   echo "`date '+%Y-%m-%d %H:%M:%S'` ERROR:$server_list_file NOT exists!" >>$web_stat_log
exit 1
fi

for website in `cat $server_list_file`
do
   url="http://$website/index.html"
   server_status_code=`curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "$url"`
   if [ "$server_status_code" = "200" ]; then
        echo "`date '+%Y-%m-%d %H:%M:%S'` visit $website status code 200 OK" >>$web_stat_log
   else
        echo "`date '+%Y-%m-%d %H:%M:%S'` visit $website $server_status_code error!!! server can't connect at 10s or stop response at 10 s, send alerm sms ..." >>$web_stat_log
        echo "$website 已经10秒打不开了!#$server_status_code#"  | mail -s $ctime  ***@139.com
   fi
done
exit 0

转载于:https://www.cnblogs.com/rongkang/p/4418780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值