'https://majeunesse.app/', 'QC2-CA1' => 'https://qc2.majeunesse.app/', 'QC3' => 'https://qc3.majeunesse.app/', // Add more servers as needed ); $instance_id = uniqid(); $threshold = 2.0; // Max acceptable response time in seconds $timeout = 30; // Max timeout per server in seconds $mysql_host = 'localhost'; $mysql_user = 'majeu542_monitor'; $mysql_pass = 'otNA&cnDt1kd'; $mysql_db = 'majeu542_monitor'; $email_to = 'admin@yourdomain.com'; $email_from = 'monitor@yourdomain.com'; $email_subject = 'Server Monitor Alert'; $json_output = 'server_status.json'; // Path to JSON file (must be web-accessible) // Prevent script overlap /* $lock_file = '/tmp/server_monitor.lock'; $fp = fopen($lock_file, 'w'); if (!flock($fp, LOCK_EX | LOCK_NB)) { exit("Script already running.\n"); } */ $conn = mysqli_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_db); if (!$conn) { die("MySQL Connection failed: " . mysqli_connect_error()); } function checkServer($url, $threshold, $timeout) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $start_time = microtime(true); $response = curl_exec($ch); $end_time = microtime(true); $response_time = $end_time - $start_time; $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($response === false || $http_code >= 400) { return array('status' => 'DOWN', 'response_time' => null); } if ($response_time > $threshold) { return array('status' => 'SLOW', 'response_time' => $response_time); } return array('status' => 'UP', 'response_time' => $response_time); } // Function to send email alert function sendAlert($server_name, $url, $status, $response_time) { global $email_to, $email_from, $email_subject; $message = "Server: $server_name ($url)\n"; $message .= "Status: $status\n"; $message .= $response_time ? "Response Time: " . number_format($response_time, 3) . " seconds\n" : ""; $message .= "Timestamp: " . date('Y-m-d H:i:s') . "\n"; $headers = "From: $email_from\r\n"; mail($email_to, $email_subject, $message, $headers); } // Check each server foreach ($servers as $name => $url) { $result = checkServer($url, $threshold, $timeout); $status = $result['status']; $response_time = $result['response_time']; // Insert status into database $response_time_sql = $response_time !== null ? $response_time : 'NULL'; $query = " INSERT INTO server_status (instance_id, server_name, url, status, response_time, timestamp) VALUES ('$instance_id', '$name', '$url', '$status', $response_time_sql, NOW()) "; mysqli_query($conn, $query); // Send alert if server is down or slow if ($status === 'DOWN' || $status === 'SLOW') { sendAlert($name, $url, $status, $response_time); } } /* $result = mysqli_query($conn, " SELECT server_name, status, timestamp FROM server_status WHERE timestamp >= DATE_SUB(NOW(), INTERVAL 30 MINUTE) ORDER BY timestamp ASC "); $json_data = array(); while ($row = mysqli_fetch_assoc($result)) { $json_data[] = array( 'server_name' => $row['server_name'], 'status' => $row['status'], 'timestamp' => $row['timestamp'] ); } file_put_contents($json_output, json_encode($json_data, JSON_PRETTY_PRINT)); */ // Clean up old records (optional, keeps last 30 days) mysqli_query($conn, "DELETE FROM server_status WHERE timestamp < DATE_SUB(NOW(), INTERVAL 30 DAY)"); // Close MySQL connection mysqli_close($conn) /* flock($fp, LOCK_UN); fclose($fp); */ ?>