PHP网站监控设置代码
,包含服务器性能监控、网站可用性检查和日志记录功能。
1. 数据库设计 (MySQL)
sql
CREATE TABLE `system_monitor` ( `id` int(11) NOT NULL AUTO_INCREMENT, `check_time` datetime NOT NULL, `server_load` float DEFAULT NULL, `memory_usage` float DEFAULT NULL, `disk_usage` float DEFAULT NULL, `response_time` float DEFAULT NULL, `http_status` int(11) DEFAULT NULL, `is_online` tinyint(1) DEFAULT 1, `error_message` text DEFAULT NULL, PRIMARY KEY (`id`), INDEX `check_time_index` (`check_time`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
2. 监控脚本 (monitor.php)
php
// 配置设置$config = [ 'db_host' => 'localhost', 'db_user' => 'monitor_user', 'db_pass' => 'secure_password', 'db_name' => 'website_monitor', 'websites_to_check' => [ ['url' => 'https://example.com', 'name' => '主网站'], ['url' => 'https://api.example.com', 'name' => 'API服务'], ], 'check_interval' => 300, // 秒 'alert_threshold' => [ 'response_time' => 3.0, // 秒 'server_load' => 75, // % 'memory_usage' => 85, // % 'disk_usage' => 90, // % ], 'admin_email' => 'admin@example.com'];// 数据库连接function connectDB($config) { $dsn = "mysql:host={$config['db_host']};dbname={$config['db_name']};charset=utf8mb4"; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]; try { return new PDO($dsn, $config['db_user'], $config['db_pass'], $options); } catch (PDOException $e) { die(" . $e->getMessage()); }}// 获取服务器状态function getServerStatus() { // 服务器负载 (Linux系统) $load = sys_getloadavg(); $server_load = $load[0]; // 1分钟负载 // 内存使用情况 $free = shell_exec('free'); $free = (string)trim($free); $free_arr = explode("\n", $free); $mem = explode(" ", $
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.