| 24 | |
| 25 | == sarを表示 |
| 26 | |
| 27 | {{{ |
| 28 | $ sar -A -o test.sar 1 10 |
| 29 | }}} |
| 30 | |
| 31 | などで取得したsarを表示するサンプルは、こんな感じ。 |
| 32 | |
| 33 | |
| 34 | {{{ |
| 35 | %matplotlib inline |
| 36 | |
| 37 | import subprocess |
| 38 | from subprocess import Popen,PIPE |
| 39 | from datetime import datetime |
| 40 | |
| 41 | import matplotlib.pyplot as plt |
| 42 | from matplotlib.font_manager import FontProperties |
| 43 | import matplotlib.dates as mdates |
| 44 | |
| 45 | def readSarCPU(f): |
| 46 | args = ['/bin/bash','-c', "sadf -T -- -C " + f + " |awk '{print $3,$4,$6,$7}'"] |
| 47 | p = Popen(args,stdout=PIPE,stderr=PIPE) |
| 48 | out,err = p.communicate() |
| 49 | cpu = {'%user': [], '%nice': [], '%system': [], '%iowait': [], '%steal': [], '%idle': []} |
| 50 | timestamp = [] |
| 51 | x = 0 |
| 52 | prev = "" |
| 53 | for l in out.split('\n'): |
| 54 | if l == "": |
| 55 | break; |
| 56 | r = l.split(" ") |
| 57 | |
| 58 | if prev != r[1]: |
| 59 | timestamp.append(datetime.strptime(r[0]+" "+r[1], '%Y-%m-%d %H:%M:%S')) |
| 60 | prev = r[1] |
| 61 | x = x+1 |
| 62 | cpu[r[2]].append(float(r[3])) |
| 63 | return (timestamp, cpu) |
| 64 | |
| 65 | |
| 66 | |
| 67 | def plot(x, data, title="",xlabel="",ylabel=""): |
| 68 | |
| 69 | fp = FontProperties(fname='/usr/share/fonts/ipa-gothic/ipag.ttf') |
| 70 | |
| 71 | if title!="": |
| 72 | plt.title(title, fontproperties=fp) |
| 73 | if xlabel!="": |
| 74 | plt.xlabel(xlabel,fontproperties=fp) |
| 75 | if ylabel!="": |
| 76 | plt.ylabel(ylabel,fontproperties=fp) |
| 77 | |
| 78 | plt.xticks(rotation=30) |
| 79 | xfmt = mdates.DateFormatter('%m/%d %H:%M:%S') |
| 80 | ax = plt.gca() |
| 81 | ax.xaxis.set_major_formatter(xfmt) |
| 82 | |
| 83 | for k in cpu.keys(): |
| 84 | plt.plot(x,cpu[k], label=k) |
| 85 | |
| 86 | plt.legend(prop=fp,bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) |
| 87 | plt.show() |
| 88 | |
| 89 | timestamp, cpu = readSarCPU("test.sar") |
| 90 | plot(x=timestamp, data=cpu,ylabel=u"%",xlabel=u"time") |
| 91 | }}} |