cmtrace-open 1.5.0

Free, open-source CMTrace replacement: Windows log viewer with ConfigMgr/SCCM, Intune, and Autopilot ESP diagnostics, DSRegCmd triage, and real-time tailing.
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
use std::path::{Path, PathBuf};

/// Result of scanning a folder for timeline-eligible sources. Used by the
/// frontend ingestion layer (and future command wrappers) to decide which
/// paths to pass into `build_timeline` as log files vs. IME folders.
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClassifiedSource {
    LogFile(PathBuf),
    ImeLogsFolder(PathBuf),
}

/// Walk the given root (one level deep) and classify what we find.
/// Produces log files for every recognized log path plus at most one
/// IME-events source per IME folder detected.
#[allow(dead_code)]
pub fn classify_folder(root: &Path) -> Vec<ClassifiedSource> {
    let mut out: Vec<ClassifiedSource> = Vec::new();
    if !root.is_dir() {
        return out;
    }

    let ime_hint_files = ["AgentExecutor.log", "IntuneManagementExtension.log"];
    let mut contains_ime_logs = false;

    if let Ok(rd) = std::fs::read_dir(root) {
        for entry in rd.flatten() {
            let path = entry.path();
            if path.is_file() {
                let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
                if ime_hint_files.contains(&name) {
                    contains_ime_logs = true;
                }
                if is_log_file(name) {
                    out.push(ClassifiedSource::LogFile(path));
                }
            }
        }
    }
    if contains_ime_logs {
        out.push(ClassifiedSource::ImeLogsFolder(root.to_path_buf()));
    }
    out
}

fn is_log_file(name: &str) -> bool {
    let lower = name.to_ascii_lowercase();
    lower.ends_with(".log")
        || lower.ends_with(".cmtlog")
        || lower.ends_with(".txt")
        || lower == "setupact.log"
        || lower == "setupapi.app.log"
        || lower == "setupapi.dev.log"
}

#[cfg(test)]
mod tests_classify {
    use super::*;

    #[test]
    fn classifies_plain_log_directory() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("foo.log"), b"hello\n").unwrap();
        std::fs::write(dir.path().join("bar.txt"), b"bye\n").unwrap();
        std::fs::write(dir.path().join("ignore.bin"), b"x").unwrap();
        let mut out = classify_folder(dir.path());
        out.sort_by_key(|c| format!("{:?}", c));
        assert_eq!(out.len(), 2);
        assert!(matches!(out[0], ClassifiedSource::LogFile(_)));
        assert!(matches!(out[1], ClassifiedSource::LogFile(_)));
    }

    #[test]
    fn detects_ime_folder_when_agentexecutor_present() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("AgentExecutor.log"), b"hello\n").unwrap();
        std::fs::write(dir.path().join("IntuneManagementExtension.log"), b"hi\n").unwrap();
        let out = classify_folder(dir.path());
        assert!(out
            .iter()
            .any(|c| matches!(c, ClassifiedSource::ImeLogsFolder(_))));
        assert_eq!(
            out.iter()
                .filter(|c| matches!(c, ClassifiedSource::LogFile(_)))
                .count(),
            2
        );
    }
}

use std::collections::{HashMap, HashSet};

use crate::timeline::models::*;
use crate::timeline::query::SourceRuntime;
use crate::timeline::store::Timeline;

pub const DEFAULT_ENTRY_LIMIT: u64 = 5_000_000;

#[derive(Clone, Debug, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceRequest {
    pub path: String,
    pub display_name: Option<String>,
}

/// Build a timeline from a set of source requests. Each request is either
/// a log file path or a folder (containing IME logs).
pub fn build_timeline(
    requests: &[SourceRequest],
    limit: u64,
    denied_seed: Vec<String>,
) -> Result<(Timeline, HashMap<u16, SourceRuntime>), TimelineError> {
    if requests.is_empty() {
        return Err(TimelineError::NoSources);
    }

    const PALETTE: [&str; 8] = [
        "#2563eb", "#a855f7", "#16a34a", "#dc2626", "#f59e0b", "#0891b2", "#ea580c", "#65a30d",
    ];
    let mut sources: Vec<TimelineSourceMeta> = Vec::new();
    let mut indexes: HashMap<u16, Vec<EntryIndex>> = HashMap::new();
    let mut ime_events: HashMap<u16, Vec<crate::intune::models::IntuneEvent>> = HashMap::new();
    let mut runtimes: HashMap<u16, SourceRuntime> = HashMap::new();
    let mut errors: Vec<SourceError> = Vec::new();
    let mut total_entries: u64 = 0;
    let mut time_min = i64::MAX;
    let mut time_max = i64::MIN;

    for (i, req) in requests.iter().enumerate() {
        let idx = i as u16;
        let color = PALETTE[i % PALETTE.len()].to_string();
        let path = std::path::PathBuf::from(&req.path);
        let display_name = req.display_name.clone().unwrap_or_else(|| {
            path.file_name()
                .and_then(|s| s.to_str())
                .unwrap_or("source")
                .to_string()
        });

        if path.is_dir() {
            match extract_ime_events(&path) {
                Ok(events) => {
                    for ev in &events {
                        if let Some(ts) = ev.start_time_epoch_ms() {
                            time_min = time_min.min(ts);
                            time_max = time_max.max(ts);
                        }
                    }
                    total_entries += events.len() as u64;
                    if total_entries > limit {
                        return Err(TimelineError::TooLarge {
                            estimated: total_entries,
                            limit,
                        });
                    }
                    sources.push(TimelineSourceMeta {
                        idx,
                        color,
                        display_name,
                        kind: TimelineSourceKind::IntuneEvents,
                        path: req.path.clone(),
                        entry_count: events.len() as u32,
                    });
                    ime_events.insert(idx, events);
                }
                Err(e) => errors.push(SourceError {
                    path: req.path.clone(),
                    message: e.to_string(),
                }),
            }
            continue;
        }

        match parse_to_index(&path) {
            Ok((parser_kind, idx_vec, parser, entry_count)) => {
                for ei in &idx_vec {
                    time_min = time_min.min(ei.timestamp_ms);
                    time_max = time_max.max(ei.timestamp_ms);
                }
                total_entries += entry_count as u64;
                if total_entries > limit {
                    return Err(TimelineError::TooLarge {
                        estimated: total_entries,
                        limit,
                    });
                }
                sources.push(TimelineSourceMeta {
                    idx,
                    color,
                    display_name,
                    kind: TimelineSourceKind::LogFile { parser_kind },
                    path: req.path.clone(),
                    entry_count,
                });
                indexes.insert(idx, idx_vec);
                runtimes.insert(idx, SourceRuntime { path, parser });
            }
            Err(e) => errors.push(SourceError {
                path: req.path.clone(),
                message: e.to_string(),
            }),
        }
    }

    if time_min == i64::MAX {
        time_min = 0;
        time_max = 0;
    }

    let mut denied: HashSet<String> = denied_seed
        .into_iter()
        .filter_map(|g| crate::timeline::correlation::normalize_guid(&g))
        .collect();
    let samples = sample_source_messages(&runtimes, &indexes, 200);
    let samples_as_refs: HashMap<u16, Vec<&str>> = samples
        .iter()
        .map(|(k, v)| (*k, v.iter().map(|s| s.as_str()).collect()))
        .collect();
    let hf = crate::timeline::correlation::high_frequency_guids(&samples_as_refs, 0.35, 2);
    denied.extend(hf);

    let tunables = TimelineTunables::default();
    let materialize = |src: u16, eref: u32| -> Option<String> {
        let ei = indexes.get(&src).and_then(|v| v.get(eref as usize))?;
        let rt = runtimes.get(&src)?;
        crate::timeline::query::materialize_msg(&rt.path, &rt.parser, ei)
    };
    let (raw_signals, incidents) = crate::timeline::incidents::detect_incidents(
        &indexes,
        &ime_events,
        &tunables,
        &denied,
        &materialize,
    );

    let bundle = TimelineBundle {
        id: uuid::Uuid::new_v4().to_string(),
        sources,
        time_range_ms: (time_min, time_max),
        total_entries,
        incidents,
        denied_guids: denied.into_iter().collect(),
        errors,
        tunables,
    };
    Ok((
        Timeline {
            bundle,
            indexes,
            ime_events,
            raw_signals,
        },
        runtimes,
    ))
}

/// Parse one log file. Returns parser kind, index vec, resolved parser,
/// and entry count.
fn parse_to_index(
    path: &std::path::Path,
) -> Result<
    (
        crate::models::log_entry::ParserKind,
        Vec<EntryIndex>,
        crate::parser::ResolvedParser,
        u32,
    ),
    anyhow::Error,
> {
    let path_str = path.to_string_lossy().to_string();
    let (parse_result, parser) =
        crate::parser::parse_file(&path_str).map_err(|e| anyhow::anyhow!("parse_file: {}", e))?;

    // Re-read the raw bytes so we can compute per-line byte offsets.
    // The byte offset for line N (1-based) is offsets[N - 1]. offsets[0] is 0.
    let bytes = std::fs::read(path).map_err(|e| anyhow::anyhow!("read: {}", e))?;
    let mut offsets: Vec<u64> = Vec::with_capacity(bytes.len() / 40 + 1);
    offsets.push(0);
    for (i, b) in bytes.iter().enumerate() {
        if *b == b'\n' {
            offsets.push((i + 1) as u64);
        }
    }

    let parser_kind = parser.parser;
    let mut idx_vec: Vec<EntryIndex> = Vec::with_capacity(parse_result.entries.len());
    for entry in &parse_result.entries {
        let timestamp_ms = entry.timestamp.unwrap_or(0);
        let byte_offset = if entry.line_number == 0 {
            0
        } else {
            offsets
                .get((entry.line_number - 1) as usize)
                .copied()
                .unwrap_or(0)
        };
        let mut flags: u8 = 0;
        if !entry.error_code_spans.is_empty() {
            flags |= SIGNAL_FLAG_HAS_ERROR_CODE;
        }
        idx_vec.push(EntryIndex {
            timestamp_ms,
            severity: entry.severity,
            source_idx: 0, // filled in by caller (we set via per-source insertion anyway, but keep zero placeholder)
            byte_offset,
            line_number: entry.line_number,
            signal_flags: flags,
        });
    }
    let entry_count = idx_vec.len() as u32;
    Ok((parser_kind, idx_vec, parser, entry_count))
}

/// Walk an IME-logs folder and extract Intune events by running the same
/// pipeline the `analyze_intune_logs` command uses: ime_parser → GuidRegistry
/// ingest → event_tracker::extract_events → timeline::build_timeline.
#[cfg(feature = "intune-diagnostics")]
fn extract_ime_events(
    folder: &std::path::Path,
) -> Result<Vec<crate::intune::models::IntuneEvent>, anyhow::Error> {
    let rd = std::fs::read_dir(folder)
        .map_err(|e| anyhow::anyhow!("read_dir {}: {}", folder.display(), e))?;

    // Collect IME-relevant log files in the folder. Match on the pattern table
    // used by the intune command (case-insensitive substring on file name).
    let mut files: Vec<std::path::PathBuf> = Vec::new();
    for entry in rd.flatten() {
        let p = entry.path();
        if !p.is_file() {
            continue;
        }
        let name_lower = p
            .file_name()
            .and_then(|n| n.to_str())
            .map(|s| s.to_ascii_lowercase())
            .unwrap_or_default();
        if !name_lower.ends_with(".log") {
            continue;
        }
        if IME_LOG_HINTS.iter().any(|h| name_lower.contains(h)) {
            files.push(p);
        }
    }

    // Parse each file, build a global GUID registry, then extract events.
    let mut per_file: Vec<(String, Vec<crate::intune::ime_parser::ImeLine>)> = Vec::new();
    let mut registry = crate::intune::guid_registry::GuidRegistry::new();
    for p in &files {
        let content = std::fs::read_to_string(p)
            .map_err(|e| anyhow::anyhow!("read_to_string {}: {}", p.display(), e))?;
        let lines = crate::intune::ime_parser::parse_ime_content(&content);
        registry.ingest_lines(&lines);
        per_file.push((p.to_string_lossy().to_string(), lines));
    }

    let mut all_events: Vec<crate::intune::models::IntuneEvent> = Vec::new();
    for (source_file, lines) in &per_file {
        let events = crate::intune::event_tracker::extract_events(lines, source_file, &registry);
        all_events.extend(events);
    }

    // Run the same timeline build (dedupe + sort + epoch_ms population) used
    // by the intune command path.
    Ok(crate::intune::timeline::build_timeline(all_events))
}

/// When built without the `intune-diagnostics` feature, IME event extraction
/// is unavailable. The timeline builder surfaces the per-source failure as a
/// `SourceError`, matching the pattern used for individual file read errors.
#[cfg(not(feature = "intune-diagnostics"))]
fn extract_ime_events(
    _folder: &std::path::Path,
) -> Result<Vec<crate::intune::models::IntuneEvent>, anyhow::Error> {
    Err(anyhow::anyhow!(
        "IME event extraction requires the `intune-diagnostics` feature"
    ))
}

#[cfg(feature = "intune-diagnostics")]
const IME_LOG_HINTS: &[&str] = &[
    "intunemanagementextension",
    "appworkload",
    "appactionprocessor",
    "agentexecutor",
    "healthscripts",
    "clienthealth",
    "clientcertcheck",
    "devicehealthmonitoring",
    "sensor",
    "win32appinventory",
    "imeui",
];

fn sample_source_messages(
    runtimes: &HashMap<u16, SourceRuntime>,
    indexes: &HashMap<u16, Vec<EntryIndex>>,
    per_source_cap: usize,
) -> HashMap<u16, Vec<String>> {
    let mut out: HashMap<u16, Vec<String>> = HashMap::new();
    for (src, idx_vec) in indexes {
        let rt = match runtimes.get(src) {
            Some(r) => r,
            None => continue,
        };
        let step = (idx_vec.len() / per_source_cap).max(1);
        let mut msgs: Vec<String> = Vec::with_capacity(per_source_cap);
        for ei in idx_vec.iter().step_by(step).take(per_source_cap) {
            if let Some(m) = crate::timeline::query::materialize_msg(&rt.path, &rt.parser, ei) {
                msgs.push(m);
            }
        }
        out.insert(*src, msgs);
    }
    out
}