5
5
class RRDDiskIO extends RRDBase {
6
6
7
7
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
+ }
8
23
9
24
protected function touchGraph ()
10
25
{
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
+ }
12
85
}
13
86
14
87
private function runCommand ($ cmd )
@@ -25,10 +98,44 @@ private function runCommand($cmd)
25
98
return trim ($ stat );
26
99
}
27
100
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
+
28
134
public function collect ()
29
135
{
30
- $ stat = $ this ->runCommand ('cat /proc/meminfo ' );
31
- var_dump ($ stat );
136
+ foreach ($ this ->devices as $ device ) {
137
+ $ this ->collectForDevice ($ device );
138
+ }
32
139
}
33
140
34
141
public function graph ($ period = 'day ' , $ graphPath = __DIR__ )
@@ -39,6 +146,6 @@ public function graph($period = 'day', $graphPath = __DIR__)
39
146
}
40
147
}
41
148
42
- $ p = new RRDDiskIO (__DIR__ , true );
149
+ $ p = new RRDDiskIO (__DIR__ , true , [ ' nb0 ' , ' nb1 ' ] );
43
150
$ p ->collect ();
44
151
$ p ->graph ('hour ' , __DIR__ . '/../httpdocs/img ' );
0 commit comments