Skip to content

Instantly share code, notes, and snippets.

@bvcelari
Created July 3, 2018 06:47
Show Gist options
  • Save bvcelari/1de55f8ff3db007cdc2c21b7e2fcb4c4 to your computer and use it in GitHub Desktop.
Save bvcelari/1de55f8ff3db007cdc2c21b7e2fcb4c4 to your computer and use it in GitHub Desktop.
#Script has one input paramerter, folder where are the top and jstack files
#Script will get all the content from PATH, look for files with topdashHOutput name, check the CPU_USAGE_COLUMN and when the value is higher than HIGH_CPU will chck in the jstak files for the Hex code
import rlcompleter, readline
import sys
import subprocess
print 'Searching in ... :'+ sys.argv[1]
readline.parse_and_bind('tab:complete')
PATH = sys.argv[1]
HIGH_CPU = 90
CPU_USAGE_COLUMN = 8
PID_COLUMN = 0
STRINGS_TO_IGNORE = ['GC task thread',]
# get folder info
task = subprocess.Popen('ls '+PATH,
shell=True,
stdout=subprocess.PIPE)
directory_raw_content = task.stdout.read()
directory_content = directory_raw_content.split('\n')
pids_to_check = []
######Obtain high cpu from top.. topdashHOutput.XXXX.txt
print directory_content
for topdashfile in directory_content:
#if 'mytest' in topdashfile:
if 'topdashHOutput' in topdashfile:
print "checking high cpu in "+topdashfile
f_topdashfile = open(PATH+topdashfile)
line = f_topdashfile.readline()
while line:
fields = line.split()
if len(fields) >= CPU_USAGE_COLUMN:
#check if it's a float
try:
#if the number it's less 100 to avoid bad headers from top
if float(fields[CPU_USAGE_COLUMN]) > HIGH_CPU and float(fields[CPU_USAGE_COLUMN]) < 101:
pids_to_check.append(fields[PID_COLUMN])
except ValueError:
None
#print fields[CPU_USAGE_COLUMN]
line = f_topdashfile.readline()
f_topdashfile.close()
hex_pid_to_check = []
#hexadecimalize it...
for pid in pids_to_check:
hex_pid_to_check.append(hex(int(pid)))
coincidences = []
######Obtain jstack
for entry in directory_content:
if 'jstack' in entry:
print 'Analizing...'+entry
jstack_file = open(PATH+entry, "r")
jstack_line = jstack_file.readline()
while jstack_line:
if 'nid' in jstack_line:
#black magiC that check all the array of HEX pids in each line of the jstack
if [e for e in hex_pid_to_check if e in jstack_line]:
#black magiC that exclude known strings
if not [l for l in STRINGS_TO_IGNORE if l in jstack_line]:
#I would like to retrieve here the whole stack
fullstack = jstack_line
jstack_line = jstack_file.readline()
#this is ugle as Hell
while jstack_line and 'Locked ownable synchronizers:' not in jstack_line:
fullstack += jstack_line
#print "we are seeking: ######3 :"+jstack_line+" 3###"
jstack_line = jstack_file.readline()
coincidences.append(fullstack)
jstack_line = jstack_file.readline()
jstack_file.close()
for i in coincidences:
print i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment