I've created a new tool, which parses a specified log file, and if an Oracle exception is there, it kills Tomcat and sends an email notification.
I tried to take into account comments from my previous post, but I'm sure there is enough stuff that can be improved.
This utility uses python-tail module to get data from the log.
As the tool is big enough, here are just a few main functions:
# sending notifications def sendmail(event): try: # get time time = datetime.datetime.now() timemes = '\n\nSysevent at %s' % time # connect to local SMTP smtpconnect = smtplib.SMTP('localhost:25') # debug to sys.stdout currently is $OUTPUT_DIR/logparcer_mail.log smtpconnect.set_debuglevel(1) # sending email smtpconnect.sendmail(sender, receivers, message + event + timemes) # close conenction smtpconnect.quit() logging.info('Email sent from %s to %s.' % (sender, receivers)) # if anything wrong on local SMTP except socket.gaierror as s: logging.exception('Local SMTP error: %s' % s) except smtplib.SMTPException as e: logging.exception('ERROR during email sent: %s' % e)
# running main tail tail = tail.Tail(catalinalog) # when new line appears - sent it to 'err' function tail.register_callback(err) tail.follow()
Errors to rise KILL:
# list of errors to parce errors = ('ORA-01017', 'ORA-28000')
err function, which parses data from
# main error parces function def err(data): # check both errors if any(ora in data for ora in errors): # on PROD we don't need kill Tomcat if not env == 'PROD': # get Tomcat's PID pid = catapid(tpidf) # kill Tomcat with kill -9 try: # killing Tomcat with kill -9 os.kill(int(pid), signal.SIGKILL) # save message for email and log event = "Tomcat killed with PID %s.\n\nError data: \n\n%s" % (pid, data) logging.error(event) # sending email sendmail(event) logging.info('Email sent from %s to %s.\n' % (sender, receivers)) # as Tomcat killed - LogParcer doesn't need anymore # will be started again when LMSmanager -start sys.exit(0) # if can't find catalina.pid except OSError as e: event = "Got ALARM, but can't kill Tomcat: %s with PID %s" % (e, pid) sendmail(event) logging.critical(event) # if PROD - just send email, no kill else: event = "Tomcat wasn't killed. Please - check logs manually.\n" sendmail(event) logging.error(event)
And one from my 'main' question is: is it good practice to call one function from inside of another (