first commit

This commit is contained in:
2026-05-18 14:05:06 -04:00
commit a91a0f9cf3
27 changed files with 3030 additions and 0 deletions

239
docs/API.md Normal file
View File

@@ -0,0 +1,239 @@
# Fsync Redis Schema Documentation
This document describes the Redis data structures used by the Fsync file system monitor.
## Overview
Fsync stores file system events in Redis with the following information:
- **Hostname**: The server where the change occurred
- **File Path**: The relative path of the changed file
- **Event Type**: The type of file system event
- **Checksum**: SHA256 checksum of file contents (for modify events)
- **Metadata**: File size, permissions, timestamp, PID
## Data Structures
### Event Hash Keys
Each file system event is stored as a Redis Hash with the following key format:
```
fsync:events:{timestamp}:{hostname}:{pid}
```
**Example:**
```
fsync:events:2026-05-18T13:28:51Z:server1:12345
```
### Event Hash Fields
| Field | Type | Description |
|-------|------|-------------|
| `hostname` | string | The hostname where the event occurred |
| `path` | string | Absolute file path |
| `event_type` | string | Type of event (OPEN, CLOSE_WRITE, MODIFY, CREATE, DELETE, MOVED_FROM, MOVED_TO, ATTRIB) |
| `checksum` | string | SHA256 checksum of file contents (64 hex chars) |
| `timestamp` | string | ISO8601 timestamp (2026-05-18T13:28:51Z) |
| `file_size` | number | Size of file in bytes |
| `permissions` | string | Octal permissions (e.g., "0644") |
| `process_id` | number | PID of process that triggered the event |
### Example Event
```
Key: fsync:events:2026-05-18T13:28:51Z:server1:1234
Value:
hostname "server1"
path "/var/www/app.js"
event_type "MODIFY"
checksum "a1b2c3d4e5f6..."
timestamp "2026-05-18T13:28:51Z"
file_size "1024"
permissions "0644"
process_id "1234"
```
### Events List
All event keys are added to a list for quick retrieval:
```
Key: fsync:events:list
Type: List (LPUSH)
```
This allows efficient range queries of all recorded events.
## Common Queries
### Get All Events
```bash
redis-cli LRANGE fsync:events:list 0 -1
```
### Get Event Details
```bash
redis-cli HGETALL fsync:events:2026-05-18T13:28:51Z:server1:1234
```
### Get Events by Hostname
```bash
# Get list of all events
redis-cli LRANGE fsync:events:list 0 -1
# Filter by hostname using pattern
redis-cli --pattern 'fsync:events:*:server1:*'
```
### Get Events by Path
```bash
# Scan and filter by path field
redis-cli --scan --pattern 'fsync:events:*' | xargs redis-cli HGETALL
```
### Get Recent Events (Last 100)
```bash
redis-cli LRANGE fsync:events:list 0 99
```
### Get Events Modified in Last Hour
```bash
# Using Lua script or application-level filtering
redis-cli LRANGE fsync:events:list 0 -1 | \
xargs -I {} redis-cli HGET {} timestamp | \
grep -E '(2026-05-18T1[2-3]:[0-5][0-9]:[0-5][0-9]Z)'
```
## Redis CLI Examples
### Monitor Events in Real-time
```bash
redis-cli MONITOR | grep fsync:events
```
### Get Event Count
```bash
redis-cli LLEN fsync:events:list
```
### Delete Old Events
```bash
redis-cli EVAL "
local keys = redis.call('LRANGE', 'fsync:events:list', 0, -1)
for _, key in ipairs(keys) do
redis.call('DEL', key)
end
" 0
```
### Export Events to JSON (with redis-cli)
```bash
redis-cli --json LRANGE fsync:events:list 0 -1
```
## Event Types
| Event | Description |
|-------|-------------|
| `OPEN` | File was opened |
| `CLOSE_WRITE` | File was closed after write |
| `CLOSE_NOWRITE` | File was closed without write |
| `MODIFY` | File contents were modified |
| `CREATE` | File was created |
| `DELETE` | File was deleted |
| `MOVED_FROM` | File was moved (source) |
| `MOVED_TO` | File was moved (destination) |
| `ATTRIB` | File attributes changed (permissions, owner, etc.) |
## Checksum Calculation
SHA256 checksums are calculated for:
- `MODIFY` events: Capture content changes
- `CLOSE_WRITE` events: Verify final content
- Other events: Empty hash or error indicator
**Special Cases:**
- Files > 100MB: Checksum = `0000000000000000000000000000000000000000000000000000000000000000`
- Files that can't be read: Checksum = `error`
- Directories/symlinks: No checksum (empty string)
## Data Expiration
Events are automatically expired after 30 days using Redis `EXPIRE` command.
```
EXPIRE fsync:events:{key} 2592000 # 30 days in seconds
```
## Performance Considerations
1. **List Growth**: The `fsync:events:list` can grow large. Consider periodic cleanup or archival.
2. **Hash Storage**: Each event stores ~1-2KB of data in Redis memory.
3. **Network Events**: High-frequency events (millions/day) may benefit from batching.
## Development Usage
### Python Example
```python
import redis
import json
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
# Get all event keys
event_keys = r.lrange('fsync:events:list', 0, -1)
# Get event details
for key in event_keys:
event = r.hgetall(key)
print(json.dumps(event, indent=2))
```
### Node.js Example
```javascript
const redis = require('redis');
const client = redis.createClient();
client.lrange('fsync:events:list', 0, -1, (err, keys) => {
keys.forEach(key => {
client.hgetall(key, (err, event) => {
console.log(event);
});
});
});
```
## Troubleshooting
### No Events Showing Up
1. Verify Redis connection: `redis-cli PING` → `PONG`
2. Check fsync-monitor is running: `ps aux | grep fsync-monitor`
3. Verify file system activity: `touch /tmp/test.txt`
4. Check Redis logs: `redis-cli INFO stats`
### Memory Growing Too Large
1. Check event count: `redis-cli LLEN fsync:events:list`
2. Manually cleanup old events
3. Consider archiving to external storage
4. Reduce monitored paths to high-priority directories
### Checksum Errors
1. Verify file permissions: `ls -la /path/to/file`
2. Check OpenSSL installation: `which openssl`
3. Monitor disk space: `df -h`

401
docs/RABBITMQ.md Normal file
View File

@@ -0,0 +1,401 @@
# RabbitMQ Integration Guide
Fsync integrates with RabbitMQ for distributed file synchronization across multiple hosts.
## Architecture
```
Host A RabbitMQ Host B
┌──────────────┐ ┌──────────┐ ┌──────────────┐
│ Monitor │ │Exchange │ │ Monitor │
│ (Publish) │────────>│fsync.* │────────>│ (Subscribe) │
│ │ │ │ │ │
│ Detects: │ └──────────┘ │ Receives: │
│ - File mods │ │ │ - Updates │
│ - Creates │ ┌─────────────┐ │ - Adds queue │
│ - Deletes │ │Queue │ │ - Fetches │
└──────────────┘ │fsync.files │ │ - Verifies │
└─────────────┘ └──────────────┘
```
## How File Sync Works
### 1. Local File Change Detection (Host A)
- File modified on Host A
- Fanotify detects change immediately
- Calculate SHA256 checksum
- Store event in Redis
- **Publish to RabbitMQ** with hostname, path, checksum
### 2. Remote Host Receives Update (Host B)
- Consume message from RabbitMQ queue
- Parse JSON with file details
- **Add to priority queue** (oldest/smallest first)
- Record in Redis
### 3. File Fetch and Sync (Host B)
- Process queue every 5 seconds
- Get next highest priority file
- **Fetch via SCP** from Host A (hostname:path)
- Verify checksum matches
- Store locally in cache
- Update Redis with status
## Message Format
Events published to RabbitMQ as JSON:
```json
{
"hostname": "server1",
"path": "/var/www/app.js",
"event": "MODIFY",
"checksum": "a1b2c3d4e5f6...",
"timestamp": 1684423051,
"size": 1024
}
```
## Queue Priority Algorithm
Files fetched in this order:
1. **By Age** (oldest first) - files detected earliest get priority
2. **By Size** (smallest first) - smaller files processed faster
Example queue:
```
Order | Host | File | Age | Size | Status
------|-------|----------------|-------|---------|--------
1 | host2 | /etc/config | 2h | 2KB | Fetch now
2 | host1 | /app/db.sql | 90m | 50MB | Next
3 | host2 | /cache.tmp | 30m | 100MB | Waiting
4 | host1 | /data/doc.pdf | 20m | 500KB | Waiting
```
## Configuration
Edit `src/config.h`:
```c
/* RabbitMQ Server */
#define AMQP_HOST "127.0.0.1"
#define AMQP_PORT 5672
#define AMQP_USER "guest"
#define AMQP_PASSWORD "guest"
#define AMQP_VHOST "/"
/* Exchange and Queue */
#define AMQP_EXCHANGE "fsync.events"
#define AMQP_QUEUE_UPDATES "fsync.file.updates"
#define AMQP_ROUTING_KEY "fsync.file.*"
/* File Cache */
#define CACHE_DIR "/tmp/fsync-cache"
#define MAX_QUEUE_SIZE 1000
```
## Quick Start
### 1. Start Services
```bash
docker-compose up -d
make build
```
### 2. Run Monitor on Multiple Hosts
**Host A:**
```bash
sudo ./build/fsync-monitor
```
**Host B:**
```bash
sudo ./build/fsync-monitor
```
### 3. Test Cross-Host Sync
On Host A, modify a file:
```bash
echo "test data" >> /var/www/index.html
```
Host B will:
- Receive notification via RabbitMQ
- Add `/var/www/index.html` to fetch queue
- Fetch file from Host A via SCP
- Verify checksum
- Cache locally
### 4. Monitor Progress
**Check queue status:**
```bash
redis-cli LLEN fsync:events:list
```
**View cached files:**
```bash
ls -lh /tmp/fsync-cache/
```
**Monitor RabbitMQ:**
Open http://localhost:15672 (guest/guest)
## Operational Scenarios
### Scenario 1: Config File Update
```
Host1: /etc/app.conf modified
├─> Checksum: a1b2c3d4
└─> Publish to RabbitMQ
│
Host2: Receives notification
├─> Add to queue (priority 1)
├─> Fetch /etc/app.conf from Host1
├─> Verify checksum matches
└─> Cache at /tmp/fsync-cache/host1_app.conf
```
### Scenario 2: Bulk File Changes
```
1000 files modified on Host1
└─> 1000 messages to RabbitMQ
Host2 receives 1000 updates
└─> Add all to priority queue
│
Processing:
├─> Fetch oldest files first
├─> Then by size (KB before MB)
├─> One at a time sequentially
└─> Verify each checksum
```
### Scenario 3: Multiple Source Hosts
```
Host1 changes: /var/www/app.js
└─> Publish to RabbitMQ
Host2 changes: /etc/nginx.conf
└─> Publish to RabbitMQ
Host3 receives both notifications
└─> Queue both files
└─> Fetch oldest first
└─> Then by size
```
## File Transfer Mechanism
### SCP-Based Transfer
Current implementation uses `scp`:
```bash
scp -o ConnectTimeout=30 hostname:path local_path
```
**Requirements:**
- SSH key authentication configured
- SSH access between hosts
- Read permissions on source file
### Checksum Verification
After transfer:
1. Calculate SHA256 of fetched file
2. Compare with checksum from notification
3. If mismatch: retry (max 3 attempts)
4. If success: cache and record in Redis
## Redis Storage
### Published Events
```
Key: fsync:events:{timestamp}:{hostname}:{pid}
Example:
fsync:events:2026-05-18T13:30:00Z:server1:1234
├─ hostname: "server1"
├─ path: "/var/www/app.js"
├─ event_type: "MODIFY"
├─ checksum: "a1b2c3d4..."
└─ timestamp: "2026-05-18T13:30:00Z"
```
### Fetch Records
```
Key: fsync:fetches:success:{hostname}:{path}:{timestamp}
Key: fsync:fetches:failed:{hostname}:{path}:{timestamp}
```
## Performance
### Throughput
- **Event detection:** <1ms
- **Publishing:** ~5-10ms per event
- **File fetch:** Depends on size/network
- **Verification:** ~100-500ms per file
### Queue Processing
- Check interval: 1 second
- Process 1 file per 5 seconds (configurable)
- Network-bound for file transfer
### Memory Usage
- Queue: ~1-2KB per pending file
- Cache: Limited by disk space
- Connections: ~5MB per RabbitMQ connection
## Troubleshooting
### Queue Not Draining
```
[QUEUE] Added: host2:/etc/config (queue size: 100)
[FETCHER] Failed to fetch: host2:/etc/config
```
**Causes:**
1. SSH key not configured
2. File permissions on source
3. Network connectivity
4. Host not reachable
**Debug:**
```bash
# Test SSH access
ssh user@host2 'ls -la /etc/config'
# Check firewall
nc -zv host2 22
```
### Checksum Mismatch
```
[FETCHER] Checksum mismatch: /tmp/fsync-cache/host2_config
```
**Causes:**
1. File changed during transfer
2. Network corruption
3. Filesystem error
**Action:** Automatic retry (up to 3 times)
### RabbitMQ Connection Lost
```
Failed to open AMQP socket
```
**Fix:**
```bash
docker-compose logs rabbitmq
docker-compose restart rabbitmq
```
### File Transfer Timeout
```
[FETCHER] Failed to fetch after 3 attempts
```
**Solutions:**
1. Increase `FETCH_TIMEOUT_SEC` in config
2. Check network bandwidth
3. Check host load
## Advanced Configuration
### Custom Cache Directory
```c
#define CACHE_DIR "/var/lib/fsync-cache"
```
Persist fetched files across restarts.
### Parallel Fetching (Future)
Currently processes 1 file at a time. Can be extended to:
- Multi-threaded fetcher
- Batch SCP transfers
- Parallel checksums
### Routing Filters
Modify routing key to sync specific paths:
```c
#define AMQP_ROUTING_KEY "fsync.file.var.www.*" /* Only /var/www */
```
## Security Considerations
### SSH Authentication
- Configure SSH keys for password-less access
- Use key-based authentication
- Restrict SSH to internal network
### RabbitMQ
- Change default guest/guest credentials
- Use TLS for AMQP connections
- Restrict network access
### File Permissions
- Monitor has read access to synced files
- Cache directory writable by monitor
- Source files readable by SSH user
## Monitoring and Metrics
### Queue Depth
```bash
redis-cli LLEN fsync:events:list
```
### Fetch Success Rate
```bash
redis-cli KEYS "fsync:fetches:*" | wc -l
```
### Cache Size
```bash
du -sh /tmp/fsync-cache/
```
## Roadmap
- [x] Basic file sync via RabbitMQ
- [x] Priority queue (age + size)
- [x] Checksum verification
- [ ] Multi-threaded fetching
- [ ] Rsync support (resume capability)
- [ ] Compression for transfers
- [ ] Bandwidth throttling
- [ ] Web UI for monitoring
- [ ] Metrics export (Prometheus)