Wl-domain.py

From PeformIQ Upgrade
Jump to navigation Jump to search
#! /usr/bin/env python
#
#  Purpose: Parse and display an informix log file
#
#  $Id:$
#
#---------------------------------------------------------------------

"""
Parse Weblogic wl-domain file and produce exception summary report
"""

#---------------------------------------------------------------------

import re
import os
import sys
import time
import getopt

#---------------------------------------------------------------------

__version__  = "1.0.0"

debugFlg     = 0
stdoutFlg    = 0
verboseFlg   = 0
wallclockFlg = 0
rewriteFlg   = 0

filename     = "wl-domain.log"
mode         = None
data         = None
td_start     = None

cnt           = 0
cntExceptions = 1
cntLogLines   = 0
cntAt         = 0
cntBad        = 0

#---------------------------------------------------------------------

stCount      = {}
stLevel      = {}
stTrace      = {}
stCurrent    = None

#---------------------------------------------------------------------

def startSeconds(s):
   return time.mktime(time.strptime(s, "%Y-%m-%d %H:%M:%S"))

#---------------------------------------------------------------------

def seconds(hms):
   s = hms.split(':')

   return ((s[0] * 60) + s[1]) * 60 + s[2]

#---------------------------------------------------------------------

def hms(t_secs):
   hm     = t_secs / 60
   hours  = hm / 60
   mins   = hm % 60
   secs   = t_secs % 60

   return "%02d:%02d:%02d" % ( hours, mins, secs)

#---------------------------------------------------------------------

def newTrace(currentTrace, level, newSignature):
   if stTrace.has_key(newSignature):
      copyTrace = stTrace[newSignature]
      stCount[newSignature] += 1
      #stCount[oldSignature] -= 1
   else:
      copyTrace = []

      for i in range(level):
         copyTrace.append(currentTrace[i])

      copyTrace.append(newSignature)

      stCount[newSignature] = 1
      stTrace[newSignature] = copyTrace
      stLevel[newSignature] = level

   # print str(copyTrace)

   return copyTrace

#---------------------------------------------------------------------

def parse(filename):
   global cnt
   global cntExceptions
   global cntLogLines
   global cntAt
   global cntBad
   global stCount
   global stLevel
   global stTrace
   global stCurrent

   tmatch = re.search(r'(.*?).log', filename)
   if tmatch:
      name = tmatch.group(1)
   else:
      name = filename + ".log"
      filename += ".log"

   outfile = name + ".out"

   try:
      ifo = open(filename, 'r')
   except IOError, msg:
      sys.stderr.write(filename + ': cannot open: ' + `msg` + '\n')
      sys.exit(1)

   if stdoutFlg == 1:
      ofo = sys.stdout
   else:
      try:
         ofo = open(outfile, 'w')
      except IOError, msg:
         sys.stderr.write(outfile + ': cannot open: ' + `msg` + '\n')
         sys.exit(1)


   t_offset      = 0
   inStackTrace  = 0
   inException   = 0
   stackMethod   = None

   while 1:
      line = ifo.readline()

      if not line: break

      line = line[:-1]
      line = line.replace("\r","")
      re.sub("	at[^#]*#", "#", line)

      tmatch = re.search(r'	at .*>', line)
      if tmatch:
         line = re.sub("	at .*>", "\tat BAD", line)
         cntBad += 1

      if re.search("^$", line):
         continue

      if re.search("^\r$", line):
         continue

      cnt += 1

      # print line

      tmatch = re.search(r'####<', line)
      if tmatch:
         tmatch = re.search(r'<([^<]*?)>.*<([^<]*?)>', line)
         dt = tmatch.group(1)
         xx = tmatch.group(2)
         # print "dt \"%s\"  xx \"%s\"" % (dt, xx)
         inTrace      = 1
         cntLogLines += 1
         if inStackTrace == 1:
            inStackTrace   = 0
            cntExceptions += 1
            print stackMethod
      else:
         inTrace           = 0

      tmatch = re.search(r'\tat ([^ ]*\)?)', line)
      if tmatch:
         stackMethod = tmatch.group(1)
         # print "at \"%s\"" % method
         if inStackTrace != 1:
            print "** Start of \"%s\" [%d]" % (stackMethod, cnt)
            level                  = 0
            if stCount.has_key(stackMethod):
               stCount[stackMethod] += 1
            else:
               stLevel[stackMethod] = 0
               stCount[stackMethod] = 1
               stTrace[stackMethod] = [stackMethod,]
            inStackTrace  = 1
            stCurrent = stTrace[stackMethod]
         else:
            level                     += 1
            if level == len(stCurrent):  # Still assembling first one of this type
               # print "+%03d+ %s" % (level, stackMethod)
               stCurrent.append(stackMethod)
            else:
               # print ">%03d> %s" % (level, stackMethod)
               if re.search(r'BAD', stackMethod):
                  print ">---> skipping"
                  continue
               # print "Level %d  stCurrent \"%s\"" % (level, len(stCurrent))
               # print stCurrent[level].find(stackMethod)
               if stCurrent[level].find(stackMethod) != 0:
                  # print "[%s]  len1 = %d " % (stCurrent[level], len(stCurrent[level]))
                  # print "[%s]  len2 = %d " % (stackMethod, len(stackMethod))
                  # print "Bogus method \"%s\" at level %d" % (stackMethod, level)
                  # print "  of \"%s\"" % stCurrent[0]
                  stCurrent = newTrace(stCurrent, level, stackMethod)
                  # sys.exit(0)
         cntAt        += 1
      else:
         inStackTrace  = 0

      # p = line.split(' ')
      # print p
      # f_elapsed = float(p[0])
      # location = p[3]
      # task     = p[4]

      if None:
         tmatch = re.search(r'value_request_id \[Value=(.*?)\]', p[5])
         if tmatch:
            request_id = tmatch.group(1)
            continue

      elif None:
         tmatch = re.search(r'item \[(.*?)\] rq \[Value=(.*?)\] awb \[Value=(.*?)\]', p[5])
         if tmatch:
            item_no = tmatch.group(1)
            continue

      if rewriteFlg:
         if endFlg == 1:
            endFlg = 0
            print >>ofo, "%s" % "xx"
         else:
            print >>ofo, "%s" % "xx"
      else:
         if (None):
            if None:
               print >>ofo, "%s" % "xx"
            else:
               print >>ofo, "%s" % "xx"

   ifo.close()

   summary(sys.stdout)
   summary(ofo)

   for method in stTrace.keys():
      printTrace(ofo, stTrace[method], stLevel[method], stCount[method])

   ofo.close()


#---------------------------------------------------------------------

def printTrace(out, trace, level, no):
   print >>out
   print >>out, " ***** %s *****" % trace[0]
   print >>out, " No of calls deep %d  Signature level %d  No of occurences %d " % (len(trace), level,  no)
   for i in range(len(trace)):
      print >>out, "  %3d  %s" % (i, trace[i])

#---------------------------------------------------------------------

def summary(out):
   print >>out, "Processed %d lines" % cnt
   print >>out, "Processed %d log lines" % cntLogLines
   print >>out, "Processed %d at lines" % cntAt
   print >>out, "Processed %d truncated stack traces" % cntBad
   print >>out, "Processed %d Exceptions" % cntExceptions
   print >>out, "Found %d Distinct Exceptions" % len(stCount.keys())
   print >>out, "Found %d Distinct Exceptions" % len(stCount.keys())

#---------------------------------------------------------------------

def main():
   global debugFlg
   global verboseFlg

   try:
      opts, args = getopt.getopt(sys.argv[1:], "dvVw?")
   except getopt.error, msg:
      print __doc__
      return 1

   for o, a in opts:
      if o == '-d':
         debugFlg = 1
      elif o == '-v':
         verboseFlg = 1
      elif o == '-V':
         print "Version: %s" % __version__
         return 1
      elif o == '-?':
         print __doc__
         return 1

   if (debugFlg):
      print ">> Flg    %s" % debugFlg

   parse(filename)

   return 1

#---------------------------------------------------------------------

if __name__ == '__main__' or __name__ == sys.argv[0]:
   sys.exit(main())

#---------------------------------------------------------------------

"""
Revision History:

     Date     Who   Description
   --------   ---   --------------------------------------------------
   20040603   plh   Initial implementation

Problems to fix:

To Do:

Issues:

"""