利用gnuplot繪圖來監測系統資訊:
作者:衡山飛狐
出處:HappyLinux,Just for fun!
本 著作 係採用 Creative Commons 授權條款授權.
1.1 gnuplot介紹:
gnuplot 是一個命令導向的交談式的繪圖軟體,在 1986 年由 Colin Kelley 和 Thomas Williams 發展而成,gnuplot 的功能就是把數值資料和數學函數轉換成容易觀察的平面或立體的圖形,gnuplot 並不是一般常見的美工繪圖軟體,也不是從事數學運算的程式,它最適合的是在科學研究的過程中,代替研究人員完成數據資料繪製與理論模型比對等機械化的工作,來加速研究的進行。摘錄自http://phi./aspac/reports/95/95006/chap1.html#1.1
1.2 利用MRTG繪圖限制很多:
如果用MRTG來繪圖,MRTG只能處理2個值及整數部份,例如在我的系統裡用lm_sensors所測得的電壓資料就有VCore 1、VCore 2、+3.3V、+5V、+12V、-12V、-5V、V5SB、VBat九種電壓值,用MRTG就無法呈現在一張圖中了,而且小數點後的值是無效的,例如我的MRTGVCore 1、VCore 2(X100倍)所顯示的圖形:

再來比較利用gnuplot所產生的圖形:

所以如果用MRTG來繪圖,限制很多,圖形又千篇一律。
2.1 需要的工具:
如果系統中沒有安裝gnuplot,請安裝。
如果系統中沒有安裝lm_sensors(Hardware Sensors Monitoring),請安裝。
2.2 利用gnuplot產生系統資訊圖表的兩個例子:
2.2.1:CPU及及系統電壓
在本系統中,當下了sensors的指令,所顯示的資訊如下:
eeprom-i2c-0-50
Adapter: SMBus nForce2 adapter at 5000
Memory type: DDR SDRAM DIMM
Memory size (MB): 256w83627hf-isa-0290
Adapter: ISA adapter
VCore 1: +1.58 V (min = +1.89 V, max = +2.29 V)
VCore 2: +1.58 V (min = +1.89 V, max = +2.29 V)
+3.3V: +2.59 V (min = +2.82 V, max = +3.79 V)
+5V: +4.92 V (min = +4.38 V, max = +4.30 V)
+12V: +11.93 V (min = +13.91 V, max = +4.26 V)
-12V: -7.20 V (min = -11.47 V, max = -3.40 V)
-5V: -5.39 V (min = +4.76 V, max = +0.39 V)
V5SB: +5.46 V (min = +2.82 V, max = +5.75 V)
VBat: +3.10 V (min = +1.42 V, max = +1.62 V)
fan1: 2045 RPM (min = 8881 RPM, div = 4)
fan2: 0 RPM (min = 2836 RPM, div = 4)
fan3: 3375 RPM (min = 969 RPM, div = 8 )
temp1: +37°C (high = +69°C, hyst = -108°C) sensor = thermistor
temp2: +54.5°C (high = +100°C, hyst = +95°C) sensor = thermistor
temp3: +37.5°C (high = +100°C, hyst = +95°C) sensor = thermistor
vid: +2.100 V
alarms:
beep_enable:
Sound alarm disabled
其中的VCore 1、VCore 2、+3.3V、+5V、+12V、-12V、-5V、V5SB、VBat電壓值變化圖就是本小節所要產生的。
首先寫個shell來產生給gnuplot的data檔。
gnuplot_v.sh
#!/bin/sh
VCore_1=`sensors | grep "VCore 1" | awk ‘{print $3}‘| cut -c 2-5 `
VCore_2=`sensors | grep "VCore 2" | awk ‘{print $3}‘| cut -c 2-5 `
p_3_3V=`sensors | grep "+3.3V" | awk ‘{print $2}‘| cut -c 2-5 `
p_5V=`sensors | grep "+5V" |awk ‘{print $2}‘| cut -c 2-5 `
p_12V=`sensors | grep "+12V" |awk ‘{print $2}‘| cut -c 2-5 `
m_12V=`sensors | grep "\-12V" |awk ‘{print $2}‘| cut -c 1-5 `
m_5V=`sensors | grep "\-5V" |awk ‘{print $2}‘| cut -c 1-5 `
V5SB=`sensors | grep "V5SB" | awk ‘{print $2}‘| cut -c 2-5 `
VBat=`sensors | grep "VBat" | awk ‘{print $2}‘| cut -c 2-5 `
time=`date +%Y%m%d%H%M%S`
echo "$time $VCore_1 $VCore_2 $p_3_3V $p_5V $p_12V $m_12V $m_5V $V5SB $VBat" >> /usr/share/sensor/gnuplot_v.data
gnuplot /usr/share/sensor/gnuplot_v.script > /var/www/localhost/htdocs/gnuplot_v.png
再來寫個gnuplot的script檔
gnuplot_v.script
###################################
# gnuplot script for CPU & System Volte.
# http://www./
# Author:flyfox@gen2.
# File created: Fri JUL 16 2005
# Last updated: Sat JUL 17 2005
###################################
set term png
set timefmt "%Y%m%d%H%M"
set notime
set data style lines
set grid
set autoscale x
set autoscale y
set yrange [-10 : 15]
set mytics 5
set title "CPU & System volte"
set xlabel "Time"
set xdata time
set ylabel "Volte"
set timestamp "Created: %b %d, %Y at %H:%M" top 12,-4.5
plot "/usr/share/sensor/gnuplot_v.data" using 1:2 title ‘VCore_1‘,\
"/usr/share/sensor/gnuplot_v.data" using 1:3 title ‘VCore_2‘,\
"/usr/share/sensor/gnuplot_v.data" using 1:4 title ‘+3.3V‘,\
"/usr/share/sensor/gnuplot_v.data" using 1:5 title ‘+5V‘,\
"/usr/share/sensor/gnuplot_v.data" using 1:6 title ‘+12V‘,\
"/usr/share/sensor/gnuplot_v.data" using 1:7 title ‘-12V‘,\
"/usr/share/sensor/gnuplot_v.data" using 1:8 title ‘-5V‘,\
"/usr/share/sensor/gnuplot_v.data" using 1:9 title ‘V5SB‘,\
"/usr/share/sensor/gnuplot_v.data" using 1:10 title ‘VBat‘
排入crontab排程
*/5 * * * * /usr/bin/gnuplot_v.sh > /dev/null 2>&1
CPU及及系統電壓圖例:

2.2.2:CPU及系統溫度
本例則擷取temp1、temp2及temp3三個DATA來產生CPU及系統溫度圖表。
首先寫個shell來產生給gnuplot的data檔。
gnuplot_cpu_temp.sh
#!/bin/sh
systemp_1=`sensors | grep temp3 |awk ‘{print $2}‘|cut -c 2-5`
cputemp=`sensors | grep temp2 |awk ‘{print $2}‘|cut -c 2-5`
systemp_2=`sensors | grep temp1 |awk ‘{print $2}‘|cut -c 2-3`
time=`date +%Y%m%d%H%M%S`
echo "$time $cputemp $systemp_1 $systemp_2" >> /usr/share/sensor/gnuplot_temp.data
gnuplot /usr/share/sensor/gnuplot_temp.script > /var/www/localhost/htdocs/gnuplot_temp.png
再來寫個gnuplot的script檔:
gnuplot_temp.script
###################################
# gnuplot script for CPU & System Temperature.
# http://www./
# Author:flyfox@gen2.
# File created: Fri JUL 16 2005
# Last updated: Sat JUL 17 2005
###################################
set term png
set timefmt "%Y%m%d%H%M"
set notime
set data style lines
set grid
set autoscale x
set autoscale y
set yrange [30 : 65]
set mytics 5
set title "CPU & System Temperature"
set xlabel "Time"
set xdata time
set ylabel "CelsiusDegree"
set timestamp "Created: %b %d, %Y at %H:%M" top 12,-4.5
plot "/usr/share/sensor/gnuplot_temp.data" using 1:2 title ‘CPU Temperature‘,\
"/usr/share/sensor/gnuplot_temp.data" using 1:3 title ‘System_2 Temperature‘,\
"/usr/share/sensor/gnuplot_temp.data" using 1:4 title ‘System_1 Temperature‘
CPU & System溫度圖例:
再秀一張CPU效能的圖例:
3.1 gnuplot所繪製的系統監測圖呈現的方法:
本站則採取與XOOPS模組化的phpsysinfo來呈現gnuplot所繪製的系統監測圖。
展示網頁:系統監測圖
http://www./modules/phpsysinfo/