咱們都知道gnuplot能夠繪製圖片,可是時候,咱們的數據是以date文件的形式存在,如何編寫一個腳本幫助咱們繪製出相應的圖片:
以某個進程的CPU使用狀況爲例:
css
catprocessX_CPU_Usage.log
shell
WedOct1614:47:35CST2013:ProcessCPUsage(%)Total
bash
WedOct1614:47:37CST2013:1828172008
ide
WedOct1614:47:39CST2013:1828171706
spa
WedOct1614:47:42CST2013:1828121689
code
WedOct1614:47:44CST2013:1828211673
進程
WedOct1614:47:46CST2013:1828151668
圖片
WedOct1614:47:48CST2013:182881699
terminal
WedOct1614:47:50CST2013:1828171699
it
WedOct1614:47:53CST2013:1828172148
WedOct1614:47:55CST2013:1828151815
WedOct1614:47:57CST2013:1828321727
WedOct1614:47:59CST2013:1828111669
WedOct1614:48:01CST2013:182841667
WedOct1614:48:03CST2013:182881678
WedOct1614:48:05CST2013:1828161694
WedOct1614:48:08CST2013:182891669
WedOct1614:48:10CST2013:1828261750
WedOct1614:48:12CST2013:182861792
WedOct1614:48:14CST2013:1828141720
WedOct1614:48:17CST2013:1828121800
WedOct1614:48:19CST2013:1828241921
WedOct1614:48:21CST2013:1828111749
WedOct1614:48:23CST2013:182891708
WedOct1614:48:25CST2013:1828151665
WedOct1614:48:27CST2013:182831751
WedOct1614:48:30CST2013:1828101692
WedOct1614:48:32CST2013:1828151678
WedOct1614:48:34CST2013:1828191708
WedOct1614:48:36CST2013:1828141707
WedOct1614:48:38CST2013:1828151680
WedOct1614:48:40CST2013:1828181678
咱們看到了輸出是這麼個狀況,咱們但願繪製一張簡單的圖片,把CPUusage的狀況繪製出來:每一行的第八列是咱們想要的數據,咱們能夠繪製一個時間序列:
manu@manu-hacks:~/code/shell/gnuplot$catwork.sh
#!/bin/sh file=$1 field=$2 yfield=`cat $file |awk '{if(NR==1) print $'''$field'''}'` title=`basename $file` if [ $# -eq 2 ] then echo " set terminal pngcairo lw 2 set title \"$title\" set ylabel \"$yfield\" set output './output/$title-$yfield.png' plot \"$file\" using $field w lp pt 7 title \"$yfield\" set output set terminal wxt " | gnuplot fi
我這個方法比較簡單,title就是數據來源的文件名字,ylabel是字段名字,實際上,由用戶輸入titleylabel以及圖例的title會更加合適。我這只是一個簡單的sample,來分享如何在shell腳本利用gnuplot繪製圖片。
使用命令以下:
./work.sh/home/manu/processX_CPU_Usage.log8
輸出圖片以下:我這是一個簡單的例子,實際上,咱們實際可能會跑出來40個進程的CPU使用狀況,咱們能夠將參數寫入配置文件,而後每一行去執行work.sh,這樣的話,咱們就能夠直接維護配置文件就能夠了。固然了,個人腳本還很簡陋很粗糙粗,遠遠不能在工程中實際使用。畢竟是晚上搗鼓出來的小玩意兒。