-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
318 lines (257 loc) · 10.8 KB
/
Copy pathmonitor.py
File metadata and controls
318 lines (257 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python3
"""
ClowFlow Monitoring - Watches ~/.openclaw for file changes and logs events.
Usage:
python monitor.py # Run with default config.json
python monitor.py -c myconf.json # Run with custom config
python monitor.py --snapshot # Take a one-time snapshot and exit
python monitor.py --diff # Show changes since last snapshot
"""
import argparse
import hashlib
import json
import os
import signal
import sys
import time
from datetime import datetime, timezone
from pathlib import Path
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def load_config(path: str) -> dict:
with open(path) as f:
return json.load(f)
def expand(p: str) -> Path:
return Path(os.path.expanduser(p)).resolve()
def file_hash(path: Path) -> str | None:
try:
return hashlib.sha256(path.read_bytes()).hexdigest()
except (OSError, PermissionError):
return None
def is_ignored(path: str, patterns: list[str]) -> bool:
from fnmatch import fnmatch
rel = path
for pat in patterns:
if fnmatch(rel, pat) or fnmatch(os.path.basename(rel), pat):
return True
return False
def ensure_dir(p: Path):
p.mkdir(parents=True, exist_ok=True)
# ---------------------------------------------------------------------------
# Snapshot: captures full directory state (file paths + hashes)
# ---------------------------------------------------------------------------
def take_snapshot(watch_path: Path, ignore_patterns: list[str]) -> dict:
snapshot = {}
for root, dirs, files in os.walk(watch_path):
# Skip ignored directories
dirs[:] = [d for d in dirs if not is_ignored(d, ignore_patterns) and not is_ignored(os.path.join(root, d), ignore_patterns)]
for fname in files:
full = os.path.join(root, fname)
rel = os.path.relpath(full, watch_path)
if is_ignored(rel, ignore_patterns):
continue
stat = os.stat(full)
snapshot[rel] = {
"hash": file_hash(Path(full)),
"size": stat.st_size,
"mtime": stat.st_mtime,
}
return snapshot
def save_snapshot(snapshot: dict, log_dir: Path):
ensure_dir(log_dir)
ts = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
path = log_dir / f"snapshot_{ts}.json"
with open(path, "w") as f:
json.dump({"timestamp": ts, "files": snapshot}, f, indent=2)
# Also save as "latest" for quick diff
latest = log_dir / "snapshot_latest.json"
with open(latest, "w") as f:
json.dump({"timestamp": ts, "files": snapshot}, f, indent=2)
return path
def load_latest_snapshot(log_dir: Path) -> dict | None:
latest = log_dir / "snapshot_latest.json"
if latest.exists():
with open(latest) as f:
return json.load(f)
return None
def diff_snapshots(old: dict, new: dict) -> dict:
old_files = old.get("files", {})
new_files = new.get("files", {})
added = [f for f in new_files if f not in old_files]
removed = [f for f in old_files if f not in new_files]
modified = [
f for f in new_files
if f in old_files and new_files[f]["hash"] != old_files[f]["hash"]
]
return {"added": added, "removed": removed, "modified": modified}
# ---------------------------------------------------------------------------
# Event logger
# ---------------------------------------------------------------------------
class ChangeLogger:
def __init__(self, log_dir: Path, sensitive_files: list[str]):
ensure_dir(log_dir)
self.log_dir = log_dir
self.sensitive_files = sensitive_files
self._event_log = log_dir / "events.jsonl"
def log_event(self, event_type: str, path: str, extra: dict | None = None):
basename = os.path.basename(path)
is_sensitive = basename in self.sensitive_files
entry = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"event": event_type,
"path": path,
"sensitive": is_sensitive,
}
if extra:
entry.update(extra)
line = json.dumps(entry)
with open(self._event_log, "a") as f:
f.write(line + "\n")
# Print to stdout for live monitoring
icon = "!" if is_sensitive else " "
print(f"[{entry['timestamp']}] {icon} {event_type:10s} {path}")
# ---------------------------------------------------------------------------
# Watchdog handler
# ---------------------------------------------------------------------------
class OpenClawHandler(FileSystemEventHandler):
def __init__(self, watch_path: Path, logger: ChangeLogger, ignore_patterns: list[str]):
self.watch_path = watch_path
self.logger = logger
self.ignore_patterns = ignore_patterns
def _rel(self, path: str) -> str:
return os.path.relpath(path, self.watch_path)
def _should_ignore(self, path: str) -> bool:
return is_ignored(self._rel(path), self.ignore_patterns)
def on_created(self, event):
if event.is_directory or self._should_ignore(event.src_path):
return
self.logger.log_event("CREATED", self._rel(event.src_path))
def on_modified(self, event):
if event.is_directory or self._should_ignore(event.src_path):
return
self.logger.log_event("MODIFIED", self._rel(event.src_path))
def on_deleted(self, event):
if event.is_directory or self._should_ignore(event.src_path):
return
self.logger.log_event("DELETED", self._rel(event.src_path))
def on_moved(self, event):
if event.is_directory:
return
if self._should_ignore(event.src_path) and self._should_ignore(event.dest_path):
return
self.logger.log_event(
"MOVED",
self._rel(event.src_path),
{"dest": self._rel(event.dest_path)},
)
# ---------------------------------------------------------------------------
# Periodic snapshot thread
# ---------------------------------------------------------------------------
def periodic_snapshot(watch_path: Path, log_dir: Path, ignore_patterns: list[str], interval: int):
"""Runs in a background thread, taking snapshots at regular intervals."""
import threading
def _run():
while True:
time.sleep(interval)
snap = take_snapshot(watch_path, ignore_patterns)
path = save_snapshot(snap, log_dir)
print(f"[snapshot] Saved periodic snapshot -> {path}")
t = threading.Thread(target=_run, daemon=True)
t.start()
# ---------------------------------------------------------------------------
# Log rotation
# ---------------------------------------------------------------------------
def rotate_logs(log_dir: Path, max_files: int):
snapshots = sorted(log_dir.glob("snapshot_2*.json"))
if len(snapshots) > max_files:
for old in snapshots[: len(snapshots) - max_files]:
old.unlink()
print(f"[rotate] Removed old snapshot: {old.name}")
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(description="ClowFlow Monitoring for ~/.openclaw")
parser.add_argument("-c", "--config", default=os.path.join(os.path.dirname(__file__), "config.json"), help="Path to config file")
parser.add_argument("--snapshot", action="store_true", help="Take a snapshot and exit")
parser.add_argument("--diff", action="store_true", help="Show diff since last snapshot")
args = parser.parse_args()
cfg = load_config(args.config)
watch_path = expand(cfg["watch_path"])
log_dir = expand(cfg["log_dir"])
ignore_patterns = cfg.get("ignore_patterns", [])
sensitive_files = cfg.get("alert_on_sensitive_files", [])
snapshot_interval = cfg.get("snapshot_interval_seconds", 300)
max_log_files = cfg.get("max_log_files", 30)
ensure_dir(log_dir)
if not watch_path.exists():
print(f"Error: Watch path does not exist: {watch_path}")
sys.exit(1)
# --- Snapshot mode ---
if args.snapshot:
snap = take_snapshot(watch_path, ignore_patterns)
path = save_snapshot(snap, log_dir)
print(f"Snapshot saved: {path}")
print(f"Files tracked: {len(snap)}")
return
# --- Diff mode ---
if args.diff:
old = load_latest_snapshot(log_dir)
if not old:
print("No previous snapshot found. Run --snapshot first.")
sys.exit(1)
new_snap = take_snapshot(watch_path, ignore_patterns)
changes = diff_snapshots(old, {"files": new_snap})
if not any(changes.values()):
print("No changes since last snapshot.")
else:
if changes["added"]:
print(f"\n Added ({len(changes['added'])}):")
for f in changes["added"]:
print(f" + {f}")
if changes["removed"]:
print(f"\n Removed ({len(changes['removed'])}):")
for f in changes["removed"]:
print(f" - {f}")
if changes["modified"]:
print(f"\n Modified ({len(changes['modified'])}):")
for f in changes["modified"]:
print(f" ~ {f}")
return
# --- Live monitoring mode ---
print(f"ClowFlow Monitoring")
print(f" Watching: {watch_path}")
print(f" Log dir: {log_dir}")
print(f" Snapshots: every {snapshot_interval}s")
print(f" Sensitive: {', '.join(sensitive_files)}")
print()
# Take initial snapshot
snap = take_snapshot(watch_path, ignore_patterns)
save_snapshot(snap, log_dir)
print(f"Initial snapshot: {len(snap)} files tracked")
print("Monitoring for changes... (Ctrl+C to stop)\n")
logger = ChangeLogger(log_dir, sensitive_files)
handler = OpenClawHandler(watch_path, logger, ignore_patterns)
observer = Observer()
observer.schedule(handler, str(watch_path), recursive=True)
observer.start()
# Start periodic snapshots
periodic_snapshot(watch_path, log_dir, ignore_patterns, snapshot_interval)
# Graceful shutdown
def _shutdown(sig, frame):
print("\nShutting down...")
observer.stop()
# Final snapshot
snap = take_snapshot(watch_path, ignore_patterns)
save_snapshot(snap, log_dir)
rotate_logs(log_dir, max_log_files)
print("Final snapshot saved. Goodbye.")
sys.exit(0)
signal.signal(signal.SIGINT, _shutdown)
signal.signal(signal.SIGTERM, _shutdown)
observer.join()
if __name__ == "__main__":
main()