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