forked from heartbeatsjp/check_log_ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_check_log_ng.py
executable file
·1311 lines (1092 loc) · 47.3 KB
/
test_check_log_ng.py
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Unit test for check_log_ng"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import unittest
import warnings
import os
import glob
import io
import time
import datetime
import subprocess
from check_log_ng import LogChecker
class LogCheckerTestCase(unittest.TestCase):
"""Unit test."""
# Class constant
MESSAGE_OK = "OK - No matches found."
MESSAGE_WARNING_ONE = "WARNING: Found 1 lines (limit=1/0): {0} at {1}"
MESSAGE_WARNING_ONE_WITH_QUIET = "WARNING: Found 1 lines (limit=1/0, QUIET): at {0}"
MESSAGE_WARNING_ONE_WITH_HEADER = "WARNING: Found 1 lines (limit=1/0, HEADER): {0} at {1}"
MESSAGE_WARNING_TWO = "WARNING: Found 2 lines (limit=1/0): {0},{1} at {2}"
MESSAGE_WARNING_TWO_WITH_QUIET = "WARNING: Found 2 lines (limit=1/0, QUIET): at {0}"
MESSAGE_WARNING_TWO_WITH_HEADER = "WARNING: Found 2 lines (limit=1/0, HEADER): {0},{1} at {2}"
MESSAGE_WARNING_TWO_IN_TWO_FILES = (
"WARNING: Found 2 lines (limit=1/0): {0} at {1},{2} at {3}")
MESSAGE_CRITICAL_ONE = "CRITICAL: Critical Found 1 lines: {0} at {1}"
MESSAGE_CRITICAL_ONE_WITH_QUIET = "CRITICAL: Critical Found 1 lines (QUIET): at {0}"
MESSAGE_CRITICAL_ONE_WITH_HEADER = "CRITICAL: Critical Found 1 lines (HEADER): {0} at {1}"
MESSAGE_UNKNOWN_LOCK_TIMEOUT = (
"UNKNOWN: Lock timeout. Another process is running.")
# Class variablesex
BASEDIR = None
TESTDIR = None
LOGDIR = None
STATEDIR = None
@classmethod
def setUpClass(cls):
cls.BASEDIR = os.getcwd()
cls.TESTDIR = os.path.join(cls.BASEDIR, 'test')
cls.LOGDIR = os.path.join(cls.TESTDIR, 'log')
cls.STATEDIR = os.path.join(cls.TESTDIR, 'state')
if not os.path.isdir(cls.TESTDIR):
os.mkdir(cls.TESTDIR)
if not os.path.isdir(cls.LOGDIR):
os.mkdir(cls.LOGDIR)
if not os.path.isdir(cls.STATEDIR):
os.mkdir(cls.STATEDIR)
@classmethod
def tearDownClass(cls):
if os.path.exists(cls.LOGDIR):
os.removedirs(cls.LOGDIR)
if os.path.exists(cls.STATEDIR):
os.removedirs(cls.STATEDIR)
if os.path.exists(cls.TESTDIR):
os.removedirs(cls.TESTDIR)
def setUp(self):
# log files
self.logfile = os.path.join(self.LOGDIR, 'testlog')
self.logfile1 = os.path.join(self.LOGDIR, 'testlog.1')
self.logfile2 = os.path.join(self.LOGDIR, 'testlog.2')
self.logfile_pattern = os.path.join(self.LOGDIR, 'testlog*')
# seek files
self.tag1 = 'tag1'
self.tag2 = 'tag2'
self.seekfile = os.path.join(self.STATEDIR, 'testlog.seek')
# lock file
self.lockfile = os.path.join(self.STATEDIR, 'check_log_ng.lock')
# configuration
# Set cachetime to 0 for convenience in testing.
self.config = {
"logformat": LogChecker.FORMAT_SYSLOG,
"state_directory": self.STATEDIR,
"pattern_list": [],
"critical_pattern_list": [],
"negpattern_list": [],
"critical_negpattern_list": [],
"case_insensitive": False,
"encoding": "utf-8",
"warning": 1,
"critical": 0,
"trace_inode": False,
"multiline": False,
"scantime": 86400,
"expiration": 691200,
"cachetime": 0,
"lock_timeout": 3
}
def tearDown(self):
# remove log files.
for logfile in [self.logfile, self.logfile1, self.logfile2]:
if os.path.exists(logfile):
os.unlink(logfile)
# remove seek files.
seekfiles = glob.glob(
os.path.join(self.STATEDIR, '*' + LogChecker._SUFFIX_SEEK))
for seekfile in seekfiles:
if os.path.exists(seekfile):
os.unlink(seekfile)
# remove a cache file.
cachefiles = glob.glob(
os.path.join(self.STATEDIR, '*' + LogChecker._SUFFIX_CACHE))
for cachefile in cachefiles:
if os.path.exists(cachefile):
os.unlink(cachefile)
# remove a lock file.
lockfiles = glob.glob(
os.path.join(self.STATEDIR, '*' + LogChecker._SUFFIX_LOCK))
for lockfile in lockfiles:
if os.path.exists(lockfile):
os.unlink(lockfile)
def test_dry_run(self):
"""--dry-run option
"""
self.config["pattern_list"] = ["ERROR"]
log = LogChecker(self.config)
# create a seek file
# Dec 5 12:34:50 hostname noop: NOOP
line4 = self._make_line(self._get_timestamp(), "noop", "NOOP")
self._write_logfile(self.logfile, line4)
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_OK)
self.assertEqual(log.get_message(), self.MESSAGE_OK)
# verify a seek file is not updated.
self.config["dry_run"] = True
log = LogChecker(self.config)
# Dec 5 12:34:50 hostname test: ERROR
line = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line)
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line, self.logfile))
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line, self.logfile))
def test_format(self):
"""--format option
"""
self.config["logformat"] = r"^(\[%a %b %d %T %Y\] \[\S+\] )(.*)$"
self.config["pattern_list"] = ["ERROR"]
log = LogChecker(self.config)
# [Thu Dec 05 12:34:50 2013] [error] ERROR
line = self._make_customized_line(
self._get_customized_timestamp(), "error", "ERROR")
self._write_customized_logfile(self.logfile, line)
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line, self.logfile))
def test_pattern(self):
"""--pattern option
"""
self.config["pattern_list"] = ["ERROR"]
log = LogChecker(self.config)
# 1 line matched
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line1)
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line1, self.logfile))
# 2 lines matched
# Dec 5 12:34:50 hostname test: ERROR1
# Dec 5 12:34:50 hostname test: ERROR2
line2 = self._make_line(self._get_timestamp(), "test", "ERROR1")
line3 = self._make_line(self._get_timestamp(), "test", "ERROR2")
self._write_logfile(self.logfile, [line2, line3])
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_TWO.format(line2, line3, self.logfile))
# no line matched
# Dec 5 12:34:50 hostname noop: NOOP
line4 = self._make_line(self._get_timestamp(), "noop", "NOOP")
self._write_logfile(self.logfile, line4)
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_OK)
self.assertEqual(log.get_message(), self.MESSAGE_OK)
def test_pattern_with_quiet(self):
"""--pattern and --quiet options
"""
self.config["pattern_list"] = ["ERROR"]
self.config['output_quiet'] = True
log = LogChecker(self.config)
# 1 line matched
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line1)
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE_WITH_QUIET.format(self.logfile))
# 2 lines matched
# Dec 5 12:34:50 hostname test: ERROR1
# Dec 5 12:34:50 hostname test: ERROR2
line2 = self._make_line(self._get_timestamp(), "test", "ERROR1")
line3 = self._make_line(self._get_timestamp(), "test", "ERROR2")
self._write_logfile(self.logfile, [line2, line3])
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_TWO_WITH_QUIET.format(self.logfile))
# no line matched
# Dec 5 12:34:50 hostname noop: NOOP
line4 = self._make_line(self._get_timestamp(), "noop", "NOOP")
self._write_logfile(self.logfile, line4)
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_OK)
self.assertEqual(log.get_message(), self.MESSAGE_OK)
def test_pattern_with_header(self):
"""--pattern and --header options
"""
self.config["pattern_list"] = ["ERROR"]
self.config['output_header'] = True
log = LogChecker(self.config)
# 1 line matched
# Dec 5 12:34:50 hostname test: ERROR
timestamp = self._get_timestamp()
line1 = self._make_line(timestamp, "test", "ERROR")
header1 = self._make_line(timestamp, "test", "")
self._write_logfile(self.logfile, line1)
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE_WITH_HEADER.format(header1, self.logfile))
# 2 lines matched
# Dec 5 12:34:50 hostname test: ERROR1
# Dec 5 12:34:50 hostname test: ERROR2
timestamp = self._get_timestamp()
line2 = self._make_line(timestamp, "test", "ERROR1")
line3 = self._make_line(timestamp, "test", "ERROR2")
header2 = self._make_line(timestamp, "test", "")
header3 = self._make_line(timestamp, "test", "")
self._write_logfile(self.logfile, [line2, line3])
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_TWO_WITH_HEADER.format(header2, header3, self.logfile))
# no line matched
# Dec 5 12:34:50 hostname noop: NOOP
line4 = self._make_line(self._get_timestamp(), "noop", "NOOP")
self._write_logfile(self.logfile, line4)
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_OK)
self.assertEqual(log.get_message(), self.MESSAGE_OK)
def test_critical_pattern(self):
"""--critical-pattern option
"""
self.config["critical_pattern_list"] = ["FATAL"]
log = LogChecker(self.config)
# Dec 5 12:34:50 hostname test: FATAL
line = self._make_line(self._get_timestamp(), "test", "FATAL")
self._write_logfile(self.logfile, line)
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_CRITICAL)
self.assertEqual(
log.get_message(),
self.MESSAGE_CRITICAL_ONE.format(line, self.logfile))
def test_critical_pattern_with_quiet(self):
"""--critical-pattern and --quiet options
"""
self.config["critical_pattern_list"] = ["FATAL"]
self.config['output_quiet'] = True
log = LogChecker(self.config)
# Dec 5 12:34:50 hostname test: FATAL
line = self._make_line(self._get_timestamp(), "test", "FATAL")
self._write_logfile(self.logfile, line)
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_CRITICAL)
self.assertEqual(
log.get_message(),
self.MESSAGE_CRITICAL_ONE_WITH_QUIET.format(self.logfile))
def test_critical_pattern_with_header(self):
"""--critical-pattern and --header options
"""
self.config["critical_pattern_list"] = ["FATAL"]
self.config['output_header'] = True
log = LogChecker(self.config)
# Dec 5 12:34:50 hostname test: FATAL
timestamp = self._get_timestamp()
line = self._make_line(timestamp, "test", "FATAL")
header = self._make_line(timestamp, "test", "")
self._write_logfile(self.logfile, line)
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_CRITICAL)
self.assertEqual(
log.get_message(),
self.MESSAGE_CRITICAL_ONE_WITH_HEADER.format(header, self.logfile))
def test_negpattern(self):
"""--negpattern option
"""
self.config["pattern_list"] = ["ERROR"]
self.config["critical_pattern_list"] = ["FATAL"]
self.config["negpattern_list"] = ["IGNORE"]
log = LogChecker(self.config)
# check --pattern
# Dec 5 12:34:50 hostname test: ERROR IGNORE
line1 = self._make_line(self._get_timestamp(), "test", "ERROR IGNORE")
self._write_logfile(self.logfile, line1)
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_OK)
self.assertEqual(log.get_message(), self.MESSAGE_OK)
# check --critical-pattern
# Dec 5 12:34:50 hostname test: FATAL IGNORE
line2 = self._make_line(self._get_timestamp(), "test", "FATAL IGNORE")
self._write_logfile(self.logfile, line2)
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_CRITICAL)
self.assertEqual(
log.get_message(),
self.MESSAGE_CRITICAL_ONE.format(line2, self.logfile))
def test_critical_negpattern(self):
"""--critical-negpattern option
"""
self.config["pattern_list"] = ["ERROR"]
self.config["critical_pattern_list"] = ["FATAL"]
self.config["critical_negpattern_list"] = ["IGNORE"]
log = LogChecker(self.config)
# check --pattern and --critical-negpattern
# Dec 5 12:34:50 hostname test: ERROR IGNORE
line1 = self._make_line(self._get_timestamp(), "test", "ERROR IGNORE")
self._write_logfile(self.logfile, line1)
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_OK)
self.assertEqual(log.get_message(), self.MESSAGE_OK)
# check --critical-pattern and --ciritical-negpattern
# Dec 5 12:34:50 hostname test: FATAL IGNORE
line2 = self._make_line(self._get_timestamp(), "test", "FATAL IGNORE")
self._write_logfile(self.logfile, line2)
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_OK)
self.assertEqual(log.get_message(), self.MESSAGE_OK)
# check --pattern, --critical-pattern and --critical-negpattern
# Dec 5 12:34:50 hostname test: ERROR FATAL IGNORE
line3 = self._make_line(
self._get_timestamp(), "test", "ERROR FATAL IGNORE")
self._write_logfile(self.logfile, line3)
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_OK)
self.assertEqual(log.get_message(), self.MESSAGE_OK)
def test_case_insensitive(self):
"""--case-insensitive option
"""
self.config["pattern_list"] = ["error"]
self.config["critical_pattern_list"] = ["fatal"]
self.config["negpattern_list"] = ["ignore"]
self.config["case_insensitive"] = True
log = LogChecker(self.config)
# check --pattern
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line1)
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line1, self.logfile))
# check --critical-pattern
# Dec 5 12:34:50 hostname test: FATAL
line2 = self._make_line(self._get_timestamp(), "test", "FATAL")
self._write_logfile(self.logfile, line2)
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_CRITICAL)
self.assertEqual(
log.get_message(),
self.MESSAGE_CRITICAL_ONE.format(line2, self.logfile))
# check --pattern and --negpattern
# Dec 5 12:34:50 hostname test: ERROR ERROR IGNORE
line3 = self._make_line(self._get_timestamp(), "test", "ERROR IGNORE")
self._write_logfile(self.logfile, line3)
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_OK)
self.assertEqual(log.get_message(), self.MESSAGE_OK)
def test_encoding(self):
"""--pattern and --encoding
"""
self.config["pattern_list"] = ["エラー"]
self.config["encoding"] = "EUC-JP"
log = LogChecker(self.config)
# Dec 5 12:34:50 hostname test: エラー
line = self._make_line(self._get_timestamp(), "test", "エラー")
self._write_logfile(self.logfile, line, encoding='EUC-JP')
log.clear_state()
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line, self.logfile))
def test_multiline(self):
"""--multiline
"""
self.config["pattern_list"] = ["ERROR1.*ERROR2"]
self.config["negpattern_list"] = ["IGNORE"]
self.config["multiline"] = True
log = LogChecker(self.config)
# check --pattern, --multiline
# Dec 5 12:34:50 hostname test: ERROR1
# Dec 5 12:34:50 hostname test: ERROR2
timestamp = self._get_timestamp()
lines = []
messages = ["ERROR1", "ERROR2"]
for message in messages:
lines.append(self._make_line(timestamp, "test", message))
self._write_logfile(self.logfile, lines)
log.clear_state()
log.check(self.logfile)
# detected line: Dec 5 12:34:50 hostname test: ERROR1 ERROR2
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(
lines[0] + " " + messages[1], self.logfile))
# check --pattern, --negpattern and --multiline
# Dec 5 12:34:50 hostname test: ERROR
# Dec 5 12:34:50 hostname test: ERROR IGNORE
timestamp = self._get_timestamp()
lines = []
messages = ["ERROR", "ERROR IGNORE"]
for message in messages:
lines.append(self._make_line(timestamp, "test", message))
self._write_logfile(self.logfile, lines)
log.clear_state()
log.check(self.logfile)
# detected line: Dec 5 12:34:50 hostname test: ERROR ERROR IGNORE
self.assertEqual(log.get_state(), LogChecker.STATE_OK)
self.assertEqual(log.get_message(), self.MESSAGE_OK)
def test_logfile(self):
"""--logfile option
"""
self.config["pattern_list"] = ["ERROR"]
log = LogChecker(self.config)
# check -logfile option with wild card '*'
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile1, line1)
time.sleep(1)
# Dec 5 12:34:51 hostname test: ERROR
line2 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile2, line2)
log.clear_state()
log.check(self.logfile_pattern)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_TWO_IN_TWO_FILES.format(
line1, self.logfile1, line2, self.logfile2))
# --logfile option with multiple filenames
# Dec 5 12:34:51 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile1, line1)
time.sleep(1)
# Dec 5 12:34:52 hostname test: ERROR
line2 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile2, line2)
logfile_pattern = "{0} {1}".format(self.logfile1, self.logfile2)
log.clear_state()
log.check(logfile_pattern)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_TWO_IN_TWO_FILES.format(
line1, self.logfile1, line2, self.logfile2))
def test_trace_inode(self):
"""--trace_inode
"""
self.config["pattern_list"] = ["ERROR"]
self.config["trace_inode"] = True
log = LogChecker(self.config)
# within expiration
# create logfile
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line1)
# create seekfile of logfile
log.check(self.logfile_pattern)
seekfile_1 = log._create_seek_filename(
self.logfile_pattern, self.logfile, trace_inode=True)
# update logfile
# Dec 5 12:34:51 hostname test: ERROR
line2 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line2)
# log rotation
os.rename(self.logfile, self.logfile1)
# create a new logfile
# Dec 5 12:34:52 hostname noop: NOOP
line3 = self._make_line(self._get_timestamp(), "noop", "NOOP")
self._write_logfile(self.logfile, line3)
# create seekfile of logfile
log.clear_state()
log.check(self.logfile_pattern)
seekfile_2 = log._create_seek_filename(
self.logfile_pattern, self.logfile, trace_inode=True)
seekfile1_2 = log._create_seek_filename(
self.logfile_pattern, self.logfile1, trace_inode=True)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line2, self.logfile1))
self.assertEqual(seekfile_1, seekfile1_2)
self.assertTrue(os.path.exists(seekfile_2))
self.assertTrue(os.path.exists(seekfile1_2))
def test_scantime(self):
"""--scantime option
"""
self.config["pattern_list"] = ["ERROR"]
self.config["scantime"] = 2
log = LogChecker(self.config)
# within scantime.
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile1, line1)
log.clear_state()
log.check(self.logfile_pattern)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line1, self.logfile1))
# over scantime
# Dec 5 12:34:50 hostname test: ERROR
line2 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile1, line2)
time.sleep(4)
log.clear_state()
log.check(self.logfile_pattern)
self.assertEqual(log.get_state(), LogChecker.STATE_OK)
self.assertEqual(log.get_message(), self.MESSAGE_OK)
# multiple logfiles.
# Dec 5 12:34:50 hostname test: ERROR
line3 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile1, line3)
time.sleep(4)
# Dec 5 12:34:54 hostname test: ERROR
line4 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile2, line4)
# logfile1 should be older than spantime. Therefore, don't scan it.
log.clear_state()
log.check(self.logfile_pattern)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line4, self.logfile2))
def test_remove_seekfile(self):
"""--expiration and --remove-seekfile options
"""
self.config["pattern_list"] = ["ERROR"]
self.config["scantime"] = 2
self.config["expiration"] = 4
log = LogChecker(self.config)
# within expiration
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile1, line1)
log.check(self.logfile_pattern, remove_seekfile=True)
self.seekfile1 = log._create_seek_filename(
self.logfile_pattern, self.logfile1)
time.sleep(2)
# Dec 5 12:34:54 hostname test: ERROR
line2 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile2, line2)
# seek file of logfile1 should not be purged.
log.clear_state()
log.check(self.logfile_pattern, remove_seekfile=True)
self.seekfile2 = log._create_seek_filename(
self.logfile_pattern, self.logfile2)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line2, self.logfile2))
self.assertTrue(os.path.exists(self.seekfile1))
self.assertTrue(os.path.exists(self.seekfile2))
# over expiration
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile1, line1)
log.check(self.logfile_pattern, remove_seekfile=True)
time.sleep(6)
# Dec 5 12:34:54 hostname test: ERROR
line2 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile2, line2)
# seek file of logfile1 should be purged.
log.clear_state()
log.check(self.logfile_pattern, remove_seekfile=True)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line2, self.logfile2))
self.assertFalse(os.path.exists(self.seekfile1))
self.assertTrue(os.path.exists(self.seekfile2))
def test_remove_seekfile_with_dry_run(self):
"""--expiration, --remove-seekfile, and --dry-run options
"""
self.config["pattern_list"] = ["ERROR"]
self.config["scantime"] = 2
self.config["expiration"] = 4
log = LogChecker(self.config)
# within expiration
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile1, line1)
log.check(self.logfile_pattern, remove_seekfile=True)
self.seekfile1 = log._create_seek_filename(
self.logfile_pattern, self.logfile1)
time.sleep(2)
# Dec 5 12:34:54 hostname test: ERROR
line2 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile2, line2)
# seek file of logfile1 should not be purged.
log.clear_state()
log.check(self.logfile_pattern, remove_seekfile=True)
self.seekfile2 = log._create_seek_filename(
self.logfile_pattern, self.logfile2)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line2, self.logfile2))
self.assertTrue(os.path.exists(self.seekfile1))
self.assertTrue(os.path.exists(self.seekfile2))
# with dry run
self.config["dry_run"] = True
log = LogChecker(self.config)
# over expiration
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile1, line1)
log.check(self.logfile_pattern, remove_seekfile=True)
time.sleep(6)
# Dec 5 12:34:54 hostname test: ERROR
line2 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile2, line2)
log.clear_state()
log.check(self.logfile_pattern, remove_seekfile=True)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line2, self.logfile2))
self.assertTrue(os.path.exists(self.seekfile1))
self.assertTrue(os.path.exists(self.seekfile2))
def test_remove_seekfile_inode(self):
"""--trace_inode, --expiration and --remove-seekfile options
"""
self.config["pattern_list"] = ["ERROR"]
self.config["trace_inode"] = True
self.config["scantime"] = 2
self.config["expiration"] = 3
log = LogChecker(self.config)
# create logfile
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line1)
# log rotation
os.rename(self.logfile, self.logfile1)
# create new logfile
# Dec 5 12:34:50 hostname test: ERROR
line2 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line2)
# do check_log_multi, and create seekfile and seekfile1
log.clear_state()
log.check(self.logfile_pattern, remove_seekfile=True)
seekfile_1 = log._create_seek_filename(
self.logfile_pattern, self.logfile, trace_inode=True)
seekfile1_1 = log._create_seek_filename(
self.logfile_pattern, self.logfile1, trace_inode=True)
time.sleep(4)
# update logfile
# Dec 5 12:34:54 hostname test: ERROR
line3 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line3)
# log rotation, purge old logfile2
os.rename(self.logfile1, self.logfile2)
os.rename(self.logfile, self.logfile1)
# seek file of old logfile1 should be purged.
log.clear_state()
log.check(
self.logfile_pattern, remove_seekfile=True)
seekfile1_2 = log._create_seek_filename(
self.logfile_pattern, self.logfile1, trace_inode=True)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line3, self.logfile1))
self.assertEqual(seekfile_1, seekfile1_2)
self.assertFalse(os.path.exists(seekfile1_1))
self.assertTrue(os.path.exists(seekfile1_2))
def test_remove_seekfile_inode_with_dry_run(self):
"""--trace_inode, --expiration, --remove-seekfile, and --dry-run options
"""
self.config["pattern_list"] = ["ERROR"]
self.config["trace_inode"] = True
self.config["scantime"] = 2
self.config["expiration"] = 3
log = LogChecker(self.config)
# create logfile
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line1)
# log rotation
os.rename(self.logfile, self.logfile1)
# create new logfile
# Dec 5 12:34:50 hostname test: ERROR
line2 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line2)
# do check_log_multi, and create seekfile and seekfile1
log.clear_state()
log.check(self.logfile_pattern, remove_seekfile=True)
seekfile_1 = log._create_seek_filename(
self.logfile_pattern, self.logfile, trace_inode=True)
seekfile1_1 = log._create_seek_filename(
self.logfile_pattern, self.logfile1, trace_inode=True)
time.sleep(4)
# update logfile
# Dec 5 12:34:54 hostname test: ERROR
line3 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line3)
# log rotation, purge old logfile2
os.rename(self.logfile1, self.logfile2)
os.rename(self.logfile, self.logfile1)
# with dry run
self.config["dry_run"] = True
log = LogChecker(self.config)
log.clear_state()
log.check(
self.logfile_pattern, remove_seekfile=True)
seekfile1_2 = log._create_seek_filename(
self.logfile_pattern, self.logfile1, trace_inode=True)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line3, self.logfile1))
self.assertEqual(seekfile_1, seekfile1_2)
self.assertTrue(os.path.exists(seekfile1_1))
self.assertTrue(os.path.exists(seekfile1_2))
def test_replace_pipe_symbol(self):
"""replace pipe symbol
"""
line = "Dec | 5 12:34:56 hostname test: ERROR"
self.config["pattern_list"] = ["ERROR"]
log = LogChecker(self.config)
# Dec 5 12:34:50 hostname test: ERROR |
line = self._make_line(self._get_timestamp(), "test", "ERROR |")
self._write_logfile(self.logfile, line)
log.check(self.logfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(
line.replace("|", "(pipe)"), self.logfile))
def test_seekfile(self):
"""--seekfile option
"""
self.config["pattern_list"] = ["ERROR"]
log = LogChecker(self.config)
# 1 line matched
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line1)
log.check(self.logfile, seekfile=self.seekfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line1, self.logfile))
# 2 lines matched
# Dec 5 12:34:50 hostname test: ERROR1
# Dec 5 12:34:50 hostname test: ERROR2
line2 = self._make_line(self._get_timestamp(), "test", "ERROR1")
line3 = self._make_line(self._get_timestamp(), "test", "ERROR2")
self._write_logfile(self.logfile, [line2, line3])
log.clear_state()
log.check(self.logfile, seekfile=self.seekfile)
self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_TWO.format(line2, line3, self.logfile))
# no line matched
# Dec 5 12:34:50 hostname noop: NOOP
line4 = self._make_line(self._get_timestamp(), "noop", "NOOP")
self._write_logfile(self.logfile, line4)
log.clear_state()
log.check(self.logfile, seekfile=self.seekfile)
self.assertEqual(log.get_state(), LogChecker.STATE_OK)
self.assertEqual(log.get_message(), self.MESSAGE_OK)
def test_tag(self):
"""--tag
"""
self.config["pattern_list"] = ["ERROR"]
log = LogChecker(self.config)
# create new logfiles
# Dec 5 12:34:50 hostname test: ERROR
line1 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile, line1)
# Dec 5 12:34:50 hostname test: ERROR
line2 = self._make_line(self._get_timestamp(), "test", "ERROR")
self._write_logfile(self.logfile1, line2)
# Dec 5 12:34:50 hostname test: ERROR
line3 = self._make_line(self._get_timestamp(), "noop", "NOOP")
self._write_logfile(self.logfile2, line3)
# create seekfile of logfile
seekfile_1 = log._create_seek_filename(
self.logfile_pattern, self.logfile, tag=self.tag1)
seekfile_2 = log._create_seek_filename(
self.logfile_pattern, self.logfile, tag=self.tag1)
seekfile_3 = log._create_seek_filename(
self.logfile_pattern, self.logfile, tag=self.tag2)
log.check(self.logfile, seekfile=seekfile_3)
log.clear_state()
log.check(