diff --git a/includes/lib_remote.php b/includes/lib_remote.php index aafb450e..9fda8640 100755 --- a/includes/lib_remote.php +++ b/includes/lib_remote.php @@ -1198,5 +1198,27 @@ class OGPRemoteLibrary //Unknown response return -4; } + + /// Get system-wide resource usage from agent + public function get_system_resource_usage() + { + $args = $this->encrypt_params("system_resources"); + $this->add_enc_chk($args); + $request = xmlrpc_encode_request("get_system_resource_usage", $args); + $response = $this->sendRequest($request); + + return $response; + } + + /// Get resource usage for a specific game server from agent + public function get_gameserver_resource_usage($home_id) + { + $args = $this->encrypt_params($home_id); + $this->add_enc_chk($args); + $request = xmlrpc_encode_request("get_gameserver_resource_usage", $args); + $response = $this->sendRequest($request); + + return $response; + } } ?> diff --git a/modules/resource_monitor/README.md b/modules/resource_monitor/README.md new file mode 100644 index 00000000..f5657829 --- /dev/null +++ b/modules/resource_monitor/README.md @@ -0,0 +1,184 @@ +# OGP Resource Monitoring System + +This module provides comprehensive resource monitoring for Open Game Panel (OGP) installations, including real-time monitoring, alerting, and historical data visualization. + +## Features + +- **System-wide Monitoring**: CPU, RAM, and disk usage for entire servers +- **Per-Game-Server Monitoring**: Individual resource usage for each game instance +- **Discord Alerts**: Configurable threshold-based alerts sent to Discord channels +- **Historical Data**: 30-day retention with trend visualization +- **Automated Collection**: 5-minute interval data collection via cron jobs +- **Web Dashboard**: Real-time status with color-coded indicators + +## Installation + +1. **Database Setup**: The resource monitoring tables are automatically created when applying the main database schema, or you can apply them individually: + ```bash + mysql -u username -p database_name < db/resource_monitoring_schema.sql + ``` + +2. **Agent Enhancement**: The monitoring functions are added to the OGP agent (`_agent-linux/ogp_agent.pl`). The agent must be restarted for the new functions to be available. + +3. **Cron Job Setup**: Set up automated data collection: + ```bash + # Run the setup script + ./scripts/setup_monitoring_cron.sh + + # Or manually add to crontab: + */5 * * * * /usr/bin/php /path/to/GSP/scripts/resource_collector.php >> /var/log/ogp_monitoring.log 2>&1 + ``` + +4. **PHP Requirements**: Ensure the following PHP extensions are installed: + - mysqli (for database access) + - curl (for Discord webhooks) + - xmlrpc (for agent communication) + +## Usage + +### Dashboard Access + +Navigate to the Resource Monitor module in your OGP panel: +- **Dashboard**: Real-time resource status for all servers +- **History**: Historical data and trend analysis +- **Alerts**: Configure Discord alerts and thresholds +- **Configuration**: Setup instructions and system status + +### Setting Up Alerts + +1. **Create Discord Webhook**: + - In your Discord server, go to Server Settings → Integrations → Webhooks + - Create a new webhook and copy the webhook URL + +2. **Configure Alert Thresholds**: + - Access the "Alert Configuration" page + - Set the default Discord webhook URL + - Add alerts for specific servers and resource types + - Configure threshold percentages and duration requirements + +3. **Alert Parameters**: + - **Threshold**: Percentage at which to trigger alerts (default: 80%) + - **Duration**: Minutes the threshold must be exceeded (default: 30) + - **Cooldown**: Minimum time between identical alerts (default: 60 minutes) + +### Data Collection + +The system collects the following metrics: + +**System-wide (per server)**: +- CPU usage percentage +- Memory usage percentage and absolute values +- Disk usage percentage and absolute values +- Network traffic (cumulative) + +**Per-game-server**: +- CPU usage by game processes +- Memory usage by game processes +- Process count + +### API Endpoints + +The system provides API endpoints for data collection: +- `?m=resource_monitor&type=api_collect` - Trigger manual collection + +## Technical Details + +### Database Schema + +- `ogp_resource_monitoring`: Timestamped resource data +- `ogp_resource_alerts`: Alert configurations +- `ogp_resource_alert_history`: Alert trigger history +- `ogp_discord_settings`: Discord integration settings + +### Agent Functions + +New XML-RPC functions added to the agent: +- `get_system_resource_usage()`: System-wide metrics +- `get_gameserver_resource_usage(home_id)`: Game-specific metrics + +### Data Retention + +- Resource monitoring data: 30 days +- Alert history: 90 days +- Automatic cleanup via scheduled database operations + +## Configuration Examples + +### Basic Alert Setup +``` +Server: My Game Server +Resource Type: CPU +Threshold: 80% +Duration: 30 minutes +``` + +### Discord Webhook Format +```json +{ + "username": "OGP Resource Monitor", + "embeds": [{ + "title": "🚨 Resource Alert Triggered", + "color": 15158332, + "fields": [ + {"name": "Server", "value": "My Server", "inline": true}, + {"name": "Resource", "value": "CPU", "inline": true}, + {"name": "Usage", "value": "85.2%", "inline": true} + ] + }] +} +``` + +## Troubleshooting + +### Common Issues + +1. **No Data Appearing**: + - Verify cron job is running: `crontab -l` + - Check agent connectivity and authentication + - Review log files: `tail -f /var/log/ogp_monitoring.log` + +2. **Agent Connection Errors**: + - Ensure agent is running and accessible + - Verify encryption keys match between panel and agent + - Check firewall settings for agent port + +3. **Discord Alerts Not Sending**: + - Verify webhook URL is correct and active + - Check Discord server permissions + - Review PHP curl extension availability + +### Log Files + +- Monitoring collection: `/var/log/ogp_monitoring.log` +- PHP errors: Check your web server's error log +- Agent logs: `_agent-linux/ogp_agent.log` + +## Performance Considerations + +- Data collection occurs every 5 minutes per server +- Database queries are optimized with proper indexing +- Historical data is automatically pruned after 30 days +- Dashboard queries are limited to recent data for performance + +## Security + +- All agent communication uses XXTEA encryption +- Discord webhooks use HTTPS +- Database queries use proper escaping to prevent injection +- Alert cooldowns prevent webhook spam + +## Future Enhancements + +- Advanced charting and graphing +- Email alert options +- Mobile-responsive dashboard improvements +- Custom alert conditions and thresholds +- Integration with external monitoring systems + +## Support + +For issues and questions: +1. Check the OGP documentation +2. Review log files for error messages +3. Verify all dependencies are installed +4. Test individual components (database, agent, webhooks) \ No newline at end of file diff --git a/modules/resource_monitor/resource_functions.php b/modules/resource_monitor/resource_functions.php index 013e9a30..745e2ec0 100644 --- a/modules/resource_monitor/resource_functions.php +++ b/modules/resource_monitor/resource_functions.php @@ -86,26 +86,39 @@ function store_resource_data($remote_server_id, $home_id, $data) { global $db; + // Sanitize values + $cpu_usage = isset($data['cpu_usage']) ? floatval($data['cpu_usage']) : 'NULL'; + $memory_usage = isset($data['memory_usage']) ? floatval($data['memory_usage']) : 'NULL'; + $memory_used_mb = isset($data['memory_used_mb']) ? intval($data['memory_used_mb']) : 'NULL'; + $memory_total_mb = isset($data['memory_total_mb']) ? intval($data['memory_total_mb']) : 'NULL'; + $disk_usage = isset($data['disk_usage']) ? floatval($data['disk_usage']) : 'NULL'; + $disk_used_mb = isset($data['disk_used_mb']) ? intval($data['disk_used_mb']) : 'NULL'; + $disk_total_mb = isset($data['disk_total_mb']) ? intval($data['disk_total_mb']) : 'NULL'; + $process_count = isset($data['process_count']) ? intval($data['process_count']) : 'NULL'; + $network_rx_mb = isset($data['network_rx_mb']) ? intval($data['network_rx_mb']) : 'NULL'; + $network_tx_mb = isset($data['network_tx_mb']) ? intval($data['network_tx_mb']) : 'NULL'; + + $home_id_val = $home_id ? intval($home_id) : 'NULL'; + $query = "INSERT INTO ogp_resource_monitoring (remote_server_id, home_id, cpu_usage, memory_usage, memory_used_mb, memory_total_mb, disk_usage, disk_used_mb, disk_total_mb, process_count, network_rx_mb, network_tx_mb) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + VALUES ( + " . intval($remote_server_id) . ", + $home_id_val, + $cpu_usage, + $memory_usage, + $memory_used_mb, + $memory_total_mb, + $disk_usage, + $disk_used_mb, + $disk_total_mb, + $process_count, + $network_rx_mb, + $network_tx_mb + )"; - $stmt = $db->prepare($query); - $stmt->execute([ - $remote_server_id, - $home_id, - isset($data['cpu_usage']) ? floatval($data['cpu_usage']) : null, - isset($data['memory_usage']) ? floatval($data['memory_usage']) : null, - isset($data['memory_used_mb']) ? intval($data['memory_used_mb']) : null, - isset($data['memory_total_mb']) ? intval($data['memory_total_mb']) : null, - isset($data['disk_usage']) ? floatval($data['disk_usage']) : null, - isset($data['disk_used_mb']) ? intval($data['disk_used_mb']) : null, - isset($data['disk_total_mb']) ? intval($data['disk_total_mb']) : null, - isset($data['process_count']) ? intval($data['process_count']) : null, - isset($data['network_rx_mb']) ? intval($data['network_rx_mb']) : null, - isset($data['network_tx_mb']) ? intval($data['network_tx_mb']) : null - ]); + return $db->query($query); } /** @@ -117,10 +130,9 @@ function check_system_alerts($server, $data) // Get active alerts for this server (system-wide) $query = "SELECT * FROM ogp_resource_alerts - WHERE remote_server_id = ? AND home_id IS NULL AND is_active = 1"; - $stmt = $db->prepare($query); - $stmt->execute([$server['remote_server_id']]); - $alerts = $stmt->fetchAll(); + WHERE remote_server_id = " . intval($server['remote_server_id']) . " + AND home_id IS NULL AND is_active = 1"; + $alerts = $db->resultQuery($query); foreach ($alerts as $alert) { $resource_value = null; @@ -407,17 +419,16 @@ function show_dashboard() echo "
No recent resource data available. Make sure the monitoring system is configured and running.
"; echo "Configure Monitoring"; return; @@ -442,7 +453,7 @@ function show_dashboard() foreach ($servers as $server_id => $server) { $data = $server['latest']; - $server_name = $data['agent_name'] ?: $data['agent_ip']; + $server_name = $data['remote_server_name'] ?: $data['agent_ip']; echo "| Time | Server | Target | CPU % | Memory % | Disk % |
|---|---|---|---|---|---|