-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_lag.py
executable file
·36 lines (32 loc) · 945 Bytes
/
log_lag.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
#!/usr/bin/env python
import os
import sys
import time
from datetime import datetime
from functools import partial
def get_latest_time(fn):
f = open(fn)
f.seek(0, os.SEEK_END)
f.seek(f.tell() - 4096, os.SEEK_SET)
end = f.read().splitlines()[1:-1] #ignore possibly incomplete first and last lines
times = [line.split()[0] for line in end]
timestamps = map(float, times)
latest = max(timestamps)
return latest
def lag(fn, lagvalue):
try :
latest = get_latest_time(fn)
except (IOError, ValueError):
#File could be rotating, wait and try again
time.sleep(10)
latest = get_latest_time(fn)
now = time.time()
lag = now - latest
if (lag < float(lagvalue)):
print ("ERROR - File %s lags by %f",fn,lag)
sys.exit(2)
else:
print ("OK - No lag for file %s",fn)
sys.exit(0)
if __name__ == "__main__":
lag(sys.argv[1],sys.argv[2])