命令行判断网站是否宕机, php判断网站是否宕机, wget/curl 判断 Web 是否宕机的脚本, 检查网站是否可以正常访问

 

说明:一个脚本用于确认服务器有没有宕机

#!/usr/bin/env bash

if [[ $# -eq 2 ]]; then
    URL=$1
    MAILTO=$2
else
    echo "usage : $0 check_url alert_mail_address"
    exit 1
fi

TIMEOUT=60
TRYCOUNT=1
SUBJECT="ALERT:Web Server is Fail!!!!"

alive=`wget -S --spider -t ${TRYCOUNT} -T ${TIMEOUT} ${URL} 2>&1 | grep -c "200 OK"`

if [[ ${alive} == 0 ]] ; then
    echo "$URL is down." | mail -s "$SUBJECT" ${MAILTO}
fi

 

用法:

sh site.sh justcode.ikeepstudying.com  ikeepstudying@gmail.com

 

演换成PHP版:

<?php
$store_name = 'justcode.ikeepstudying.com';
$status = shell_exec('wget -S --spider -t 1 -T 60 '.$store_name.' 2>&1 | grep -c "200 OK"');
$status = (int)trim($status)*1;
$msg    = $store_id.': '.$store_name.' '.($status?'OK':'Down');

echo $msg."\n";

 

当然用php自身语言也可以检查:

<?php
function Networkcheck($url){
  $agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0";
  //curl_init-初始化一个curl会话
  $ch=curl_init();
  //curl_setopt — 为一个curl设置会话参数
  curl_setopt($ch, CURLOPT_URL,$url );
  curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch,CURLOPT_VERBOSE,false);
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch,CURLOPT_SSLVERSION,3);
  curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
  //curl_exec —执行一个curl会话
  $page=curl_exec($ch);
  //curl_getinfo — 获取一个curl连接资源句柄的信息
  $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  //curl_close()函数的作用是关闭一个curl会话,唯一的参数是curl_init()函数返回的句柄。
  curl_close($ch);
  if($httpcode>=200 && $httpcode<300)
    return true;
  else
    return false;
}
//函数参数为要检查的网站的网址路径
if(Networkcheck("justcode.ikeepstudying.com"))
  echo "Website OK";
else
  echo "Website DOWN";

 

如果是使用curl,也可以用:

curl -I -m 10 -o /dev/null -s -w %{http_code}  justcode.ikeepstudying.com

一般正常是返回200

自动检测网站健康shell脚本

一、背景介绍

我的WordPress网站最近老是宕机无法提供服务,网站架构是经典的 Linux + OpenResty + MySQL + PHP , 宕机后大多数情况下不能立即处理,于是想写个自动检测网站健康的shell脚本,主要功能是定时检测网站运行是否正常,如果宕机,则自动初始化MySQL和PHP服务,如果初始化后还不能访问,则邮件告警通知我。

 

二、实现过程

1  编写脚本

脚本的内容如下,目前运行十来天左右还算正常。

#!/bin/bash

#Description:this script is made to check website health and roll back automattically.

dst_mail="xxxxxxxx@xx.com"
mail_title="Linode Website Health Report"
mail_content="\nOops,Your website is still out of service after initiating!\n\n$(iptables -vnL)\n\n\n$(netstat -nltp)\n\n"
free_mem=$(free -m | awk '/Mem/ { print $4 }')
min_mem="300"
echo "free memory is : ${free_mem} MB"

#function to check current website health.
chk_health(){
        curl https://peloo.net/?p=262 2>/dev/null  | grep -io "routeros"
        if [ $? -eq 0 ] ; then
                return 0
        else
                return 1
        fi
}

#function to restart mysqld and php-fpm process.
init_service(){

chk_health
if [ ! $? -eq 0 ] ; then
        for((i=1;i<=2;i++))
        do
                service php-fpm stop 2>/dev/null
                service php-fpm start
                echo -e "\n……php-fpm started successfully !\n"
                echo -e "\n++++++++++++++++++++++++++++++++++++\n"
                service mysqld stop 2>/dev/null
                service mysqld start
                echo -e "\n…… MySQL server started successfully !\n"
        done
else
echo -e "\n……Your website is OK !\n"
fi

}

#function to do something when backup ending.
post_do(){

ping -c 30 127.1 1>/dev/null 2>&1
chk_health
if [ ! $? -eq 0 ] ; then
        echo -e "\n Damn it , Your website is still out of service !!! \n"
        echo -e "${mail_content}" | mailx -v -s "${mail_title}" "${dst_mail}"
fi

}

init_service
post_do

exit 0

 

解释:

A)函数chk_health()用来通过curl请求网页内容,如果返回的字符串检测不到,则判定网站宕机。

B)函数 init_service()先调用chk_health(),如果宕机,则重启MySQL和PHP服务。

C)post_do()函数用来做后置处理工作,主要是再次调用chk_health()函数来检测网站是否恢复正常,如果不正常,则邮件提示我自己。

 

2  添加Linux定时任务计划

每隔20分钟运行健康检测脚本

echo "*/20 * * * *    root /bin/bash /srv/sh/init_service.sh" >>/etc/crontab 

 

本文:命令行判断网站是否宕机, php判断网站是否宕机, wget/curl 判断 Web 是否宕机的脚本, 检查网站是否可以正常访问

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.