咱们都知道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,这样的话,咱们就能够直接维护配置文件就能够了。固然了,个人脚本还很简陋很粗糙粗,远远不能在工程中实际使用。毕竟是晚上捣鼓出来的小玩意儿。