-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathliveMonitor.py
More file actions
157 lines (132 loc) · 5.54 KB
/
liveMonitor.py
File metadata and controls
157 lines (132 loc) · 5.54 KB
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
# Copyright (C) Mark McIntyre
import time
import os
import sys
import glob
from sendToLive import uploadOneEvent
import datetime
import logging
import RMS.ConfigReader as cr
from stat import ST_INO
from uploadToArchive import readKeyFile, readIniFile
from ukmonPostProc import setupLogging
log = logging.getLogger("ukmonlogger")
timetowait = 300 # seconds to wait for a new line before deciding the log is stale
# Images created more than this many seconds ago won't be uploaded. Prevents reuploads.
MAXAGE=int(os.getenv('UKMMAXAGE', default='1800'))
# frequency at which to check for fireball requests. Zero means dont check
#FBINTERVAL = int(os.getenv('UKMFBINTERVAL', default='1800'))
def follow(fname, logf_ino):
thefile = open(fname, 'r')
t = 0
while True:
line = thefile.readline()
if not os.path.isfile(fname):
time.sleep(1)
sres = os.stat(fname)
if logf_ino != sres[ST_INO]:
yield 'log rolled'
if not line:
time.sleep(0.1)
t = t + 0.1
if t > timetowait:
t = 0
yield 'log stale'
else:
continue
else:
t = 0
yield line.strip()
def monitorLogFile(camloc, rmscfg):
""" This function monitors the latest RMS log file for meteor detections, convert the FF file
to a jpg and upload it to the livestream.
This function is called from the shell script *liveMonitor.sh* and should not be called directly.
"""
cfg = cr.parse(os.path.expanduser(rmscfg))
datadir = cfg.data_dir
logdir = os.path.expanduser(os.path.join(datadir, cfg.log_dir))
setupLogging(logdir, 'ukmonlive_')
log.info('--------------------------------')
log.info(' live feed started')
log.info('--------------------------------')
log.info('Camera location is {}'.format(camloc))
log.info('RMS config file is {}'.format(rmscfg))
myloc = os.path.split(os.path.abspath(__file__))[0]
# get credentials
inifvals = readIniFile(os.path.join(myloc, 'ukmon.ini'))
if not inifvals:
log.error('ukmon.ini not present, aborting')
exit(1)
keys = readKeyFile(os.path.join(myloc, 'live.key'), inifvals)
if not keys:
log.error('config file not present, aborting')
exit(1)
keepon = True
logf = ''
capdir = ''
while keepon is True:
try:
logfs = glob.glob(os.path.join(logdir, 'log_{}*.log*').format(cfg.stationID))
logfs.sort(key=lambda x: os.path.getmtime(x))
newlogf = logfs[-1]
if newlogf != logf:
logf = newlogf
log.info('Now monitoring {}'.format(logf))
lis = open(logf,'r').readlines()
dd = [li for li in lis if 'Data directory' in li or 'New data directory' in li]
if len(dd) > 0:
capdir = dd[0].split(' ')[5].strip()
#log.info('Capture dir is {}'.format(capdir))
# iterate over the generator
logf_ino = os.stat(logf)[ST_INO]
loglines = follow(logf, logf_ino)
for line in loglines:
nowtm = datetime.datetime.now(datetime.timezone.utc)
if line == 'log stale' or line == 'log rolled':
#log.info(line)
logfs = glob.glob(os.path.join(logdir, 'log_{}*.log*').format(cfg.stationID))
logfs.sort(key=lambda x: os.path.getmtime(x))
logf = logfs[-1]
loglines.close()
raise StopIteration
else:
if "Data directory" in line or 'New data directory' in line:
newcapdir = line.split(' ')[5].strip()
if '/' not in newcapdir:
newcapdir = line.split(' ')[6].strip()
if capdir != newcapdir:
capdir = newcapdir
log.info('Latest capture dir is {}'.format(capdir))
nowtm = datetime.datetime.now(datetime.timezone.utc)
if "detected meteors" in line and ": 0" not in line and "TOTAL" not in line:
log.info('meteor detected')
if capdir != '':
ffname = line.split(' ')[3]
ftime = datetime.datetime.strptime(ffname[10:25], '%Y%m%d_%H%M%S').replace(tzinfo=datetime.timezone.utc)
if (nowtm - ftime).seconds < MAXAGE:
log.info('uploading {}'.format(ffname))
uploadOneEvent(capdir, ffname, cfg, keys, camloc)
else:
log.info(f'too long ago: {(nowtm - ftime).seconds}')
pass
else:
log.warning('cap dir not set')
except StopIteration:
# reload the latest log
pass
except Exception as e:
log.info('Problem reading RMS log: {} - will retry'.format(logf))
log.info(e, exc_info=True)
# reload the RMS config file in case its been updated
cfg = cr.parse(os.path.expanduser(rmscfg))
pass
if __name__ == '__main__':
if len(sys.argv) < 2:
print('LOCATION missing')
exit(1)
if len(sys.argv) < 3:
rmscfg = os.path.expanduser('~/source/RMS/.config')
else:
rmscfg = sys.argv[2]
camloc = sys.argv[1]
monitorLogFile(camloc, rmscfg)