Skip to content

Instantly share code, notes, and snippets.

@joeclark-phd
Created July 23, 2020 19:30
Show Gist options
  • Save joeclark-phd/86123e5a4425002ecf8481bd6e00e7ed to your computer and use it in GitHub Desktop.
Save joeclark-phd/86123e5a4425002ecf8481bd6e00e7ed to your computer and use it in GitHub Desktop.
script to parse UCP data from catalina.out
import glob
import sys
import csv
print( "\n>> beginning process" )
# find and print list of files in directory
filenamesList = glob.glob("catalina.*")
print(">> found "+str(len(filenamesList))+" file(s) in this directory matching \"catalina.*\"")
# if none, say so and end
if len(filenamesList) < 1:
print(">> exiting.")
sys.exit()
# if any, open an output file and write the header line
with open('UCPstats.csv', mode='w', newline='') as output_file:
csvwriter = csv.writer(output_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow(["timestamp","available connections","borrowed connections","total connections","avg. wait time"])
# loop through files. for each:
for filename in filenamesList:
print(">>> opening file " + filename)
## read through lines until the beginning of a UCP data dump
with open(filename) as file:
count = 0
for line in file:
if line.startswith("========== Pool Stats"):
count += 1
date = line[22:32]+" "+line[33:45]
if date.endswith("===="): # this occurs if the date has no fractional seconds (the ".000" is not logged)
date = date[0:len(date)-4]
## extract statistics from the next several lines
nextline = file.readline()
availableConnections = nextline.split(":")[1].rstrip()
nextline = file.readline()
borrowedConnections = nextline.split(":--->")[1].rstrip()
while not nextline.startswith("getAverageConnectionWaitTime"):
nextline = file.readline()
averageWaitTime = nextline.split(":")[1].rstrip()
while not nextline.startswith("getTotalConnectionsCount"):
nextline = file.readline()
totalConnections = nextline.split(":")[1].rstrip()
## write a row of the output
#print("ts,available,borrowed,total,avgwait="+date+","+availableConnections+","+borrowedConnections+","+totalConnections+","+averageWaitTime)
csvwriter.writerow([date,availableConnections,borrowedConnections,totalConnections,averageWaitTime])
print(">>> captured "+str(count)+" measurements in file.")
# print something to screen saying you're finished
print(">>Finished processing.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment