From 475a2eccfb823c37254e469470b966c3b1724141 Mon Sep 17 00:00:00 2001 From: coderick14 Date: Wed, 24 May 2017 14:08:52 +0530 Subject: Add cron module --- health_monitor.py | 78 -------------------------------------- maintenance/__init__.py | 0 maintenance/admin.py | 6 +++ maintenance/apps.py | 8 ++++ maintenance/health_monitor.py | 78 ++++++++++++++++++++++++++++++++++++++ maintenance/migrations/__init__.py | 0 maintenance/models.py | 6 +++ maintenance/tests.py | 6 +++ maintenance/views.py | 6 +++ requirements.txt | 3 +- sbhs_server/settings.py | 7 +++- 11 files changed, 118 insertions(+), 80 deletions(-) delete mode 100644 health_monitor.py create mode 100644 maintenance/__init__.py create mode 100644 maintenance/admin.py create mode 100644 maintenance/apps.py create mode 100644 maintenance/health_monitor.py create mode 100644 maintenance/migrations/__init__.py create mode 100644 maintenance/models.py create mode 100644 maintenance/tests.py create mode 100644 maintenance/views.py diff --git a/health_monitor.py b/health_monitor.py deleted file mode 100644 index aa19cb1..0000000 --- a/health_monitor.py +++ /dev/null @@ -1,78 +0,0 @@ -import os, serial, smtplib -from time import sleep -from sbhs_server.settings import online_mids, ADMIN_EMAIL -from sbhs_server.helpers import mailer - -MAX_PORTS = 256 -MIN_TEMP = 10 -MAX_TEMP = 70 - -def write_to_port(s, com, arg): - s.write(chr(int(com))) - sleep(0.5) - s.write(chr(int(arg))) - sleep(0.5) - -def read_from_port(s, com): - s.write(chr(int(com))) - sleep(0.5) - if com == 255: - result = ord(s.read(1)) + (0.1 * ord(s.read(1))) - elif com == 252: - result = ord(s.read(1)) - else: - result = -1 - sleep(0.5) - return result - -def create_message(check_mids, defective_ports): - msg = '' - if len(check_mids) != 0: - msg += 'Please check the following machine ids :\n' - for i,mid in enumerate(check_mids): - msg = msg + str(i) + '. ' + str(mid) + '\n' - if len(defective_ports) != 0: - msg = msg + '\nPlease check the following ports :\n' - for port in defective_ports: - msg = msg + str(port) + '\n' - return msg - -def main(): - present_online_mids, offline_mids, defective_ports = [], [], [] - for i in range(0,MAX_PORTS): - path = '/dev/ttyUSB' + str(i) - if os.path.exists(path): - try: - s = serial.Serial(port=path, baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=2) - if not s.is_open: - s.open() - assert s.is_open - mid = read_from_port(s, 252) # get machine id - assert mid > 0 - write_to_port(s, 253, 100) # set fan speed - write_to_port(s, 254, 0) # set heater value - current_temp = read_from_port(s, 255) # get current temperature - assert current_temp >= MIN_TEMP and current_temp <= MAX_TEMP - present_online_mids.append(mid) - except: - if s.is_open: - if mid > 0: - offline_mids.append(mid) - else: - defective_ports.append(path) - if s.is_open: - s.close() - check_mids = list(set(online_mids).difference(set(present_online_mids))) - msg = create_message(check_mids, defective_ports) - if len(msg) == 0: - msg = "Nothing out of order was detected on the server. :)" - #thread.start_new_thread(mailer.email, (ADMIN_EMAIL, "Health report of SBHS Server", msg)) - mailer.email(ADMIN_EMAIL, "Health report of SBHS Server", msg) - print msg - -if __name__ == "__main__": - main() - - - - diff --git a/maintenance/__init__.py b/maintenance/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/maintenance/admin.py b/maintenance/admin.py new file mode 100644 index 0000000..13be29d --- /dev/null +++ b/maintenance/admin.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.contrib import admin + +# Register your models here. diff --git a/maintenance/apps.py b/maintenance/apps.py new file mode 100644 index 0000000..4262aa8 --- /dev/null +++ b/maintenance/apps.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.apps import AppConfig + + +class MaintenanceConfig(AppConfig): + name = 'maintenance' diff --git a/maintenance/health_monitor.py b/maintenance/health_monitor.py new file mode 100644 index 0000000..87946b2 --- /dev/null +++ b/maintenance/health_monitor.py @@ -0,0 +1,78 @@ +import os, serial, smtplib +from time import sleep +from sbhs_server.credentials import ADMIN_EMAIL +from sbhs_server.helpers import mailer + +MAX_PORTS = 256 +MIN_TEMP = 10 +MAX_TEMP = 70 + +def write_to_port(s, com, arg): + s.write(chr(int(com))) + sleep(0.5) + s.write(chr(int(arg))) + sleep(0.5) + +def read_from_port(s, com): + s.write(chr(int(com))) + sleep(0.5) + if com == 255: + result = ord(s.read(1)) + (0.1 * ord(s.read(1))) + elif com == 252: + result = ord(s.read(1)) + else: + result = -1 + sleep(0.5) + return result + +def create_message(check_mids, defective_ports): + msg = '' + if len(check_mids) != 0: + msg += 'Please check the following machine ids :\n' + for i,mid in enumerate(check_mids): + msg = msg + str(i) + '. ' + str(mid) + '\n' + if len(defective_ports) != 0: + msg = msg + '\nPlease check the following ports :\n' + for port in defective_ports: + msg = msg + str(port) + '\n' + return msg + +def main(): + present_online_mids, offline_mids, defective_ports = [], [], [] + for i in range(0,MAX_PORTS): + path = '/dev/ttyUSB' + str(i) + if os.path.exists(path): + try: + s = serial.Serial(port=path, baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=2) + if not s.is_open: + s.open() + assert s.is_open + mid = read_from_port(s, 252) # get machine id + assert mid > 0 + write_to_port(s, 253, 100) # set fan speed + write_to_port(s, 254, 0) # set heater value + current_temp = read_from_port(s, 255) # get current temperature + assert current_temp >= MIN_TEMP and current_temp <= MAX_TEMP + present_online_mids.append(mid) + except: + if s.is_open: + if mid > 0: + offline_mids.append(mid) + else: + defective_ports.append(path) + if s.is_open: + s.close() + check_mids = list(set(online_mids).difference(set(present_online_mids))) + msg = create_message(check_mids, defective_ports) + if len(msg) == 0: + msg = "Nothing out of order was detected on the server. :)" + #thread.start_new_thread(mailer.email, (ADMIN_EMAIL, "Health report of SBHS Server", msg)) + mailer.email(ADMIN_EMAIL, "Health report of SBHS Server", msg) + #print msg + +if __name__ == "__main__": + main() + + + + diff --git a/maintenance/migrations/__init__.py b/maintenance/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/maintenance/models.py b/maintenance/models.py new file mode 100644 index 0000000..1dfab76 --- /dev/null +++ b/maintenance/models.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models + +# Create your models here. diff --git a/maintenance/tests.py b/maintenance/tests.py new file mode 100644 index 0000000..5982e6b --- /dev/null +++ b/maintenance/tests.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.test import TestCase + +# Create your tests here. diff --git a/maintenance/views.py b/maintenance/views.py new file mode 100644 index 0000000..e784a0b --- /dev/null +++ b/maintenance/views.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.shortcuts import render + +# Create your views here. diff --git a/requirements.txt b/requirements.txt index 323eaed..600139a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ appdirs==1.4.3 backports-abc==0.5 certifi==2017.4.17 Django==1.11.1 +django-crontab==0.7.1 django-taggit==0.18.1 django-undelete==0.1 oauthlib==2.0.2 @@ -9,7 +10,7 @@ packaging==16.8 pkg-resources==0.0.0 PyJWT==1.5.0 pyparsing==2.2.0 -pyserial==2.7 +pyserial==3.0 python-openid==2.2.5 python-social-auth==0.2.19 pytz==2017.2 diff --git a/sbhs_server/settings.py b/sbhs_server/settings.py index 15dc8ae..7161f29 100644 --- a/sbhs_server/settings.py +++ b/sbhs_server/settings.py @@ -63,6 +63,7 @@ INSTALLED_APPS = ( #'yaksh', 'taggit', #'corsheaders' + 'django_crontab', 'account', 'myadmin', @@ -184,6 +185,10 @@ TEMPLATES = [ }, ] +CRONJOBS = [ + ('2 * * * *', 'maintenance.health_monitor.main', '>> /tmp/health_monitor.log'), +] + import warnings warnings.filterwarnings( 'ignore', r"DateTimeField .* received a naive datetime", @@ -270,6 +275,6 @@ print "No of machines online : ", len(online_mids) import sys print >>sys.stderr, online_mids[1:33] #srikant #srikant -#f = open('/tmp/online_mids', 'w') +#f = open(os.path.join(BASE_DIR, 'maintenance/online_mids.txt'), 'w') #f.write(online_mids) #f.close() -- cgit