129 lines
4.7 KiB
PHP
129 lines
4.7 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
// Prevent multiple instances
|
|
$lockFile = __DIR__ . '/monitor.lock';
|
|
$fp = fopen($lockFile, 'c');
|
|
if (!flock($fp, LOCK_EX | LOCK_NB)) {
|
|
// Silently exit if already running (good for cron)
|
|
exit(0);
|
|
}
|
|
|
|
echo "Monitoring started with Multi-Threading...\n";
|
|
|
|
function sendTelegramNotification($message) {
|
|
try {
|
|
$db = db();
|
|
$stmt = $db->query("SELECT name, value FROM settings WHERE name IN ('telegram_bot_token', 'telegram_chat_id')");
|
|
$settings = [];
|
|
while ($row = $stmt->fetch()) {
|
|
$settings[$row['name']] = $row['value'];
|
|
}
|
|
|
|
if (empty($settings['telegram_bot_token']) || empty($settings['telegram_chat_id'])) {
|
|
return;
|
|
}
|
|
|
|
$url = "https://api.telegram.org/bot" . $settings['telegram_bot_token'] . "/sendMessage";
|
|
$data = [
|
|
'chat_id' => $settings['telegram_chat_id'],
|
|
'text' => $message,
|
|
'parse_mode' => 'HTML'
|
|
];
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
curl_exec($ch);
|
|
curl_close($ch);
|
|
} catch (Exception $e) {
|
|
// Suppress errors to not break the monitor loop
|
|
}
|
|
}
|
|
|
|
// Loop indefinitely
|
|
while (true) {
|
|
try {
|
|
$db = db();
|
|
$urls = $db->query("SELECT * FROM urls WHERE is_active = 1")->fetchAll();
|
|
|
|
if (count($urls) > 0) {
|
|
$mh = curl_multi_init();
|
|
$curl_arr = [];
|
|
$url_info = [];
|
|
|
|
// Add handles
|
|
foreach ($urls as $urlData) {
|
|
$ch = curl_init($urlData['url']);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HEADER, false); // We don't need body, just headers
|
|
curl_setopt($ch, CURLOPT_NOBODY, true); // HEAD request is faster
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 10s timeout
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
curl_multi_add_handle($mh, $ch);
|
|
$curl_arr[] = $ch;
|
|
// Store info mapped by intval of handle
|
|
$url_info[(int)$ch] = $urlData;
|
|
}
|
|
|
|
// Execute handles
|
|
$active = null;
|
|
do {
|
|
$mrc = curl_multi_exec($mh, $active);
|
|
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
|
|
|
|
while ($active && $mrc == CURLM_OK) {
|
|
if (curl_multi_select($mh) != -1) {
|
|
do {
|
|
$mrc = curl_multi_exec($mh, $active);
|
|
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
|
|
}
|
|
}
|
|
|
|
// Read results
|
|
foreach ($curl_arr as $ch) {
|
|
$ch_id = (int)$ch;
|
|
$urlData = $url_info[$ch_id];
|
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$total_time = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
|
|
$total_time_ms = round($total_time * 1000);
|
|
|
|
// Determine new status: UP if 200-399
|
|
$new_status = ($http_code >= 200 && $http_code < 400) ? 'UP' : 'DOWN';
|
|
$old_status = $urlData['status'];
|
|
|
|
// Update database
|
|
$stmt = $db->prepare("UPDATE urls SET status = ?, last_checked = NOW() WHERE id = ?");
|
|
$stmt->execute([$new_status, $urlData['id']]);
|
|
|
|
$stmt = $db->prepare("INSERT INTO logs (url_id, status, response_time, checked_at) VALUES (?, ?, ?, NOW())");
|
|
$stmt->execute([$urlData['id'], $new_status, $total_time_ms]);
|
|
|
|
// Check for status change and notify
|
|
if ($old_status && $old_status !== $new_status) {
|
|
$icon = $new_status === 'UP' ? '✅' : '🚨';
|
|
$msg = "{$icon} <b>Monitor Alert</b>\n\n";
|
|
$msg .= "<b>URL:</b> {$urlData['url']}\n";
|
|
$msg .= "<b>Status:</b> {$new_status} (HTTP {$http_code})\n";
|
|
$msg .= "<b>Response:</b> {$total_time_ms}ms";
|
|
sendTelegramNotification($msg);
|
|
}
|
|
|
|
curl_multi_remove_handle($mh, $ch);
|
|
curl_close($ch);
|
|
}
|
|
curl_multi_close($mh);
|
|
}
|
|
} catch (Exception $e) {
|
|
echo "Error: " . $e->getMessage() . "\n";
|
|
}
|
|
|
|
// Wait 30 seconds before next check
|
|
sleep(30);
|
|
} |