this was kinda dumb

This commit is contained in:
petrucci4prez 2017-05-31 00:26:28 -04:00
parent 4c15239996
commit 2e45edad1e
2 changed files with 11 additions and 11 deletions

View File

@ -5,7 +5,7 @@ from datetime import datetime
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
_logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
gmail = ConfigFile('config.yaml')['gmail'] gmail = ConfigFile('config.yaml')['gmail']
@ -20,7 +20,7 @@ def _scheduleAction(action):
while 1: while 1:
nextDate = _getNextDate() nextDate = _getNextDate()
sleepTime = nextDate - datetime.today() sleepTime = nextDate - datetime.today()
_logger.info('Next monthly test scheduled at %s (%s)', nextDate, sleepTime) logger.info('Next monthly test scheduled at %s (%s)', nextDate, sleepTime)
time.sleep(sleepTime.days * 86400 + sleepTime.seconds) time.sleep(sleepTime.days * 86400 + sleepTime.seconds)
action() action()
@ -42,14 +42,14 @@ def monthlyTest():
subject = 'harrison4hegemon - automated monthly test' subject = 'harrison4hegemon - automated monthly test'
body = 'this is an automated message - please do not reply\n\nin the future this may have useful information' body = 'this is an automated message - please do not reply\n\nin the future this may have useful information'
sendEmail(gmail['username'], gmail['passwd'], gmail['recipientList'], subject, body) sendEmail(gmail['username'], gmail['passwd'], gmail['recipientList'], subject, body)
_logger.debug('Sending monthly test to email list') logger.debug('Sending monthly test to email list')
def intruderAlert(): def intruderAlert():
subject = 'harrison4hegemon - intruder detected' subject = 'harrison4hegemon - intruder detected'
body = 'intruder detected - alarm was tripped on ' + time.strftime("%H:%M:%S - %d/%m/%Y") body = 'intruder detected - alarm was tripped on ' + time.strftime("%H:%M:%S - %d/%m/%Y")
sendEmail(gmail['username'], gmail['passwd'], gmail['recipientList'], subject, body) sendEmail(gmail['username'], gmail['passwd'], gmail['recipientList'], subject, body)
_logger.info('intruder detected') logger.info('intruder detected')
_logger.debug('Sending intruder alert to email list') logger.debug('Sending intruder alert to email list')
class GmailHandler(logging.Handler): class GmailHandler(logging.Handler):
''' '''

View File

@ -12,10 +12,10 @@ Logger conventions
Init order (very essential) Init order (very essential)
1) init console output (this will go to journald) and format as console only 1) init console output (this will go to journald) and format as console only
2) init the module level _logger so we can log anything that happens as we build 2) init the module level logger so we can log anything that happens as we build
the other loggers the other loggers
3) mount glusterfs, any errors here will go to console output 3) mount glusterfs, any errors here will go to console output
4) once gluster is mounted, add to root _logger and remove "console only" warning 4) once gluster is mounted, add to root logger and remove "console only" warning
from console from console
5) import gmail, this must come here as it uses loggers for some of its setup 5) import gmail, this must come here as it uses loggers for some of its setup
6) init gmail handler 6) init gmail handler
@ -49,7 +49,7 @@ class GlusterFSHandler(TimedRotatingFileHandler):
if not os.path.exists(logdest): if not os.path.exists(logdest):
os.mkdir(logdest) os.mkdir(logdest)
elif os.path.isfile(logdest): elif os.path.isfile(logdest):
_logger.error('%s is present but is a file (vs a directory). ' \ logger.error('%s is present but is a file (vs a directory). ' \
'Please (re)move this file to prevent data loss', logdest) 'Please (re)move this file to prevent data loss', logdest)
raise SystemExit raise SystemExit
@ -63,7 +63,7 @@ class GlusterFSHandler(TimedRotatingFileHandler):
def _mount(self): def _mount(self):
if os.path.ismount(self._mountpoint): if os.path.ismount(self._mountpoint):
# this assumes that the already-mounted device is the one intended # this assumes that the already-mounted device is the one intended
_logger.warning('Device already mounted at {}'.format(self._mountpoint)) logger.warning('Device already mounted at {}'.format(self._mountpoint))
else: else:
dst = self._server + ':/' + self._volume dst = self._server + ':/' + self._volume
cmd = ['mount', '-t', 'glusterfs', dst, self._mountpoint] cmd = ['mount', '-t', 'glusterfs', dst, self._mountpoint]
@ -79,7 +79,7 @@ class GlusterFSHandler(TimedRotatingFileHandler):
run(cmd, check=True, stdout=PIPE, stderr=PIPE) run(cmd, check=True, stdout=PIPE, stderr=PIPE)
except CalledProcessError as e: except CalledProcessError as e:
stderr = e.stderr.decode('ascii').rstrip() stderr = e.stderr.decode('ascii').rstrip()
_logger.error(stderr) logger.error(stderr)
raise SystemExit raise SystemExit
def close(self): def close(self):
@ -102,7 +102,7 @@ rootLogger.setLevel(logging.DEBUG)
rootLogger.addHandler(console) rootLogger.addHandler(console)
# 2 # 2
_logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# 3 # 3
gluster = GlusterFSHandler( gluster = GlusterFSHandler(