使用RRDTOOL监控人民币汇率

     2013年03月03日       teddy.sun       运维笔记->系统管理       rrdtool rrd php 

1. 使用rrdtool建立rrd文件,对人民币对美元汇率做5分钟统计,建立AVERAGE和MAX两种类型的RRA归档
rrdtool create rmbrate.rrd \
-s 300 \
-b `date +%s` \
DS:rmbrate:GAUGE:600:U:U \
RRA:AVERAGE:0.5:1:603 \
RRA:AVERAGE:0.5:6:603 \
RRA:AVERAGE:0.5:24:603 \
RRA:AVERAGE:0.5:228:800 \
RRA:MAX:0.5:1:603 \
RRA:MAX:0.5:6:603 \
RRA:MAX:0.5:24:603 \
RRA:MAX:0.5:288:603
2. 通过Yahoo提供的汇率查询解决取得当前汇率,并存入rrd文件。
#!/bin/bash
PATH='/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'
timestamp=`date +%s`
rrdfile='/home/sandbox/rmb/rmbrate.rrd'
rmbrate=`curl 'http://download.finance.yahoo.com/d/quotes.csv?s=USDCNY=X&f=sl1d1t1&e=.csv' 2>/dev/null | awk -F, '{print $2}'`
rrdtool update $rrdfile ${timestamp}:${rmbrate}
3. 加一个crond对汇率做5分钟记录。
*/5 * * * *  /home/sandbox/rmb/rmb.sh &>/dev/null
4. 使用php脚本展示人民币汇率rrd图。
<?php
header("Content-type:image/png");
$cmd='
/usr/bin/rrdtool graph - \
-h 120 -w 500 \
--start now-1d \
-v "RMB/USD" --title "RMB-USD Rate" \
DEF:rmbrate=/home/sandbox/rmb/rmbrate.rrd:rmbrate:MAX AREA:rmbrate#00CF00FF:"RMB Rate" \
GPRINT:rmbrate:LAST:"Current\:%lf %s" \
GPRINT:rmbrate:AVERAGE:"Average\:%lf %s" \
GPRINT:rmbrate:MAX:"Max\:%lf %s" \
COMMENT:"\n"
';
passthru($cmd);
?>
最后展示的效果:
人民币汇率RRD图