同じ共有ホスティング アカウントでホストされているすべての Web サイトに対して、基本的な稼働時間の監視を実行しようとしています。ネットワークの問題をローカル (サーバーの負荷) の問題から切り離したいので、外部のアップタイム モニターを使用できません。
次の php スクリプトは、他の IP アドレスでホストされているサイトをポーリングできますが、独自の IP アドレスはポーリングできません。SSH コンソール経由でプライマリ ドメインとアドオン ドメイン (すべて同じ IP) から wget と curl を実行できますが、PHP 経由で同じループバック操作を行うことはできないようです。なんで?
<?php
check('some-other-host.com', 'validation text 2'); //succeeds
check('addon-domain-on-same-host.com', 'validation text 1'); //fails
// other sites go here //
function check($host, $find) {
$fp = fsockopen($host, 80, $errno, $errstr, 10);
if (!$fp) {
echo "$errstr ($errno)\n";
} else {
$header = "GET / HTTP/1.1\r\n";
$header .= "Host: $host\r\n";
$header .= "Connection: close\r\n\r\n";
fputs($fp, $header);
while (!feof($fp)) {
$str .= fgets($fp, 1024);
}
fclose($fp);
if (strpos($str, $find) == false) {
echo $host." is down. ";
mail('me@mine.com', $host.' is down.', $str);
}
}
}
?>