Last modified 8 years ago
Jupyter Notebookメモ
CentOS7へのインストール。折角だからPython3系を利用してみる。 bashカーネルも入れておく。
yum install -y epel-release yum install -y python34 curl -L https://bootstrap.pypa.io/get-pip.py | python3 pip3 install jupyter pip3 install bash_kernel python3 -m bash_kernel.install jupyter-notebook
外部からアクセスするときは、下記のファイルを適宜編集。
~/.jupyter/jupyter_notebook_config.py
c.NotebookApp.ip = '0.0.0.0' c.NotebookApp.port = 8888
sarを表示
$ sar -A -o test.sar 1 10
などで取得したsarを表示するサンプルは、こんな感じ。
%matplotlib inline import subprocess from subprocess import Popen,PIPE from datetime import datetime import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties import matplotlib.dates as mdates def readSarCPU(f): args = ['/bin/bash','-c', "sadf -T -- -C " + f + " |awk '{print $3,$4,$6,$7}'"] p = Popen(args,stdout=PIPE,stderr=PIPE) out,err = p.communicate() cpu = {'%user': [], '%nice': [], '%system': [], '%iowait': [], '%steal': [], '%idle': []} timestamp = [] x = 0 prev = "" for l in out.split('\n'): if l == "": break; r = l.split(" ") if prev != r[1]: timestamp.append(datetime.strptime(r[0]+" "+r[1], '%Y-%m-%d %H:%M:%S')) prev = r[1] x = x+1 cpu[r[2]].append(float(r[3])) return (timestamp, cpu) def plot(x, data, title="",xlabel="",ylabel=""): fp = FontProperties(fname='/usr/share/fonts/ipa-gothic/ipag.ttf') if title!="": plt.title(title, fontproperties=fp) if xlabel!="": plt.xlabel(xlabel,fontproperties=fp) if ylabel!="": plt.ylabel(ylabel,fontproperties=fp) plt.xticks(rotation=30) xfmt = mdates.DateFormatter('%m/%d %H:%M:%S') ax = plt.gca() ax.xaxis.set_major_formatter(xfmt) for k in cpu.keys(): plt.plot(x,cpu[k], label=k) plt.legend(prop=fp,bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plt.show() timestamp, cpu = readSarCPU("test.sar") plot(x=timestamp, data=cpu,ylabel=u"%",xlabel=u"time")
Jupyter Notebook + HighCharts?
Jupyter Notebook + HighCharts?でリッチなチャートを表示する例です。
python-highchartsをpipでインストールしておく必要があります。
import subprocess from subprocess import Popen,PIPE from datetime import datetime from highcharts import Highchart H = Highchart(width=640, height=400) def readSarCPU(f): args = ['/bin/bash','-c', "sadf -T -- -C " + f + " |awk '{print $3,$4,$6,$7}'"] p = Popen(args,stdout=PIPE,stderr=PIPE) out,err = p.communicate() cpu = {'%user': [], '%nice': [], '%system': [], '%iowait': [], '%steal': [], '%idle': []} timestamp = [] x = 0 prev = "" for l in out.decode('utf8').split('\n'): if l == "": break; r = l.split(" ") if prev != r[1]: timestamp.append(datetime.strptime(r[0]+" "+r[1], '%Y-%m-%d %H:%M:%S')) prev = r[1] x = x+1 cpu[r[2]].append(float(r[3])) return (timestamp, cpu) def plot(x, data, highchart, title="",xlabel="",ylabel=""): options = { 'title': { 'text': 'CPU Usage' }, 'subtitle': { 'text': 'Source: test.sar' }, 'xAxis': { 'type': 'datetime', 'minRange': 1000*len(x) }, 'yAxis': { 'title': { 'text': ylabel }, 'labels': { 'formatter': 'function () {\ return this.value ;\ }' }, 'max': 100 }, 'tooltip': { 'shared': True, 'valueSuffix': ylabel }, 'plotOptions': { 'area': { 'stacking': 'normal', 'lineColor': '#666666', 'lineWidth': 1, 'marker': { 'lineWidth': 1, 'lineColor': '#666666' } } } } highchart.set_dict_options(options) for k in data.keys(): highchart.add_data_set(data[k],'area',k,pointInterval=1000, pointStart=x[0]) timestamp, cpu = readSarCPU("test.sar") plot(x=timestamp, data=cpu,ylabel=u"%",xlabel=u"time",highchart=H) H