Linux用shell脚本监控网站运行状态并发告警邮件
2016-04-01 11:23
1296 查看
#!/bin/bash
#
DATE=`date "+%Y.%m.%d-%H:%M:%S"`
ACCESS_DIR=/root/curl/access
ERRO_DIR=/root/curl/erro
SUCCESS_DIR=/root/curl/success
FILE=`date "+%Y.%m.%d-%H"`
WRONG(){
cat $ERRO_DIR/$FILE | awk -F ' ' '{print $2$3"\n"}'#有错误状态码的域名
}
CURL(){
for i in `cat /root/sites.txt`;do #sites.txt得先放在指定目录,里面放了你想监控的域名
STATUS_CODE=`curl -o /dev/null -s -w %{http_code} $i`
if [ $STATUS_CODE -ne 200 ];then
echo -e "$DATE $i:\t$STATUS_CODE" | tee -a $ERRO_DIR/$FILE
else
echo -e "$DATE $i:\t$STATUS_CODE" | tee -a $SUCCESS_DIR/$FILE
fi
done
}
NUM=`CURL | grep -v 200 | wc -l`
echo $NUM
if [ $NUM -ge 1 ];then
echo -e "Web site have $NUM can't be accessed\nWrong name is:`WRONG`" | mail -s "Web Site Down" 123456789@qq.com
else
echo "$DATE All Web Site is good" &>>$ACCESS_DIR/$FILE.log
fi
附:发送邮件需要的配置:
1.修改/etc/mail.rc (/etc/mail.rc)
2.set from=123456789@qq.com smtp=smtp.qq.com set smtp-auth-user=123456789@qq.com smtp-3.auth-password=123456789 set smtp-auth=login
PS:
from:是发送的邮箱
smtp:是发生的外部smtp服务器的地址
smtp-auth-user:是外部smtp服务器认证的用户名(你指定的邮箱)
smtp-auth-password:是外部smtp服务器认证的用户密码(邮箱的密码)
smtp-auth:是邮件认证的方式
发送一封邮件测试一下:
echo "hello,word" | mail -s "test" 123456789@qq.com
-s后面的是邮件标题
123456789@qq.com:收件人地址
本文出自 “linux” 博客,请务必保留此出处http://study89757.blog.51cto.com/8683527/1759119
#
DATE=`date "+%Y.%m.%d-%H:%M:%S"`
ACCESS_DIR=/root/curl/access
ERRO_DIR=/root/curl/erro
SUCCESS_DIR=/root/curl/success
FILE=`date "+%Y.%m.%d-%H"`
WRONG(){
cat $ERRO_DIR/$FILE | awk -F ' ' '{print $2$3"\n"}'#有错误状态码的域名
}
CURL(){
for i in `cat /root/sites.txt`;do #sites.txt得先放在指定目录,里面放了你想监控的域名
STATUS_CODE=`curl -o /dev/null -s -w %{http_code} $i`
if [ $STATUS_CODE -ne 200 ];then
echo -e "$DATE $i:\t$STATUS_CODE" | tee -a $ERRO_DIR/$FILE
else
echo -e "$DATE $i:\t$STATUS_CODE" | tee -a $SUCCESS_DIR/$FILE
fi
done
}
NUM=`CURL | grep -v 200 | wc -l`
echo $NUM
if [ $NUM -ge 1 ];then
echo -e "Web site have $NUM can't be accessed\nWrong name is:`WRONG`" | mail -s "Web Site Down" 123456789@qq.com
else
echo "$DATE All Web Site is good" &>>$ACCESS_DIR/$FILE.log
fi
附:发送邮件需要的配置:
1.修改/etc/mail.rc (/etc/mail.rc)
2.set from=123456789@qq.com smtp=smtp.qq.com set smtp-auth-user=123456789@qq.com smtp-3.auth-password=123456789 set smtp-auth=login
PS:
from:是发送的邮箱
smtp:是发生的外部smtp服务器的地址
smtp-auth-user:是外部smtp服务器认证的用户名(你指定的邮箱)
smtp-auth-password:是外部smtp服务器认证的用户密码(邮箱的密码)
smtp-auth:是邮件认证的方式
发送一封邮件测试一下:
echo "hello,word" | mail -s "test" 123456789@qq.com
-s后面的是邮件标题
123456789@qq.com:收件人地址
本文出自 “linux” 博客,请务必保留此出处http://study89757.blog.51cto.com/8683527/1759119
相关文章推荐
- 使用shell脚本往数据库批量插入数据和执行sql
- [转载]Linux login、non-login shell以及su,sudo相关概念
- [转载]Linux中profile、bashrc、bash_profile之间的区别和联系
- hadoop Shell命令详解
- shell之wget
- mac 更改 php 版本 和-bash 命令失效的解决方法
- shell 获取当前ip
- 客户端putty, xshell连接linux中vim的小键盘问题
- Adb命令问题
- shell之traceroute命令
- 通过JAVA在命令行(如控制台)运行Shell指令
- 在WinDBG中, 使用.shell命令来搜索字符串
- Linux shell 之 提取文件名和目录名的一些方法
- xshell 连接到虚拟机
- 批量远程执行shell命令工具
- 怎么获得webshell asp/PHP环境搭建 2016.03.31回顾
- Ubuntu更换bash为zsh
- bash逐行读取文件内容
- shell学习日记2
- SHELL学习日记1