我是一个独立的Web开发人员,拥有自己的Centos VPS,为我的客户托pipe几个小型网站。 今天我发现我的httpd服务已经停止(没有明显的原因 – 但这是另一个线程)。 我重新启动它,但现在我需要find一种方式,如果它再次发生我可以通过电子邮件和/或SMS通知 – 我不喜欢当我的客户响我告诉我他们的网站不工作!
我知道可能有许多不同的可能性,包括服务器监控软件。 我想我真正需要的是一个脚本,我可以从我的开发主机(永久运行在我的办公室)运行一个cron作业,试图从我的生产服务器加载一个页面,如果它没有加载在说30几秒钟,然后它给我发一封电子邮件或短信。 我在shell脚本上非常垃圾,所以这个问题。
任何build议将不胜感激。
那么…最简单的脚本,我写:
/usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null | grep "Normal operation string" || echo "The site is down" | /usr/bin/mail -v -s "Site is down" [email protected]
将它添加到cron中:
* * * * * /usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null | grep "Normal operation string" || echo "The site is down" | /usr/bin/mail -v -s "Site is down" [email protected]
但是如果问题存在的话,告诉你这个问题太简单了。
UPD:现在这个单线程将检查页面上的特定string(“正常操作string”),该string只应在正常操作中出现。
UPD2:在电子邮件中发送错误页面的简单方法:
/usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null | grep "Normal operation string" || /usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null | /usr/bin/mail -v -s "Site is down" [email protected]
减去第一个testing失败的情况下,页面被重新请求。 这次请求可能会成功,您将看不到错误。 当然,可以存储输出并将其作为附件发送,但会使脚本更加复杂。
看看这个脚本:
curl是一个获取URL的命令行工具。 脚本检查退出代码($?是指shell脚本中最近命令的退出代码),如果它不是0,则报告错误(退出代码0通常表示成功)。 正如HUB的回答中所提到的,你也可以|| 在命令行上运行第二个命令时,第一个失败。
一旦你知道了状态,你只需要发送一些邮件。 下面是一个使用mail命令从shell脚本发送邮件的例子,假设您正在testing的邮箱具有SMTP设置:
顺便说一句:如果你不擅长shell脚本,不要限制自己的shell脚本。 你可以使用Ruby脚本,一个PHP脚本,你的服务器可以运行的任何脚本! 只需在脚本的开头添加#!/path/to/executable行,例如:
#!/usr/bin/php
我知道所有上面的脚本正是你所要求的,但我会build议看monit,因为如果apache发生故障,它会发送给你一个电子邮件,但是它也会重新启动它(如果它closures的话)。
检查这个脚本 。 它检查一个网站列表,并发送电子邮件(电子邮件列表),每当错误(HTTP响应不同于200)。 该脚本创build一个.temp文件来“记住”最后检查哪个网站失败,所以你不会得到多个电子邮件。 .temp文件在网站重新运行时被删除。
#!/bin/bash # list of websites. each website in new line. leave an empty line in the end. LISTFILE=/scripts/isOnline/websites.lst # Send mail in case of failure to. leave an empty line in the end. EMAILLISTFILE=/scripts/isOnline/emails.lst # `Quiet` is true when in crontab; show output when it's run manually from shell. # Set THIS_IS_CRON=1 in the beginning of your crontab -e. # else you will get the output to your email every time if [ -n "$THIS_IS_CRON" ]; then QUIET=true; else QUIET=false; fi function test { response=$(curl --write-out %{http_code} --silent --output /dev/null $1) filename=$( echo $1 | cut -f1 -d"/" ) if [ "$QUIET" = false ] ; then echo -n "$p "; fi if [ $response -eq 200 ] ; then # website working if [ "$QUIET" = false ] ; then echo -n "$response "; echo -e "\e[32m[ok]\e[0m" fi # remove .temp file if exist. if [ -f cache/$filename ]; then rm -f cache/$filename; fi else # website down if [ "$QUIET" = false ] ; then echo -n "$response "; echo -e "\e[31m[DOWN]\e[0m"; fi if [ ! -f cache/$filename ]; then while read e; do # using mailx command echo "$p WEBSITE DOWN" | mailx -s "$1 WEBSITE DOWN" $e # using mail command #mail -s "$p WEBSITE DOWN" "$EMAIL" done < $EMAILLISTFILE echo > cache/$filename fi fi } # main loop while read p; do test $p done < $LISTFILE
将以下行添加到crontab config($ crontab -e)
THIS_IS_CRON=1 */30 * * * * /path/to/isOnline/checker.sh
在Github上可用
我会为此推荐pingdom。 他们的免费服务允许您检查1个网站,但这是所有你需要检查1台服务器。 如果你有一个iPhone他们免费推送消息,所以没有必要从他们购买短信,他们有多个设置,你可以使用。 在2次重试(10分钟)和每10分钟的停机时间之后,矿井会通知我。 这真棒,因为它也检查HTTP 500消息,指出一个网站已closures。 如果失败,它会立即从另一个不同位置的服务器再次检查您的站点。 如果那个失败了,那么这会触发你的喜好,以便在何时/何时获得通知。
由于您的VPS上有很多网站,我build议您可以使用网站监控站点(如host-tracker.com)开设帐户。 除了提醒您网站是否停机之外,他们还会为您提供每周,每月和每年正常运行的网站。 对pipe理和绩效非常有帮助。
这个怎么样:
#!/bin/bash /etc/init.d/httpd status if [[ $? == 3 ]]; then echo "Httpd is down `date`" | mail [email protected] exit 1 fi exit 0
以上的轻微变化。
每10秒检查一次网站是否可用的脚本。 logging失败的尝试到一个siteuptime.txt文件,以便siteuptime.txt可以查看(或绘制在Excel中)。
#!/bin/bash # Check site every 10 seconds, log failed connection attempts in siteuptime.txt while true; do echo "Checking site..."; /usr/bin/wget "http://www.mysite" --timeout 6 -O - 2>/dev/null | grep "My String On page" || echo "The site is down" | date --iso-8601=seconds >> siteuptime.txt; sleep 10; done;