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):
                # 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
                # sending email
                smtpconnect.sendmail(sender, receivers, message + event + timemes)
                # close conenction
                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)

Tail calls:

# running main tail
tail = tail.Tail(catalinalog)
# when new line appears - sent it to 'err' function

Errors to rise KILL:

# list of errors to parce
errors = ('ORA-01017', 'ORA-28000')

And err function, which parses data from Tail:

# 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
                            # 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)
                            # sending email
                            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
                    # if can't find catalina.pid
                    except OSError as e:
                            event = "Got ALARM, but can't kill Tomcat: %s with PID %s" % (e, pid)
                # if PROD - just send email, no kill
                       event = "Tomcat wasn't killed. Please - check logs manually.\n"

And one from my 'main' question is: is it good practice to call one function from inside of another (sendmail() from err())?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.