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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! check_timed_logs_fast.
//!
//! ```
//! extern crate check_timed_logs_fast;
//!
//! use check_timed_logs_fast::*;
//!
//! fn main() {
//!   let conf = Config::new(
//!     5,                               // interval in minutes to check
//!     "timeout".to_owned(),            // regex to match in the file
//!     "./fixtures/logfile".to_owned(), // path to the log file
//!     5,                               // max_critical_matches
//!     1,                               // max_warning_matches
//!     "%Y-%m-%d %H:%M:%S".to_owned(),  // datepattern
//!     0,                               // timeposition = position of datepattern in logfile
//!     false,                           // flag to enable debug output
//!     false,                           // flag to enable verbose output
//!   ).unwrap();
//!
//!   let res = check_timed_logs_fast::run(&conf);
//!   match res {
//!     Err(err) => {
//!       eprintln!("ERROR: {}", err);
//!       // ...
//!     },
//!     Ok((matches, files_matched)) => {
//!       // ...
//!     }
//!   }
//! }
//! ```

extern crate chrono;
extern crate glob;
extern crate memmap;
extern crate time;

pub use config::*;
use chrono::prelude::*;
use glob::glob;
use memmap::Mmap;
use std::fs::File;
use std::str;
use std::time::SystemTime;

mod config;
mod utils;

#[derive(Debug, Clone, PartialEq, Eq, Copy)]
enum SearchError {
  NotFile,
  EmptyFile,
  NotUtf8,
  TimestampTooOld,
}

impl From<SearchError> for String {
  fn from(error: SearchError) -> Self {
    match error {
      SearchError::NotFile => "not a file".to_owned(),
      SearchError::EmptyFile => "file empty".to_owned(),
      SearchError::NotUtf8 => "file not utf8".to_owned(),
      SearchError::TimestampTooOld => "timestamp in line too old".to_owned(),
    }
  }
}

pub fn run(conf: &Config) -> Result<(u64, u64), String> {
  let mut files_matched = 0;
  let mut exp = conf.logfile.to_owned();
  let star = String::from("*");
  exp.push_str(&star);

  let mut matches = 0;

  let pattern_spaces: Vec<&str> = conf.date_pattern.split_whitespace().collect();
  let whitespaces_in_date = pattern_spaces.len();
  
  if conf.debug {
    println!("looking for files matching {}", exp);
  }

  // the timestamp is adjusted to local time
  let now = SystemTime::now();
  let oldest_ts = utils::get_oldest_allowed_local_ts(conf, now);

  if conf.debug {
    let oldest_date_no_tz_offset = NaiveDateTime::from_timestamp(utils::get_oldest_allowed_utc_ts(conf, now) as i64, 0);
    let adjusted_date = NaiveDateTime::from_timestamp(utils::get_oldest_allowed_local_ts(conf, now) as i64, 0);
    println!("oldest allowed date in utc: {} and with tz offset: {}", oldest_date_no_tz_offset, adjusted_date);
  }
  
  // for all files that match pattern
  for entry in glob(&exp).expect("failed to read glob pattern") {
    match entry {
      Ok(path) => {
        let p = path.to_str().expect("path not available");

        if !utils::check_file_age(&conf, p) {
          files_matched += 1;
          if conf.debug {
            println!("skipping {:?} because too old", conf.logfile);
          }
          continue; 
        }

        let local_matches = search_file(p, &conf, whitespaces_in_date, oldest_ts);
        match local_matches {
          Ok(matches_in_file) => {
            files_matched += 1;
            matches += matches_in_file;
          },
          Err((err, matches_in_file)) => {
            // an error can occur because e.g. the file is empty, not utf8 or
            // because the timestamp of the line is too old. so we can
            // just stop searching further and add the matches found so far.
            if conf.debug {
              let err: String = err.into();
              println!("ERROR while searching the file {}: {}
                        There were {} matches until the error appeared.", p, err, matches);
            }

            match err {
              SearchError::TimestampTooOld => files_matched += 1,
              _ => {},
            }

            matches += matches_in_file;
            continue;
          }
        }
      },
      Err(e) => println!("ERROR: {:?}", e),
    }
  }
  Ok((matches, files_matched))
}

fn search_file(path: &str, conf: &Config, whitespaces_in_date: usize, oldest_ts: u64) -> Result<u64, (SearchError, u64)> {
  let mmap;
  let mut matches = 0;

  let file_in = File::open(path).expect("cannot open file");
  let metadata = file_in.metadata().expect("cannot get metadata");
  if !metadata.is_file() {
    return Err((SearchError::NotFile, 0));
  } else if metadata.len() > isize::max_value() as u64 {
    panic!("the file {} is too large to be safely mapped to memory:
            https://github.com/danburkert/memmap-rs/issues/69", path);
  } else if metadata.len() == 0 {
    return Err((SearchError::EmptyFile, 0));
  } 

  let (file, len) = {
    mmap = Mmap::open_path(path, memmap::Protection::Read).expect("cannot memmap");
    let bytes = unsafe { mmap.as_slice() };
    (bytes, mmap.len())
  };

  let mut last_printed = len as i64;
  let mut index = last_printed - 1;
  while index >= -1 {
    if index == -1 || file[index as usize] == '\n' as u8 {
      let line = &file[(index + 1) as usize..last_printed as usize];
      let is_match = search_line(line, whitespaces_in_date, oldest_ts, &conf);
      match is_match {
        Ok(v) => {
          if v {
            matches += 1;
          }
        },
        Err(err) => {
          return Err((err, matches));
        }
      }

      last_printed = index + 1;
    }

    index -= 1;
  }

  Ok(matches)
}

fn search_line(bytes: &[u8], whitespaces_in_datefields: usize, oldest_ts: u64, conf: &Config) -> Result<bool, SearchError> {
  if bytes.len() == 0 {
    return Ok(false);
  }

  let l = str::from_utf8(bytes);
  if l.is_err() {
      if conf.debug {
        println!("skipping file because not utf8 parseable!");
      }
      return Err(SearchError::NotUtf8);
  }
  let line = l.unwrap().trim();
  if line.len() == 0 {
    return Ok(false);
  }

  if conf.debug {
    println!("searching line: {}", line);
  }

  let words: Vec<&str> = line.split_whitespace().collect();
  let datefields = words.get(conf.timeposition..(conf.timeposition + whitespaces_in_datefields));
  let extracted_date;
  match datefields {
    None => return Ok(false),
    Some(fields) => {
      extracted_date = fields.join(" ");
    }
  };

  let date = utils::parse_date(&extracted_date, &conf.date_pattern);
  match date {
    None => Ok(false),
    Some(date) => {
      if conf.debug {
        println!("parsed {} to date {}", extracted_date, date);
      }

      let ts_line = utils::get_timestamp(date);
      if oldest_ts > ts_line {
        return Err(SearchError::TimestampTooOld);
      }

      let is_match = conf.re.captures_from_pos(&line, 0).unwrap();
      let is_match = is_match.is_some();
      if is_match && conf.verbose {
        // no println, "\n" is already contained in line
        print!("{}", line);
      }
      Ok(is_match)
    }
  }
}

#[cfg(test)]
mod tests {
  extern crate filetime;
  extern crate tempfile;

  use super::*;
  use self::tempfile::NamedTempFile;
  use self::filetime::FileTime;
  use std::io::Write;
  use std::time::UNIX_EPOCH;
  use time as t;
  use utils::*;

  const DUMMY_SEARCH_PATTERN: &str = ".*";
  const SOME_LOG_FILE: &str = "/tmp/some-file.log";
  const CHECK_LAST_MINUTE: u64 = 1;

  // Adjusts for local timezone
  fn str_to_filetime(format: &str, s: &str) -> FileTime {
    let mut tm = time::strptime(s, format).unwrap();
    tm.tm_utcoff = time::now().tm_utcoff;
    let ts = tm.to_timespec();
    FileTime::from_unix_time(ts.sec as i64, ts.nsec as u32)
  }

  fn get_dummy_conf(interval_to_check: u64, search_pattern: String, logfile: String) -> Config {
    get_dummy_conf_format(interval_to_check, search_pattern, logfile, "".to_owned(), 0)
  }

  fn get_dummy_conf_format(interval_to_check: u64, search_pattern: String, logfile: String, date_pattern: String, timeposition: usize) -> Config {
    Config::new(
      interval_to_check,
      search_pattern,
      logfile,
      1,              // max_critical_matches
      1,              // max_warning_matches
      date_pattern,
      timeposition,
      true ,          // debug is set to true to also test these branches
      true,           // verbose is set to true to also test these branches
    ).unwrap()
  }

  fn create_temp_file(content: &str) -> (NamedTempFile, String) {
    let mut file = NamedTempFile::new().expect("not able to create tempfile");
    if content.len() > 0 {
      writeln!(file, "{}", content).expect("tempfile cannot be written");
    }
    let path = file.path().to_str().expect("oh no").to_string();
    (file, path)
  }

  /// returns approximately the minutes since unix epoch.
  /// we return a large value here, because this value will be used as
  /// the interval to check -- i.e. a large value means log entries
  /// which are valid go back further into the past.
  fn forever() -> u64 {
    // we subtract the tz offset for los angeles (-8h) because some
    // of the tests use that tz and it is sufficient to return a
    // very old timestamp from this function.
    (get_now_secs() / 60) - (8 * 60)
  }

  fn get_now_secs() -> u64 {
    let now = std::time::SystemTime::now();
    let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
    since_the_epoch.as_secs()
  }

  fn reset_tz() {
    std::env::set_var("TZ", "Africa/Abidjan"); // Africa/Abidjan = +00:00 UTC Offset
    t::tzset();
  }

  #[test]
  fn should_correctly_calculate_oldest_allowed_ts_utc() {
    // given
    let now = std::time::SystemTime::now(); // TODO use a fixed time. this test
                                            // could be flaky in corner cases.
    let interval_to_check: u64 = 1;
    let conf = get_dummy_conf(interval_to_check,
                              DUMMY_SEARCH_PATTERN.to_owned(),
                              SOME_LOG_FILE.to_owned());

    // when
    let oldest_allowed_ts = get_oldest_allowed_utc_ts(&conf, now);

    // then
    let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
    assert_eq!(oldest_allowed_ts, since_the_epoch.as_secs() - (interval_to_check * 60));
  }

  #[test]
  fn should_correctly_calculate_oldest_allowed_ts_adjusted_to_local_tz() {
    // given
    std::env::set_var("TZ", "America/Los_Angeles");
    t::tzset();

    let now = std::time::SystemTime::now();
    let interval_to_check: u64 = 13; // minutes
    let conf = get_dummy_conf(interval_to_check,
                              DUMMY_SEARCH_PATTERN.to_owned(),
                              SOME_LOG_FILE.to_owned());

    // when
    let oldest_ts = get_oldest_allowed_local_ts(&conf, now);

    // then
    // the oldest allowed timestamp in this case should not be
    // `current utc - interval_to_check`, but rather the current
    // time adjusted to `local tz - interval_to_check`.
    let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
    let offset = 8 * 60 * 60; // 8 hours is the timezone offset from utc to los angeles
    assert_eq!(oldest_ts, since_the_epoch.as_secs() - (interval_to_check * 60) - offset);
  }

  #[test]
  fn should_search_in_file() {
    // given
    reset_tz();
    let now_unix_ts = get_now_secs();
    let format = "%b %d %H:%M:%S";

    let dt = NaiveDateTime::from_timestamp(now_unix_ts as i64, 0);
    let now_formatted = dt.format(format).to_string();

    let five_minutes = now_unix_ts - (5 * 60);
    let dt_five_minutes_ago = NaiveDateTime::from_timestamp(five_minutes as i64, 0);
    let five_minutes_ago = dt_five_minutes_ago.format(format).to_string();

    let content = format!("{} foo_bar\n{} bar\n{} foo-bar\n{} foo_bar",
                           five_minutes_ago, now_formatted, now_formatted, now_formatted);
    let (_file, path) = create_temp_file(&content);

    let interval_to_check: u64 = 2;
    let conf = get_dummy_conf_format(interval_to_check, "foo[-_]+bar".to_owned(), path, format.to_owned(), 0);

    // when
    let res = run(&conf);

    // then
    let matches = 2;
    let files_matched = 1;
    assert_eq!(res, Ok((matches, files_matched)));

  }
  #[test]
  fn should_handle_timeposition() {
    // given
    reset_tz();
    let now_unix_ts = get_now_secs();
    let format = "%b %d %H:%M:%S";

    let dt = NaiveDateTime::from_timestamp(now_unix_ts as i64, 0);
    let now_formatted = dt.format(format).to_string();

    let five_minutes = now_unix_ts - (5 * 60);
    let dt_five_minutes_ago = NaiveDateTime::from_timestamp(five_minutes as i64, 0);
    let five_minutes_ago = dt_five_minutes_ago.format(format).to_string();

    let content = format!("foo_bar {}\nbar {}\nfoo-bar {}\nfoo_bar {}",
                          five_minutes_ago, now_formatted, now_formatted, now_formatted);
    let (_file, path) = create_temp_file(&content);

    let interval_to_check: u64 = 2;
    let conf = get_dummy_conf_format(interval_to_check, "foo[-_]+bar".to_owned(), path, format.to_owned(), 1);

    // when
    let res = run(&conf);

    // then
    let matches = 2;
    let files_matched = 1;
    assert_eq!(res, Ok((matches, files_matched)));
  }

  #[test]
  fn should_handle_empty_file_correctly() {
    // given
    let (_file, path) = create_temp_file("");
    let conf = get_dummy_conf(CHECK_LAST_MINUTE, DUMMY_SEARCH_PATTERN.to_owned(), path);

    // when
    let res = run(&conf);

    // then
    let matches = 0;
    let files_matched = 0;
    assert_eq!(res, Ok((matches, files_matched)));
  }

  #[test]
  fn should_skip_binary_files() {
    // given
    let path = "./fixtures/1x1.png";
    let conf = get_dummy_conf(forever(), DUMMY_SEARCH_PATTERN.to_owned(), path.to_owned());
    let whitespaces_in_date = 3; // doesn't matter, should not be considered anyway
    let oldest_ts = forever();

    // when
    let res = search_file(path, &conf, whitespaces_in_date, oldest_ts);

    // then
    let files_matched = 0;
    assert_eq!(res, Err((SearchError::NotUtf8, files_matched)));
  }

  #[test]
  fn should_handle_utf8_file_content_correctly() {
    // given
    let (_file, path) = create_temp_file("2018-09-13 00:03:01 🐱");
    let conf = get_dummy_conf(forever(), "🐱".to_owned(), path);

    // when
    let res = run(&conf);

    // then
    let matches = 1;
    let files_matched = 1;
    assert_eq!(res, Ok((matches, files_matched)));
  }

  #[test]
  fn should_handle_files_with_lines_without_dates() {
    // given
    // one of the lines intentionally contains as many whitespaces as the
    // valid lines which include a date.
    let (_file, path) = create_temp_file("2018-09-13 00:03:01 foo\nsome\nsome some\nsome foo bar\n2018-09-13 00:03:01 bar\n");
    let conf = get_dummy_conf(forever(), "bar".to_owned(), path);

    // when
    let res = run(&conf);

    // then
    let matches = 1;
    let files_matched = 1;
    assert_eq!(res, Ok((matches, files_matched)));
  }

  #[test]
  fn should_ignore_trailing_comma() {
    // given
    let format = "%Y-%m-%d %H:%M:%S";
    let (_file, path) = create_temp_file("2018-09-13 00:01:51,079 foo\n2018-09-13 00:01:51,079 foobar\n");
    let conf = get_dummy_conf_format(forever(), "foo".to_owned(), path, format.to_owned(), 0);

    // when
    let res = run(&conf);

    // then
    let matches = 2;
    let files_matched = 1;
    assert_eq!(res, Ok((matches, files_matched)));
  }

  #[test]
  fn should_handle_non_default_date_format() {
    // given
    reset_tz();
    let now_unix_ts = get_now_secs();
    let format = "%b %d %H:%M:%S";

    let dt = NaiveDateTime::from_timestamp(now_unix_ts as i64, 0);
    let now_formatted = dt.format(format).to_string();

    let five_minutes = now_unix_ts - (5 * 60);
    let dt_five_minutes_ago = NaiveDateTime::from_timestamp(five_minutes as i64, 0);
    let five_minutes_ago = dt_five_minutes_ago.format(format).to_string();

    let content = format!("{} foo\n{} bar\n{} foobar",
                           five_minutes_ago, now_formatted, now_formatted);
    let (_file, path) = create_temp_file(&content);

    let interval_to_check: u64 = 2;
    let conf = get_dummy_conf_format(interval_to_check, "foo".to_owned(), path, format.to_owned(), 0);

    // when
    let res = run(&conf);

    // then
    // the entry which was five minutes ago should not be matched
    let matches = 1;
    let files_matched = 1;
    assert_eq!(res, Ok((matches, files_matched)));
  }

  #[test]
  fn should_handle_non_default_date_format_and_trailing_comma_and_different_timeposition() {
    // given
    reset_tz();
    let now_unix_ts = get_now_secs();
    let format = "%b %d %H:%M:%S";

    let dt = NaiveDateTime::from_timestamp(now_unix_ts as i64, 0);
    let now_formatted = dt.format(format).to_string();

    let five_minutes = now_unix_ts - (5 * 60);
    let dt_five_minutes_ago = NaiveDateTime::from_timestamp(five_minutes as i64, 0);
    let five_minutes_ago = dt_five_minutes_ago.format(format).to_string();

    let content = format!("foo {},123\nbar{},345\nfoo {},567",
                           five_minutes_ago, now_formatted, now_formatted);
    let (_file, path) = create_temp_file(&content);

    let interval_to_check: u64 = 2;
    let conf = get_dummy_conf_format(interval_to_check, "foo".to_owned(), path, format.to_owned(), 1);

    // when
    let res = run(&conf);

    // then
    // the entry which was five minutes ago should not be matched
    let matches = 1;
    let files_matched = 1;
    assert_eq!(res, Ok((matches, files_matched)));
  }

  #[test]
  fn should_skip_old_files() {
    // given
    let (file, path) = create_temp_file("2018-09-13 00:03:01 foo");
    let five_minutes = 5;
    let conf = get_dummy_conf(five_minutes, "foo".to_owned(), path);

    let start_of_year = str_to_filetime("%Y%m%d%H%M", "201501010000");
    let path = file.path();
    filetime::set_file_times(path, start_of_year, start_of_year).unwrap();

    // when
    let res = run(&conf);

    // then
    let matches = 0;
    let files_matched = 1;
    assert_eq!(res, Ok((matches, files_matched)));
  }

  #[test]
  fn should_handle_file_age_correctly() {
    // given
    let (file, path) = create_temp_file("2018-09-13 00:03:01 foo");
    let conf = get_dummy_conf(forever(), "foo".to_owned(), path);
    let start_of_year = str_to_filetime("%Y%m%d%H%M", "201809130000");

    let path = file.path();
    filetime::set_file_times(path, start_of_year, start_of_year).unwrap();

    // when
    let res = run(&conf);

    // then
    let matches = 1;
    let files_matched = 1;
    assert_eq!(res, Ok((matches, files_matched)));
  }

  #[test]
  fn should_search_matching_files() {
    // given
    // logfile.0 should also be searched
    let conf = get_dummy_conf(forever(), "foobar".to_owned(), "./fixtures/logfile".to_owned());

    // when
    let res = run(&conf);

    // then
    let matches = 2;
    let files_matched = 2;
    assert_eq!(res, Ok((matches, files_matched)));
  }

  #[test]
  fn should_abort_when_stdin_used_as_logfile() {
    // given
    let stdin = "-".to_owned();

    // when
    let conf = Config::new(
      forever(),
      "foobar".to_owned(),
      stdin,
      1,              // max_critical_matches
      1,              // max_warning_matches
      "".to_owned(),  // datepattern
      0,              // timeposition
      true,           // debug is set to true to also test these branches
      true,           // verbose is set to true to also test these branches
    );

    // then
    assert_eq!(conf.is_err(), true);
  }

}