您的位置:首页 > 运维架构 > Shell

shell脚本利用gnuplot绘制图片

2013-10-23 14:15 429 查看
我们都知道gnuplot可以绘制图片,但是时候,我们的数据是以date文件的形式存在,如何编写一个脚本帮助我们绘制出相应的图片:
以某个进程的CPU使用情况为例:

catprocessX_CPU_Usage.log

WedOct1614:47:35CST2013:ProcessCPUsage(%)Total

WedOct1614:47:37CST2013:1828172008

WedOct1614:47:39CST2013:1828171706

WedOct1614:47:42CST2013:1828121689

WedOct1614:47:44CST2013:1828211673

WedOct1614:47:46CST2013:1828151668

WedOct1614:47:48CST2013:182881699

WedOct1614:47:50CST2013:1828171699

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,这样的话,我们就可以直接维护配置文件就可以了。当然了,我的脚本还很简陋很粗糙粗,远远不能在工程中实际使用。毕竟是晚上捣鼓出来的小玩意儿。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: