使用 php rrdtool 工具畫機房溫度圖

建立 rrd 檔案. 然後更新溫度資訊到 rrd 檔案內 :
initial rrd file and update temperature into rrd file :

// update.php
$rrd_file = "r733.rrd";

function init_rrd() {
    global $rrd_file;

    $opts = array("--step", "300", "--start", 0,
                 "DS:Battery:GAUGE:600:1:100",
                 "DS:EMU:GAUGE:600:1:100",
                 "RRA:AVERAGE:0.5:1:600",
                 "RRA:AVERAGE:0.5:6:700",
                 "RRA:AVERAGE:0.5:24:775",
                 "RRA:AVERAGE:0.5:288:797",
                 "RRA:MAX:0.5:1:600",
                 "RRA:MAX:0.5:6:700",
                 "RRA:MAX:0.5:24:775",
                 "RRA:MAX:0.5:288:797");
    $rtn = rrd_create($rrd_file, $opts, count($opts));

    if ($rtn == 0) {
        $err = rrd_error();
        exit("ERROR : $err\n");
    }
    
} // end function init_rrd()

function update_rrd() {
    global $rrd_file;
    $battery_cmd = "/usr/bin/snmpwalk -v 1 -c public 192.168.170.250 1.3.6.1.4.1.318.1.1.1.2.2.2.0";
    $emu_cmd = "/usr/bin/snmpwalk -v 1 -c public 192.168.170.250 1.3.6.1.4.1.318.1.1.10.2.3.2.1.4.1";
    $flag = true;

    while($flag) {
        $battery_temperature = "";
        $emu_temperature = "";
        while($battery_temperature == "") {
            $output = shell_exec($battery_cmd);
            $battery_temperature = trim(str_replace("SNMPv2-SMI::enterprises.318.1.1.1.2.2.2.0 = Gauge32:", "", $output));
            if ($battery_temperature == "") sleep(5);
        }
        
        while($emu_temperature == "") {
            $output = shell_exec($emu_cmd);
            $emu_temperature = trim(str_replace("SNMPv2-SMI::enterprises.318.1.1.10.2.3.2.1.4.1 = INTEGER:", "", $output));
            if ($emu_temperature == "") sleep(5);
        }
        $battery_temperature = (int)$battery_temperature;
        $emu_temperature = (int)$emu_temperature;
        $rtn = rrd_update($rrd_file, "N:$battery_temperature:$emu_temperature");
        if ($rtn == 0) {
            $err = rrd_error();
            exit("ERROR : $err\n");
        }
        
        if (isset($_GET["t"])) {
            sleep(300);
        } else {
            $flag = false;
        }
    } // end while(true)

} // end function update_rrd()

if (file_exists($rrd_file)) {
    update_rrd();
} else {
    init_rrd();
    update_rrd();
}

畫 rrd 圖 :
draw rrd graphs :

// index.php
$rrd_file = "r733.rrd";

include_once "update_r733.php";

function draw_rrd() {
    global $rrd_file;

    $opts = array( "--start", "-1h", "--vertical-label=deg C", "--title=R733 Temperature last hour",
                   "DEF:battery=".$rrd_file.":Battery:AVERAGE",
                   "DEF:emu=".$rrd_file.":EMU:AVERAGE",
                   "COMMENT:                                           Max    Average    Last\\n",
                   "AREA:battery#99CC00:Battery \:",
                   "GPRINT:battery:MAX:%2.0lfdeg C",
                   "GPRINT:battery:AVERAGE:%2.0lfdeg C",
                   "GPRINT:battery:LAST:%2.0lfdeg C\\r",
                   "LINE2:emu#6699CC:EMU \:",
                   "GPRINT:emu:MAX:%2.0lfdeg C",
                   "GPRINT:emu:AVERAGE:%2.0lfdeg C",
                   "GPRINT:emu:LAST:%2.0lfdeg C\\r"
               );

    $ret = rrd_graph("r733_1h.gif", $opts, count($opts));

    if(!is_array($ret)) {
        $err = rrd_error();
        exit("draw_rrd() ERROR: $err\n");
    }

    $opts = array( "--start", "-1d", "--vertical-label=deg C", "--title=R733 Temperature last day",
                   "DEF:battery=".$rrd_file.":Battery:AVERAGE",
                   "DEF:emu=".$rrd_file.":EMU:AVERAGE",
                   "COMMENT:                                           Max    Average    Last\\n",
                   "AREA:battery#99CC00:Battery \:",
                   "GPRINT:battery:MAX:%2.0lfdeg C",
                   "GPRINT:battery:AVERAGE:%2.0lfdeg C",
                   "GPRINT:battery:LAST:%2.0lfdeg C\\r",
                   "LINE2:emu#6699CC:EMU \:",
                   "GPRINT:emu:MAX:%2.0lfdeg C",
                   "GPRINT:emu:AVERAGE:%2.0lfdeg C",
                   "GPRINT:emu:LAST:%2.0lfdeg C\\r"
               );

    $ret = rrd_graph("r733_1d.gif", $opts, count($opts));

    if(!is_array($ret)) {
        $err = rrd_error();
        exit("draw_rrd() ERROR: $err\n");
    }

    $opts = array( "--start", "-1w", "--vertical-label=deg C", "--title=R733 Temperature last week",
                   "DEF:battery=".$rrd_file.":Battery:AVERAGE",
                   "DEF:emu=".$rrd_file.":EMU:AVERAGE",
                   "COMMENT:                                           Max    Average    Last\\n",
                   "AREA:battery#99CC00:Battery \:",
                   "GPRINT:battery:MAX:%2.0lfdeg C",
                   "GPRINT:battery:AVERAGE:%2.0lfdeg C",
                   "GPRINT:battery:LAST:%2.0lfdeg C\\r",
                   "LINE2:emu#6699CC:EMU \:",
                   "GPRINT:emu:MAX:%2.0lfdeg C",
                   "GPRINT:emu:AVERAGE:%2.0lfdeg C",
                   "GPRINT:emu:LAST:%2.0lfdeg C\\r"
               );

    $ret = rrd_graph("r733_1w.gif", $opts, count($opts));

    if(!is_array($ret)) {
        $err = rrd_error();
        exit("draw_rrd() ERROR: $err\n");
    }
    
    $opts = array( "--start", "-1m", "--vertical-label=deg C", "--title=R733 Temperature last month",
                   "DEF:battery=".$rrd_file.":Battery:AVERAGE",
                   "DEF:emu=".$rrd_file.":EMU:AVERAGE",
                   "COMMENT:                                           Max    Average    Last\\n",
                   "AREA:battery#99CC00:Battery \:",
                   "GPRINT:battery:MAX:%2.0lfdeg C",
                   "GPRINT:battery:AVERAGE:%2.0lfdeg C",
                   "GPRINT:battery:LAST:%2.0lfdeg C\\r",
                   "LINE2:emu#6699CC:EMU \:",
                   "GPRINT:emu:MAX:%2.0lfdeg C",
                   "GPRINT:emu:AVERAGE:%2.0lfdeg C",
                   "GPRINT:emu:LAST:%2.0lfdeg C\\r"
               );

    $ret = rrd_graph("r733_1m.gif", $opts, count($opts));

    if(!is_array($ret)) {
        $err = rrd_error();
        exit("draw_rrd() ERROR: $err\n");
    }

    $opts = array( "--start", "-1y", "--vertical-label=deg C", "--title=R733 Temperature last year",
                   "DEF:battery=".$rrd_file.":Battery:AVERAGE",
                   "DEF:emu=".$rrd_file.":EMU:AVERAGE",
                   "COMMENT:                                           Max    Average    Last\\n",
                   "AREA:battery#99CC00:Battery \:",
                   "GPRINT:battery:MAX:%2.0lfdeg C",
                   "GPRINT:battery:AVERAGE:%2.0lfdeg C",
                   "GPRINT:battery:LAST:%2.0lfdeg C\\r",
                   "LINE2:emu#6699CC:EMU \:",
                   "GPRINT:emu:MAX:%2.0lfdeg C",
                   "GPRINT:emu:AVERAGE:%2.0lfdeg C",
                   "GPRINT:emu:LAST:%2.0lfdeg C\\r"
               );

    $ret = rrd_graph("r733_1y.gif", $opts, count($opts));

    if(!is_array($ret)) {
        $err = rrd_error();
        exit("draw_rrd() ERROR: $err\n");
    }
} // end function draw_rrd()

draw_rrd();

?>




在 linux 內執行 curl http://xxx.liho.tw/temperature/update.php & 這樣就可以每 300 秒回報一次溫度.

展示畫一天的溫度的rrd圖 :

Comments are closed.