-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
291 lines (249 loc) · 10.8 KB
/
Copy pathmain.cpp
File metadata and controls
291 lines (249 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
#include "clawguard.h"
namespace clawguard {
std::atomic<bool> ClawGuardDaemon::running_{false};
namespace {
std::string mode_normalize(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
if (s == "read-only" || s == "monitor" || s == "monitor-only") return "readonly";
if (s != "readonly" && s != "standard") return "readonly";
return s;
}
bool is_loopback_bind(std::string ip) {
std::transform(ip.begin(), ip.end(), ip.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
if (ip.empty() || ip == "localhost" || ip == "loopback") return true;
if (ip == "127.0.0.1") return true;
return ip.rfind("127.", 0) == 0;
}
} // namespace
ClawGuardDaemon::ClawGuardDaemon(const Config& config)
: config_(config), alert_engine_(config) {
config_.mode = mode_normalize(config_.mode);
if (config_.mode == "readonly") {
// Hard gate: no containment execution in readonly mode.
config_.containment_enabled = false;
config_.containment_auto_soft_actions = false;
config_.containment_auto_hard_actions = false;
}
// Setup data directory
if (config_.data_dir.empty()) {
const char* home = getenv("HOME");
if (home) {
config_.data_dir = std::string(home) + "/.clawguard";
} else {
config_.data_dir = "/tmp/.clawguard";
}
}
std::filesystem::create_directories(config_.data_dir);
// Setup alert file path
if (config_.alert_file.empty()) {
config_.alert_file = config_.data_dir + "/alerts.txt";
}
// OpenClaw event log path (optional)
if (config_.openclaw_event_log_file.empty()) {
config_.openclaw_event_log_file = config_.data_dir + "/openclaw-events.jsonl";
}
// Trusted ops policy path (optional)
if (config_.policy_file.empty()) {
config_.policy_file = config_.data_dir + "/policy.ini";
}
// OpenClaw config file (for version/config posture checks)
if (config_.openclaw_config_file.empty()) {
const char* home = getenv("HOME");
if (home) {
config_.openclaw_config_file = std::string(home) + "/.openclaw/openclaw.json";
} else {
config_.openclaw_config_file = "/tmp/.openclaw/openclaw.json";
}
}
// Integrity baseline file for tracked OpenClaw files/skills.
if (config_.integrity_baseline_file.empty()) {
config_.integrity_baseline_file = config_.data_dir + "/integrity-baseline.txt";
}
// Containment actions log file.
if (config_.containment_actions_log_file.empty()) {
config_.containment_actions_log_file = config_.data_dir + "/containment-actions.jsonl";
}
// Ensure AlertEngine uses the finalized runtime config (including readonly hard-gates).
alert_engine_.set_config(config_);
}
ClawGuardDaemon::~ClawGuardDaemon() { stop(); }
void ClawGuardDaemon::signal_handler(int sig) {
(void)sig;
running_ = false;
}
void ClawGuardDaemon::print_banner() {
std::cout << "\n"
" ┌─────────────────────────────────────────┐\n"
" │ 🦞 ClawGuard v" << VERSION << " │\n"
" │ System Monitor for OpenClaw │\n"
" ├─────────────────────────────────────────┤\n"
" │ Dashboard: http://localhost:" << config_.http_port << " │\n"
" │ API: http://localhost:" << config_.http_port << "/api │\n"
" │ Mode: " << config_.mode
<< " │\n"
" │ Polling: every " << config_.poll_interval_sec << "s"
" │\n"
" │ History: " << config_.history_max_minutes << " min"
" │\n"
" │ Contain: " << (config_.containment_enabled ? (config_.containment_shadow_mode ? "shadow" : "enforced") : "off")
<< " │\n"
" │ Alerts: " << config_.alert_file << "\n"
" └─────────────────────────────────────────┘\n"
<< std::endl;
auto info = collector_.get_system_info();
std::cout << " Host: " << info.hostname << "\n"
<< " OS: " << info.os << " (" << info.kernel << ")\n"
<< " Arch: " << info.arch << "\n"
<< " CPU: " << info.cpu_cores << " cores\n"
<< " RAM: " << util::format_bytes(info.total_ram) << "\n"
<< " Up: " << util::format_duration(info.uptime_seconds) << "\n"
<< std::endl;
if (is_loopback_bind(config_.http_bind) && !config_.allow_remote_http) {
std::cout << " [✓] REMOTE HTTP DISABLED (loopback-only)\n";
} else {
std::cout << " [!] REMOTE HTTP ENABLED (" << config_.http_bind
<< ") with token auth required\n";
}
}
void ClawGuardDaemon::run() {
running_ = true;
// Install signal handlers
std::signal(SIGINT, signal_handler);
std::signal(SIGTERM, signal_handler);
print_banner();
// Start HTTP server
http_server_ = std::make_unique<HttpServer>(
config_.http_port, history_, collector_, alert_engine_, config_);
http_server_->start();
std::cout << " [✓] HTTP server started on port " << config_.http_port << "\n";
// Save default config if none exists
std::string cfg_path = config_.data_dir + "/config.ini";
if (!std::filesystem::exists(cfg_path)) {
config_.save(cfg_path);
std::cout << " [✓] Default config saved to " << cfg_path << "\n";
}
std::cout << " [✓] Monitoring started. Press Ctrl+C to stop.\n\n";
// Main collection loop
collect_loop();
std::cout << "\n [!] Shutting down ClawGuard...\n";
http_server_->stop();
std::cout << " [✓] Goodbye! 🦞\n\n";
}
void ClawGuardDaemon::collect_loop() {
int cycle = 0;
while (running_) {
auto snap = collector_.collect_all();
history_.push(snap);
// Evaluate alerts
auto new_alerts = alert_engine_.evaluate(snap);
// Write alert file for OpenClaw
if (config_.openclaw_alerts) {
alert_engine_.write_alert_file(config_.alert_file);
}
// Print new alerts to console
for (const auto& alert : new_alerts) {
std::string level = alert.level == AlertLevel::CRITICAL ? "CRIT" :
alert.level == AlertLevel::WARNING ? "WARN" : "INFO";
std::cout << " [" << level << "] " << alert.message << "\n";
}
// Periodic status to console (every 12 cycles = ~1 min at 5s intervals)
if (cycle % 12 == 0) {
std::cout << " [" << util::format_timestamp(snap.timestamp_ms) << "] "
<< "CPU: " << util::to_json_number(snap.cpu.usage_pct) << "% | "
<< "MEM: " << util::to_json_number(snap.mem.usage_pct) << "% | "
<< "NET: ▼" << util::format_bytes(snap.net.bytes_recv_rate) << "/s "
<< "▲" << util::format_bytes(snap.net.bytes_sent_rate) << "/s | "
<< "Procs: " << snap.procs.total_processes << "\n";
}
cycle++;
// Sleep with early exit check
for (int i = 0; i < config_.poll_interval_sec * 10 && running_; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
}
void ClawGuardDaemon::stop() {
running_ = false;
}
} // namespace clawguard
// ─── MAIN ─────────────────────────────────────────────────────
int main(int argc, char* argv[]) {
// Parse args (CLI overrides config file).
std::string config_path;
bool no_alerts = false;
int port_override = -1;
int interval_override = -1;
std::string data_dir_override;
std::string mode_override;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
std::cout << "ClawGuard v" << clawguard::VERSION << " — System Monitor for OpenClaw\n\n"
<< "Usage: clawguard [options]\n\n"
<< "Options:\n"
<< " -p, --port PORT HTTP port (default: 7677)\n"
<< " -i, --interval SEC Poll interval (default: 5)\n"
<< " -c, --config FILE Config file path\n"
<< " -d, --data-dir DIR Data directory\n"
<< " -m, --mode MODE Operation mode: readonly|standard\n"
<< " --no-alerts Disable OpenClaw alert file\n"
<< " -h, --help Show this help\n"
<< " -v, --version Show version\n";
return 0;
}
if (arg == "-v" || arg == "--version") {
std::cout << "ClawGuard v" << clawguard::VERSION << "\n";
return 0;
}
if ((arg == "-c" || arg == "--config") && i + 1 < argc) {
config_path = argv[++i];
continue;
}
if ((arg == "-p" || arg == "--port") && i + 1 < argc) {
try {
port_override = std::stoi(argv[++i]);
} catch (...) {
std::cerr << "Invalid port value for " << arg << "\n";
return 2;
}
continue;
}
if ((arg == "-i" || arg == "--interval") && i + 1 < argc) {
try {
interval_override = std::stoi(argv[++i]);
} catch (...) {
std::cerr << "Invalid interval value for " << arg << "\n";
return 2;
}
continue;
}
if ((arg == "-d" || arg == "--data-dir") && i + 1 < argc) {
data_dir_override = argv[++i];
continue;
}
if ((arg == "-m" || arg == "--mode") && i + 1 < argc) {
mode_override = argv[++i];
continue;
}
if (arg == "--no-alerts") {
no_alerts = true;
continue;
}
}
clawguard::Config config;
if (!config_path.empty()) {
config = clawguard::Config::load(config_path);
}
if (port_override > 0) config.http_port = port_override;
if (interval_override > 0) config.poll_interval_sec = interval_override;
if (!data_dir_override.empty()) config.data_dir = data_dir_override;
if (!mode_override.empty()) config.mode = mode_override;
if (no_alerts) config.openclaw_alerts = false;
clawguard::ClawGuardDaemon daemon(config);
daemon.run();
return 0;
}