1、使用rrdtool建立rrd文件,对人民币对美元汇率每20秒进行一次抓取(setp = 20s)

1rrdtool create USDCNY.rrd                       \
2        --start `date +%s`                      \
3        --step 20                             \
4        DS:content:GAUGE:100:0:U              \
5        RRA:AVERAGE:0:1:400

2、 通过Yahoo提供的汇率查询解决取得当前汇率,并存入rrd文件

 1#!/bin/bash
 2PATH='/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'
 3rrdfile='/tmp/361way/USDCNY.rrd'
 4while true
 5do
 6timestamp=`date +%s`
 7rmbrate=`curl 'http://download.finance.yahoo.com/d/quotes.csv?s=USDCNY=X&f=sl1d1t1&e=.csv' 2>/dev/null | awk -F, '{print $2}'`
 8rrdtool update $rrdfile ${timestamp}:${rmbrate}
 9sleep 19
10echo 'get now'
11done

3、使用php脚本展示人民币汇率rrd图

 1# cat rmb.php
 2<?php header("Content-type:image/png");
 3$cmd='
 4/usr/bin/rrdtool graph - \
 5-h 120 -w 500 \
 6--start now-1h \
 7-v "RMB/USD" --title "RMB-USD Rate" \
 8DEF:rmbrate=/tmp/361way/USDCNY.rrd:content:AVERAGE \
 9LINE:rmbrate#00CF00FF:"RMB Rate" \
10COMMENT:"\n"
11';
12passthru($cmd);
13??>

效果图如下:

usbcny

参考页面:http://www.vandenbogaerdt.nl/rrdtool/after_the_fact.php