implemented a watchdog

This commit is contained in:
moo
2022-06-21 13:31:40 +02:00
parent 80903d71f8
commit 515b88785e
2 changed files with 90 additions and 9 deletions

View File

@@ -83,6 +83,16 @@ def main():
os.remove('/home/zutritt/Documents/sarah/system_request_commands/manual_door_open_request_set') os.remove('/home/zutritt/Documents/sarah/system_request_commands/manual_door_open_request_set')
#in order to be able to implement a watchdog, the system checks for a file and deletes it is it's there
if os.path.isfile('/home/zutritt/Documents/sarah/system_request_commands/watchdog_attention_request_set'):
#if this file exists, there was a watchdog request made
debug_file = open("/home/zutritt/Documents/sarah/logs/"+datetime.today().strftime('%Y%m%d')+"_debug_log.csv", "a")
debug_file.write("\n"+datetime.now().strftime("%d/%m/%Y %H:%M:%S") + ", answered the watchdog.")
debug_file.close()
os.remove('/home/zutritt/Documents/sarah/system_request_commands/watchdog_attention_request_set')
#after reading a tag, we check if any numbers have been read #after reading a tag, we check if any numbers have been read
#no numbers mean no tag has been detected #no numbers mean no tag has been detected
if (not isinstance(uid,bytearray)): if (not isinstance(uid,bytearray)):

View File

@@ -0,0 +1,71 @@
import smtplib, ssl
from datetime import datetime
import time
import os
from email.mime.text import MIMEText
#
# This tool was written by Pet (pet@fet.at) in 6.2022
#
# It is a watchdog for the RFID reader "sarah", in reader.py
#
def main():
print("Watchdog watches! Started at: " + datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
#if the file is already there, sarah didn't anwer last time and we don't need to send an additional mail
if os.path.isfile('/home/zutritt/Documents/sarah/system_request_commands/watchdog_attention_request_set'):
print("Sarah was down the last time we checked, no new check initiated. ")
#create a file which should be removed by the reader.py
with open("/home/zutritt/Documents/sarah/system_request_commands/watchdog_attention_request_set", "a") as x:
x.write(datetime.now().strftime("%d/%m/%Y %H:%M:%S") + ", Watchdog request set. \n")
x.close()
print("Watchdog file written.")
#wait for the read to delete the file
time.sleep(10)
#if the file is still there, sarah didn't anwer
if os.path.isfile('/home/zutritt/Documents/sarah/system_request_commands/watchdog_attention_request_set'):
print("Sarah didn't answer, we need to send a mail to the lab people. ")
send_mail()
else:
print("Sarah answered, everything is good.")
def send_mail():
debug_file_text = open("/home/zutritt/Documents/sarah/logs/"+datetime.today().strftime('%Y%m%d')+"_debug_log.csv", "r").read()
port = 587 # For starttls
smtp_server = "buran.htu.tuwien.ac.at"
sender_email = "doorknob@fet.at"
sender_username = "doorknob"
receiver_email = "lab@fet.at"
password = "JYgPXDWuX47N6ef"
message = "Hallo, \nich bin Sarahs Watchdog. Sarah, die Türöffnerin im FET Lab, reagiert nicht. \n" +\
"Hier ein paar Logs zum debuggen:\n" + \
"Es ist jetzt: " + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + "\nDas Debug file beinhaltet: \n" + debug_file_text
msg = MIMEText(message)
msg['Subject'] = 'FET Doorknob issues'
msg['From'] = sender_email
msg['To'] = receiver_email
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(sender_username, password)
server.sendmail(sender_email, receiver_email, msg.as_string())
print("Mail sent.")
if __name__ == '__main__':
main()