-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_file_report
executable file
·77 lines (58 loc) · 1.97 KB
/
check_file_report
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
#!/usr/bin/env python
import sys
import os
import datetime
import os.path
def parse_datetime(datestring, default):
if datestring == '':
return default
else:
return datetime.datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S');
def trim(text):
return text.strip(' \n\t\r')
def parse_int(intstring):
if intstring == '':
return 0
else:
return int(intstring)
if len(sys.argv) != 4:
print 'CRITICAL - Input error, usage: ./check_file_report <file> <warn_secs> <crit_secs>'
sys.exit(2);
filename = sys.argv[1]
warn_seconds = parse_int(sys.argv[2])
crit_seconds = parse_int(sys.argv[3])
if warn_seconds < 1:
print 'CRITICAL - Input error: Warning seconds must be numeric and greater 0.'
sys.exit(2);
if crit_seconds < 1:
print 'CRITICAL - Input error: Critical seconds must be numeric and creater 0.'
sys.exit(2);
if not os.path.isfile(filename):
print 'CRITICAL - Input error: File not found.'
sys.exit(2);
with open(filename) as f:
lines = f.readlines()
if len(lines) != 3:
print 'CRITICAL - Input error: File does not have two lines.'
sys.exit(2);
report_time = parse_datetime(trim(lines[0]), datetime.datetime(2000, 1, 1))
now = datetime.datetime.now()
report_status = trim(lines[1]);
report_detail = trim(lines[2]);
if now > (report_time + datetime.timedelta(seconds=crit_seconds)):
print 'CRITICAL - Critical time expired.'
sys.exit(2);
if report_status == 'CRITICAL':
print 'CRITICAL - Report status is critical. Report detail: ' + report_detail
sys.exit(2);
if now > (report_time + datetime.timedelta(seconds=warn_seconds)):
print 'WARNING - Warning time expired. Report detail: ' + report_detail
sys.exit(1);
if report_status == 'WARNING':
print 'WARNING - Report status is warning. Report detail: ' + report_detail
sys.exit(1);
if report_status == 'OK':
print 'OK - Report status is OK. Report detail: ' + report_detail
sys.exit(0);
print 'UNKNOWN - Report status is unknown. Report detail: ' + report_detail
sys.exit(3);