Bug#404018: Break out of the `while (loop)'-loop when no socket can be opened.
[collectd.git] / contrib / collection.cgi
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 no warnings ('qw');
7
8 use CGI;
9 use RRDs;
10 use Fcntl (qw(:flock));
11 use Carp (qw(carp cluck confess croak));
12
13 our $Config = read_config ();
14
15 our $AbsDir;
16 our $RelDir;
17 our $Type;
18 our $Inst;
19 our $TimeSpan;
20
21 for (qw(Red Green Blue Yellow Cyan Magenta))
22 {
23         $Config->{'Colors'}{"Half$_"} = color_calculate_transparent ($Config->{'Colors'}{'Alpha'},
24                 $Config->{'Colors'}{'Canvas'}, $Config->{'Colors'}{"Full$_"});
25 }
26
27 $Config->{'Colors'}{'HalfBlueGreen'} = color_calculate_transparent ($Config->{'Colors'}{'Alpha'},
28         $Config->{'Colors'}{'Canvas'}, $Config->{'Colors'}{'FullGreen'}, $Config->{'Colors'}{'FullBlue'});
29 $Config->{'Colors'}{'HalfRedBlue'} = color_calculate_transparent ($Config->{'Colors'}{'Alpha'},
30         $Config->{'Colors'}{'Canvas'}, $Config->{'Colors'}{'FullBlue'}, $Config->{'Colors'}{'FullRed'});
31
32 our $GraphDefs;
33 {
34         my $Alpha  = $Config->{'Colors'}{'Alpha'};
35         my $Canvas = $Config->{'Colors'}{'Canvas'};
36
37         my $FullRed    = $Config->{'Colors'}{'FullRed'};
38         my $FullGreen  = $Config->{'Colors'}{'FullGreen'};
39         my $FullBlue   = $Config->{'Colors'}{'FullBlue'};
40         my $FullYellow = $Config->{'Colors'}{'FullYellow'};
41         my $FullCyan   = $Config->{'Colors'}{'FullCyan'};
42         my $FullMagenta= $Config->{'Colors'}{'FullMagenta'};
43
44         my $HalfRed    = $Config->{'Colors'}{'HalfRed'};
45         my $HalfGreen  = $Config->{'Colors'}{'HalfGreen'};
46         my $HalfBlue   = $Config->{'Colors'}{'HalfBlue'};
47         my $HalfYellow = $Config->{'Colors'}{'HalfYellow'};
48         my $HalfCyan   = $Config->{'Colors'}{'HalfCyan'};
49         my $HalfMagenta= $Config->{'Colors'}{'HalfMagenta'};
50
51         my $HalfBlueGreen = $Config->{'Colors'}{'HalfBlueGreen'};
52         my $HalfRedBlue   = $Config->{'Colors'}{'HalfRedBlue'};
53         
54         $GraphDefs =
55         {
56                 apache_bytes => ['DEF:min_raw={file}:count:MIN',
57                         'DEF:avg_raw={file}:count:AVERAGE',
58                         'DEF:max_raw={file}:count:MAX',
59                         'CDEF:min=min_raw,8,*',
60                         'CDEF:avg=avg_raw,8,*',
61                         'CDEF:max=max_raw,8,*',
62                         'CDEF:mytime=avg_raw,TIME,TIME,IF',
63                         'CDEF:sample_len_raw=mytime,PREV(mytime),-',
64                         'CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF',
65                         'CDEF:avg_sample=avg_raw,UN,0,avg_raw,IF,sample_len,*',
66                         'CDEF:avg_sum=PREV,UN,0,PREV,IF,avg_sample,+',
67                         "AREA:avg#$HalfBlue",
68                         "LINE1:avg#$FullBlue:Bit/s",
69                         'GPRINT:min:MIN:%5.1lf%s Min,',
70                         'GPRINT:avg:AVERAGE:%5.1lf%s Avg,',
71                         'GPRINT:max:MAX:%5.1lf%s Max,',
72                         'GPRINT:avg:LAST:%5.1lf%s Last',
73                         'GPRINT:avg_sum:LAST:(ca. %5.1lf%sB Total)\l'
74                 ],
75                 apache_requests => ['DEF:min={file}:count:MIN',
76                         'DEF:avg={file}:count:AVERAGE',
77                         'DEF:max={file}:count:MAX',
78                         "AREA:max#$HalfBlue",
79                         "AREA:min#$Canvas",
80                         "LINE1:avg#$FullBlue:Requests/s",
81                         'GPRINT:min:MIN:%6.2lf Min,',
82                         'GPRINT:avg:AVERAGE:%6.2lf Avg,',
83                         'GPRINT:max:MAX:%6.2lf Max,',
84                         'GPRINT:avg:LAST:%6.2lf Last'
85                 ],
86                 apache_scoreboard => ['DEF:min={file}:count:MIN',
87                         'DEF:avg={file}:count:AVERAGE',
88                         'DEF:max={file}:count:MAX',
89                         "AREA:max#$HalfBlue",
90                         "AREA:min#$Canvas",
91                         "LINE1:avg#$FullBlue:Processes",
92                         'GPRINT:min:MIN:%6.2lf Min,',
93                         'GPRINT:avg:AVERAGE:%6.2lf Avg,',
94                         'GPRINT:max:MAX:%6.2lf Max,',
95                         'GPRINT:avg:LAST:%6.2lf Last'
96                 ],
97                 charge => [
98                         'DEF:avg={file}:charge:AVERAGE',
99                         'DEF:min={file}:charge:MIN',
100                         'DEF:max={file}:charge:MAX',
101                         "AREA:max#$HalfBlue",
102                         "AREA:min#$Canvas",
103                         "LINE1:avg#$FullBlue:Charge",
104                         'GPRINT:min:MIN:%5.1lf%sAh Min,',
105                         'GPRINT:avg:AVERAGE:%5.1lf%sAh Avg,',
106                         'GPRINT:max:MAX:%5.1lf%sAh Max,',
107                         'GPRINT:avg:LAST:%5.1lf%sAh Last\l'
108                 ],
109                 charge_percent => [
110                         'DEF:avg={file}:percent:AVERAGE',
111                         'DEF:min={file}:percent:MIN',
112                         'DEF:max={file}:percent:MAX',
113                         "AREA:max#$HalfBlue",
114                         "AREA:min#$Canvas",
115                         "LINE1:avg#$FullBlue:Charge",
116                         'GPRINT:min:MIN:%5.1lf%s%% Min,',
117                         'GPRINT:avg:AVERAGE:%5.1lf%s%% Avg,',
118                         'GPRINT:max:MAX:%5.1lf%s%% Max,',
119                         'GPRINT:avg:LAST:%5.1lf%s%% Last\l'
120                 ],
121                 cpu => ['DEF:user_avg={file}:user:AVERAGE',
122                         'DEF:user_min={file}:user:MIN',
123                         'DEF:user_max={file}:user:MAX',
124                         'DEF:nice_avg={file}:nice:AVERAGE',
125                         'DEF:nice_min={file}:nice:MIN',
126                         'DEF:nice_max={file}:nice:MAX',
127                         'DEF:syst_avg={file}:syst:AVERAGE',
128                         'DEF:syst_min={file}:syst:MIN',
129                         'DEF:syst_max={file}:syst:MAX',
130                         'DEF:idle_avg={file}:idle:AVERAGE',
131                         'DEF:idle_min={file}:idle:MIN',
132                         'DEF:idle_max={file}:idle:MAX',
133                         'DEF:wait_avg={file}:wait:AVERAGE',
134                         'DEF:wait_min={file}:wait:MIN',
135                         'DEF:wait_max={file}:wait:MAX',
136                         'CDEF:user_avg_notnull=user_avg,UN,0,user_avg,IF',
137                         'CDEF:nice_avg_notnull=nice_avg,UN,0,nice_avg,IF',
138                         'CDEF:syst_avg_notnull=syst_avg,UN,0,syst_avg,IF',
139                         'CDEF:idle_avg_notnull=idle_avg,UN,0,idle_avg,IF',
140                         'CDEF:wait_avg_notnull=wait_avg,UN,0,wait_avg,IF',
141                         'CDEF:totl_avg_notnull=user_avg_notnull,nice_avg_notnull,+,syst_avg_notnull,+,idle_avg_notnull,+,wait_avg_notnull,+',
142                         'CDEF:user_avg_pct=user_avg_notnull,100,*,totl_avg_notnull,/',
143                         'CDEF:nice_avg_pct=nice_avg_notnull,100,*,totl_avg_notnull,/',
144                         'CDEF:syst_avg_pct=syst_avg_notnull,100,*,totl_avg_notnull,/',
145                         'CDEF:wait_avg_pct=wait_avg_notnull,100,*,totl_avg_notnull,/',
146                         'CDEF:nice_acc=syst_avg_pct,wait_avg_pct,user_avg_pct,nice_avg_pct,+,+,+',
147                         'CDEF:user_acc=syst_avg_pct,wait_avg_pct,user_avg_pct,+,+',
148                         'CDEF:wait_acc=syst_avg_pct,wait_avg_pct,+',
149                         'CDEF:syst_acc=syst_avg_pct',
150 #                       'CDEF:nice_acc=syst_avg_notnull,wait_avg_notnull,user_avg_notnull,nice_avg_notnull,+,+,+',
151 #                       'CDEF:user_acc=syst_avg_notnull,wait_avg_notnull,user_avg_notnull,+,+',
152 #                       'CDEF:wait_acc=syst_avg_notnull,wait_avg_notnull,+',
153 #                       'CDEF:syst_acc=syst_avg_notnull',
154                         "AREA:nice_acc#$HalfGreen",
155                         "AREA:user_acc#$HalfBlue",
156                         "AREA:wait_acc#$HalfYellow",
157                         "AREA:syst_acc#$HalfRed",
158                         "LINE1:nice_acc#$FullGreen:Nice   ",
159                         'GPRINT:nice_min:MIN:%5.1lf%% Min,',
160                         'GPRINT:nice_avg:AVERAGE:%5.1lf%% Avg,',
161                         'GPRINT:nice_max:MAX:%5.1lf%% Max,',
162                         'GPRINT:nice_avg:LAST:%5.1lf%% Last\l',
163                         "LINE1:user_acc#$FullBlue:User   ",
164                         'GPRINT:user_min:MIN:%5.1lf%% Min,',
165                         'GPRINT:user_avg:AVERAGE:%5.1lf%% Avg,',
166                         'GPRINT:user_max:MAX:%5.1lf%% Max,',
167                         'GPRINT:user_avg:LAST:%5.1lf%% Last\l',
168                         "LINE1:wait_acc#$FullYellow:Wait-IO",
169                         'GPRINT:wait_min:MIN:%5.1lf%% Min,',
170                         'GPRINT:wait_avg:AVERAGE:%5.1lf%% Avg,',
171                         'GPRINT:wait_max:MAX:%5.1lf%% Max,',
172                         'GPRINT:wait_avg:LAST:%5.1lf%% Last\l',
173                         "LINE1:syst_acc#$FullRed:System ",
174                         'GPRINT:syst_min:MIN:%5.1lf%% Min,',
175                         'GPRINT:syst_avg:AVERAGE:%5.1lf%% Avg,',
176                         'GPRINT:syst_max:MAX:%5.1lf%% Max,',
177                         'GPRINT:syst_avg:LAST:%5.1lf%% Last\l'
178                 ],
179                 current => [
180                         'DEF:avg={file}:current:AVERAGE',
181                         'DEF:min={file}:current:MIN',
182                         'DEF:max={file}:current:MAX',
183                         "AREA:max#$HalfBlue",
184                         "AREA:min#$Canvas",
185                         "LINE1:avg#$FullBlue:Current",
186                         'GPRINT:min:MIN:%5.1lf%sA Min,',
187                         'GPRINT:avg:AVERAGE:%5.1lf%sA Avg,',
188                         'GPRINT:max:MAX:%5.1lf%sA Max,',
189                         'GPRINT:avg:LAST:%5.1lf%sA Last\l'
190                 ],
191                 df => [
192                         'DEF:free_avg={file}:free:AVERAGE',
193                         'DEF:free_min={file}:free:MIN',
194                         'DEF:free_max={file}:free:MAX',
195                         'DEF:used_avg={file}:used:AVERAGE',
196                         'DEF:used_min={file}:used:MIN',
197                         'DEF:used_max={file}:used:MAX',
198                         'CDEF:total=free_avg,used_avg,+',
199                         'CDEF:free_pct=100,free_avg,*,total,/',
200                         'CDEF:used_pct=100,used_avg,*,total,/',
201                         'CDEF:free_acc=free_pct,used_pct,+',
202                         'CDEF:used_acc=used_pct',
203                         "AREA:free_acc#$HalfGreen",
204                         "AREA:used_acc#$HalfRed",
205                         "LINE1:free_acc#$FullGreen:Free",
206                         'GPRINT:free_min:MIN:%5.1lf%sB Min,',
207                         'GPRINT:free_avg:AVERAGE:%5.1lf%sB Avg,',
208                         'GPRINT:free_max:MAX:%5.1lf%sB Max,',
209                         'GPRINT:free_avg:LAST:%5.1lf%sB Last\l',
210                         "LINE1:used_acc#$FullRed:Used",
211                         'GPRINT:used_min:MIN:%5.1lf%sB Min,',
212                         'GPRINT:used_avg:AVERAGE:%5.1lf%sB Avg,',
213                         'GPRINT:used_max:MAX:%5.1lf%sB Max,',
214                         'GPRINT:used_avg:LAST:%5.1lf%sB Last\l'
215                 ],
216                 disk => [
217                         'DEF:rtime_avg={file}:rtime:AVERAGE',
218                         'DEF:rtime_min={file}:rtime:MIN',
219                         'DEF:rtime_max={file}:rtime:MAX',
220                         'DEF:wtime_avg={file}:wtime:AVERAGE',
221                         'DEF:wtime_min={file}:wtime:MIN',
222                         'DEF:wtime_max={file}:wtime:MAX',
223                         'CDEF:rtime_avg_ms=rtime_avg,1000,/',
224                         'CDEF:rtime_min_ms=rtime_min,1000,/',
225                         'CDEF:rtime_max_ms=rtime_max,1000,/',
226                         'CDEF:wtime_avg_ms=wtime_avg,1000,/',
227                         'CDEF:wtime_min_ms=wtime_min,1000,/',
228                         'CDEF:wtime_max_ms=wtime_max,1000,/',
229                         'CDEF:total_avg_ms=rtime_avg_ms,wtime_avg_ms,+',
230                         'CDEF:total_min_ms=rtime_min_ms,wtime_min_ms,+',
231                         'CDEF:total_max_ms=rtime_max_ms,wtime_max_ms,+',
232                         "AREA:total_max_ms#$HalfRed",
233                         "AREA:total_min_ms#$Canvas",
234                         "LINE1:wtime_avg_ms#$FullGreen:Write",
235                         'GPRINT:wtime_min_ms:MIN:%5.1lf%s Min,',
236                         'GPRINT:wtime_avg_ms:AVERAGE:%5.1lf%s Avg,',
237                         'GPRINT:wtime_max_ms:MAX:%5.1lf%s Max,',
238                         'GPRINT:wtime_avg_ms:LAST:%5.1lf%s Last\n',
239                         "LINE1:rtime_avg_ms#$FullBlue:Read ",
240                         'GPRINT:rtime_min_ms:MIN:%5.1lf%s Min,',
241                         'GPRINT:rtime_avg_ms:AVERAGE:%5.1lf%s Avg,',
242                         'GPRINT:rtime_max_ms:MAX:%5.1lf%s Max,',
243                         'GPRINT:rtime_avg_ms:LAST:%5.1lf%s Last\n',
244                         "LINE1:total_avg_ms#$FullRed:Total",
245                         'GPRINT:total_min_ms:MIN:%5.1lf%s Min,',
246                         'GPRINT:total_avg_ms:AVERAGE:%5.1lf%s Avg,',
247                         'GPRINT:total_max_ms:MAX:%5.1lf%s Max,',
248                         'GPRINT:total_avg_ms:LAST:%5.1lf%s Last'
249                 ],
250                 fanspeed => [
251                         'DEF:temp_avg={file}:value:AVERAGE',
252                         'DEF:temp_min={file}:value:MIN',
253                         'DEF:temp_max={file}:value:MAX',
254                         "AREA:temp_max#$HalfBlue",
255                         "AREA:temp_min#$Canvas",
256                         "LINE1:temp_avg#$FullBlue:RPM",
257                         'GPRINT:temp_min:MIN:%4.1lf Min,',
258                         'GPRINT:temp_avg:AVERAGE:%4.1lf Avg,',
259                         'GPRINT:temp_max:MAX:%4.1lf Max,',
260                         'GPRINT:temp_avg:LAST:%4.1lf Last\l'
261                 ],
262                 frequency_offset => [ # NTPd
263                         'DEF:ppm_avg={file}:ppm:AVERAGE',
264                         'DEF:ppm_min={file}:ppm:MIN',
265                         'DEF:ppm_max={file}:ppm:MAX',
266                         "AREA:ppm_max#$HalfBlue",
267                         "AREA:ppm_min#$Canvas",
268                         "LINE1:ppm_avg#$FullBlue:{inst}",
269                         'GPRINT:ppm_min:MIN:%5.2lf Min,',
270                         'GPRINT:ppm_avg:AVERAGE:%5.2lf Avg,',
271                         'GPRINT:ppm_max:MAX:%5.2lf Max,',
272                         'GPRINT:ppm_avg:LAST:%5.2lf Last'
273                 ],
274                 hddtemp => [
275                         'DEF:temp_avg={file}:value:AVERAGE',
276                         'DEF:temp_min={file}:value:MIN',
277                         'DEF:temp_max={file}:value:MAX',
278                         "AREA:temp_max#$HalfBlue",
279                         "AREA:temp_min#$Canvas",
280                         "LINE1:temp_avg#$FullBlue:Temperature",
281                         'GPRINT:temp_min:MIN:%4.1lf Min,',
282                         'GPRINT:temp_avg:AVERAGE:%4.1lf Avg,',
283                         'GPRINT:temp_max:MAX:%4.1lf Max,',
284                         'GPRINT:temp_avg:LAST:%4.1lf Last\l'
285                 ],
286                 if_packets => ['DEF:tx_min={file}:tx:MIN',
287                         'DEF:tx_avg={file}:tx:AVERAGE',
288                         'DEF:tx_max={file}:tx:MAX',
289                         'DEF:rx_min={file}:rx:MIN',
290                         'DEF:rx_avg={file}:rx:AVERAGE',
291                         'DEF:rx_max={file}:rx:MAX',
292                         'CDEF:overlap=tx_avg,rx_avg,GT,rx_avg,tx_avg,IF',
293                         'CDEF:mytime=tx_avg,TIME,TIME,IF',
294                         'CDEF:sample_len_raw=mytime,PREV(mytime),-',
295                         'CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF',
296                         'CDEF:tx_avg_sample=tx_avg,UN,0,tx_avg,IF,sample_len,*',
297                         'CDEF:tx_avg_sum=PREV,UN,0,PREV,IF,tx_avg_sample,+',
298                         'CDEF:rx_avg_sample=rx_avg,UN,0,rx_avg,IF,sample_len,*',
299                         'CDEF:rx_avg_sum=PREV,UN,0,PREV,IF,rx_avg_sample,+',
300                         "AREA:tx_avg#$HalfGreen",
301                         "AREA:rx_avg#$HalfBlue",
302                         "AREA:overlap#$HalfBlueGreen",
303                         "LINE1:tx_avg#$FullGreen:TX",
304                         'GPRINT:tx_avg:AVERAGE:%5.1lf%s Avg,',
305                         'GPRINT:tx_max:MAX:%5.1lf%s Max,',
306                         'GPRINT:tx_avg:LAST:%5.1lf%s Last',
307                         'GPRINT:tx_avg_sum:LAST:(ca. %.0lf Total)\l',
308                         "LINE1:rx_avg#$FullBlue:RX",
309                         #'GPRINT:rx_min:MIN:%5.1lf %s Min,',
310                         'GPRINT:rx_avg:AVERAGE:%5.1lf%s Avg,',
311                         'GPRINT:rx_max:MAX:%5.1lf%s Max,',
312                         'GPRINT:rx_avg:LAST:%5.1lf%s Last',
313                         'GPRINT:rx_avg_sum:LAST:(ca. %.0lf Total)\l'
314                 ],
315                 load => ['DEF:s_avg={file}:shortterm:AVERAGE',
316                         'DEF:s_min={file}:shortterm:MIN',
317                         'DEF:s_max={file}:shortterm:MAX',
318                         'DEF:m_avg={file}:midterm:AVERAGE',
319                         'DEF:m_min={file}:midterm:MIN',
320                         'DEF:m_max={file}:midterm:MAX',
321                         'DEF:l_avg={file}:longterm:AVERAGE',
322                         'DEF:l_min={file}:longterm:MIN',
323                         'DEF:l_max={file}:longterm:MAX',
324                         "AREA:s_max#$HalfGreen",
325                         "AREA:s_min#$Canvas",
326                         "LINE1:s_avg#$FullGreen: 1m average",
327                         'GPRINT:s_min:MIN:%4.2lf Min,',
328                         'GPRINT:s_avg:AVERAGE:%4.2lf Avg,',
329                         'GPRINT:s_max:MAX:%4.2lf Max,',
330                         'GPRINT:s_avg:LAST:%4.2lf Last\n',
331                         "LINE1:m_avg#$FullBlue: 5m average",
332                         'GPRINT:m_min:MIN:%4.2lf Min,',
333                         'GPRINT:m_avg:AVERAGE:%4.2lf Avg,',
334                         'GPRINT:m_max:MAX:%4.2lf Max,',
335                         'GPRINT:m_avg:LAST:%4.2lf Last\n',
336                         "LINE1:l_avg#$FullRed:15m average",
337                         'GPRINT:l_min:MIN:%4.2lf Min,',
338                         'GPRINT:l_avg:AVERAGE:%4.2lf Avg,',
339                         'GPRINT:l_max:MAX:%4.2lf Max,',
340                         'GPRINT:l_avg:LAST:%4.2lf Last'
341                 ],
342                 load_percent => [
343                         'DEF:avg={file}:percent:AVERAGE',
344                         'DEF:min={file}:percent:MIN',
345                         'DEF:max={file}:percent:MAX',
346                         "AREA:max#$HalfBlue",
347                         "AREA:min#$Canvas",
348                         "LINE1:avg#$FullBlue:Load",
349                         'GPRINT:min:MIN:%5.1lf%s%% Min,',
350                         'GPRINT:avg:AVERAGE:%5.1lf%s%% Avg,',
351                         'GPRINT:max:MAX:%5.1lf%s%% Max,',
352                         'GPRINT:avg:LAST:%5.1lf%s%% Last\l'
353                 ],
354                 mails => ['DEF:rawgood={file}:good:AVERAGE',
355                         'DEF:rawspam={file}:spam:AVERAGE',
356                         'CDEF:good=rawgood,UN,0,rawgood,IF',
357                         'CDEF:spam=rawspam,UN,0,rawspam,IF',
358                         'CDEF:negspam=spam,-1,*',
359                         "AREA:good#$HalfGreen",
360                         "LINE1:good#$FullGreen:Good mails",
361                         'GPRINT:good:AVERAGE:%4.1lf Avg,',
362                         'GPRINT:good:MAX:%4.1lf Max,',
363                         'GPRINT:good:LAST:%4.1lf Last\n',
364                         "AREA:negspam#$HalfRed",
365                         "LINE1:negspam#$FullRed:Spam mails",
366                         'GPRINT:spam:AVERAGE:%4.1lf Avg,',
367                         'GPRINT:spam:MAX:%4.1lf Max,',
368                         'GPRINT:spam:LAST:%4.1lf Last',
369                         'HRULE:0#000000'],
370                 memory => [
371                         'DEF:used_avg={file}:used:AVERAGE',
372                         'DEF:free_avg={file}:free:AVERAGE',
373                         'DEF:buffers_avg={file}:buffers:AVERAGE',
374                         'DEF:cached_avg={file}:cached:AVERAGE',
375                         'DEF:used_min={file}:used:MIN',
376                         'DEF:free_min={file}:free:MIN',
377                         'DEF:buffers_min={file}:buffers:MIN',
378                         'DEF:cached_min={file}:cached:MIN',
379                         'DEF:used_max={file}:used:MAX',
380                         'DEF:free_max={file}:free:MAX',
381                         'DEF:buffers_max={file}:buffers:MAX',
382                         'DEF:cached_max={file}:cached:MAX',
383                         'CDEF:cached_avg_nn=cached_avg,UN,0,cached_avg,IF',
384                         'CDEF:buffers_avg_nn=buffers_avg,UN,0,buffers_avg,IF',
385                         'CDEF:free_cached_buffers_used=free_avg,cached_avg_nn,+,buffers_avg_nn,+,used_avg,+',
386                         'CDEF:cached_buffers_used=cached_avg,buffers_avg_nn,+,used_avg,+',
387                         'CDEF:buffers_used=buffers_avg,used_avg,+',
388                         "AREA:free_cached_buffers_used#$HalfGreen",
389                         "AREA:cached_buffers_used#$HalfBlue",
390                         "AREA:buffers_used#$HalfYellow",
391                         "AREA:used_avg#$HalfRed",
392                         "LINE1:free_cached_buffers_used#$FullGreen:Free        ",
393                         'GPRINT:free_min:MIN:%5.1lf%s Min,',
394                         'GPRINT:free_avg:AVERAGE:%5.1lf%s Avg,',
395                         'GPRINT:free_max:MAX:%5.1lf%s Max,',
396                         'GPRINT:free_avg:LAST:%5.1lf%s Last\n',
397                         "LINE1:cached_buffers_used#$FullBlue:Page cache  ",
398                         'GPRINT:cached_min:MIN:%5.1lf%s Min,',
399                         'GPRINT:cached_avg:AVERAGE:%5.1lf%s Avg,',
400                         'GPRINT:cached_max:MAX:%5.1lf%s Max,',
401                         'GPRINT:cached_avg:LAST:%5.1lf%s Last\n',
402                         "LINE1:buffers_used#$FullYellow:Buffer cache",
403                         'GPRINT:buffers_min:MIN:%5.1lf%s Min,',
404                         'GPRINT:buffers_avg:AVERAGE:%5.1lf%s Avg,',
405                         'GPRINT:buffers_max:MAX:%5.1lf%s Max,',
406                         'GPRINT:buffers_avg:LAST:%5.1lf%s Last\n',
407                         "LINE1:used_avg#$FullRed:Used        ",
408                         'GPRINT:used_min:MIN:%5.1lf%s Min,',
409                         'GPRINT:used_avg:AVERAGE:%5.1lf%s Avg,',
410                         'GPRINT:used_max:MAX:%5.1lf%s Max,',
411                         'GPRINT:used_avg:LAST:%5.1lf%s Last'
412                 ],
413                 mysql_commands => [
414                         "DEF:val_avg={file}:value:AVERAGE",
415                         "DEF:val_min={file}:value:MIN",
416                         "DEF:val_max={file}:value:MAX",
417                         "AREA:val_max#$HalfBlue",
418                         "AREA:val_min#$Canvas",
419                         "LINE1:val_avg#$FullBlue:{inst}",
420                         'GPRINT:val_min:MIN:%5.2lf Min,',
421                         'GPRINT:val_avg:AVERAGE:%5.2lf Avg,',
422                         'GPRINT:val_max:MAX:%5.2lf Max,',
423                         'GPRINT:val_avg:LAST:%5.2lf Last'
424                 ],
425                 mysql_handler => [
426                         "DEF:val_avg={file}:value:AVERAGE",
427                         "DEF:val_min={file}:value:MIN",
428                         "DEF:val_max={file}:value:MAX",
429                         "AREA:val_max#$HalfBlue",
430                         "AREA:val_min#$Canvas",
431                         "LINE1:val_avg#$FullBlue:{inst}",
432                         'GPRINT:val_min:MIN:%5.2lf Min,',
433                         'GPRINT:val_avg:AVERAGE:%5.2lf Avg,',
434                         'GPRINT:val_max:MAX:%5.2lf Max,',
435                         'GPRINT:val_avg:LAST:%5.2lf Last'
436                 ],
437                 mysql_qcache => [
438                         "DEF:hits_min={file}:hits:MIN",
439                         "DEF:hits_avg={file}:hits:AVERAGE",
440                         "DEF:hits_max={file}:hits:MAX",
441                         "DEF:inserts_min={file}:inserts:MIN",
442                         "DEF:inserts_avg={file}:inserts:AVERAGE",
443                         "DEF:inserts_max={file}:inserts:MAX",
444                         "DEF:not_cached_min={file}:not_cached:MIN",
445                         "DEF:not_cached_avg={file}:not_cached:AVERAGE",
446                         "DEF:not_cached_max={file}:not_cached:MAX",
447                         "DEF:lowmem_prunes_min={file}:lowmem_prunes:MIN",
448                         "DEF:lowmem_prunes_avg={file}:lowmem_prunes:AVERAGE",
449                         "DEF:lowmem_prunes_max={file}:lowmem_prunes:MAX",
450                         "DEF:queries_min={file}:queries_in_cache:MIN",
451                         "DEF:queries_avg={file}:queries_in_cache:AVERAGE",
452                         "DEF:queries_max={file}:queries_in_cache:MAX",
453                         "CDEF:unknown=queries_avg,UNKN,+",
454                         "CDEF:not_cached_agg=hits_avg,inserts_avg,+,not_cached_avg,+",
455                         "CDEF:inserts_agg=hits_avg,inserts_avg,+",
456                         "CDEF:hits_agg=hits_avg",
457                         "AREA:not_cached_agg#$HalfYellow",
458                         "AREA:inserts_agg#$HalfBlue",
459                         "AREA:hits_agg#$HalfGreen",
460                         "LINE1:not_cached_agg#$FullYellow:Not Cached      ",
461                         'GPRINT:not_cached_min:MIN:%5.2lf Min,',
462                         'GPRINT:not_cached_avg:AVERAGE:%5.2lf Avg,',
463                         'GPRINT:not_cached_max:MAX:%5.2lf Max,',
464                         'GPRINT:not_cached_avg:LAST:%5.2lf Last\l',
465                         "LINE1:inserts_agg#$FullBlue:Inserts         ",
466                         'GPRINT:inserts_min:MIN:%5.2lf Min,',
467                         'GPRINT:inserts_avg:AVERAGE:%5.2lf Avg,',
468                         'GPRINT:inserts_max:MAX:%5.2lf Max,',
469                         'GPRINT:inserts_avg:LAST:%5.2lf Last\l',
470                         "LINE1:hits_agg#$FullGreen:Hits            ",
471                         'GPRINT:hits_min:MIN:%5.2lf Min,',
472                         'GPRINT:hits_avg:AVERAGE:%5.2lf Avg,',
473                         'GPRINT:hits_max:MAX:%5.2lf Max,',
474                         'GPRINT:hits_avg:LAST:%5.2lf Last\l',
475                         "LINE1:lowmem_prunes_avg#$FullRed:Lowmem Prunes   ",
476                         'GPRINT:lowmem_prunes_min:MIN:%5.2lf Min,',
477                         'GPRINT:lowmem_prunes_avg:AVERAGE:%5.2lf Avg,',
478                         'GPRINT:lowmem_prunes_max:MAX:%5.2lf Max,',
479                         'GPRINT:lowmem_prunes_avg:LAST:%5.2lf Last\l',
480                         "LINE1:unknown#$Canvas:Queries in cache",
481                         'GPRINT:queries_min:MIN:%5.0lf Min,',
482                         'GPRINT:queries_avg:AVERAGE:%5.0lf Avg,',
483                         'GPRINT:queries_max:MAX:%5.0lf Max,',
484                         'GPRINT:queries_avg:LAST:%5.0lf Last\l'
485                 ],
486                 mysql_threads => [
487                         "DEF:running_min={file}:running:MIN",
488                         "DEF:running_avg={file}:running:AVERAGE",
489                         "DEF:running_max={file}:running:MAX",
490                         "DEF:connected_min={file}:connected:MIN",
491                         "DEF:connected_avg={file}:connected:AVERAGE",
492                         "DEF:connected_max={file}:connected:MAX",
493                         "DEF:cached_min={file}:cached:MIN",
494                         "DEF:cached_avg={file}:cached:AVERAGE",
495                         "DEF:cached_max={file}:cached:MAX",
496                         "DEF:created_min={file}:created:MIN",
497                         "DEF:created_avg={file}:created:AVERAGE",
498                         "DEF:created_max={file}:created:MAX",
499                         "CDEF:unknown=created_avg,UNKN,+",
500                         "CDEF:cached_agg=connected_avg,cached_avg,+",
501                         "AREA:cached_agg#$HalfGreen",
502                         "AREA:connected_avg#$HalfBlue",
503                         "AREA:running_avg#$HalfRed",
504                         "LINE1:cached_agg#$FullGreen:Cached   ",
505                         'GPRINT:cached_min:MIN:%5.1lf Min,',
506                         'GPRINT:cached_avg:AVERAGE:%5.1lf Avg,',
507                         'GPRINT:cached_max:MAX:%5.1lf Max,',
508                         'GPRINT:cached_avg:LAST:%5.1lf Last\l',
509                         "LINE1:connected_avg#$FullBlue:Connected",
510                         'GPRINT:connected_min:MIN:%5.1lf Min,',
511                         'GPRINT:connected_avg:AVERAGE:%5.1lf Avg,',
512                         'GPRINT:connected_max:MAX:%5.1lf Max,',
513                         'GPRINT:connected_avg:LAST:%5.1lf Last\l',
514                         "LINE1:running_avg#$FullRed:Running  ",
515                         'GPRINT:running_min:MIN:%5.1lf Min,',
516                         'GPRINT:running_avg:AVERAGE:%5.1lf Avg,',
517                         'GPRINT:running_max:MAX:%5.1lf Max,',
518                         'GPRINT:running_avg:LAST:%5.1lf Last\l',
519                         "LINE1:unknown#$Canvas:Created  ",
520                         'GPRINT:created_min:MIN:%5.0lf Min,',
521                         'GPRINT:created_avg:AVERAGE:%5.0lf Avg,',
522                         'GPRINT:created_max:MAX:%5.0lf Max,',
523                         'GPRINT:created_avg:LAST:%5.0lf Last\l'
524                 ],
525                 nfs3_procedures => [
526                         "DEF:null_avg={file}:null:AVERAGE",
527                         "DEF:getattr_avg={file}:getattr:AVERAGE",
528                         "DEF:setattr_avg={file}:setattr:AVERAGE",
529                         "DEF:lookup_avg={file}:lookup:AVERAGE",
530                         "DEF:access_avg={file}:access:AVERAGE",
531                         "DEF:readlink_avg={file}:readlink:AVERAGE",
532                         "DEF:read_avg={file}:read:AVERAGE",
533                         "DEF:write_avg={file}:write:AVERAGE",
534                         "DEF:create_avg={file}:create:AVERAGE",
535                         "DEF:mkdir_avg={file}:mkdir:AVERAGE",
536                         "DEF:symlink_avg={file}:symlink:AVERAGE",
537                         "DEF:mknod_avg={file}:mknod:AVERAGE",
538                         "DEF:remove_avg={file}:remove:AVERAGE",
539                         "DEF:rmdir_avg={file}:rmdir:AVERAGE",
540                         "DEF:rename_avg={file}:rename:AVERAGE",
541                         "DEF:link_avg={file}:link:AVERAGE",
542                         "DEF:readdir_avg={file}:readdir:AVERAGE",
543                         "DEF:readdirplus_avg={file}:readdirplus:AVERAGE",
544                         "DEF:fsstat_avg={file}:fsstat:AVERAGE",
545                         "DEF:fsinfo_avg={file}:fsinfo:AVERAGE",
546                         "DEF:pathconf_avg={file}:pathconf:AVERAGE",
547                         "DEF:commit_avg={file}:commit:AVERAGE",
548                         "DEF:null_max={file}:null:MAX",
549                         "DEF:getattr_max={file}:getattr:MAX",
550                         "DEF:setattr_max={file}:setattr:MAX",
551                         "DEF:lookup_max={file}:lookup:MAX",
552                         "DEF:access_max={file}:access:MAX",
553                         "DEF:readlink_max={file}:readlink:MAX",
554                         "DEF:read_max={file}:read:MAX",
555                         "DEF:write_max={file}:write:MAX",
556                         "DEF:create_max={file}:create:MAX",
557                         "DEF:mkdir_max={file}:mkdir:MAX",
558                         "DEF:symlink_max={file}:symlink:MAX",
559                         "DEF:mknod_max={file}:mknod:MAX",
560                         "DEF:remove_max={file}:remove:MAX",
561                         "DEF:rmdir_max={file}:rmdir:MAX",
562                         "DEF:rename_max={file}:rename:MAX",
563                         "DEF:link_max={file}:link:MAX",
564                         "DEF:readdir_max={file}:readdir:MAX",
565                         "DEF:readdirplus_max={file}:readdirplus:MAX",
566                         "DEF:fsstat_max={file}:fsstat:MAX",
567                         "DEF:fsinfo_max={file}:fsinfo:MAX",
568                         "DEF:pathconf_max={file}:pathconf:MAX",
569                         "DEF:commit_max={file}:commit:MAX",
570                         "CDEF:other_avg=null_avg,readlink_avg,create_avg,mkdir_avg,symlink_avg,mknod_avg,remove_avg,rmdir_avg,rename_avg,link_avg,readdir_avg,readdirplus_avg,fsstat_avg,fsinfo_avg,pathconf_avg,+,+,+,+,+,+,+,+,+,+,+,+,+,+",
571                         "CDEF:other_max=null_max,readlink_max,create_max,mkdir_max,symlink_max,mknod_max,remove_max,rmdir_max,rename_max,link_max,readdir_max,readdirplus_max,fsstat_max,fsinfo_max,pathconf_max,+,+,+,+,+,+,+,+,+,+,+,+,+,+",
572                         "CDEF:stack_read=read_avg",
573                         "CDEF:stack_getattr=stack_read,getattr_avg,+",
574                         "CDEF:stack_access=stack_getattr,access_avg,+",
575                         "CDEF:stack_lookup=stack_access,lookup_avg,+",
576                         "CDEF:stack_write=stack_lookup,write_avg,+",
577                         "CDEF:stack_commit=stack_write,commit_avg,+",
578                         "CDEF:stack_setattr=stack_commit,setattr_avg,+",
579                         "CDEF:stack_other=stack_setattr,other_avg,+",
580                         "AREA:stack_other#$HalfRed",
581                         "AREA:stack_setattr#$HalfGreen",
582                         "AREA:stack_commit#$HalfYellow",
583                         "AREA:stack_write#$HalfGreen",
584                         "AREA:stack_lookup#$HalfBlue",
585                         "AREA:stack_access#$HalfMagenta",
586                         "AREA:stack_getattr#$HalfCyan",
587                         "AREA:stack_read#$HalfBlue",
588                         "LINE1:stack_other#$FullRed:Other  ",
589                         'GPRINT:other_max:MAX:%5.1lf Max,',
590                         'GPRINT:other_avg:AVERAGE:%5.1lf Avg,',
591                         'GPRINT:other_avg:LAST:%5.1lf Last\l',
592                         "LINE1:stack_setattr#$FullGreen:setattr",
593                         'GPRINT:setattr_max:MAX:%5.1lf Max,',
594                         'GPRINT:setattr_avg:AVERAGE:%5.1lf Avg,',
595                         'GPRINT:setattr_avg:LAST:%5.1lf Last\l',
596                         "LINE1:stack_commit#$FullYellow:commit ",
597                         'GPRINT:commit_max:MAX:%5.1lf Max,',
598                         'GPRINT:commit_avg:AVERAGE:%5.1lf Avg,',
599                         'GPRINT:commit_avg:LAST:%5.1lf Last\l',
600                         "LINE1:stack_write#$FullGreen:write  ",
601                         'GPRINT:write_max:MAX:%5.1lf Max,',
602                         'GPRINT:write_avg:AVERAGE:%5.1lf Avg,',
603                         'GPRINT:write_avg:LAST:%5.1lf Last\l',
604                         "LINE1:stack_lookup#$FullBlue:lookup ",
605                         'GPRINT:lookup_max:MAX:%5.1lf Max,',
606                         'GPRINT:lookup_avg:AVERAGE:%5.1lf Avg,',
607                         'GPRINT:lookup_avg:LAST:%5.1lf Last\l',
608                         "LINE1:stack_access#$FullMagenta:access ",
609                         'GPRINT:access_max:MAX:%5.1lf Max,',
610                         'GPRINT:access_avg:AVERAGE:%5.1lf Avg,',
611                         'GPRINT:access_avg:LAST:%5.1lf Last\l',
612                         "LINE1:stack_getattr#$FullCyan:getattr",
613                         'GPRINT:getattr_max:MAX:%5.1lf Max,',
614                         'GPRINT:getattr_avg:AVERAGE:%5.1lf Avg,',
615                         'GPRINT:getattr_avg:LAST:%5.1lf Last\l',
616                         "LINE1:stack_read#$FullBlue:read   ",
617                         'GPRINT:read_max:MAX:%5.1lf Max,',
618                         'GPRINT:read_avg:AVERAGE:%5.1lf Avg,',
619                         'GPRINT:read_avg:LAST:%5.1lf Last\l'
620                 ],
621                 partition => [
622                         "DEF:rbyte_avg={file}:rbytes:AVERAGE",
623                         "DEF:rbyte_min={file}:rbytes:MIN",
624                         "DEF:rbyte_max={file}:rbytes:MAX",
625                         "DEF:wbyte_avg={file}:wbytes:AVERAGE",
626                         "DEF:wbyte_min={file}:wbytes:MIN",
627                         "DEF:wbyte_max={file}:wbytes:MAX",
628                         'CDEF:overlap=wbyte_avg,rbyte_avg,GT,rbyte_avg,wbyte_avg,IF',
629                         "AREA:wbyte_avg#$HalfGreen",
630                         "AREA:rbyte_avg#$HalfBlue",
631                         "AREA:overlap#$HalfBlueGreen",
632                         "LINE1:wbyte_avg#$FullGreen:Write",
633                         'GPRINT:wbyte_min:MIN:%5.1lf%s Min,',
634                         'GPRINT:wbyte_avg:AVERAGE:%5.1lf%s Avg,',
635                         'GPRINT:wbyte_max:MAX:%5.1lf%s Max,',
636                         'GPRINT:wbyte_avg:LAST:%5.1lf%s Last\l',
637                         "LINE1:rbyte_avg#$FullBlue:Read ",
638                         'GPRINT:rbyte_min:MIN:%5.1lf%s Min,',
639                         'GPRINT:rbyte_avg:AVERAGE:%5.1lf%s Avg,',
640                         'GPRINT:rbyte_max:MAX:%5.1lf%s Max,',
641                         'GPRINT:rbyte_avg:LAST:%5.1lf%s Last\l'
642                 ],
643                 ping => ['DEF:ping_avg={file}:ping:AVERAGE',
644                         'DEF:ping_min={file}:ping:MIN',
645                         'DEF:ping_max={file}:ping:MAX',
646                         "AREA:ping_max#$HalfBlue",
647                         "AREA:ping_min#$Canvas",
648                         "LINE1:ping_avg#$FullBlue:Ping",
649                         'GPRINT:ping_min:MIN:%4.1lf ms Min,',
650                         'GPRINT:ping_avg:AVERAGE:%4.1lf ms Avg,',
651                         'GPRINT:ping_max:MAX:%4.1lf ms Max,',
652                         'GPRINT:ping_avg:LAST:%4.1lf ms Last'],
653                 processes => [
654                         "DEF:running_avg={file}:running:AVERAGE",
655                         "DEF:running_min={file}:running:MIN",
656                         "DEF:running_max={file}:running:MAX",
657                         "DEF:sleeping_avg={file}:sleeping:AVERAGE",
658                         "DEF:sleeping_min={file}:sleeping:MIN",
659                         "DEF:sleeping_max={file}:sleeping:MAX",
660                         "DEF:zombies_avg={file}:zombies:AVERAGE",
661                         "DEF:zombies_min={file}:zombies:MIN",
662                         "DEF:zombies_max={file}:zombies:MAX",
663                         "DEF:stopped_avg={file}:stopped:AVERAGE",
664                         "DEF:stopped_min={file}:stopped:MIN",
665                         "DEF:stopped_max={file}:stopped:MAX",
666                         "DEF:paging_avg={file}:paging:AVERAGE",
667                         "DEF:paging_min={file}:paging:MIN",
668                         "DEF:paging_max={file}:paging:MAX",
669                         "DEF:blocked_avg={file}:blocked:AVERAGE",
670                         "DEF:blocked_min={file}:blocked:MIN",
671                         "DEF:blocked_max={file}:blocked:MAX",
672                         'CDEF:paging_acc=sleeping_avg,running_avg,stopped_avg,zombies_avg,blocked_avg,paging_avg,+,+,+,+,+',
673                         'CDEF:blocked_acc=sleeping_avg,running_avg,stopped_avg,zombies_avg,blocked_avg,+,+,+,+',
674                         'CDEF:zombies_acc=sleeping_avg,running_avg,stopped_avg,zombies_avg,+,+,+',
675                         'CDEF:stopped_acc=sleeping_avg,running_avg,stopped_avg,+,+',
676                         'CDEF:running_acc=sleeping_avg,running_avg,+',
677                         'CDEF:sleeping_acc=sleeping_avg',
678                         "AREA:paging_acc#$HalfYellow",
679                         "AREA:blocked_acc#$HalfCyan",
680                         "AREA:zombies_acc#$HalfRed",
681                         "AREA:stopped_acc#$HalfMagenta",
682                         "AREA:running_acc#$HalfGreen",
683                         "AREA:sleeping_acc#$HalfBlue",
684                         "LINE1:paging_acc#$FullYellow:Paging  ",
685                         'GPRINT:paging_min:MIN:%5.1lf Min,',
686                         'GPRINT:paging_avg:AVERAGE:%5.1lf Average,',
687                         'GPRINT:paging_max:MAX:%5.1lf Max,',
688                         'GPRINT:paging_avg:LAST:%5.1lf Last\l',
689                         "LINE1:blocked_acc#$FullCyan:Blocked ",
690                         'GPRINT:blocked_min:MIN:%5.1lf Min,',
691                         'GPRINT:blocked_avg:AVERAGE:%5.1lf Average,',
692                         'GPRINT:blocked_max:MAX:%5.1lf Max,',
693                         'GPRINT:blocked_avg:LAST:%5.1lf Last\l',
694                         "LINE1:zombies_acc#$FullRed:Zombies ",
695                         'GPRINT:zombies_min:MIN:%5.1lf Min,',
696                         'GPRINT:zombies_avg:AVERAGE:%5.1lf Average,',
697                         'GPRINT:zombies_max:MAX:%5.1lf Max,',
698                         'GPRINT:zombies_avg:LAST:%5.1lf Last\l',
699                         "LINE1:stopped_acc#$FullMagenta:Stopped ",
700                         'GPRINT:stopped_min:MIN:%5.1lf Min,',
701                         'GPRINT:stopped_avg:AVERAGE:%5.1lf Average,',
702                         'GPRINT:stopped_max:MAX:%5.1lf Max,',
703                         'GPRINT:stopped_avg:LAST:%5.1lf Last\l',
704                         "LINE1:running_acc#$FullGreen:Running ",
705                         'GPRINT:running_min:MIN:%5.1lf Min,',
706                         'GPRINT:running_avg:AVERAGE:%5.1lf Average,',
707                         'GPRINT:running_max:MAX:%5.1lf Max,',
708                         'GPRINT:running_avg:LAST:%5.1lf Last\l',
709                         "LINE1:sleeping_acc#$FullBlue:Sleeping",
710                         'GPRINT:sleeping_min:MIN:%5.1lf Min,',
711                         'GPRINT:sleeping_avg:AVERAGE:%5.1lf Average,',
712                         'GPRINT:sleeping_max:MAX:%5.1lf Max,',
713                         'GPRINT:sleeping_avg:LAST:%5.1lf Last\l'
714                 ],
715                 swap => [
716                         'DEF:used_avg={file}:used:AVERAGE',
717                         'DEF:used_min={file}:used:MIN',
718                         'DEF:used_max={file}:used:MAX',
719                         'DEF:free_avg={file}:free:AVERAGE',
720                         'DEF:free_min={file}:free:MIN',
721                         'DEF:free_max={file}:free:MAX',
722                         'DEF:cach_avg={file}:cached:AVERAGE',
723                         'DEF:cach_min={file}:cached:MIN',
724                         'DEF:cach_max={file}:cached:MAX',
725                         'DEF:resv_avg={file}:resv:AVERAGE',
726                         'DEF:resv_min={file}:resv:MIN',
727                         'DEF:resv_max={file}:resv:MAX',
728                         'CDEF:cach_avg_notnull=cach_avg,UN,0,cach_avg,IF',
729                         'CDEF:resv_avg_notnull=resv_avg,UN,0,resv_avg,IF',
730                         'CDEF:used_acc=used_avg',
731                         'CDEF:resv_acc=used_acc,resv_avg_notnull,+',
732                         'CDEF:cach_acc=resv_acc,cach_avg_notnull,+',
733                         'CDEF:free_acc=cach_acc,free_avg,+',
734                         "AREA:free_acc#$HalfGreen",
735                         "AREA:cach_acc#$HalfBlue",
736                         "AREA:resv_acc#$HalfYellow",
737                         "AREA:used_acc#$HalfRed",
738                         "LINE1:free_acc#$FullGreen:Free    ",
739                         'GPRINT:free_min:MIN:%5.1lf%s Min,',
740                         'GPRINT:free_avg:AVERAGE:%5.1lf%s Avg,',
741                         'GPRINT:free_max:MAX:%5.1lf%s Max,',
742                         'GPRINT:free_avg:LAST:%5.1lf%s Last\n',
743                         "LINE1:cach_acc#$FullBlue:Cached  ",
744                         'GPRINT:cach_min:MIN:%5.1lf%s Min,',
745                         'GPRINT:cach_avg:AVERAGE:%5.1lf%s Avg,',
746                         'GPRINT:cach_max:MAX:%5.1lf%s Max,',
747                         'GPRINT:cach_avg:LAST:%5.1lf%s Last\l',
748                         "LINE1:resv_acc#$FullYellow:Reserved",
749                         'GPRINT:resv_min:MIN:%5.1lf%s Min,',
750                         'GPRINT:resv_avg:AVERAGE:%5.1lf%s Avg,',
751                         'GPRINT:resv_max:MAX:%5.1lf%s Max,',
752                         'GPRINT:resv_avg:LAST:%5.1lf%s Last\n',
753                         "LINE1:used_acc#$FullRed:Used    ",
754                         'GPRINT:used_min:MIN:%5.1lf%s Min,',
755                         'GPRINT:used_avg:AVERAGE:%5.1lf%s Avg,',
756                         'GPRINT:used_max:MAX:%5.1lf%s Max,',
757                         'GPRINT:used_avg:LAST:%5.1lf%s Last\l'
758                 ],
759                 temperature => [
760                         'DEF:temp_avg={file}:value:AVERAGE',
761                         'DEF:temp_min={file}:value:MIN',
762                         'DEF:temp_max={file}:value:MAX',
763                         "AREA:temp_max#$HalfBlue",
764                         "AREA:temp_min#$Canvas",
765                         "LINE1:temp_avg#$FullBlue:Value",
766                         'GPRINT:temp_min:MIN:%4.1lf Min,',
767                         'GPRINT:temp_avg:AVERAGE:%4.1lf Avg,',
768                         'GPRINT:temp_max:MAX:%4.1lf Max,',
769                         'GPRINT:temp_avg:LAST:%4.1lf Last\l'
770                 ],
771                 timeleft => [
772                         'DEF:avg={file}:timeleft:AVERAGE',
773                         'DEF:min={file}:timeleft:MIN',
774                         'DEF:max={file}:timeleft:MAX',
775                         "AREA:max#$HalfBlue",
776                         "AREA:min#$Canvas",
777                         "LINE1:avg#$FullBlue:Time left [min]",
778                         'GPRINT:min:MIN:%5.1lf%s Min,',
779                         'GPRINT:avg:AVERAGE:%5.1lf%s Avg,',
780                         'GPRINT:max:MAX:%5.1lf%s Max,',
781                         'GPRINT:avg:LAST:%5.1lf%s Last\l'
782                 ],
783                 time_offset => [ # NTPd
784                         'DEF:s_avg={file}:seconds:AVERAGE',
785                         'DEF:s_min={file}:seconds:MIN',
786                         'DEF:s_max={file}:seconds:MAX',
787                         "AREA:s_max#$HalfBlue",
788                         "AREA:s_min#$Canvas",
789                         "LINE1:s_avg#$FullBlue:{inst}",
790                         'GPRINT:s_min:MIN:%7.3lf%s Min,',
791                         'GPRINT:s_avg:AVERAGE:%7.3lf%s Avg,',
792                         'GPRINT:s_max:MAX:%7.3lf%s Max,',
793                         'GPRINT:s_avg:LAST:%7.3lf%s Last'
794                 ],
795                 traffic => ['DEF:out_min_raw={file}:outgoing:MIN',
796                         'DEF:out_avg_raw={file}:outgoing:AVERAGE',
797                         'DEF:out_max_raw={file}:outgoing:MAX',
798                         'DEF:inc_min_raw={file}:incoming:MIN',
799                         'DEF:inc_avg_raw={file}:incoming:AVERAGE',
800                         'DEF:inc_max_raw={file}:incoming:MAX',
801                         'CDEF:out_min=out_min_raw,8,*',
802                         'CDEF:out_avg=out_avg_raw,8,*',
803                         'CDEF:out_max=out_max_raw,8,*',
804                         'CDEF:inc_min=inc_min_raw,8,*',
805                         'CDEF:inc_avg=inc_avg_raw,8,*',
806                         'CDEF:inc_max=inc_max_raw,8,*',
807                         'CDEF:overlap=out_avg,inc_avg,GT,inc_avg,out_avg,IF',
808                         'CDEF:mytime=out_avg_raw,TIME,TIME,IF',
809                         'CDEF:sample_len_raw=mytime,PREV(mytime),-',
810                         'CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF',
811                         'CDEF:out_avg_sample=out_avg_raw,UN,0,out_avg_raw,IF,sample_len,*',
812                         'CDEF:out_avg_sum=PREV,UN,0,PREV,IF,out_avg_sample,+',
813                         'CDEF:inc_avg_sample=inc_avg_raw,UN,0,inc_avg_raw,IF,sample_len,*',
814                         'CDEF:inc_avg_sum=PREV,UN,0,PREV,IF,inc_avg_sample,+',
815                         "AREA:out_avg#$HalfGreen",
816                         "AREA:inc_avg#$HalfBlue",
817                         "AREA:overlap#$HalfBlueGreen",
818                         "LINE1:out_avg#$FullGreen:Outgoing",
819                         'GPRINT:out_avg:AVERAGE:%5.1lf%s Avg,',
820                         'GPRINT:out_max:MAX:%5.1lf%s Max,',
821                         'GPRINT:out_avg:LAST:%5.1lf%s Last',
822                         'GPRINT:out_avg_sum:LAST:(ca. %5.1lf%sB Total)\l',
823                         "LINE1:inc_avg#$FullBlue:Incoming",
824                         #'GPRINT:inc_min:MIN:%5.1lf %s Min,',
825                         'GPRINT:inc_avg:AVERAGE:%5.1lf%s Avg,',
826                         'GPRINT:inc_max:MAX:%5.1lf%s Max,',
827                         'GPRINT:inc_avg:LAST:%5.1lf%s Last',
828                         'GPRINT:inc_avg_sum:LAST:(ca. %5.1lf%sB Total)\l'
829                 ],
830                 cpufreq => [
831                         'DEF:cpufreq_avg={file}:value:AVERAGE',
832                         'DEF:cpufreq_min={file}:value:MIN',
833                         'DEF:cpufreq_max={file}:value:MAX',
834                         "AREA:cpufreq_max#$HalfBlue",
835                         "AREA:cpufreq_min#$Canvas",
836                         "LINE1:cpufreq_avg#$FullBlue:Frequency",
837                         'GPRINT:cpufreq_min:MIN:%5.1lf%s Min,',
838                         'GPRINT:cpufreq_avg:AVERAGE:%5.1lf%s Avg,',
839                         'GPRINT:cpufreq_max:MAX:%5.1lf%s Max,',
840                         'GPRINT:cpufreq_avg:LAST:%5.1lf%s Last\l'
841                 ],
842                 users => [
843                             'DEF:users_avg={file}:users:AVERAGE',
844                             'DEF:users_min={file}:users:MIN',
845                             'DEF:users_max={file}:users:MAX',
846                             "AREA:users_max#$HalfBlue",
847                             "AREA:users_min#$Canvas",
848                             "LINE1:users_avg#$FullBlue:Users",
849                             'GPRINT:users_min:MIN:%4.1lf Min,',
850                             'GPRINT:users_avg:AVERAGE:%4.1lf Average,',
851                             'GPRINT:users_max:MAX:%4.1lf Max,',
852                             'GPRINT:users_avg:LAST:%4.1lf Last\l'
853                 ],
854                 voltage => [
855                         'DEF:avg={file}:voltage:AVERAGE',
856                         'DEF:min={file}:voltage:MIN',
857                         'DEF:max={file}:voltage:MAX',
858                         "AREA:max#$HalfBlue",
859                         "AREA:min#$Canvas",
860                         "LINE1:avg#$FullBlue:Voltage",
861                         'GPRINT:min:MIN:%5.1lf%sV Min,',
862                         'GPRINT:avg:AVERAGE:%5.1lf%sV Avg,',
863                         'GPRINT:max:MAX:%5.1lf%sV Max,',
864                         'GPRINT:avg:LAST:%5.1lf%sV Last\l'
865                 ],
866                 vs_threads => [
867                         "DEF:total_avg={file}:total:AVERAGE",
868                         "DEF:total_min={file}:total:MIN",
869                         "DEF:total_max={file}:total:MAX",
870                         "DEF:running_avg={file}:running:AVERAGE",
871                         "DEF:running_min={file}:running:MIN",
872                         "DEF:running_max={file}:running:MAX",
873                         "DEF:uninterruptible_avg={file}:uninterruptible:AVERAGE",
874                         "DEF:uninterruptible_min={file}:uninterruptible:MIN",
875                         "DEF:uninterruptible_max={file}:uninterruptible:MAX",
876                         "DEF:onhold_avg={file}:onhold:AVERAGE",
877                         "DEF:onhold_min={file}:onhold:MIN",
878                         "DEF:onhold_max={file}:onhold:MAX",
879                         "LINE1:total_avg#$FullYellow:Total   ",
880                         'GPRINT:total_min:MIN:%5.1lf Min,',
881                         'GPRINT:total_avg:AVERAGE:%5.1lf Avg.,',
882                         'GPRINT:total_max:MAX:%5.1lf Max,',
883                         'GPRINT:total_avg:LAST:%5.1lf Last\l',
884                         "LINE1:running_avg#$FullRed:Running ",
885                         'GPRINT:running_min:MIN:%5.1lf Min,',
886                         'GPRINT:running_avg:AVERAGE:%5.1lf Avg.,',          
887                         'GPRINT:running_max:MAX:%5.1lf Max,',
888                         'GPRINT:running_avg:LAST:%5.1lf Last\l',
889                         "LINE1:uninterruptible_avg#$FullGreen:Unintr  ",
890                         'GPRINT:uninterruptible_min:MIN:%5.1lf Min,',
891                         'GPRINT:uninterruptible_avg:AVERAGE:%5.1lf Avg.,',
892                         'GPRINT:uninterruptible_max:MAX:%5.1lf Max,',
893                         'GPRINT:uninterruptible_avg:LAST:%5.1lf Last\l',
894                         "LINE1:onhold_avg#$FullBlue:Onhold  ",
895                         'GPRINT:onhold_min:MIN:%5.1lf Min,',
896                         'GPRINT:onhold_avg:AVERAGE:%5.1lf Avg.,',
897                         'GPRINT:onhold_max:MAX:%5.1lf Max,',
898                         'GPRINT:onhold_avg:LAST:%5.1lf Last\l'
899                 ],
900                 vs_memory => [
901                         'DEF:vm_avg={file}:vm:AVERAGE',
902                         'DEF:vm_min={file}:vm:MIN',
903                         'DEF:vm_max={file}:vm:MAX',
904                         'DEF:vml_avg={file}:vml:AVERAGE',
905                         'DEF:vml_min={file}:vml:MIN',
906                         'DEF:vml_max={file}:vml:MAX',
907                         'DEF:rss_avg={file}:rss:AVERAGE',
908                         'DEF:rss_min={file}:rss:MIN',
909                         'DEF:rss_max={file}:rss:MAX',
910                         'DEF:anon_avg={file}:anon:AVERAGE',
911                         'DEF:anon_min={file}:anon:MIN',
912                         'DEF:anon_max={file}:anon:MAX',
913                         "LINE1:vm_avg#$FullYellow:VM     ",
914                         'GPRINT:vm_min:MIN:%5.1lf%s Min,',
915                         'GPRINT:vm_avg:AVERAGE:%5.1lf%s Avg.,',
916                         'GPRINT:vm_max:MAX:%5.1lf%s Avg.,',
917                         'GPRINT:vm_avg:LAST:%5.1lf%s Last\l',
918                         "LINE1:vml_avg#$FullRed:Locked ",
919                         'GPRINT:vml_min:MIN:%5.1lf%s Min,',
920                         'GPRINT:vml_avg:AVERAGE:%5.1lf%s Avg.,',
921                         'GPRINT:vml_max:MAX:%5.1lf%s Avg.,',
922                         'GPRINT:vml_avg:LAST:%5.1lf%s Last\l',
923                         "LINE1:rss_avg#$FullGreen:RSS    ",
924                         'GPRINT:rss_min:MIN:%5.1lf%s Min,',
925                         'GPRINT:rss_avg:AVERAGE:%5.1lf%s Avg.,',
926                         'GPRINT:rss_max:MAX:%5.1lf%s Avg.,',
927                         'GPRINT:rss_avg:LAST:%5.1lf%s Last\l',
928                         "LINE1:anon_avg#$FullBlue:Anon.  ",
929                         'GPRINT:anon_min:MIN:%5.1lf%s Min,',
930                         'GPRINT:anon_avg:AVERAGE:%5.1lf%s Avg.,',
931                         'GPRINT:anon_max:MAX:%5.1lf%s Avg.,',
932                         'GPRINT:anon_avg:LAST:%5.1lf%s Last\l',
933                 ],
934                 vs_processes => [
935                         'DEF:proc_avg={file}:total:AVERAGE',
936                         'DEF:proc_min={file}:total:MIN',
937                         'DEF:proc_max={file}:total:MAX',
938                         "AREA:proc_max#$HalfBlue",
939                         "AREA:proc_min#$Canvas",
940                         "LINE1:proc_avg#$FullBlue:Processes",
941                         'GPRINT:proc_min:MIN:%4.1lf Min,',
942                         'GPRINT:proc_avg:AVERAGE:%4.1lf Avg.,',
943                         'GPRINT:proc_max:MAX:%4.1lf Max,',
944                         'GPRINT:proc_avg:LAST:%4.1lf Last\l'
945                 ],
946         };
947         $GraphDefs->{'disk'} = $GraphDefs->{'partition'};
948         $GraphDefs->{'if_errors'} = $GraphDefs->{'if_packets'};
949         $GraphDefs->{'meminfo'} = $GraphDefs->{'memory'};
950         $GraphDefs->{'sensors'} = $GraphDefs->{'temperature'};
951
952         $GraphDefs->{'delay'}           = $GraphDefs->{'time_offset'};
953         $GraphDefs->{'time_dispersion'} = $GraphDefs->{'time_offset'};
954 }
955
956 our $GraphArgs =
957 {
958         apache_bytes => ['-t', 'apache traffic', '-v', 'Bit/s'],
959         apache_requests => ['-t', 'apache requests', '-v', 'Requests/s'],
960         apache_scoreboard => ['-t', 'apache scoreboard {inst}', '-v', 'Processes'],
961         charge => ['-t', '{host} charge', '-v', 'Ampere hours'],
962         charge_percent => ['-t', '{host} charge', '-v', 'Percent'],
963         cpu => ['-t', '{host} cpu{inst} usage', '-v', 'Percent', '-l', '0'],
964         cpufreq => ['-t', '{host} cpu{inst} usage', '-v', 'Mhz'],
965         current => ['-t', '{host} current', '-v', 'Ampere'],
966         #disk => ['-t', '{host} disk {inst} IO wait', '-v', 'Seconds'],
967         delay => ['-t', 'NTPd peer delay ({inst})', '-v', 'Seconds'],
968         df => ['-t', '{host}:{inst} usage', '-v', 'Percent', '-l', '0'],
969         disk => ['-t', '{host} disk {inst} usage', '-v', 'Byte/s'],
970         fanspeed => ['-t', '{host} fanspeed {inst}', '-v', 'rpm'],
971         frequency_offset => ['-t', 'NTPd frequency offset ({inst})', '-v', 'Parts per million'],
972         hddtemp => ['-t', '{host} hdd temperature {inst}', '-v', '°Celsius'],
973         if_errors => ['-t', '{host} {inst} errors', '-v', 'Errors/s'],
974         if_packets => ['-t', '{host} {inst} packets', '-v', 'Packets/s'],
975         load => ['-t', '{host} load average', '-v', 'System load', '-X', '0'],
976         load_percent => ['-t', '{host} load', '-v', 'Percent'],
977         mails   => ['-t', '{host} mail count', '-v', 'Amount', '-X', '0'],
978         memory => ['-t', '{host} memory usage', '-v', 'Bytes', '-b', '1024', '-l', '0'],
979         mysql_commands => ['-t', 'mysql command {inst}', '-v', 'Issues/s' ],
980         mysql_handler => ['-t', 'mysql handler {inst}', '-v', 'Issues/s' ],
981         mysql_qcache => ['-t', 'mysql query cache', '-v', 'Queries/s' ],
982         mysql_threads => ['-t', 'mysql threads', '-v', 'Threads' ],
983         nfs3_procedures => ['-t', '{host} NFSv3 {inst} procedures', '-v', 'Procedures/s' ],
984         partition => ['-t', '{host} partition {inst} usage', '-v', 'Byte/s'],
985         ping => ['-t', '{host} ping to {inst}', '-v', 'ms'],
986         processes => ['-t', '{host} processes', '-v', 'Processes'],
987         sensors => ['-t', '{host} sensor {inst}', '-v', '°Celsius'],
988         swap => ['-t', '{host} swap usage', '-v', 'Bytes', '-b', '1024', '-l', '0'],
989         temperature => ['-t', '{host} temperature {inst}', '-v', '°Celsius'],
990         timeleft => ['-t', '{host} UPS time left', '-v', 'Time [min]'],
991         time_offset => ['-t', 'NTPd time offset ({inst})', '-v', 'Seconds'],
992         time_dispersion => ['-t', 'NTPd time dispersion ({inst})', '-v', 'Seconds'],
993         traffic => ['-t', '{host} {inst} traffic', '-v', 'Bit/s'],
994         users => ['-t', '{host} users', '-v', 'Users'],
995         voltage => ['-t', '{host} voltage', '-v', 'Volts'],
996         vs_threads => ['-t', '{host} threads', '-v', 'Threads'],
997         vs_memory => ['-t', '{host} memory usage', '-v', 'Bytes'],
998         vs_processes => ['-t', '{host} processes', '-v', 'Processes'],
999 };
1000
1001 our $GraphMulti =
1002 {
1003         apache_scoreboard => \&output_graph_apache_scoreboard,
1004         cpu     => \&output_graph_cpu,
1005         cpufreq => 1,
1006         disk    => 1,
1007         load    => 0,
1008         mails   => 0,
1009         memory  => 0,
1010         mysql_commands => \&output_graph_mysql_commands,
1011         mysql_handler => \&output_graph_mysql_handler,
1012         partition => 1,
1013         ping    => \&output_graph_ping,
1014         sensors => 1,
1015         traffic => 1,
1016         users => 1
1017 };
1018
1019 our @Info;
1020 if (defined ($ENV{'GATEWAY_INTERFACE'}))
1021 {
1022         @Info = ($ENV{'PATH_INFO'} || '') =~ m#([\w\-\.]+)#g;
1023 }
1024 else
1025 {
1026         @Info = @ARGV;
1027 }
1028
1029 parse_pathinfo (@Info);
1030
1031 if ($TimeSpan)
1032 {
1033         output_graph ();
1034 }
1035 else
1036 {
1037         output_page ();
1038 }
1039
1040 exit (0);
1041
1042 sub output_graph_cpu
1043 {
1044         my @inst = @_;
1045         my @ret = ();
1046
1047         die if (@inst < 2);
1048
1049         for (@inst)
1050         {
1051                 push (@ret,
1052                         "DEF:user_avg_$_=$AbsDir/cpu-$_.rrd:user:AVERAGE",
1053                         "DEF:user_min_$_=$AbsDir/cpu-$_.rrd:user:MIN",
1054                         "DEF:user_max_$_=$AbsDir/cpu-$_.rrd:user:MAX",
1055                         "DEF:nice_avg_$_=$AbsDir/cpu-$_.rrd:nice:AVERAGE",
1056                         "DEF:nice_min_$_=$AbsDir/cpu-$_.rrd:nice:MIN",
1057                         "DEF:nice_max_$_=$AbsDir/cpu-$_.rrd:nice:MAX",
1058                         "DEF:syst_avg_$_=$AbsDir/cpu-$_.rrd:syst:AVERAGE",
1059                         "DEF:syst_min_$_=$AbsDir/cpu-$_.rrd:syst:MIN",
1060                         "DEF:syst_max_$_=$AbsDir/cpu-$_.rrd:syst:MAX",
1061                         "DEF:wait_avg_$_=$AbsDir/cpu-$_.rrd:wait:AVERAGE",
1062                         "DEF:wait_min_$_=$AbsDir/cpu-$_.rrd:wait:MIN",
1063                         "DEF:wait_max_$_=$AbsDir/cpu-$_.rrd:wait:MAX");
1064         }
1065
1066         for (qw(user nice syst wait))
1067         {
1068                 my $def = $_;
1069                 my $cdef;
1070
1071                 my $default_value = ($def eq 'user' or $def eq 'syst') ? 'UNKN' : '0';
1072
1073                 for (qw(avg min max))
1074                 {
1075                         my $cf = $_;
1076
1077                         for (@inst)
1078                         {
1079                                 push (@ret, "CDEF:${def}_${cf}_notnull_${_}=${def}_${cf}_${_},UN,0,${def}_${cf}_${_},IF");
1080                                 push (@ret, "CDEF:${def}_${cf}_defined_${_}=${def}_${cf}_${_},UN,0,1,IF");
1081                         }
1082
1083                         $cdef = "CDEF:${def}_${cf}_num=" . join (',', map { "${def}_${cf}_defined_${_}" } (@inst));
1084                         $cdef .= ',+' x (scalar (@inst) - 1);
1085                         push (@ret, $cdef);
1086
1087                         $cdef = "CDEF:${def}_${cf}=${def}_${cf}_num," . join (',', map { "${def}_${cf}_notnull_${_}" } (@inst));
1088                         $cdef .= ',+' x (scalar (@inst) - 1);
1089                         $cdef .= ",${def}_${cf}_num,${def}_${cf}_num,1,IF,/,$default_value,IF";
1090                         push (@ret, $cdef);
1091                         push (@ret, "CDEF:${def}_${cf}_notnull=${def}_${cf},UN,0,${def}_${cf},IF");
1092                 }
1093         }
1094
1095         push (@ret,
1096                 "CDEF:nice_acc=syst_avg_notnull,wait_avg_notnull,user_avg_notnull,nice_avg_notnull,+,+,+",
1097                 "CDEF:user_acc=syst_avg_notnull,wait_avg_notnull,user_avg_notnull,+,+",
1098                 "CDEF:wait_acc=syst_avg_notnull,wait_avg_notnull,+",
1099                 "CDEF:syst_acc=syst_avg_notnull");
1100
1101         push (@ret, grep { $_ !~ m/^C?DEF/ } (@{$GraphDefs->{'cpu'}}));
1102
1103         return (@ret);
1104 }
1105
1106 sub output_graph_apache_scoreboard
1107 {
1108         my @inst = @_;
1109         my @ret = ();
1110
1111         die if (@inst < 2);
1112
1113         my @colors = get_n_colors (scalar (@inst));
1114
1115         for (my $i = 0; $i < scalar (@inst); $i++)
1116         {
1117                 my $inst = $inst[$i];
1118                 push (@ret,
1119                         "DEF:avg_$i=$AbsDir/apache_scoreboard-$inst.rrd:count:AVERAGE",
1120                         "DEF:min_$i=$AbsDir/apache_scoreboard-$inst.rrd:count:MIN",
1121                         "DEF:max_$i=$AbsDir/apache_scoreboard-$inst.rrd:count:MAX");
1122         }
1123
1124         for (my $i = 0; $i < scalar (@inst); $i++)
1125         {
1126                 my $inst = $inst[$i];
1127                 my $color = $colors[$i];
1128
1129                 if (length ($inst) > 15)
1130                 {
1131                         $inst = substr ($inst, 0, 12) . '...';
1132                 }
1133                 else
1134                 {
1135                         $inst = sprintf ('%-15s', $inst);
1136                 }
1137
1138                 push (@ret,
1139                         "LINE1:avg_$i#$color:$inst",
1140                         "GPRINT:min_$i:MIN:%6.2lf Min,",
1141                         "GPRINT:avg_$i:AVERAGE:%6.2lf Avg,",
1142                         "GPRINT:max_$i:MAX:%6.2lf Max,",
1143                         "GPRINT:avg_$i:LAST:%6.2lf Last\\l");
1144         }
1145
1146         return (@ret);
1147 }
1148
1149 sub output_graph_ping
1150 {
1151         my @inst = @_;
1152         my @ret = ();
1153
1154         die if (@inst < 2);
1155
1156         my @colors = get_n_colors (scalar (@inst));
1157
1158         for (my $i = 0; $i < scalar (@inst); $i++)
1159         {
1160                 my $inst = $inst[$i];
1161                 push (@ret,
1162                         "DEF:avg_$i=$AbsDir/ping-$inst.rrd:ping:AVERAGE",
1163                         "DEF:min_$i=$AbsDir/ping-$inst.rrd:ping:MIN",
1164                         "DEF:max_$i=$AbsDir/ping-$inst.rrd:ping:MAX");
1165         }
1166
1167         for (my $i = 0; $i < scalar (@inst); $i++)
1168         {
1169                 my $inst = $inst[$i];
1170                 my $color = $colors[$i];
1171
1172                 if (length ($inst) > 15)
1173                 {
1174                         $inst = substr ($inst, 0, 12) . '...';
1175                 }
1176                 else
1177                 {
1178                         $inst = sprintf ('%-15s', $inst);
1179                 }
1180
1181                 push (@ret,
1182                         "LINE1:avg_$i#$color:$inst",
1183                         "GPRINT:min_$i:MIN:%4.1lf ms Min,",
1184                         "GPRINT:avg_$i:AVERAGE:%4.1lf ms Avg,",
1185                         "GPRINT:max_$i:MAX:%4.1lf ms Max,",
1186                         "GPRINT:avg_$i:LAST:%4.1lf ms Last\\l");
1187         }
1188
1189         return (@ret);
1190 }
1191
1192 sub output_graph_mysql_commands
1193 {
1194         my @inst = @_;
1195         my @ret = ();
1196
1197         die if (@inst < 2);
1198
1199         my @colors = get_n_colors (scalar (@inst));
1200
1201         for (my $i = 0; $i < scalar (@inst); $i++)
1202         {
1203                 my $inst = $inst[$i];
1204                 push (@ret,
1205                         "DEF:avg_$i=$AbsDir/mysql_commands-$inst.rrd:value:AVERAGE",
1206                         "DEF:min_$i=$AbsDir/mysql_commands-$inst.rrd:value:MIN",
1207                         "DEF:max_$i=$AbsDir/mysql_commands-$inst.rrd:value:MAX");
1208         }
1209
1210         for (my $i = 0; $i < scalar (@inst); $i++)
1211         {
1212                 my $inst = $inst[$i];
1213                 my $color = $colors[$i];
1214
1215                 if (length ($inst) > 18)
1216                 {
1217                         $inst = substr ($inst, 0, 15) . '...';
1218                 }
1219                 else
1220                 {
1221                         $inst = sprintf ('%-18s', $inst);
1222                 }
1223
1224                 push (@ret,
1225                         "LINE1:avg_$i#$color:$inst",
1226                         "GPRINT:min_$i:MIN:%6.1lf Min,",
1227                         "GPRINT:avg_$i:AVERAGE:%6.1lf Avg,",
1228                         "GPRINT:max_$i:MAX:%6.1lf Max,",
1229                         "GPRINT:avg_$i:LAST:%6.1lf Last\\l");
1230         }
1231
1232         return (@ret);
1233 }
1234
1235 sub output_graph_mysql_handler
1236 {
1237         my @inst = @_;
1238         my @ret = ();
1239
1240         die if (@inst < 2);
1241
1242         my @colors = get_n_colors (scalar (@inst));
1243
1244         for (my $i = 0; $i < scalar (@inst); $i++)
1245         {
1246                 my $inst = $inst[$i];
1247                 push (@ret,
1248                         "DEF:avg_$i=$AbsDir/mysql_handler-$inst.rrd:value:AVERAGE",
1249                         "DEF:min_$i=$AbsDir/mysql_handler-$inst.rrd:value:MIN",
1250                         "DEF:max_$i=$AbsDir/mysql_handler-$inst.rrd:value:MAX");
1251         }
1252
1253         for (my $i = 0; $i < scalar (@inst); $i++)
1254         {
1255                 my $inst = $inst[$i];
1256                 my $color = $colors[$i];
1257
1258                 if (length ($inst) > 18)
1259                 {
1260                         $inst = substr ($inst, 0, 15) . '...';
1261                 }
1262                 else
1263                 {
1264                         $inst = sprintf ('%-18s', $inst);
1265                 }
1266
1267                 push (@ret,
1268                         "LINE1:avg_$i#$color:$inst",
1269                         "GPRINT:min_$i:MIN:%6.1lf Min,",
1270                         "GPRINT:avg_$i:AVERAGE:%6.1lf Avg,",
1271                         "GPRINT:max_$i:MAX:%6.1lf Max,",
1272                         "GPRINT:avg_$i:LAST:%6.1lf Last\\l");
1273         }
1274
1275         return (@ret);
1276 }
1277
1278 sub output_graph
1279 {
1280         die unless (defined ($GraphDefs->{$Type}));
1281
1282         my $host;
1283         my @cmd = ();
1284         my $file = $AbsDir . '/';
1285         my $files = get_all_files ($AbsDir);
1286
1287         #
1288         # get hostname
1289         #
1290         if ($RelDir =~ m#([^/]+)$#)
1291         {
1292                 $host = $1;
1293         }
1294         else
1295         {
1296                 $host = $Config->{'HostName'};
1297         }
1298
1299         #
1300         # get timespan
1301         #
1302         if ($TimeSpan =~ m/(\d+)/)
1303         {
1304                 $TimeSpan = -1 * int ($1);
1305         }
1306         else
1307         {
1308                 my %t = (hour => -3600, day => -86400, week => -604800, month => -2678400, year => -31622400);
1309                 die unless (defined ($t{$TimeSpan}));
1310                 $TimeSpan = $t{$TimeSpan};
1311         }
1312
1313         if (scalar (@{$files->{$Type}}) == 1)
1314         {
1315                 $Inst = $files->{$Type}[0];
1316         }
1317
1318         #push (@cmd, '-', '-a', 'PNG', '-s', $TimeSpan, '-w', 800, '-h', 150);
1319         push (@cmd, '-', '-a', 'PNG', '-s', $TimeSpan);
1320         push (@cmd, @{$GraphArgs->{$Type}}) if (defined ($GraphArgs->{$Type}));
1321
1322         for (qw(Back ShadeA ShadeB Font Canvas Grid MGrid Frame Arrow))
1323         {
1324                 push (@cmd, '-c', uc ($_) . '#' . $Config->{'Colors'}{$_});
1325         }
1326
1327         if ((length ($Inst) == 0) and (ref ($GraphMulti->{$Type}) eq 'CODE'))
1328         {
1329                 push (@cmd, $GraphMulti->{$Type}->(@{$files->{$Type}}));
1330         }
1331         else
1332         {
1333                 if (length ("$Inst"))
1334                 {
1335                         $file .= "$Type-$Inst.rrd";
1336                 }
1337                 else
1338                 {
1339                         $file .= "$Type.rrd";
1340                 }
1341
1342                 die ("File not found: $file") unless (-e $file);
1343
1344                 push (@cmd, @{$GraphDefs->{$Type}});
1345         }
1346
1347         for (@cmd)
1348         {
1349                 $_ =~ s/{file}/$file/g;
1350                 $_ =~ s/{host}/$host/g;
1351                 $_ =~ s/{inst}/$Inst/g;
1352                 $_ =~ s/{type}/$Type/g;
1353         }
1354
1355         $| = 1;
1356
1357         print STDOUT <<HEADER if (defined ($ENV{'GATEWAY_INTERFACE'}));
1358 Content-Type: image/png
1359 Cache-Control: no-cache
1360
1361 HEADER
1362
1363         if (1)
1364         {
1365                 my $fh;
1366                 open ($fh, ">/tmp/collection.log") or die ("open: $!");
1367                 flock ($fh, LOCK_EX) or die ("flock: $!");
1368
1369                 print $fh join ("\n\t", @cmd) . "\n";
1370
1371                 close ($fh);
1372         }
1373
1374         RRDs::graph (@cmd);
1375
1376         die ('RRDs::error: ' . RRDs::error ()) if (RRDs::error ());
1377 }
1378
1379 sub output_page
1380 {
1381         my $files = get_all_files ($AbsDir);
1382         my $dirs  = get_all_dirs  ($AbsDir);
1383
1384         print STDOUT <<HEADER if (defined ($ENV{'GATEWAY_INTERFACE'}));
1385 Content-Type: text/html
1386 Cache-Control: no-cache
1387
1388 <html>
1389         <head>
1390                 <title>Collection: $RelDir</title>
1391                 <style type="text/css">
1392                         img { border: none; display: block; }
1393                 </style>
1394         </head>
1395
1396         <body>
1397 HEADER
1398
1399         my $MySelf = defined ($ENV{'GATEWAY_INTERFACE'}) ? $ENV{'SCRIPT_NAME'} : $0;
1400
1401         if ((length ($Type) != 0) and (length ($Inst) == 0) and (ref ($GraphMulti->{$Type}) eq 'CODE') and (scalar (@{$files->{$Type}}) > 1))
1402         {
1403                 print qq(\t\t<div><a href="$MySelf$RelDir">Go up</a></div>\n);
1404
1405                 print "\t\t<ul>\n";
1406                 for (@{$files->{$Type}})
1407                 {
1408                         print qq(\t\t\t<li><a href="$MySelf$RelDir/$Type/$_">$_</a></li>\n);
1409                 }
1410                 print <<HTML;
1411                 </ul>
1412
1413                 <h3>Hourly</h3>
1414                 <div><img src="$MySelf$RelDir/$Type/hour" /></div>
1415                 <h3>Daily</h3>
1416                 <div><img src="$MySelf$RelDir/$Type/day" /></div>
1417                 <h3>Weekly</h3>
1418                 <div><img src="$MySelf$RelDir/$Type/week" /></div>
1419                 <h3>Monthly</h3>
1420                 <div><img src="$MySelf$RelDir/$Type/month" /></div>
1421                 <h3>Yearly</h3>
1422                 <div><img src="$MySelf$RelDir/$Type/year" /></div>
1423 HTML
1424         }
1425         elsif (length ($Type) != 0)
1426         {
1427                 my $ext = length ($Inst) ? "$Type/$Inst" : $Type;
1428
1429                 if ((ref ($GraphMulti->{$Type}) eq 'CODE') and (scalar (@{$files->{$Type}}) > 1))
1430                 {
1431                         print qq(<div><a href="$MySelf$RelDir/$Type">Go up</a></div>\n);
1432                 }
1433                 else
1434                 {
1435                         print qq(<div><a href="$MySelf$RelDir">Go up</a></div>\n);
1436                 }
1437
1438                 print <<HTML;
1439                 <h3>Hourly</h3>
1440                 <div><img src="$MySelf$RelDir/$ext/hour" /></div>
1441                 <h3>Daily</h3>
1442                 <div><img src="$MySelf$RelDir/$ext/day" /></div>
1443                 <h3>Weekly</h3>
1444                 <div><img src="$MySelf$RelDir/$ext/week" /></div>
1445                 <h3>Monthly</h3>
1446                 <div><img src="$MySelf$RelDir/$ext/month" /></div>
1447                 <h3>Yearly</h3>
1448                 <div><img src="$MySelf$RelDir/$ext/year" /></div>
1449 HTML
1450         }
1451         else
1452         {
1453                 if ($RelDir)
1454                 {
1455                         my ($up) = $RelDir =~ m#(.*)/[^/]+$#;
1456                         print qq(\t\t<div><a href="$MySelf$up">Go up</a></div>\n);
1457                 }
1458
1459                 if (@$dirs)
1460                 {
1461                         print "<ul>\n";
1462                         for (@$dirs)
1463                         {
1464                                 print qq(<li>$AbsDir/<a href="$MySelf$RelDir/$_">$_</a></li>\n);
1465                         }
1466                         print "</ul>\n";
1467                 }
1468
1469                 for (sort (keys %$files))
1470                 {
1471                         my $type = $_;
1472
1473                         if (ref ($GraphMulti->{$type}) eq 'CODE')
1474                         {
1475                                 print qq(\t\t<a href="$MySelf$RelDir/$type" />),
1476                                 qq(<img src="$MySelf$RelDir/$type/day" /></a>\n);
1477                                 next;
1478                         }
1479
1480                         for (@{$files->{$type}})
1481                         {
1482                                 my $inst = "$_";
1483
1484                                 if (length ($inst))
1485                                 {
1486                                         print qq(\t\t<a href="$MySelf$RelDir/$type/$inst" />),
1487                                         qq(<img src="$MySelf$RelDir/$type/$inst/day" /></a>\n);
1488                                 }
1489                                 else
1490                                 {
1491                                         print qq(\t\t<a href="$MySelf$RelDir/$type" />),
1492                                         qq(<img src="$MySelf$RelDir/$type/day" /></a>\n);
1493                                 }
1494                         }
1495                 }
1496         }
1497
1498         print STDOUT <<FOOTER if (defined ($ENV{'GATEWAY_INTERFACE'}));
1499         </body>
1500 </html>
1501 FOOTER
1502 }
1503
1504 sub output_xml
1505 {
1506         my $files = get_all_files ();
1507
1508         print STDOUT <<HEADER if (defined ($ENV{'GATEWAY_INTERFACE'}));
1509 Content-Type: text/xml
1510 Cache-Control: no-cache
1511
1512 HEADER
1513         print STDOUT pl2xml ($files);
1514 }
1515
1516 sub read_config
1517 {
1518         my $file = @_ ? shift : '/etc/collection.conf';
1519         my $conf;
1520         my $fh;
1521
1522 #       if (open ($fh, "< $file"))
1523 #       {
1524 #               my $xml;
1525 #               local $/ = undef;
1526 #               $xml = <$fh>;
1527 #
1528 #               eval
1529 #               {
1530 #                       $conf = xml2pl ($xml);
1531 #               };
1532 #               close ($fh);
1533 #       }
1534
1535         if (!$conf)
1536         {
1537                 return ({
1538                                 Colors =>
1539                                 {
1540                                         Back    => 'FFFFFF',
1541                                         ShadeA  => 'FFFFFF',
1542                                         ShadeB  => 'FFFFFF',
1543                                         Font    => '000000',
1544                                         Canvas  => 'F5F5F5',
1545                                         Grid    => 'D0D0D0',
1546                                         MGrid   => 'A0A0A0',
1547                                         Frame   => '646464',
1548                                         Arrow   => 'FF0000',
1549
1550                                         FullRed         => 'FF0000',
1551                                         FullBlue        => '0000FF',
1552                                         FullGreen       => '00E000',
1553                                         FullYellow      => 'F0A000',
1554                                         FullCyan        => '00A0FF',
1555                                         FullMagenta     => 'A000FF',
1556                                         Alpha           => 0.25,
1557                                         HalfRed         => 'F8B8B8',
1558                                         HalfBlue        => 'B8B8F8',
1559                                         HalfGreen       => 'B8F0B8',
1560                                         HalfYellow      => 'F4F4B8'
1561                                 },
1562                                 Directory => '/var/lib/collectd',
1563                                 HostName  => (defined ($ENV{'SERVER_NAME'}) ? $ENV{'SERVER_NAME'} : 'localhost')
1564                         });
1565         }
1566         else
1567         {
1568                 return ($conf);
1569         }
1570 }
1571
1572 sub parse_pathinfo
1573 {
1574         my @info = @_;
1575
1576         $AbsDir = $Config->{'Directory'};
1577         $RelDir = '';
1578
1579         while (@info and -d $AbsDir . '/' . $info[0])
1580         {
1581                 my $new = shift (@info);
1582                 next if ($new =~ m/^\./);
1583
1584                 $AbsDir .= '/' . $new;
1585                 $RelDir .= '/' . $new;
1586         }
1587
1588         $Type = '';
1589         $Inst = '';
1590         $TimeSpan = '';
1591
1592         confess ("parse_pathinfo: too many elements in pathinfo") if (scalar (@info) > 3);
1593         return unless (@info);
1594
1595         $Type = shift (@info);
1596         return unless (@info);
1597
1598         if ($info[-1] =~ m/^(hour|day|week|month|year)$/i)
1599         {
1600                 $TimeSpan = pop (@info);
1601         }
1602
1603         $Inst = shift (@info) if (@info);
1604
1605         confess ("unrecognized elements in pathinfo") if (@info);
1606 }
1607
1608 sub get_all_files
1609 {
1610         my $dir = @_ ? shift : $Config->{'Directory'};
1611         my $hash = {};
1612         my $dh;
1613
1614         if (opendir ($dh, $dir))
1615         {
1616                 while (my $thing = readdir ($dh))
1617                 {
1618                         next if ($thing =~ m/^\./);
1619
1620                         my $type;
1621                         my $inst;
1622
1623                         if ($thing =~ m/^(\w+)-([\w\-\.]+)\.rrd$/)
1624                         {
1625                                 $type = $1;
1626                                 $inst = $2;
1627                         }
1628                         elsif ($thing =~ m/^(\w+)\.rrd$/)
1629                         {
1630                                 $type = $1;
1631                                 $inst = '';
1632                         }
1633                         else
1634                         {
1635                                 next;
1636                         }
1637
1638                         # Only load RRD files we can actually display..
1639                         next unless (defined ($GraphDefs->{$type}));
1640
1641                         $hash->{$type} = [] unless (defined ($hash->{$type}));
1642                         push (@{$hash->{$type}}, $inst);
1643                 }
1644
1645                 closedir ($dh);
1646         }
1647
1648         return ($hash);
1649 }
1650
1651 sub get_all_dirs
1652 {
1653         my $dir = @_ ? shift : $Config->{'Directory'};
1654         my @ret = ();
1655         my $dh;
1656
1657         if (opendir ($dh, $dir))
1658         {
1659                 while (my $thing = readdir ($dh))
1660                 {
1661                         next if ($thing =~ m/^\./);
1662
1663                         next if (!-d "$dir/$thing");
1664
1665                         push (@ret, $thing);
1666                 }
1667
1668                 closedir ($dh);
1669         }
1670
1671         return (@ret) if (wantarray ());
1672         return (\@ret);
1673 }
1674
1675 sub color_hex2rgb
1676 {
1677         my $color = shift;
1678
1679         my ($red, $green, $blue) = map { ord (pack ("H2", $_)) } ($color =~ m/([A-Fa-f0-9]{2})/g);
1680         #print STDERR "$color -> rgb($red,$green,$blue)\n";
1681
1682         return ($red, $green, $blue);
1683 }
1684
1685 sub color_rgb2hex
1686 {
1687         croak unless (scalar (@_) == 3);
1688         
1689         my ($red, $green, $blue) = @_;
1690
1691         my $ret = sprintf ("%02X%02X%02X", $red, $green, $blue);
1692         #print STDERR "rgb($red,$green,$blue) -> $ret\n";
1693
1694         return ($ret);
1695 }
1696
1697 sub color_calculate_transparent
1698 {
1699         my $alpha = shift;
1700         my $canvas = [color_hex2rgb (shift)];
1701         my @colors = map { [color_hex2rgb ($_)] } (@_);
1702
1703         if (($alpha < 0.0) or ($alpha > 1.0))
1704         {
1705                 $alpha = 1.0;
1706         }
1707
1708         if ($alpha == 0.0)
1709         {
1710                 return (color_rgb2hex (@$canvas));
1711         }
1712         if ($alpha == 1.0)
1713         {
1714                 return (color_rgb2hex (@{$colors[-1]}));
1715         }
1716
1717         my $ret = _color_calculate_transparent ($alpha, $canvas, @colors);
1718
1719         return (color_rgb2hex (@$ret));
1720 }
1721
1722 sub _color_calculate_transparent
1723 {
1724         my $alpha = shift;
1725         my $canvas = shift;
1726         my $color = shift;
1727         my @colors = @_ ? shift : ();
1728         my $ret = [0, 0, 0];
1729
1730         for (my $i = 0; $i < 3; $i++)
1731         {
1732                 $ret->[$i] = ($alpha * $color->[$i]) + ((1 - $alpha) * $canvas->[$i]);
1733         }
1734
1735         return (_color_calculate_transparent ($alpha, $ret, @colors)) if (@colors);
1736         return ($ret);
1737 }
1738
1739 sub get_n_colors
1740 {
1741         my $num = shift;
1742         my @ret = ();
1743
1744         for (my $i = 0; $i < $num; $i++)
1745         {
1746                 my $pos = 6 * $i / $num;
1747                 my $n = int ($pos);
1748                 my $p = $pos - $n;
1749                 my $q = 1 - $p;
1750
1751                 my $red   = 0;
1752                 my $green = 0;
1753                 my $blue  = 0;
1754
1755                 if ($n == 0)
1756                 {
1757                         $red  = 255;
1758                         $blue = 255 * $p;
1759                 }
1760                 elsif ($n == 1)
1761                 {
1762                         $red  = 255 * $q;
1763                         $blue = 255;
1764                 }
1765                 elsif ($n == 2)
1766                 {
1767                         $green = 255 * $p;
1768                         $blue  = 255;
1769                 }
1770                 elsif ($n == 3)
1771                 {
1772                         $green = 255;
1773                         $blue  = 255 * $q;
1774                 }
1775                 elsif ($n == 4)
1776                 {
1777                         $red   = 255 * $p;
1778                         $green = 255;
1779                 }
1780                 elsif ($n == 5)
1781                 {
1782                         $red   = 255;
1783                         $green = 255 * $q;
1784                 }
1785                 else { die; }
1786
1787                 push (@ret, sprintf ("%02x%02x%02x", $red, $green, $blue));
1788         }
1789
1790         return (@ret);
1791 }