fsmon 0.4.0

Lightweight High-Performance File System Change Tracking Tool
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! Lightweight metrics registry for fsmon daemon.
//!
//! Three-layer design:
//!   Counter layer — generic atomic counters (zero deps)
//!   Format layer  — format to Prometheus text (extensible)
//!   Transport     — socket command + optional TCP HTTP

use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::atomic::{AtomicI64, AtomicU64, Ordering};
use std::sync::{Arc, RwLock};

// ── CounterVec ────────────────────────────────────────────────────────

/// Thread-safe counter with string labels (like Prometheus CounterVec).
/// Labels are interned lazily: first `inc()` call for a label set creates a counter.
#[derive(Clone)]
pub struct CounterVec {
    inner: Arc<RwLock<CounterVecInner>>,
    name: Arc<String>,
    help: Arc<String>,
}

struct CounterVecInner {
    counters: HashMap<Vec<String>, Arc<AtomicU64>>,
}

impl CounterVec {
    pub fn new(name: &str, help: &str) -> Self {
        Self {
            inner: Arc::new(RwLock::new(CounterVecInner {
                counters: HashMap::new(),
            })),
            name: Arc::new(name.to_string()),
            help: Arc::new(help.to_string()),
        }
    }

    /// Increment the counter for the given label values.
    /// Creates an entry if this label combination is seen for the first time.
    /// Read-dominant: only takes write lock on first occurrence.
    pub fn inc(&self, labels: &[&str]) {
        let label_key: Vec<String> = labels.iter().map(|s| s.to_string()).collect();

        // Fast path: try read lock first
        if let Ok(map) = self.inner.read()
            && let Some(counter) = map.counters.get(&label_key)
        {
            counter.fetch_add(1, Ordering::Relaxed);
            return;
        }
        // Slow path: insert new counter under write lock
        if let Ok(mut map) = self.inner.write() {
            let counter = map
                .counters
                .entry(label_key)
                .or_insert_with(|| Arc::new(AtomicU64::new(0)));
            counter.fetch_add(1, Ordering::Relaxed);
        }
    }

    /// Snapshot all label sets and their current values.
    pub fn gather(&self) -> Vec<(Vec<String>, u64)> {
        let mut result = Vec::new();
        if let Ok(map) = self.inner.read() {
            for (labels, counter) in &map.counters {
                result.push((labels.clone(), counter.load(Ordering::Relaxed)));
            }
        }
        result
    }

    /// Prometheus metric name.
    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn help(&self) -> &str {
        &self.help
    }
}

// ── IntGauge ────────────────────────────────────────────────────────

/// Simple atomic gauge.
#[derive(Clone)]
pub struct IntGauge {
    value: Arc<AtomicI64>,
    name: Arc<String>,
    help: Arc<String>,
}

impl IntGauge {
    pub fn new(name: &str, help: &str) -> Self {
        Self {
            value: Arc::new(AtomicI64::new(0)),
            name: Arc::new(name.to_string()),
            help: Arc::new(help.to_string()),
        }
    }

    pub fn set(&self, val: i64) {
        self.value.store(val, Ordering::Relaxed);
    }

    pub fn inc(&self) {
        self.value.fetch_add(1, Ordering::Relaxed);
    }

    pub fn dec(&self) {
        self.value.fetch_sub(1, Ordering::Relaxed);
    }

    pub fn get(&self) -> i64 {
        self.value.load(Ordering::Relaxed)
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn help(&self) -> &str {
        &self.help
    }
}

// ── MetricsRegistry ──────────────────────────────────────────────────

/// All metrics registered by the daemon.
/// Cheap to clone (Arc-backed) — pass a clone to background tasks.
#[derive(Clone)]
pub struct MetricsRegistry {
    events_total: CounterVec,
    subscribers: IntGauge,
    monitored_paths: IntGauge,
    reader_groups: IntGauge,
    pending_paths: IntGauge,
    disk_buffer_events: IntGauge,
}

impl Default for MetricsRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl MetricsRegistry {
    pub fn new() -> Self {
        Self {
            events_total: CounterVec::new(
                "fsmon_events_total",
                "Total file system events processed by fsmon",
            ),
            subscribers: IntGauge::new(
                "fsmon_subscribers",
                "Current number of active subscribe connections",
            ),
            monitored_paths: IntGauge::new(
                "fsmon_monitored_paths",
                "Current number of monitored path entries",
            ),
            reader_groups: IntGauge::new(
                "fsmon_reader_groups",
                "Current number of fanotify fd groups",
            ),
            pending_paths: IntGauge::new(
                "fsmon_pending_paths",
                "Current number of paths pending creation",
            ),
            disk_buffer_events: IntGauge::new(
                "fsmon_disk_buffer_events",
                "Current number of buffered events (disk full)",
            ),
        }
    }

    /// Increment the events_total counter.
    pub fn inc_event(&self, event_type: &str, cmd: &str) {
        self.events_total.inc(&[event_type, cmd]);
    }

    // ── Gauge accessors ──

    pub fn set_subscribers(&self, n: i64) {
        self.subscribers.set(n);
    }
    pub fn inc_subscribers(&self) {
        self.subscribers.inc();
    }
    pub fn dec_subscribers(&self) {
        self.subscribers.dec();
    }
    pub fn subscribers(&self) -> i64 {
        self.subscribers.get()
    }

    pub fn set_monitored_paths(&self, n: i64) {
        self.monitored_paths.set(n);
    }

    pub fn set_reader_groups(&self, n: i64) {
        self.reader_groups.set(n);
    }

    pub fn set_pending_paths(&self, n: i64) {
        self.pending_paths.set(n);
    }

    pub fn set_disk_buffer_events(&self, n: i64) {
        self.disk_buffer_events.set(n);
    }

    // ── Format ──

    /// Format all metrics in Prometheus text format.
    /// https://prometheus.io/docs/instrumenting/exposition_formats/
    pub fn format_prometheus(&self) -> String {
        let mut out = String::new();

        // events_total (counter vec)
        let entries = self.events_total.gather();
        if !entries.is_empty() {
            push_help_type(&mut out, self.events_total.name(), self.events_total.help(), "counter");
            for (labels, value) in &entries {
                push_metric_line(&mut out, self.events_total.name(), &[("event_type", &labels[0]), ("cmd", &labels[1])], *value);
            }
        }

        // gauges
        push_gauge(&mut out, &self.subscribers);
        push_gauge(&mut out, &self.monitored_paths);
        push_gauge(&mut out, &self.reader_groups);
        push_gauge(&mut out, &self.pending_paths);
        push_gauge(&mut out, &self.disk_buffer_events);

        out
    }
}

fn push_help_type(out: &mut String, name: &str, help: &str, typ: &str) {
    out.push_str("# HELP ");
    out.push_str(name);
    out.push(' ');
    out.push_str(help);
    out.push('\n');
    out.push_str("# TYPE ");
    out.push_str(name);
    out.push(' ');
    out.push_str(typ);
    out.push('\n');
}

fn push_metric_line(out: &mut String, name: &str, labels: &[(&str, &str)], value: u64) {
    out.push_str(name);
    if !labels.is_empty() {
        out.push('{');
        for (i, (k, v)) in labels.iter().enumerate() {
            if i > 0 {
                out.push(',');
            }
            out.push_str(k);
            out.push_str("=\"");
            out.push_str(v);
            out.push('"');
        }
        out.push('}');
    }
    out.push(' ');
    out.push_str(&value.to_string());
    out.push('\n');
}

fn push_gauge(out: &mut String, g: &IntGauge) {
    push_help_type(out, g.name(), g.help(), "gauge");
    push_metric_line(out, g.name(), &[], g.get() as u64);
}

// ── Socket handler ───────────────────────────────────────────────────

/// Handle a `metrics` socket command: write Prometheus text and close.
pub async fn handle_metrics_socket(
    mut writer: tokio::net::unix::OwnedWriteHalf,
    metrics: &MetricsRegistry,
) {
    use tokio::io::AsyncWriteExt;
    let text = metrics.format_prometheus();
    let _ = writer.write_all(text.as_bytes()).await;
}

// ── TCP HTTP server ──────────────────────────────────────────────────

/// Start a minimal TCP HTTP server serving `/metrics`.
/// Spawns a tokio task. Returns `Ok(true)` if bound successfully,
/// `Ok(false)` if binding failed (port in use).
pub async fn serve_metrics_tcp(
    addr: SocketAddr,
    metrics: MetricsRegistry,
) -> std::io::Result<bool> {
    let listener = match tokio::net::TcpListener::bind(addr).await {
        Ok(l) => l,
        Err(e) => {
            eprintln!(
                "[WARNING] Cannot bind metrics TCP address {}: {}",
                addr, e
            );
            return Ok(false);
        }
    };

    eprintln!("[INFO] Metrics TCP endpoint: http://{}/metrics", addr);

    tokio::spawn(async move {
        loop {
            match listener.accept().await {
                Ok((mut stream, _)) => {
                    let text = metrics.format_prometheus();
                    let response = format!(
                        "HTTP/1.1 200 OK\r\n\
                         Content-Type: text/plain; version=0.0.4\r\n\
                         Content-Length: {}\r\n\
                         Connection: close\r\n\
                         \r\n\
                         {}",
                        text.len(),
                        text,
                    );
                    tokio::spawn(async move {
                        use tokio::io::AsyncWriteExt;
                        let _ = stream.write_all(response.as_bytes()).await;
                    });
                }
                Err(e) => {
                    eprintln!("[WARNING] Metrics TCP accept error: {}", e);
                }
            }
        }
    });

    Ok(true)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio::io::AsyncReadExt;
    use tokio::net::TcpStream;

    /// Start metrics TCP server on a random port, return the bound address.
    async fn start_test_metrics_server(metrics: MetricsRegistry) -> std::net::SocketAddr {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move {
            loop {
                match listener.accept().await {
                    Ok((mut stream, _)) => {
                        let text = metrics.format_prometheus();
                        let response = format!(
                            "HTTP/1.1 200 OK\r\n\
                             Content-Type: text/plain; version=0.0.4\r\n\
                             Content-Length: {}\r\n\
                             Connection: close\r\n\
                             \r\n\
                             {}",
                            text.len(),
                            text,
                        );
                        tokio::spawn(async move {
                            use tokio::io::AsyncWriteExt;
                            let _ = stream.write_all(response.as_bytes()).await;
                        });
                    }
                    Err(_) => break,
                }
            }
        });
        addr
    }

    /// Read full HTTP response from a TCP stream.
    async fn read_http_response(stream: &mut TcpStream) -> (String, String) {
        let mut buf = vec![0u8; 8192];
        let n = stream.read(&mut buf).await.unwrap();
        let raw = String::from_utf8_lossy(&buf[..n]).to_string();
        // Split headers and body
        if let Some(pos) = raw.find("\r\n\r\n") {
            let headers = raw[..pos].to_string();
            let body = raw[pos + 4..].to_string();
            (headers, body)
        } else {
            (raw, String::new())
        }
    }

    #[test]
    fn test_counter_vec_inc() {
        let cv = CounterVec::new("test_total", "Test counter");
        cv.inc(&["CREATE", "nginx"]);
        cv.inc(&["CREATE", "nginx"]);
        cv.inc(&["MODIFY", "global"]);
        cv.inc(&["CREATE", "nginx"]);

        let entries = cv.gather();
        // Sort for deterministic assertion
        let find = |et: &str, cmd: &str| -> Option<u64> {
            entries
                .iter()
                .find(|(l, _)| l[0] == et && l[1] == cmd)
                .map(|(_, v)| *v)
        };
        assert_eq!(find("CREATE", "nginx"), Some(3));
        assert_eq!(find("MODIFY", "global"), Some(1));
        assert_eq!(find("DELETE", "nginx"), None);
    }

    #[test]
    fn test_counter_vec_concurrent() {
        use std::thread;
        let cv = CounterVec::new("test_total", "Test counter");
        let cv2 = cv.clone();
        let h = thread::spawn(move || {
            for _ in 0..1000 {
                cv2.inc(&["CREATE", "nginx"]);
            }
        });
        for _ in 0..1000 {
            cv.inc(&["CREATE", "nginx"]);
        }
        h.join().unwrap();
        let entries = cv.gather();
        let val = entries
            .iter()
            .find(|(l, _)| l[0] == "CREATE" && l[1] == "nginx")
            .map(|(_, v)| *v);
        assert_eq!(val, Some(2000));
    }

    #[test]
    fn test_int_gauge() {
        let g = IntGauge::new("test_gauge", "Test gauge");
        assert_eq!(g.get(), 0);
        g.inc();
        assert_eq!(g.get(), 1);
        g.inc();
        g.inc();
        assert_eq!(g.get(), 3);
        g.dec();
        assert_eq!(g.get(), 2);
        g.set(42);
        assert_eq!(g.get(), 42);
    }

    #[test]
    fn test_format_prometheus() {
        let r = MetricsRegistry::new();
        r.inc_event("CREATE", "nginx");
        r.inc_event("CREATE", "nginx");
        r.inc_event("MODIFY", "global");
        r.set_subscribers(3);
        r.set_monitored_paths(5);

        let text = r.format_prometheus();
        assert!(text.contains("fsmon_events_total{event_type=\"CREATE\",cmd=\"nginx\"} 2"));
        assert!(text.contains("fsmon_events_total{event_type=\"MODIFY\",cmd=\"global\"} 1"));
        assert!(text.contains("fsmon_subscribers 3"));
        assert!(text.contains("fsmon_monitored_paths 5"));
        assert!(text.contains("# HELP fsmon_subscribers"));
        assert!(text.contains("# TYPE fsmon_subscribers gauge"));
        assert!(text.contains("# HELP fsmon_events_total"));
        assert!(text.contains("# TYPE fsmon_events_total counter"));
    }

    #[test]
    fn test_format_prometheus_empty_counters() {
        let r = MetricsRegistry::new();
        // No events incremented
        let text = r.format_prometheus();
        // Should still have gauge lines
        assert!(text.contains("fsmon_subscribers 0"));
        assert!(!text.contains("fsmon_events_total{"));
    }

    // ── TCP HTTP /metrics integration tests ──

    #[tokio::test]
    async fn test_tcp_metrics_http_200_content_type() {
        // Verify HTTP response format: status line, Content-Type, valid body
        let r = MetricsRegistry::new();
        r.set_subscribers(7);
        r.set_monitored_paths(3);
        r.set_reader_groups(2);
        r.set_pending_paths(1);

        let addr = start_test_metrics_server(r).await;
        let mut stream = TcpStream::connect(addr).await.unwrap();
        let (headers, body) = read_http_response(&mut stream).await;

        // HTTP status line
        assert!(headers.starts_with("HTTP/1.1 200 OK"), "Expected 200 OK, got: {}", headers.lines().next().unwrap_or(""));
        // Content-Type
        assert!(headers.contains("Content-Type: text/plain"), "Expected text/plain Content-Type");
        // Body is valid Prometheus text
        assert!(body.contains("fsmon_subscribers 7"));
        assert!(body.contains("fsmon_monitored_paths 3"));
        assert!(body.contains("fsmon_reader_groups 2"));
        assert!(body.contains("fsmon_pending_paths 1"));
        assert!(body.contains("# HELP fsmon_subscribers"));
        assert!(body.contains("# TYPE fsmon_subscribers gauge"));
    }

    #[tokio::test]
    async fn test_tcp_metrics_empty_registry() {
        // Empty registry: all gauges at zero, no counter lines
        let r = MetricsRegistry::new();
        let addr = start_test_metrics_server(r).await;
        let mut stream = TcpStream::connect(addr).await.unwrap();
        let (headers, body) = read_http_response(&mut stream).await;

        assert!(headers.starts_with("HTTP/1.1 200 OK"));
        assert!(body.contains("fsmon_subscribers 0"));
        assert!(body.contains("fsmon_monitored_paths 0"));
        assert!(!body.contains("fsmon_events_total{"));
    }

    #[tokio::test]
    async fn test_tcp_metrics_with_events() {
        // Events should appear as labeled counter lines
        let r = MetricsRegistry::new();
        r.inc_event("CREATE", "nginx");
        r.inc_event("CREATE", "nginx");
        r.inc_event("DELETE", "global");
        r.inc_event("MODIFY", "global");
        r.inc_event("MODIFY", "global");
        r.inc_event("MODIFY", "global");

        let addr = start_test_metrics_server(r).await;
        let mut stream = TcpStream::connect(addr).await.unwrap();
        let (_, body) = read_http_response(&mut stream).await;

        assert!(body.contains("fsmon_events_total{event_type=\"CREATE\",cmd=\"nginx\"} 2"));
        assert!(body.contains("fsmon_events_total{event_type=\"DELETE\",cmd=\"global\"} 1"));
        assert!(body.contains("fsmon_events_total{event_type=\"MODIFY\",cmd=\"global\"} 3"));
        // HELP/TYPE for the counter
        assert!(body.contains("# HELP fsmon_events_total"));
        assert!(body.contains("# TYPE fsmon_events_total counter"));
    }

    #[tokio::test]
    async fn test_tcp_metrics_connection_close_header() {
        // Prometheus expects Connection: close for one-shot scrape
        let r = MetricsRegistry::new();
        let addr = start_test_metrics_server(r).await;
        let mut stream = TcpStream::connect(addr).await.unwrap();
        let (headers, _) = read_http_response(&mut stream).await;

        assert!(headers.contains("Connection: close"),
            "Prometheus scrape requires Connection: close, got headers: {}", headers);
    }

    #[tokio::test]
    async fn test_tcp_metrics_content_length_matches_body() {
        // Content-Length must match actual body length for HTTP compliance
        let r = MetricsRegistry::new();
        r.set_subscribers(42);
        r.set_monitored_paths(99);

        let addr = start_test_metrics_server(r).await;
        let mut stream = TcpStream::connect(addr).await.unwrap();
        let (headers, body) = read_http_response(&mut stream).await;

        // Extract Content-Length value
        let cl = headers
            .lines()
            .find(|l| l.to_lowercase().starts_with("content-length:"))
            .and_then(|l| l.split(':').nth(1))
            .and_then(|s| s.trim().parse::<usize>().ok())
            .expect("Content-Length header missing or invalid");

        assert_eq!(cl, body.len(),
            "Content-Length ({}) must match body length ({})", cl, body.len());
    }
}