-
Notifications
You must be signed in to change notification settings - Fork 2
/
watchdog.py
executable file
·372 lines (282 loc) · 11.1 KB
/
watchdog.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
#!/usr/bin/python3
from astropy.io import fits
import config
from datetime import datetime, timedelta
import ephem
import glob
import json
import logging
import MySQLdb
import math
import numpy
import os
import pika
from PIL import Image
import re
import signal
import subprocess
import sys
import time
from threading import Thread
sys.path.insert(0, '/common')
import lib
def terminate(signal,frame):
print("Start Terminating: %s" % datetime.now())
global running
running = False
thread.join()
print(" [x] Watchdog thread stopped")
sys.exit(0)
signal.signal(signal.SIGTERM, terminate)
web = lib.getWebConfig()
logging.basicConfig(filename=config.log['path'], level=config.log['level'])
logging.info('[+] Start')
uptimeStored = lib.getSensorLast('uptime')
uptimeNow = lib.getUptime()
if uptimeStored is not None and float(uptimeStored) > uptimeNow:
logging.info('[.] First time start')
lib.setSensorLast('uptime', 0, uptimeNow)
for relay in web['relays']:
f = open('/sys/class/gpio/export', 'wt')
f.write(relay['gpio'])
f.close()
time.sleep(0.1)
for relay in web['relays']:
f = open('/sys/class/gpio/gpio{}/direction'.format(relay['gpio']), 'wt')
f.write('out')
f.close()
os.chown('/sys/class/gpio/gpio{}/value'.format(relay['gpio']), 82, 82)
time.sleep(0.1)
for relay in web['relays']:
cursor.execute('SELECT state FROM relay where id = "{}"'.format(relay['name']))
state = cursor.fetchone()
f = open('/sys/class/gpio/gpio{}/value'.format(relay['gpio']), 'wt')
f.write('{}'.format(state['state']) if state else '0')
f.close()
def reload(ch, method, properties, body):
print(" [x] Received RELOAD")
ch.basic_ack(delivery_tag=method.delivery_tag)
global running
running = False
thread.join()
print(" [x] Watchdog thread stopped")
sys.exit(0);
def watchdog():
print(' [*] Start watchdog thread')
global web
# Задачи watchdog
# + сбор мусора (архив фото, видео, фитов)
# - есть ли место?
# - получен ли кадр? Перезапуски, если нет
# - жива ли база? Перезапуск, если что
# - жив ли rabbit? Перезапуск, если что
# - живы ли rabbit-worker'ы:
# - allsky
# - process
# - camera
# - жив ли веб:
# - nginx
# - php
# - (отдельно) генерация видео
# - реле (грелка)
# - все задачи уместились в 1 минуту?
#for relay in config.relay:
# if 'hotter' in relay:
# hotter = relay
# hotter['stage'] = 0
# hotter['state'] = 0
# hotter['percent'] = 0
# break
#def hotterRelay(state):
# global hotter, db, cursor
#
# hotter['state'] = state
#
# f = open('/sys/class/gpio/gpio{}/value'.format(hotter['gpio']), 'wt')
# f.write('{}'.format(state))
# f.close()
#
# ts = int(time.time())
# cursor.execute('replace into relay (id, state, date) values ("{}", {}, {})'.format(hotter['name'], state, ts))
# db.commit()
#
# logging.debug('Реле обогрева {}'.format('включено' if state == 1 else 'выключено'));
# # watchdog не контролирует время начального подбора выдержки - оно может быть значительным
# if savedDate and (savedDate + timedelta(seconds=600)) < datetime.now():
# # а потом проверяет давно ли получен кадр
# logging.error('Уже 10 минут камера не записывает кадры. Попытка рестарта allsky.py через supervisord')
# sys.exit(1)
# if hotter:
# if hotter['stage'] == 0:
# import json
#
# cursor.execute('select val from config where id = "hotPercent"')
# row = cursor.fetchone()
#
# hotter['percent'] = int(json.loads(row['val'])) if row else 0
# if hotter['percent'] > 0:
# hotterRelay(1)
#
# hotter['stage'] += 1
#
# if (hotter['stage'] * 10) > hotter['percent'] and hotter['state'] == 1:
# hotterRelay(0)
#
# if hotter['stage'] == 10:
# hotter['stage'] = 0
#
# time.sleep(6)
folders = {
'jpg': '/web/snap/',
'fit': '/web/fits/',
'mp4': '/web/video/',
}
minute = datetime.now().strftime("%Y-%m-%d_%H-%M")
hourSensor = datetime.now().strftime("%Y-%m-%d_%H")
sunrisePlusHour = None
while running:
now = datetime.now()
#print('"now" is '+ now.strftime('%d/%m/%Y %H:%M:%S'))
db = MySQLdb.connect(host=config.db['host'], user=config.db['user'], passwd=config.db['passwd'],
database=config.db['database'], charset='utf8')
cursor = db.cursor()
# TBD контроль за запущенными докерами. А пока просто список в сенсоры (и оттуда - в заббикс)
dockerMap = {
'allsky-allsky': 0, #+cycle
'allsky-camera': 1, #-cycle - надо выделить универсального INDI клиента с автопоиском камеры
'allsky-db': 2,
'allsky-indi': 3,
'allsky-nginx': 4,
'allsky-php': 5,
'allsky-process': 6, #+cycle
'allsky-rabbit': 7,
'allsky-sensor': 8, #+cycle
'allsky-watchdog': 9, #+cycle
'allsky-yolo': 10 #-cycle тут нет mysql вовсе
}
'''
docker inspect --format='{{json .State}}' allsky-watchdog
{"Status":"running","Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":319830,"ExitCode":0,"Error":"","StartedAt":"2023-11-25T18:16:09.768754342Z","FinishedAt":"2023-11-25T18:16:09.317634819Z"}
docker inspect --format='{{.State.StartedAt}}' allsky-watchdog
'''
for item in dockerMap:
startedAt = os.popen("/docker inspect --format='{{.State.StartedAt}}' "+ item).read()
#2023-11-25T06:51:52.920311027Z
#"Status": "running",
#"Status": "restarting",
if len(startedAt) > 19:
# todo а в локальные сенсоры надо это записывать?
cursor.execute("""REPLACE INTO sensor_last(date, channel, type, val)
VALUES (%(time)i, %(channel)i, '%(type)s', %(val)f)
""" % {"time": now.timestamp(), "channel": dockerMap[item], "type": 'docker-started', "val": datetime.strptime(startedAt[:19] +'+00:00', '%Y-%m-%dT%H:%M:%S%z').timestamp() })
todaySunrise = lib.getTodaySunrise(now)
#print('Today sunrise at '+ todaySunrise.strftime("%d/%m/%Y %H:%M:%S"))
#if sunrisePlusHour is None:
# print('sunrisePlusHour is None')
#else:
# print('day of sunrise is '+ sunrisePlusHour.strftime('%d'))
#print('day of now is '+ now.strftime('%d'))
if now > (todaySunrise + timedelta(hours=1)) and (sunrisePlusHour is None or sunrisePlusHour.strftime('%d') != now.strftime('%d') ):
logging.info('[+] Sunrise +1h')
sunrisePlusHour = now
videoFilename = '/web/video/'+ now.strftime('%Y-%m-%d') +'.mp4'
keoFilename = '/web/keogram/keogram-'+ now.strftime('%Y-%m-%d') +'.jpg'
if not os.path.isfile(videoFilename):
# if not os.path.isfile(keoFilename):
observatory = lib.getObservatory()
if observatory:
observatory.date = todaySunrise
begin = observatory.previous_setting(ephem.Sun()).datetime() - timedelta(hours=1)
end = todaySunrise + timedelta(hours=1)
logging.debug('[.] Begin {}, end {}'.format(begin, end))
files = []
minWidth = None
minHeight = None
for f in sorted(glob.glob('/web/snap/????-??-??_??-??.jpg')):
result = re.match(r'/web/snap/(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2}).jpg', f)
if result:
date = datetime(int(result.group(1)), int(result.group(2)), int(result.group(3)), int(result.group(4)), int(result.group(5))) - timedelta(hours=int(web['observatory']['timezone']))
if date >= begin and date <= end:
try:
im = Image.open(f)
files.append(f)
if minWidth is None or im.size[0] < minWidth:
minWidth = im.size[0]
if minHeight is None or im.size[1] < minHeight:
minHeight = im.size[1]
except:
pass
if len(files) > 5 and minWidth is not None and minHeight is not None:
logging.debug('[.] Found suitable images: {}, minWidth={}, minHeight={}'.format(len(files), minWidth, minHeight))
keogram = Image.new('RGB', (len(files), minHeight), 'black')
for f in glob.glob('/web/snap/day-*.jpg'):
os.remove(f)
i = 0
for f in files:
im = Image.open(f)
# Image.fromarray(numpy.mean(im, axis=1).reshape(keogram.size[1], 1, 3).astype('uint8')) - average keo
if im.size[0] != minWidth or im.size[1] != minHeight:
im.resize((minWidth, minHeight))
keogram.paste(im.crop( (math.floor(minWidth / 2), 0, math.floor(minWidth / 2) + 1, minHeight) ), (i, 0) )
os.symlink(f, '/web/snap/day-'+ "%06d" % i +'.jpg')
i += 1
keogram.save(keoFilename)
scale = '{}:{}'.format(minWidth, minHeight)
if 'cfa' in web['ccd']:
options = '-vf scale='+ scale
else:
options = '-pix_fmt yuv420p -vf format=gray,scale='+ scale
cmd = '/docker run --rm -v /opt/allsky.py/web:/web olegmilantiev/allsky-ffmpeg ffmpeg -f image2 -i /web/snap/day-%06d.jpg '+ options +' '+ videoFilename
logging.debug('Run ffmpeg via docker: '+ cmd)
os.system(cmd)
for f in glob.glob('/web/snap/day-*.jpg'):
os.remove(f)
#else:
# todo чтобы больше не запускался
# every hour remove old sensor data from db
if hourSensor != now.strftime("%Y-%m-%d_%H") and now.minute > 33:
print('Removing old sensors data: start')
hourSensor = now.strftime("%Y-%m-%d_%H")
cursor.execute('delete from sensor where date < '+ str(time.time() - 86400 * int(web['archive']['sensors'])))
print('Removing old sensors data: done')
# remove old jpg, fit, mp4
for ext in folders:
old = now - timedelta(web['archive'][ext])
for f in os.listdir(folders[ext]):
if os.path.isfile(folders[ext] + f):
result = re.match(r'(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2}).' + ext, f)
if result:
date = datetime(int(result.group(1)), int(result.group(2)), int(result.group(3)),
int(result.group(4)), int(result.group(5)))
if date < old:
logging.debug('Файл ' + f + ' удалён')
os.remove(folders[ext] + f)
logging.info('[+] Waiting next minute')
cursor.execute("""REPLACE INTO sensor_last(date, channel, type, val)
VALUES (%(time)i, %(channel)i, '%(type)s', %(val)f)
""" % {"time": time.time(), "channel": 9, "type": 'docker-cycle', "val": time.time() - now.timestamp() })
db.commit()
cursor.close()
db.close()
while running:
now = datetime.now().strftime("%Y-%m-%d_%H-%M")
if now != minute:
break
time.sleep(1)
minute = now
running = True
thread = Thread(target = watchdog)
thread.start()
connection = pika.BlockingConnection(
pika.ConnectionParameters(
os.getenv('RABBITMQ_HOST'),
os.getenv('RABBITMQ_PORT'),
'/',
pika.PlainCredentials(
os.getenv('RABBITMQ_DEFAULT_USER'),
os.getenv('RABBITMQ_DEFAULT_PASS'))))
channel = connection.channel()
queue = channel.queue_declare(queue=os.getenv('RABBITMQ_QUEUE_RELOAD_WATCHDOG'), durable=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.basic_consume(queue=os.getenv('RABBITMQ_QUEUE_RELOAD_WATCHDOG'), on_message_callback=reload)
channel.start_consuming()