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