From 0d491a1b346e4027863e584c2c70d036af1ae7af Mon Sep 17 00:00:00 2001 From: dk-15 Date: Sun, 18 Jun 2017 16:22:48 +0530 Subject: Add updated mailing system for master server --- .gitignore | 2 + date.txt | 3 +- new.csv | 2 +- new_cron_job.sh | 2 +- sbhs_server/create_ip_map.py | 5 +- sbhs_server/scan_rpis.sh | 16 ++- .../management/commands/initialize_machines.py | 149 --------------------- .../tables/management/commands/send_report.py | 55 ++++++++ 8 files changed, 74 insertions(+), 160 deletions(-) delete mode 100644 sbhs_server/tables/management/commands/initialize_machines.py create mode 100644 sbhs_server/tables/management/commands/send_report.py diff --git a/.gitignore b/.gitignore index e0e3300..342214e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ static/img/webcam/ experiments/ sbhs.sqlite3 sbhs_server/tables/migrations +sbhs_server/RPi_data +sbhs_server/pi-ip.json diff --git a/date.txt b/date.txt index 24cf74c..4e3bd8b 100644 --- a/date.txt +++ b/date.txt @@ -1,2 +1 @@ - -Fri Jun 9 15:02:01 IST 2017 \ No newline at end of file +Sun Jun 18 16:14:09 IST 2017 diff --git a/new.csv b/new.csv index 15f8027..cd7d386 100644 --- a/new.csv +++ b/new.csv @@ -4701,4 +4701,4 @@ 2017-06-08 15:22:26.446962 True False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False True 2017-06-08 15:30:18.130241 False False True True False False False True False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False 2017-06-09 14:43:28.646527 False False False False False False False True False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False -2017-06-09 15:02:01.132038 False False True True False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False \ No newline at end of file +2017-06-09 15:02:01.132038 False False True True False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False 2017-06-18 16:14:09.305220 False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False diff --git a/new_cron_job.sh b/new_cron_job.sh index aa55092..7bf6e6d 100644 --- a/new_cron_job.sh +++ b/new_cron_job.sh @@ -12,10 +12,10 @@ source ../bin/activate #source ./bin/activate #python sbhs_server/scan_machines.py bash sbhs_server/scan_rpis.sh +python manage.py send_report #python offline_reconnect.py python manage.py makemigrations python manage.py migrate -#python manage.py initialize_machines python manage.py generate_checksum touch index.wsgi python manage.py log_generator diff --git a/sbhs_server/create_ip_map.py b/sbhs_server/create_ip_map.py index 2964be0..0b0dad3 100644 --- a/sbhs_server/create_ip_map.py +++ b/sbhs_server/create_ip_map.py @@ -1,15 +1,14 @@ import json pi_ip_map = {} -ipaddrs = [] -with open("temp/ipaddrs.txt", "r") as filehandler: +with open("RPi_data/ipaddrs.txt", "r") as filehandler: ipaddrs = filehandler.readlines() ipaddrs = [ip.strip() for ip in ipaddrs] for ip in ipaddrs: - filename = "temp/" + ip + ".txt" + filename = "RPi_data/map/" + ip + ".txt" with open(filename, "r") as filehandler: for line in filehandler: try: diff --git a/sbhs_server/scan_rpis.sh b/sbhs_server/scan_rpis.sh index 3a1fd3c..4577a41 100644 --- a/sbhs_server/scan_rpis.sh +++ b/sbhs_server/scan_rpis.sh @@ -1,11 +1,19 @@ #!/bin/bash -mkdir -p temp -nmap -n -sP 10.42.0.255/24 | grep -E -o "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" | grep -v "10.42.0.1" > temp/ipaddrs.txt +cd sbhs_server +mkdir -p RPi_data +mkdir -p RPi_data/map RPi_data/report + +# get IP addresses of connected Raspberry Pi's +nmap -n -sP 10.42.0.255/24 | grep -E -o "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" | grep -v "10.42.0.1" > RPi_data/ipaddrs.txt + +# Get map_machine_ids.txt and server reports from all connected Raspberry Pi's while read -r ip do - curl -o temp/$ip.txt $ip/map_machine_ids.txt -done < "temp/ipaddrs.txt" + curl -o RPi_data/map/$ip.txt $ip/map_machine_ids.txt + curl -o RPi_data/report/$ip.txt $ip/report.json +done < "RPi_data/ipaddrs.txt" +# Execute script to create IP map python create_ip_map.py \ No newline at end of file diff --git a/sbhs_server/tables/management/commands/initialize_machines.py b/sbhs_server/tables/management/commands/initialize_machines.py deleted file mode 100644 index b39e4e6..0000000 --- a/sbhs_server/tables/management/commands/initialize_machines.py +++ /dev/null @@ -1,149 +0,0 @@ -from django.core.management.base import BaseCommand, CommandError -from sbhs_server import settings -from sbhs_server.tables.models import Board -from sbhs_server import helpers -import serial,time - -class Command(BaseCommand): - args = '' - help = 'Initializes the SBHS board data in the database' - - - - def handle(self, *args, **options): - """ - This function executes as a part of cron job . It monitors the condition of the SBHS devices. - To be specific it does the following checks : - 1.It tries to connect to the devices whose path exists in map_machine_id.txt - 2.It checks the status of fan ,heater, sensor on SBHS by performing a series of variations on them and analysing - the result - At the end it updates the Boards database with the device status. - It mails the admin if any fault occurs.The mail comprises of machine Id of faulty board and probable cause. - - - - This function is invoked from command line as following - python manage.py initialize_machines - - """ - previous_onlines = Board.objects.only("mid").filter(online=True) - previous_onlines = [p.mid for p in previous_onlines] - current_onlines = settings.online_mids - current_connections = settings.boards - - - MINIMUM_DELTA = 1 - - def write_to_sbhs(conn,command,value): - conn.write(chr(command)) - conn.write(chr(value)) - - def read_from_sbhs(conn,command): - conn.write(chr(command)) - result=-1 - if command==255: - result=ord(conn.read(1))+0.1*ord(conn.read(1)) - else: - result=ord(conn.read(1)) - - return result - - - - - first_reading,second_reading,final_reading={},{},{} - - - for key,value in current_connections.iteritems(): - first_reading[key]=read_from_sbhs(value['board'].boardcon,255) - write_to_sbhs(value['board'].boardcon,253,0) - write_to_sbhs(value['board'].boardcon,254,50) - - time.sleep(40) - - for key,value in current_connections.iteritems(): - second_reading[key] = read_from_sbhs(value['board'].boardcon,255) - write_to_sbhs(value['board'].boardcon,253,100) - - time.sleep(40) - - for key,value in current_connections.iteritems(): - final_reading[key] = read_from_sbhs(value['board'].boardcon,255) - - - for key,value in current_connections.iteritems(): - write_to_sbhs(value['board'].boardcon,253,100) - write_to_sbhs(value['board'].boardcon,254,0) - - - print first_reading - print second_reading - print final_reading - - - - delta1,delta2={},{} - - faulty_boards = {} - - for key in current_connections: - delta1[key]=second_reading[key]-first_reading[key] - delta2[key]=final_reading[key]-second_reading[key] - - for key in current_connections: - if delta1[key] > MINIMUM_DELTA and delta2[key] < -MINIMUM_DELTA: - pass - elif delta1[key] > MINIMUM_DELTA and delta2[key] > MINIMUM_DELTA: - #fans not working - faulty_boards[key]="Probably Fan is not working" - elif delta1[key] < MINIMUM_DELTA and delta2[key] < -MINIMUM_DELTA: - #heaters not working - faulty_boards[key]="Probably Heater is not working" - else: - #sensors not working - faulty_boards[key]="Probably Sensor is not working" - - for o in current_onlines: - try: - Board.objects.get_or_create(mid=o) - except: - pass - - new_offlines = [] - for p in previous_onlines: - if p not in current_onlines: - new_offlines.append(p) - - - message = "SBHS Administrator,\n\n" - message += "Following issue requires immidiate attention.\n\n" - if len(new_offlines)>0: - message += "SBHS could not be connected\n" - for n in new_offlines: - message += ("MID: %d\n" % n) - if bool(faulty_boards): - message += "Following devices are suspected to be faulty.\n\n" - for key in faulty_boards: - message += "MID : {} Cause : {}\n" .format(key,faulty_boards[key]) - message += "\nYou can check the SBHS status on http://vlabs.iitb.ac.in/sbhs/admin/." - message += " Possible correction actions are:\n" - message += "1. Run this command without brackets -> ( cd $SBHS_SERVER_ROOT; ./cron_job.sh )\n" - message += "2. If same machine comes offline multiple times, replacement of the machine is advised.\n\n\n" - message += "Regards,\nSBHS Vlabs Server Code" - - print "New offline board mids", new_offlines - subject = "SBHS Vlabs: Notice - SBHS not connected" - - - print message - - print len(new_offlines) > 0 or len(faulty_boards)>0 - - if len(new_offlines) > 0 or len(faulty_boards)>0: - for admin in settings.SBHS_ADMINS: - helpers.mailer.email(admin[2], subject, message) - - Board.objects.filter(mid__in=current_onlines).update(online=True) - Board.objects.exclude(mid__in=current_onlines).update(online=False) - - self.stdout.write('Boards loaded') diff --git a/sbhs_server/tables/management/commands/send_report.py b/sbhs_server/tables/management/commands/send_report.py new file mode 100644 index 0000000..0780da1 --- /dev/null +++ b/sbhs_server/tables/management/commands/send_report.py @@ -0,0 +1,55 @@ +from django.core.management.base import BaseCommand, CommandError +from sbhs_server import settings, helpers +import os, json + +class Command(BaseCommand): + args = '' + help = 'Sends email to admin' + + + + def handle(self, *args, **options): + + # Store all Raspberry Pi IP's in a list + with open(os.path.join(settings.BASE_DIR, "sbhs_server/RPi_data/ipaddrs.txt"), "r") as filehandler: + ipaddrs = filehandler.readlines() + + # Strip whitespaces (in case) + ipaddrs = [ip.strip() for ip in ipaddrs] + + new_offlines = [] + faulty_boards = {} + + # Create data for offline and faulty boards + for ip in ipaddrs: + filename = "sbhs_server/RPi_data/report/" + ip + ".txt" + with open(os.path.join(settings.BASE_DIR, filename), "r") as filehandler: + data = filehandler.read() + data = json.loads(data) + new_offlines += data["new_offlines"] + faulty_boards.update(data["faulty_boards"]) + + # Compose body for the email + message = "SBHS Administrator,\n\n" + message += "Following issue requires immidiate attention.\n\n" + if len(new_offlines) > 0: + message += "SBHS could not be connected\n" + for n in new_offlines: + message += ("MID: %d\n" % n) + if bool(faulty_boards): + message += "Following devices are suspected to be faulty.\n\n" + for key in faulty_boards: + message += "MID : {} Cause : {}\n" .format(key,faulty_boards[key]) + message += "\nYou can check the SBHS status on http://vlabs.iitb.ac.in/sbhs/admin/." + message += " Possible correction actions are:\n" + message += "1. Run this command without brackets -> ( cd $SBHS_SERVER_ROOT; ./cron_job.sh )\n" + message += "2. If same machine comes offline multiple times, replacement of the machine is advised.\n\n\n" + message += "Regards,\nSBHS Vlabs Server Code" + + print "New offline board mids", new_offlines + subject = "SBHS Vlabs: Notice - SBHS not connected" + + # Send email + if len(new_offlines) > 0 or len(faulty_boards)>0: + for admin in settings.SBHS_ADMINS: + helpers.mailer.email(admin[2], subject, message) \ No newline at end of file -- cgit