fsmon - File System Monitor
🌍 Select Language | 选择语言
Lightweight High-Performance File System Change Tracking Tool
fsmon (file system monitor) is a real-time file change monitoring tool that tracks filesystem changes and records which process executed them. When you need to answer "Who modified this file on the server?", fsmon is your answer.
Features
- Real-time Monitoring: Captures 8 core change events by default (CREATE, DELETE, DELETE_SELF, MOVED_FROM, MOVED_TO, MOVE_SELF, CLOSE_WRITE, ATTRIB),
--all-eventsenables all 14 fanotify events - Complete Process Tracking: Captures PID, command name, and username for short-lived processes (touch/rm/mv) via Proc Connector
- Recursive Monitoring:
-r/--recursiveparameter monitors all subdirectories, dynamically tracking newly created directories - Recursive Deletion Capture: Completely captures all file deletion events during recursive directory deletion (including paths of files in deleted directories)
- High Performance: Written in Rust, <5MB memory usage, zero-copy event parsing
- Flexible Filtering: Filter by time, size, process, event type
- Multiple Output Formats: Human-readable, JSON, CSV
- Daemon Mode: Run in background with persistent logging
Quick Start
Prerequisites
- OS: Linux 5.9+ (requires fanotify FID mode support)
- Filesystem: ext4 / XFS / tmpfs (btrfs partial support with race window)
- Build Tools: Rust toolchain (cargo)
Check kernel version:
Install Rust (if not installed):
|
Installation
Method 1: Build from Source (Recommended)
# 1. Clone repository
# 2. Install directly from source
Method 2: Install from crates.io
# Install from crates.io
# Or install from Git
Optional - copy to system path for sudo usage:
8 Typical Scenarios
Scenario 1: Investigate Who Modified Configuration Files
# Monitor /etc directory for modifications
# Execute modification in another terminal
|
# Expected output
)
# Query afterwards
Scenario 2: Track Large File Creation
# Monitor file creation larger than 50MB
# Trigger operation
# Expected output
}
Scenario 3: Audit Deletion Operations (Complete Recursive Deletion Capture)
# Recursively monitor deletion events
# Trigger operation
# Expected output (subdirectory file paths preserved)
)
)
Technical Highlight: Through directory handle caching mechanism, rm -rf recursive deletion completely captures deletion events of all files and subdirectories.
Scenario 4: Monitor Specific Applications (Short-lived Process Capture)
# Recursively monitor project directory
# Trigger operations (short-lived processes like touch/rm/mv)
# Expected output (short-lived process CMD correctly displayed)
)
)
)
)
Technical Highlight: Proc Connector caches information at process exec() instant, ensuring accurate CMD display for short-lived processes like touch/rm/mv.
Scenario 5: File Move/Rename Audit
# Monitor move events
# Trigger operations
# Expected output
)
)
Scenario 6: Long-term Daemon Monitoring
# Start daemon
# Check status
# JSON format (for integration with monitoring systems)
# Query analysis
# Stop daemon
Scenario 7: Multi-condition Combined Queries
# Delete/move operations by root or admin users in past 7 days
# Create/modify operations larger than 10MB in past 1 hour
# Wildcard command matching
# CSV export
Scenario 8: Log Cleanup and Space Management
# Preview cleanup effect (keep 7 days)
# Execute cleanup
# Limit size simultaneously
Command Reference
Run fsmon <command> --help for full parameter documentation:
Output Format Examples
Human-readable Format
[2024-05-01 14:30:25] [MODIFY] /var/log/syslog (PID: 1234, CMD: rsyslogd, USER: syslog, SIZE: +2.5KB)
MOVED_FROM / MOVED_TO Events
[2024-05-01 14:35:10] [MOVED_FROM] /home/user/old.txt (PID: 5678, CMD: mv, USER: user, SIZE: +0B)
[2024-05-01 14:35:10] [MOVED_TO] /home/user/new.txt (PID: 5678, CMD: mv, USER: user, SIZE: +0B)
[2024-05-01 14:40:22] [MOVED_FROM] /tmp/source/file.txt (PID: 9012, CMD: mv, USER: root, SIZE: +0B)
[2024-05-01 14:40:22] [MOVED_TO] /var/data/file.txt (PID: 9012, CMD: mv, USER: root, SIZE: +0B)
JSON Format
CSV Format
time,event_type,path,pid,cmd,user,size_change
2024-05-01T14:30:25Z,MODIFY,/var/log/syslog,1234,rsyslogd,syslog,2560
Technical Architecture
Core Technologies
- fanotify (FID mode): Linux kernel-level file monitoring with FAN_REPORT_FID | FAN_REPORT_DIR_FID | FAN_REPORT_NAME support for complete event information
- Proc Connector (Netlink): Listens to process exec() events, caches PID → (cmd, user) mapping at process startup instant, solving short-lived process detection
- name_to_handle_at: Pre-caches directory file handles for path recovery during directory deletion
- Rust + Tokio: Async runtime with high concurrency and low latency
Event Types
By default captures 8 core change events, --all-events enables all 14.
Default Events (8 Change Events):
| Event Type | fanotify Constant | Trigger Condition |
|---|---|---|
| CLOSE_WRITE | FAN_CLOSE_WRITE | Write-mode file closed (best "file modified" signal) |
| ATTRIB | FAN_ATTRIB | File metadata modified (permissions, owner, timestamps, etc.) |
| CREATE | FAN_CREATE | File/directory created |
| DELETE | FAN_DELETE | File/directory deleted |
| DELETE_SELF | FAN_DELETE_SELF | Monitored object itself deleted |
| MOVED_FROM | FAN_MOVED_FROM | File moved out from this directory |
| MOVED_TO | FAN_MOVED_TO | File moved into this directory |
| MOVE_SELF | FAN_MOVE_SELF | Monitored object itself moved |
--all-events Additional Events (6 Access/Diagnostic Events):
| Event Type | fanotify Constant | Trigger Condition |
|---|---|---|
| ACCESS | FAN_ACCESS | File read |
| MODIFY | FAN_MODIFY | File content written (triggers on every write(), very noisy) |
| CLOSE_NOWRITE | FAN_CLOSE_NOWRITE | Read-only file/directory closed |
| OPEN | FAN_OPEN | File/directory opened |
| OPEN_EXEC | FAN_OPEN_EXEC | File opened for execution |
| FS_ERROR | FAN_FS_ERROR | Filesystem error (Linux 5.16+) |
Additionally, FAN_Q_OVERFLOW is automatically delivered by kernel when event queue overflows; fsmon outputs warning to stderr.
License
MIT License