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