A lightweight PHP script to monitor and report system metrics including CPU load, memory usage, disk space, system uptime, and MySQL connectivity. The script collects data from /proc
on Linux systems and outputs an HTML-formatted report.
- CPU Load Monitoring: Calculates 1, 5, and 15-minute load averages as a percentage of CPU cores.
- Memory Usage: Parses
/proc/meminfo
to report total, used, and available memory. - Disk Usage: Reports total, used, and free space for root (
/
), home, and temporary (/tmp
) directories. - System Uptime: Reads
/proc/uptime
to display days, hours, and minutes since last boot. - MySQL Status Check: Verifies database connectivity using MySQLi and reports server version.
- Threshold Warnings: Configurable alert thresholds for high CPU, low memory, low disk space, and long uptime.
-
PHP 7.4 or higher with the following extensions enabled:
mysqli
posix
(optional for user detection)proc_*
functions (for reading/proc
filesystem)
-
MySQL or MariaDB server
-
Linux-based operating system (for
/proc
filesystem support)
-
Clone the Repository
git clone https://github.com/your-username/server-monitor.git cd server-monitor
-
Configure Database Credentials Edit
config.php
and set your database connection parameters:<?php define('DB_HOST', 'localhost'); define('DB_USER', 'your_db_user'); define('DB_PASS', 'your_db_password'); define('DB_NAME', 'your_db_name');
-
Adjust Thresholds You can define alert thresholds in
config.php
or in a dedicated settings file:define('THRESHOLD_CPU_LOAD_1', 0.8); define('THRESHOLD_CPU_LOAD_5', 0.6); define('THRESHOLD_CPU_LOAD_15', 0.6); define('THRESHOLD_RAM_PERCENT', 20); define('THRESHOLD_DISK_ROOT_PERCENT', 10); define('THRESHOLD_DISK_TMP_PERCENT', 20); define('THRESHOLD_DISK_HOME_PERCENT', 20); define('THRESHOLD_UPTIME_DAYS', 365);
-
Deploy to Web Server
- Copy the project files to your web server root (e.g.,
/var/www/html/server-monitor
). - Ensure PHP has read permissions for
/proc
and the project directory.
- Copy the project files to your web server root (e.g.,
Open your browser and navigate to:
http://<server-ip-or-domain>/server-monitor/index.php
You will see a formatted report showing system health and any warnings.
server-monitor/
├── config.php # Configuration for database and thresholds
├── index.php # Main entry point, renders the dashboard
├── src/
│ ├── SystemInfo.php # Collects CPU, memory, disk, uptime metrics
│ ├── DatabaseChecker.php # Checks MySQL connectivity
│ └── Renderer.php # Outputs HTML report
└── README.md # Project documentation
-
Blank Page or Errors: Enable error reporting in
php.ini
or add at top ofindex.php
:ini_set('display_errors', 1); error_reporting(E_ALL);
-
Permission Denied Reading
/proc
: Ensure the PHP process user (e.g.,www-data
) has read access to/proc
files. -
MySQL Connection Issues: Verify credentials and network access. Test with command-line:
mysql -u your_db_user -p -h localhost
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/your-feature
) - Commit your changes (
git commit -m 'Add some feature'
) - Push to the branch (
git push origin feature/your-feature
) - Open a Pull Request
This project is licensed under the MIT License. See LICENSE for details.
Last updated: 2025-04-26