kelora 1.5.0

A command-line log analysis tool with embedded Rhai scripting
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
use crate::event::Event;
use crate::pipeline::EventParser;
use anyhow::{Context, Result};
use regex::Regex;
use rhai::Dynamic;

pub struct SyslogParser {
    rfc5424_regex: Regex,
    rfc3164_regex: Regex,
    auto_timestamp: bool,
}

impl SyslogParser {
    fn build(auto_timestamp: bool) -> Result<Self> {
        let rfc5424_regex = Regex::new(
            r"^<(\d{1,3})>(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)(?:\s+(.*))?(?:\r?\n)?$",
        )
        .context("Failed to compile RFC5424 regex")?;

        let rfc3164_regex = Regex::new(
            r"^(?:<(\d{1,3})>)?(\w{3}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2})\s+(\S+)\s+([^:\[\s]+)(?:\[(\d+)\])?\s*:\s*(.*)(?:\r?\n)?$"
        ).context("Failed to compile RFC3164 regex")?;

        Ok(Self {
            rfc5424_regex,
            rfc3164_regex,
            auto_timestamp,
        })
    }

    pub fn new() -> Result<Self> {
        Self::build(true)
    }

    pub fn new_without_auto_timestamp() -> Result<Self> {
        Self::build(false)
    }

    /// Parse priority value into facility and severity
    fn parse_priority(priority: u32) -> (u32, u32) {
        let facility = priority >> 3;
        let severity = priority & 7;
        (facility, severity)
    }

    /// Map syslog severity (0-7) to log level string
    fn severity_to_level(severity: u32) -> &'static str {
        match severity {
            0 => "EMERG",
            1 => "ALERT",
            2 => "CRIT",
            3 => "ERROR",
            4 => "WARN",
            5 => "NOTICE",
            6 => "INFO",
            7 => "DEBUG",
            _ => "UNKNOWN",
        }
    }

    /// Try to parse as RFC5424 format first
    fn try_parse_rfc5424(&self, line: &str) -> Option<Event> {
        if let Some(captures) = self.rfc5424_regex.captures(line) {
            let priority_str = captures.get(1)?.as_str();
            let priority: u32 = priority_str.parse().ok()?;

            // Validate priority range (0-191)
            if priority > 191 {
                return None;
            }

            let (facility, severity) = Self::parse_priority(priority);

            // Pre-allocate with expected field count
            let mut event = Event::with_capacity(line.to_string(), 11);

            // Set priority fields
            event.set_field("pri".to_string(), Dynamic::from(priority as i64));
            event.set_field("facility".to_string(), Dynamic::from(facility as i64));
            event.set_field("severity".to_string(), Dynamic::from(severity as i64));
            event.set_field(
                "level".to_string(),
                Dynamic::from(Self::severity_to_level(severity)),
            );

            // Set version
            if let Some(version) = captures.get(2) {
                if let Ok(v) = version.as_str().parse::<i64>() {
                    event.set_field("version".to_string(), Dynamic::from(v));
                }
            }

            // Set timestamp
            if let Some(ts) = captures.get(3) {
                let ts_str = ts.as_str();
                if ts_str != "-" {
                    event.set_field("ts".to_string(), Dynamic::from(ts_str.to_string()));
                }
            }

            // Set hostname
            if let Some(host) = captures.get(4) {
                let host_str = host.as_str();
                if host_str != "-" {
                    event.set_field("host".to_string(), Dynamic::from(host_str.to_string()));
                }
            }

            // Set program name
            if let Some(prog) = captures.get(5) {
                let prog_str = prog.as_str();
                if prog_str != "-" {
                    event.set_field("prog".to_string(), Dynamic::from(prog_str.to_string()));
                }
            }

            // Set process ID
            if let Some(pid) = captures.get(6) {
                let pid_str = pid.as_str();
                if pid_str != "-" {
                    if let Ok(pid_num) = pid_str.parse::<i64>() {
                        event.set_field("pid".to_string(), Dynamic::from(pid_num));
                    } else {
                        event.set_field("pid".to_string(), Dynamic::from(pid_str.to_string()));
                    }
                }
            }

            // Set message ID
            if let Some(msgid) = captures.get(7) {
                let msgid_str = msgid.as_str();
                if msgid_str != "-" {
                    event.set_field("msgid".to_string(), Dynamic::from(msgid_str.to_string()));
                }
            }

            // Set structured data (skip for now, treat as part of message)
            // if let Some(sd) = captures.get(8) {
            //     let sd_str = sd.as_str();
            //     if sd_str != "-" {
            //         event.set_field("sd".to_string(), Dynamic::from(sd_str.to_string()));
            //     }
            // }

            // Set message
            if let Some(msg) = captures.get(9) {
                event.set_field("msg".to_string(), Dynamic::from(msg.as_str().to_string()));
            }

            if self.auto_timestamp {
                event.extract_timestamp();
            }
            Some(event)
        } else {
            None
        }
    }

    /// Try to parse as RFC3164 format
    fn try_parse_rfc3164(&self, line: &str) -> Option<Event> {
        if let Some(captures) = self.rfc3164_regex.captures(line) {
            // Pre-allocate with expected field count
            let mut event = Event::with_capacity(line.to_string(), 8);

            // Set priority fields if present
            if let Some(priority_match) = captures.get(1) {
                let priority: u32 = priority_match.as_str().parse().ok()?;

                // Validate priority range (0-191)
                if priority > 191 {
                    return None;
                }

                let (facility, severity) = Self::parse_priority(priority);

                event.set_field("pri".to_string(), Dynamic::from(priority as i64));
                event.set_field("facility".to_string(), Dynamic::from(facility as i64));
                event.set_field("severity".to_string(), Dynamic::from(severity as i64));
                event.set_field(
                    "level".to_string(),
                    Dynamic::from(Self::severity_to_level(severity)),
                );
            }

            // Set timestamp (group 2 now since priority is group 1)
            if let Some(ts) = captures.get(2) {
                event.set_field("ts".to_string(), Dynamic::from(ts.as_str().to_string()));
            }

            // Set hostname (group 3)
            if let Some(host) = captures.get(3) {
                event.set_field("host".to_string(), Dynamic::from(host.as_str().to_string()));
            }

            // Set program name (group 4)
            if let Some(prog) = captures.get(4) {
                event.set_field("prog".to_string(), Dynamic::from(prog.as_str().to_string()));
            }

            // Set process ID (optional, group 5)
            if let Some(pid) = captures.get(5) {
                if let Ok(pid_num) = pid.as_str().parse::<i64>() {
                    event.set_field("pid".to_string(), Dynamic::from(pid_num));
                } else {
                    event.set_field("pid".to_string(), Dynamic::from(pid.as_str().to_string()));
                }
            }

            // Set message (group 6)
            if let Some(msg) = captures.get(6) {
                event.set_field("msg".to_string(), Dynamic::from(msg.as_str().to_string()));
            }

            if self.auto_timestamp {
                event.extract_timestamp();
            }
            Some(event)
        } else {
            None
        }
    }
}

impl EventParser for SyslogParser {
    fn parse(&self, line: &str) -> Result<Event> {
        let line = line.trim_end_matches('\n').trim_end_matches('\r');
        // Try RFC5424 first, then RFC3164
        if let Some(event) = self.try_parse_rfc5424(line) {
            Ok(event)
        } else if let Some(event) = self.try_parse_rfc3164(line) {
            Ok(event)
        } else {
            Err(anyhow::anyhow!("Invalid syslog format"))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pipeline::EventParser;

    #[test]
    fn test_syslog_parser_rfc5424() {
        let parser = SyslogParser::new().unwrap();
        let line =
            "<165>1 2023-10-11T22:14:15.003Z server01 sshd 1234 ID47 - Failed password for user";
        let result = EventParser::parse(&parser, line).unwrap();

        // Check priority parsing
        assert_eq!(result.fields.get("pri").unwrap().as_int().unwrap(), 165);
        assert_eq!(result.fields.get("facility").unwrap().as_int().unwrap(), 20); // 165 >> 3 = 20
        assert_eq!(result.fields.get("severity").unwrap().as_int().unwrap(), 5); // 165 & 7 = 5

        // Check other fields
        assert_eq!(result.fields.get("version").unwrap().as_int().unwrap(), 1);
        assert_eq!(
            result
                .fields
                .get("ts")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "2023-10-11T22:14:15.003Z"
        );
        assert_eq!(
            result
                .fields
                .get("host")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "server01"
        );
        assert_eq!(
            result
                .fields
                .get("prog")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "sshd"
        );
        assert_eq!(result.fields.get("pid").unwrap().as_int().unwrap(), 1234);
        assert_eq!(
            result
                .fields
                .get("msgid")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "ID47"
        );
        assert_eq!(
            result
                .fields
                .get("msg")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "Failed password for user"
        );
    }

    #[test]
    fn test_syslog_parser_rfc3164() {
        let parser = SyslogParser::new().unwrap();
        let line =
            "Oct 11 22:14:15 server01 sshd[1234]: Failed password for user from 192.168.1.100";
        let result = EventParser::parse(&parser, line).unwrap();

        // Check fields
        assert_eq!(
            result
                .fields
                .get("ts")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "Oct 11 22:14:15"
        );
        assert_eq!(
            result
                .fields
                .get("host")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "server01"
        );
        assert_eq!(
            result
                .fields
                .get("prog")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "sshd"
        );
        assert_eq!(result.fields.get("pid").unwrap().as_int().unwrap(), 1234);
        assert_eq!(
            result
                .fields
                .get("msg")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "Failed password for user from 192.168.1.100"
        );

        assert!(result.parsed_ts.is_some());
    }

    #[test]
    fn test_syslog_parser_rfc3164_no_pid() {
        let parser = SyslogParser::new().unwrap();
        let line = "Oct 11 22:14:15 server01 kernel: CPU0: Core temperature above threshold";
        let result = EventParser::parse(&parser, line).unwrap();

        // Check fields
        assert_eq!(
            result
                .fields
                .get("ts")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "Oct 11 22:14:15"
        );
        assert_eq!(
            result
                .fields
                .get("host")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "server01"
        );
        assert_eq!(
            result
                .fields
                .get("prog")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "kernel"
        );
        assert!(result.fields.get("pid").is_none()); // No PID in this format
        assert_eq!(
            result
                .fields
                .get("msg")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "CPU0: Core temperature above threshold"
        );
    }

    #[test]
    fn test_syslog_parser_rfc3164_with_priority() {
        let parser = SyslogParser::new().unwrap();
        let line = "<34>Oct 11 22:14:15 webserver nginx: 192.168.1.10 - - [11/Oct/2023:22:14:15 +0000] \"GET /index.html HTTP/1.1\" 200 612";
        let result = EventParser::parse(&parser, line).unwrap();

        // Check priority fields
        assert_eq!(result.fields.get("pri").unwrap().as_int().unwrap(), 34);
        assert_eq!(result.fields.get("facility").unwrap().as_int().unwrap(), 4); // 34 >> 3
        assert_eq!(result.fields.get("severity").unwrap().as_int().unwrap(), 2); // 34 & 7

        // Check other fields
        assert_eq!(
            result
                .fields
                .get("ts")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "Oct 11 22:14:15"
        );
        assert_eq!(
            result
                .fields
                .get("host")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "webserver"
        );
        assert_eq!(
            result
                .fields
                .get("prog")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "nginx"
        );
        assert!(result.fields.get("pid").is_none()); // No PID in this format
        assert_eq!(
            result
                .fields
                .get("msg")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "192.168.1.10 - - [11/Oct/2023:22:14:15 +0000] \"GET /index.html HTTP/1.1\" 200 612"
        );
    }

    #[test]
    fn test_syslog_parser_priority_calculation() {
        let parser = SyslogParser::new().unwrap();

        // Test different priority values
        let test_cases = [
            (0, 0, 0),    // kern.emerg
            (33, 4, 1),   // auth.alert
            (165, 20, 5), // local4.notice
            (191, 23, 7), // local7.debug
        ];

        for (priority, expected_facility, expected_severity) in test_cases {
            let line = format!(
                "<{}>1 2023-10-11T22:14:15.003Z server01 test - - - Test message",
                priority
            );
            let result = EventParser::parse(&parser, &line).unwrap();

            assert_eq!(
                result.fields.get("pri").unwrap().as_int().unwrap(),
                priority as i64
            );
            assert_eq!(
                result.fields.get("facility").unwrap().as_int().unwrap(),
                expected_facility
            );
            assert_eq!(
                result.fields.get("severity").unwrap().as_int().unwrap(),
                expected_severity
            );
        }
    }

    #[test]
    fn test_syslog_parser_invalid_priority() {
        let parser = SyslogParser::new().unwrap();
        let line = "<999>1 2023-10-11T22:14:15.003Z server01 test - - - Test message";
        assert!(EventParser::parse(&parser, line).is_err());
    }

    #[test]
    fn test_syslog_parser_invalid_format() {
        let parser = SyslogParser::new().unwrap();
        let line = "This is not a syslog line";
        assert!(EventParser::parse(&parser, line).is_err());
    }

    #[test]
    fn test_syslog_parser_fallback_to_rfc3164() {
        let parser = SyslogParser::new().unwrap();

        // This should fail RFC5424 parsing and fall back to RFC3164
        let line = "Dec 25 14:09:07 server01 httpd: GET /index.html HTTP/1.1";
        let result = EventParser::parse(&parser, line).unwrap();

        assert_eq!(
            result
                .fields
                .get("ts")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "Dec 25 14:09:07"
        );
        assert_eq!(
            result
                .fields
                .get("host")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "server01"
        );
        assert_eq!(
            result
                .fields
                .get("prog")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "httpd"
        );
        assert_eq!(
            result
                .fields
                .get("msg")
                .unwrap()
                .clone()
                .into_string()
                .unwrap(),
            "GET /index.html HTTP/1.1"
        );
    }
}