Skip to content

Instantly share code, notes, and snippets.

@sammso
Last active June 6, 2017 13:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sammso/73cf7c9eec44a709d04005d803bd0f58 to your computer and use it in GitHub Desktop.
Save sammso/73cf7c9eec44a709d04005d803bd0f58 to your computer and use it in GitHub Desktop.
import groovy.io.FileType
if ( args.length < 2 ) {
println "Scan's thread dump *.log files from directory";
println "";
println "Usage:";
println "groovy ThreadDumpAnalyzer.groovy <directory> <first value to look from thread dump> <second value to look from thread dump> <third value to look from thread dump>";
println "";
return;
}
def dir = new File(args[0])
def list = []
def filters = [];
print "File name and path";
args.eachWithIndex { arg, index ->
if (index > 0) {
filters[index-1] = arg;
print String.format("\t%s",arg)
}
index++;
}
println ""
def files = dir.listFiles();
dir.eachFileRecurse (FileType.FILES) { it ->
if ( it.getName().endsWith(".log") || it.getName().endsWith(".out") || it.getName().endsWith(".txt")) {
def filename = it.getName();
def contents = [];
def resultCounts = [];
filters.eachWithIndex { filter, index ->
resultCounts[index] = 0;
}
def searchPos = 0;
// New thread dump is line which is starting with "
it.eachLine {
line->
if (line.indexOf("\"")==0 ||
line.indexOf("Full thread dump Java HotSpot(TM)")==0 ||
line.trim().length()==0) { // New dump start
for (int i=0 ; i < contents.size(); i++) {
resultCounts[i] = resultCounts[i] + 1;
}
contents = [];
}
if (filters.size() > contents.size()) {
if (line.contains(filters[contents.size()])) {
contents[contents.size()] = line;
}
}
}
for (int i=0 ; i < contents.size(); i++) {
resultCounts[i] = resultCounts[i] + 1;
}
String output = it.getAbsolutePath()
for (int i=0 ; i < resultCounts.size(); i++) {
output = output + String.format("\t%d",resultCounts[i]);
}
println output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment