Skip to content

Commit 52a3c02

Browse files
committed
Learning things
1 parent 86e830a commit 52a3c02

File tree

1 file changed

+111
-4
lines changed

1 file changed

+111
-4
lines changed

RRDDiskIO.php

+111-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,83 @@
55
class RRDDiskIO extends RRDBase {
66

77
protected $rrdFileName = 'diskio.rrd';
8+
private $interval = 10;
9+
private $iterations = 2;
10+
private $devices = [];
11+
12+
public function __construct($path = __DIR__, $debug = false, array $device = ['nb0']){
13+
parent::__construct($path, $debug);
14+
15+
$this->devices = $device;
16+
$this->rrdFilePath = [];
17+
18+
foreach ($this->devices as $device) {
19+
$this->rrdFilePath[$device] = $this->path . DIRECTORY_SEPARATOR . 'diskio-' . $device . '.rrd';
20+
}
21+
$this->touchGraph();
22+
}
823

924
protected function touchGraph()
1025
{
11-
// ...
26+
if (count($this->devices) < 1) {
27+
return;
28+
}
29+
foreach ($this->devices as $device) {
30+
$this->createGraph($device);
31+
}
32+
}
33+
34+
private function createGraph($device) {
35+
if (!file_exists($this->rrdFilePath[$device])) {
36+
$this->debug("Creating [{$this->rrdFilePath[$device]}]\n");
37+
if (!rrd_create($this->rrdFilePath, [
38+
"-s",60,
39+
// rrqm/s wrqm/s
40+
"DS:RRMerge:GAUGE:120:0:U",
41+
"DS:WRMerge:GAUGE:120:0:U",
42+
43+
// r/s w/s
44+
"DS:RReq:GAUGE:120:0:U",
45+
"DS:WReq:GAUGE:120:0:U",
46+
47+
// rBytes/s wBytes/s
48+
"DS:RByte:GAUGE:120:0:U",
49+
"DS:WByte:GAUGE:120:0:U",
50+
51+
// avgrq-sz (Bytes)
52+
"DS:RSize:GAUGE:120:0:U",
53+
54+
// avgqu-sz
55+
"DS:QLength:GAUGE:120:0:U",
56+
57+
// await
58+
"DS:WTime:GAUGE:120:0:U",
59+
60+
// r_await
61+
"DS:RWTime:GAUGE:120:0:U",
62+
63+
// w_await
64+
"DS:WWTime:GAUGE:120:0:U",
65+
66+
// svctm
67+
"DS:STime:GAUGE:120:0:U",
68+
69+
// %util
70+
"DS:Util:GAUGE:120:0:U",
71+
72+
"RRA:AVERAGE:0.5:1:2880",
73+
"RRA:AVERAGE:0.5:30:672",
74+
"RRA:AVERAGE:0.5:120:732",
75+
"RRA:AVERAGE:0.5:720:1460",
76+
77+
"RRA:MAX:0.5:1:2880",
78+
"RRA:MAX:0.5:30:672",
79+
"RRA:MAX:0.5:120:732",
80+
"RRA:MAX:0.5:720:1460"
81+
])){
82+
$this->fail(rrd_error());
83+
}
84+
}
1285
}
1386

1487
private function runCommand($cmd)
@@ -25,10 +98,44 @@ private function runCommand($cmd)
2598
return trim($stat);
2699
}
27100

101+
private function collectForDevice($device)
102+
{
103+
$stat = $this->runCommand("iostat -dxk {$device} {$this->interval} {$this->iterations}");
104+
$collect = false;
105+
$keys = [];
106+
$collection = [];
107+
108+
foreach (explode("\n", $stat) as $row) {
109+
if (empty($row)) {
110+
continue;
111+
}
112+
113+
$row = preg_replace('!\s+!', ' ', $row);
114+
115+
if (strpos($row, 'Device:') !== false) {
116+
$collect = true;
117+
$keys = explode(' ', $row);
118+
foreach ($keys as $key) {
119+
$stats[$key] = 0;
120+
}
121+
continue;
122+
}
123+
124+
if ($collect === true) {
125+
$values = explode(' ', $row);
126+
array_push($collection, array_combine($keys, $values));
127+
$this->debug($row . "\n");
128+
}
129+
}
130+
131+
// var_dump($collection);
132+
}
133+
28134
public function collect()
29135
{
30-
$stat = $this->runCommand('cat /proc/meminfo');
31-
var_dump($stat);
136+
foreach ($this->devices as $device) {
137+
$this->collectForDevice($device);
138+
}
32139
}
33140

34141
public function graph($period = 'day', $graphPath = __DIR__)
@@ -39,6 +146,6 @@ public function graph($period = 'day', $graphPath = __DIR__)
39146
}
40147
}
41148

42-
$p = new RRDDiskIO(__DIR__, true);
149+
$p = new RRDDiskIO(__DIR__, true, ['nb0', 'nb1']);
43150
$p->collect();
44151
$p->graph('hour', __DIR__ . '/../httpdocs/img');

0 commit comments

Comments
 (0)