Last active
June 9, 2018 02:01
-
-
Save kemsakurai/3cca7355c992c6be8665f725533f3164 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
#!/usr/bin/python | |
""" | |
top_2_csv.py | |
linux top コマンドのアウトプットをgraph出力用途にcsvに変換する | |
""" | |
__author__ = "ken sakurai" | |
__status__ = "development" | |
__version__ = "0.0.1" | |
__date__ = "17 November 2016" | |
import sys | |
import re | |
def create_cpu_summary_csv(filename, lines): | |
f = open(filename + '_cpu_summary.csv', 'w') | |
header_columns = [] | |
header_columns.append("時刻") | |
header_columns.append("ユーザが利用しているCPU使用率") | |
header_columns.append("システムが利用しているCPU使用率") | |
header_columns.append("nice値を変更しているプロセスのCPU使用率") | |
header_columns.append("待機中のCPU使用率") | |
header_columns.append("IO待ちしているCPU使用率") | |
header_columns.append("ハードウェア割り込みによるCPU使用率") | |
header_columns.append("ソフトウェア割り込みによるCPU使用率") | |
header_columns.append("ゲストOSが割り当て要求をしたが、割り当ててもらえなかったCPU使用率") | |
f.write(",".join(header_columns) + "\n") | |
for line in lines: | |
if line.find("top") == 0: | |
columns = [] | |
words = re.split(" +", line) | |
time = words[2] | |
columns.append(time) | |
elif line.find("Cpu(s):") == 0: | |
line = line.replace(",",", ") | |
words = re.split(" +", line) | |
for word in words[1:]: | |
word = word.replace("%us,","").replace("%sy,","").replace("%ni,","")\ | |
.replace("%id,","").replace("%wa,","").replace("%hi,","").replace("%si,","").replace("%st","") | |
columns.append(word) | |
f.write(",".join(columns) + "\n") | |
def create_memory_summary_csv(filename, lines): | |
f = open(filename + '_memory_summary.csv', 'w') | |
header_columns = [] | |
header_columns.append("時刻") | |
header_columns.append("合計メモリ量") | |
header_columns.append("利用メモリ量") | |
header_columns.append("空きメモリ量") | |
header_columns.append("利用メモリ量のうち、バッファ利用しているメモリ量") | |
header_columns.append("合計スワップ量") | |
header_columns.append("利用スワップ量") | |
header_columns.append("空きスワップ量") | |
header_columns.append("キャッシュしているスワップ量") | |
f.write(",".join(header_columns) + "\n") | |
for line in lines: | |
if line.find("top") == 0: | |
words = re.split(" +", line) | |
time = words[2] | |
columns = [] | |
columns.append(time) | |
elif line.find("Mem:") == 0: | |
words = re.split(" +", line) | |
for i, word in enumerate(words): | |
if i % 2 == 1: | |
word = word.replace("k","") | |
columns.append(word) | |
elif line.find("Swap:") == 0: | |
words = re.split(" +", line) | |
for i, word in enumerate(words): | |
if i % 2 == 1: | |
word = word.replace("k","") | |
columns.append(word) | |
f.write(",".join(columns) + "\n") | |
def create_load_average_csv(filename, lines): | |
f = open(filename + '_load_average.csv', 'w') | |
header_columns = [] | |
header_columns.append("時刻") | |
header_columns.append("ログインユーザ数") | |
header_columns.append("ロードアベレージ過去1分") | |
header_columns.append("ロードアベレージ過去5分") | |
header_columns.append("ロードアベレージ過去15分") | |
f.write(",".join(header_columns) + "\n") | |
for line in lines: | |
if line.find("top") == 0: | |
words = re.split(" +", line) | |
columns = [] | |
for i, word in enumerate(words): | |
if i == 2 or i == 7 or i == 11 or i == 12 or i == 13: | |
columns.append(word.replace(",","")) | |
f.write(",".join(columns) + "\n") | |
def create_task_summary_csv(filename, lines): | |
f = open(filename + '_task_summary.csv', 'w') | |
header_columns = [] | |
header_columns.append("時刻") | |
header_columns.append("タスクの合計数") | |
header_columns.append("稼働中のタスク数") | |
header_columns.append("待機中のタスク数") | |
header_columns.append("停止タスク数") | |
header_columns.append("ゾンビタスク数") | |
f.write(",".join(header_columns) + "\n") | |
for line in lines: | |
if line.find("top") == 0: | |
words = re.split(" +", line) | |
time = words[2] | |
columns = [] | |
columns.append(time) | |
elif line.find("Tasks:") == 0: | |
words = re.split(" +", line) | |
for i, word in enumerate(words): | |
if i % 2 == 1: | |
columns.append(word) | |
f.write(",".join(columns) + "\n") | |
def create_procees_id_csv(filename, lines, pid): | |
f = open(filename + '_' + str(pid) + '.csv', 'w') | |
header_columns = [] | |
header_columns.append("時刻") | |
header_columns.append("PID") | |
header_columns.append("USER") | |
header_columns.append("優先度") | |
header_columns.append("相対優先度") | |
header_columns.append("仮想メモリ") | |
header_columns.append("物理メモリ") | |
header_columns.append("共有メモリ") | |
header_columns.append("状態") | |
header_columns.append("CPU使用率") | |
header_columns.append("メモリ使用率") | |
header_columns.append("実行時間") | |
header_columns.append("コマンド") | |
f.write(",".join(header_columns) + "\n") | |
for line in lines: | |
if line.find("top") == 0: | |
columns = [] | |
words = re.split(" +", line) | |
time = words[2] | |
columns.append(time) | |
else: | |
replaced_line = re.sub("^ +","",line) | |
if replaced_line.find(pid) == 0: | |
words = re.split(" +", replaced_line) | |
columns.extend(words) | |
f.write(",".join(columns) + "\n") | |
def print_usage(): | |
print "-----------------------------------------------" | |
print "Usage: python top_2csv.py {1} {2}" | |
print "arguments:" | |
print "{1}: top result file name" | |
print "{2}: mode 'SUMMARY' or 'PID (digit like 1234)'" | |
print "-----------------------------------------------" | |
if __name__ == '__main__': | |
filename = sys.argv[1] | |
try: | |
mode = sys.argv[2] | |
except IndexError: | |
print_usage() | |
raise | |
lines = [line.rstrip('\n') for line in open(filename)] | |
if mode == "SUMMARY": | |
create_cpu_summary_csv(filename, lines) | |
create_memory_summary_csv(filename, lines) | |
create_load_average_csv(filename, lines) | |
create_task_summary_csv(filename, lines) | |
elif mode.isdigit(): | |
create_procees_id_csv(filename, lines, mode) | |
else: | |
print_usage() | |
raise | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment