-
Notifications
You must be signed in to change notification settings - Fork 2
/
watchdog.py
46 lines (40 loc) · 1.74 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
#!/usr/bin/python3
###############################################
# Copyright by IT Stall (www.itstall.de) 2018 #
# Author: Dennis Eisold #
# Created: 28.07.2018 #
###############################################
import os, json, subprocess, shlex, time
import paho.mqtt.client as mqtt
try:
with open('/opt/opencamper/config.json') as f:
config = json.load(f)
except KeyError:
print("No config file found")
exit()
config_set = "watchdog"
mqtt_server = config[config_set]['mqtt_setting']
data = {}
if(mqtt_server):
client = mqtt.Client()
client.connect(config[mqtt_server]['host'], config[mqtt_server]['port'], config[mqtt_server]['timeout'])
if config[mqtt_server]['username'] is not 0 and config[mqtt_server]['password'] is not 0:
client.username_pw_set(config[mqtt_server]['username'], config[mqtt_server]['password'])
def screen_present(process_name):
screens = subprocess.check_output(["screen -ls; true"], shell=True)
if ("." + process_name + "\t(" in screens.decode('utf-8')):
data[process_name] = 1
else:
data[process_name] = 0
print("Starting process: " + process_name + " command: " + config[config_set]['processes'][process_name]['service'])
args = shlex.split(config[config_set]['processes'][process_name]['service'])
subprocess.call(args)
time.sleep(3)
return 0
while True:
for process_name in config[config_set]['processes']:
if(config[config_set]['processes'][process_name]['active'] == 1):
screen_present(process_name)
print(data)
client.publish(config[config_set]['mqtt_topic'], json.dumps(data))
time.sleep(config[config_set]['check_delay'])