MRTG Remote Sensor - Sensors Documentation¶
Complete documentation for all 17 sensors available in MRTG Remote Sensor.
Quick Reference¶
| Sensor | Description | Parameters | Platforms |
|---|---|---|---|
| CPU Sensors | |||
| cpu | CPU load average | - | All |
| cpu% | CPU usage percentage | - | All |
| Memory Sensors | |||
| mem | Memory usage (bytes) | - | All |
| mem% | Memory usage percentage | - | All |
| Disk Sensors | |||
| disk | Disk usage (bytes) | param=/path |
All |
| disk% | Disk usage percentage | param=/path |
All |
| Battery Sensors | |||
| battery | Battery charge level | - | macOS only |
| battery% | Battery percentage | - | macOS only |
| battery- | Battery used (inverse) | - | macOS only |
| batt_volt | Battery voltage | - | macOS only |
| batt_amp | Battery amperage | - | macOS only |
| batt_cycles | Battery charge cycles | - | macOS only |
| Process Sensor | |||
| proc | Process count | param=filter |
All |
| Folder Sensors | |||
| foldersize | Folder size in bytes | param=/path |
All |
| filecount | File count in folder | param=/path |
All |
| foldercount | Subfolder count | param=/path |
All |
| Network Sensor | |||
| pingtime | Connection latency | param=host |
All |
Sensor Categories¶
System Monitoring¶
- CPU Sensors: Monitor processor load and utilization
- Memory Sensors: Track RAM usage and availability
- Disk Sensors: Monitor disk space usage
Power Management¶
- Battery Sensors: Comprehensive battery monitoring (macOS only)
- Charge level, percentage, voltage, amperage, cycles
Process Management¶
- Process Sensor: Count and filter running processes
File System¶
- Folder Sensors: Monitor directories
- Size, file count, subfolder count
- Supports recursive operations and filtering
Network¶
- Network Sensor: Measure connection latency
Usage Patterns¶
Basic Usage¶
# CPU usage
curl "http://server/index.php?key=cpu"
# Memory percentage
curl "http://server/index.php?key=mem%"
# Disk usage for root
curl "http://server/index.php?key=disk¶m=/"
With Configuration Generation¶
Add config=1 to generate MRTG configuration:
curl "http://server/index.php?key=cpu&config=1"
With Debugging¶
Add debug=1 to enable debug output:
curl "http://server/index.php?key=mem&debug=1"
With Options¶
Some sensors support additional options:
# Recursive file count with name filter
curl "http://server/index.php?key=filecount¶m=/var/log&options=recursive=1,name=*.log"
Platform Support¶
| Platform | CPU | Memory | Disk | Battery | Process | Folder | Network |
|---|---|---|---|---|---|---|---|
| Linux | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ |
| macOS | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Windows | ✅ | ✅ | ✅ | ❌ | ✅ | ⚠️* | ✅ |
| BusyBox | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ |
*Windows folder operations require PHP COM extension
Implementation Details¶
Sensor Class¶
All sensors are implemented in src/Sensor/Sensor.php as methods that return SensorResult objects.
public function cpuusage(bool $asPercent = false): SensorResult
public function memusage(bool $asPercent = false): SensorResult
public function diskusage(?string $path = null, bool $asPercent = false): SensorResult
public function battery(string $type = ''): SensorResult
public function proccount(?string $filter = null): SensorResult
public function foldersize(?string $path = null, array $options = []): SensorResult
public function filecount(?string $path = null, array $options = []): SensorResult
public function foldercount(?string $path = null, array $options = []): SensorResult
public function pingtime(string $address, int $port = 80): SensorResult
OS Abstraction¶
Each sensor uses OS-specific implementations via the OSTools interface:
- LinuxTools: Uses
sys_getloadavg(),free,df,du,ps - DarwinTools: Uses
sysctl,vm_stat,system_profiler - WindowsTools: Uses
wmiccommands - BusyBoxTools: Limited command set for Synology NAS
Caching¶
All sensor data is cached using phpfastcache: - Default TTL: 30 seconds for most sensors - Longer TTL: 3600 seconds for static data (uptime, folder sizes) - Configurable per command execution
Security¶
- Input Sanitization: All parameters sanitized via
Sensor::sanitize() - Path Validation: Disk and folder operations validate paths
- Read-Only: No write operations, no sudo/root required
- Command Injection Prevention: Shell metacharacters removed
MRTG Output Format¶
All sensors return data in standard MRTG 4-line format:
<value1>
<value2>
<uptime>
<server>
With config=1¶
When config=1 is specified, sensors return complete MRTG configuration:
Target[id]: `curl -s "http://server/index.php?key=cpu"`
Title[id]: server: CPU Usage
PageTop[id]: <h1>server: CPU Usage</h1>
ShortLegend[id]: load
Options[id]: growright,nobanner,gauge
MaxBytes[id]: 500
kMG[id]: ,k,M,G,T,P
Error Handling¶
Common Error Responses¶
# Unknown sensor type
Error: Unknown sensor type: invalid
# Missing required parameter
Error: Disk sensor requires 'param' parameter
# Invalid path
Error: Path does not exist: /invalid/path
# Platform not supported
Error: Battery sensors only available on macOS
HTTP Status Codes¶
- 200 OK: Successful sensor reading
- 400 Bad Request: Invalid parameters
- 500 Internal Server Error: Sensor execution failed
Testing¶
Each sensor can be tested individually:
# Test CPU sensor
curl -s "http://localhost/index.php?key=cpu" | head -4
# Test with debug output
curl -s "http://localhost/index.php?key=mem&debug=1"
# Test configuration generation
curl -s "http://localhost/index.php?key=disk¶m=/&config=1"
Performance Considerations¶
Caching Strategy¶
- Fast Sensors (CPU, Memory): 30-second cache
- Medium Sensors (Disk, Process): 30-second cache
- Slow Sensors (Folder operations): 3600-second cache
- Network Sensor: 30-second cache
Resource Usage¶
- CPU: Minimal impact, uses system load averages
- Memory: Instant, reads from
/procor system calls - Disk: Fast, uses
dfcommand - Folder: Can be slow for large directories (use caching!)
- Process: Fast, simple count
- Network: Depends on target latency
Best Practices¶
1. Use Appropriate Sensors¶
- Use
cpu%for percentage-based monitoring - Use
disk%when absolute bytes aren't needed - Use
procwith filters to monitor specific services
2. Configure Caching¶
- Don't disable caching for expensive operations
- Adjust TTL based on update frequency needs
- Use longer TTL for folder operations
3. MRTG Configuration¶
- Use
growrightoption for forward-growing graphs - Use
gaugefor current values (not counters) - Set appropriate
MaxBytesfor scaling
4. Security¶
- Restrict access via web server config
- Use HTTPS for remote monitoring
- Validate all paths and parameters
- Don't expose debug mode in production
Contributing¶
To add a new sensor:
- Add enum case to
src/Enum/SensorType.php - Add abstract method to
src/OS/OSTools.php - Implement in all OS tool classes
- Add sensor method to
src/Sensor/Sensor.php - Add route in
public/index.php - Write tests in
tests/Integration/ - Document in
docs/sensors/<sensor>.md