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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 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 tiara_pub 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 tiara_pub 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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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();
 
?>
<img src="r733_1h.gif"><br>
<img src="r733_1d.gif"><br>
<img src="r733_1w.gif"><br>
<img src="r733_1m.gif"><br>
<img src="r733_1y.gif">

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.