今日、プロセスのCPU使用率とかメモリ使用量をグラフにしようと、5秒ごとにps auxを取るシェルを作った。
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND -------- 2012/4/12 23:00:00 hoge 895 2.0 0.1 904 1288 ?? S 11:33PM 0:00.02 /usr/loc root 1 0.0 0.0 552 208 ?? SLs 7Oct03 0:18.52 /sbin/in root 2 0.0 0.0 0 0 ?? DL 7Oct03 2:50.83 (pageda root 3 0.0 0.0 0 0 ?? DL 7Oct03 0:00.32 (vmdaem root 4 0.0 0.0 0 0 ?? DL 7Oct03 1:19.56 (bufdae root 5 0.6 0.0 0 0 ?? DL 7Oct03 1:28.35 (syncer (略) 2012/4/12 23:00:05 hoge 895 2.0 0.1 904 1288 ?? S 11:33PM 0:00.02 /usr/loc 以下略
↑こういう感じのファイルが取れたので、これをエクセルでグラフにする時に便利な様に、特定プロセスのだけ抜き出して
proc=loc DateTime , #CPU , VZS , RSS , TIME ---------------------------------- 2012/4/12 23:00:00 , 2.0 , 904 , 1288 , 0:00.02 2012/4/12 23:00:05 , 2.0 , 904 , 1288 , 0:00.02 以下略
↑こういう形に変形したかった。
# -*- coding: utf-8 -*- import glob import sys procname="" sampletime="" def parce(line,fw): #print(procname+" "+line) global sampletime if line.find("2012/")>=0: sampletime = line.strip() #print(sampletime,line) else: if line.find(procname)>0: items = line.split() # 時刻 , #CPU , VZS , RSS , TIME outtext = sampletime+" , "+items[2] +" , "+items[4]+" , "+items[5]+" , "+items[9] print(outtext) fw.write(outetext+"\n") #<-----ここ20行目outtextをoutetextとタイプミスしてる def exec1file(filename): outfilename=procname+"_"+filename+".csv" print("inputfile= "+filename+" outputfile="+outfilename) fw =open(outfilename,"w") fw.write("proc="+procname+"\n") fw.write("DateTime , #CPU , VZS , RSS , TIME\n") fw.write("----------------------------------\n") f = open(filename) for line in f.readlines(): parce(line,fw) f.close() fw.close() #メイン #global procname argvs = sys.argv procname = argvs[1] print("prcname="+procname) filenames = glob.glob( "*.txt" ) for filename in filenames: try: print( "file="+filename ) exec1file( filename ) except: print( "Error="+file ) traceback.print_exc()
というPythonファイルを作った。
fw.write(outetext+"\n") #<-----ここ20行目outtextをoutetextとタイプミスしてる
をタイプミスしてな。
で、実行するとエラーが出るんだが、
prcname=loc file=input.txt inputfile= input.txt outputfile=loc_input.txt.csv 2012/4/12 23:00:00 , 2.0 , 904 , 1288 , 0:00.02 Traceback (most recent call last): File "test.py", line 45, in <module> During handling of the above exception, another exception occurred: Traceback (most recent call last): File "test.py", line 47, in <module>
表示されるエラー行が、問題の20行目にはかすりもしない。結構print入れて探し回ってしまった。
Javaなら実行時どころか、Eclipseで入力する傍からoutetextの部分に赤く下線が引かれて検出される所。
まぁJavaだと手元で作ってjarにして…と面倒は面倒だが。
コメント