-
Notifications
You must be signed in to change notification settings - Fork 31
/
log_freshness.py
executable file
·59 lines (54 loc) · 1.92 KB
/
log_freshness.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
#!/usr/bin/env python
#
# License: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import sys, os, time
DEFAULT_AGE=600
def check_logs(*logs):
out = []
total = 0
status = 'ok'
msg = 'everything looks good'
n = time.time()
for l in logs:
if isinstance(l, (list, tuple)):
limit, l = l
else:
limit = DEFAULT_AGE
try:
s = os.stat(l)
except Exception:
status = 'err'
msg = "file not found '%s'" % l
else:
diff = n - (s.st_mtime)
msg = 'everything looks good, modified %d seconds ago' % diff
if diff > limit:
status = 'err'
msg = '%s not modified in %d seconds' % (l, diff)
out.insert(0, "status %s %s" % (status, msg))
print '\n'.join(out)
if len(sys.argv) < 2:
sys.exit('Usage: %s <log file to check>' % sys.argv[0])
if not os.path.exists(sys.argv[1]):
sys.exit('status err file %s not found' % (sys.argv[1]))
else:
check_logs(sys.argv[1])