4b31dfbb8682d4bb32a000fd614fa5bb5ad1800a
[collectd.git] / contrib / museum / 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                 cpu => ['DEF:user_avg={file}:user:AVERAGE',
57                         'DEF:user_min={file}:user:MIN',
58                         'DEF:user_max={file}:user:MAX',
59                         'DEF:nice_avg={file}:nice:AVERAGE',
60                         'DEF:nice_min={file}:nice:MIN',
61                         'DEF:nice_max={file}:nice:MAX',
62                         'DEF:syst_avg={file}:syst:AVERAGE',
63                         'DEF:syst_min={file}:syst:MIN',
64                         'DEF:syst_max={file}:syst:MAX',
65                         'DEF:idle_avg={file}:idle:AVERAGE',
66                         'DEF:idle_min={file}:idle:MIN',
67                         'DEF:idle_max={file}:idle:MAX',
68                         'DEF:wait_avg={file}:wait:AVERAGE',
69                         'DEF:wait_min={file}:wait:MIN',
70                         'DEF:wait_max={file}:wait:MAX',
71                         'CDEF:user_avg_notnull=user_avg,UN,0,user_avg,IF',
72                         'CDEF:nice_avg_notnull=nice_avg,UN,0,nice_avg,IF',
73                         'CDEF:syst_avg_notnull=syst_avg,UN,0,syst_avg,IF',
74                         'CDEF:idle_avg_notnull=idle_avg,UN,0,idle_avg,IF',
75                         'CDEF:wait_avg_notnull=wait_avg,UN,0,wait_avg,IF',
76                         'CDEF:totl_avg_notnull=user_avg_notnull,nice_avg_notnull,+,syst_avg_notnull,+,idle_avg_notnull,+,wait_avg_notnull,+',
77                         'CDEF:user_avg_pct=user_avg_notnull,100,*,totl_avg_notnull,/',
78                         'CDEF:nice_avg_pct=nice_avg_notnull,100,*,totl_avg_notnull,/',
79                         'CDEF:syst_avg_pct=syst_avg_notnull,100,*,totl_avg_notnull,/',
80                         'CDEF:wait_avg_pct=wait_avg_notnull,100,*,totl_avg_notnull,/',
81                         'CDEF:nice_acc=syst_avg_pct,wait_avg_pct,user_avg_pct,nice_avg_pct,+,+,+',
82                         'CDEF:user_acc=syst_avg_pct,wait_avg_pct,user_avg_pct,+,+',
83                         'CDEF:wait_acc=syst_avg_pct,wait_avg_pct,+',
84                         'CDEF:syst_acc=syst_avg_pct',
85 #                       'CDEF:nice_acc=syst_avg_notnull,wait_avg_notnull,user_avg_notnull,nice_avg_notnull,+,+,+',
86 #                       'CDEF:user_acc=syst_avg_notnull,wait_avg_notnull,user_avg_notnull,+,+',
87 #                       'CDEF:wait_acc=syst_avg_notnull,wait_avg_notnull,+',
88 #                       'CDEF:syst_acc=syst_avg_notnull',
89                         "AREA:nice_acc#$HalfGreen",
90                         "AREA:user_acc#$HalfBlue",
91                         "AREA:wait_acc#$HalfYellow",
92                         "AREA:syst_acc#$HalfRed",
93                         "LINE1:nice_acc#$FullGreen:Nice   ",
94                         'GPRINT:nice_min:MIN:%5.1lf%% Min,',
95                         'GPRINT:nice_avg:AVERAGE:%5.1lf%% Avg,',
96                         'GPRINT:nice_max:MAX:%5.1lf%% Max,',
97                         'GPRINT:nice_avg:LAST:%5.1lf%% Last\l',
98                         "LINE1:user_acc#$FullBlue:User   ",
99                         'GPRINT:user_min:MIN:%5.1lf%% Min,',
100                         'GPRINT:user_avg:AVERAGE:%5.1lf%% Avg,',
101                         'GPRINT:user_max:MAX:%5.1lf%% Max,',
102                         'GPRINT:user_avg:LAST:%5.1lf%% Last\l',
103                         "LINE1:wait_acc#$FullYellow:Wait-IO",
104                         'GPRINT:wait_min:MIN:%5.1lf%% Min,',
105                         'GPRINT:wait_avg:AVERAGE:%5.1lf%% Avg,',
106                         'GPRINT:wait_max:MAX:%5.1lf%% Max,',
107                         'GPRINT:wait_avg:LAST:%5.1lf%% Last\l',
108                         "LINE1:syst_acc#$FullRed:System ",
109                         'GPRINT:syst_min:MIN:%5.1lf%% Min,',
110                         'GPRINT:syst_avg:AVERAGE:%5.1lf%% Avg,',
111                         'GPRINT:syst_max:MAX:%5.1lf%% Max,',
112                         'GPRINT:syst_avg:LAST:%5.1lf%% Last\l'
113                 ],
114                 disk => [
115                         'DEF:rtime_avg={file}:rtime:AVERAGE',
116                         'DEF:rtime_min={file}:rtime:MIN',
117                         'DEF:rtime_max={file}:rtime:MAX',
118                         'DEF:wtime_avg={file}:wtime:AVERAGE',
119                         'DEF:wtime_min={file}:wtime:MIN',
120                         'DEF:wtime_max={file}:wtime:MAX',
121                         'CDEF:rtime_avg_ms=rtime_avg,1000,/',
122                         'CDEF:rtime_min_ms=rtime_min,1000,/',
123                         'CDEF:rtime_max_ms=rtime_max,1000,/',
124                         'CDEF:wtime_avg_ms=wtime_avg,1000,/',
125                         'CDEF:wtime_min_ms=wtime_min,1000,/',
126                         'CDEF:wtime_max_ms=wtime_max,1000,/',
127                         'CDEF:total_avg_ms=rtime_avg_ms,wtime_avg_ms,+',
128                         'CDEF:total_min_ms=rtime_min_ms,wtime_min_ms,+',
129                         'CDEF:total_max_ms=rtime_max_ms,wtime_max_ms,+',
130                         "AREA:total_max_ms#$HalfRed",
131                         "AREA:total_min_ms#$Canvas",
132                         "LINE1:wtime_avg_ms#$FullGreen:Write",
133                         'GPRINT:wtime_min_ms:MIN:%5.1lf%s Min,',
134                         'GPRINT:wtime_avg_ms:AVERAGE:%5.1lf%s Avg,',
135                         'GPRINT:wtime_max_ms:MAX:%5.1lf%s Max,',
136                         'GPRINT:wtime_avg_ms:LAST:%5.1lf%s Last\n',
137                         "LINE1:rtime_avg_ms#$FullBlue:Read ",
138                         'GPRINT:rtime_min_ms:MIN:%5.1lf%s Min,',
139                         'GPRINT:rtime_avg_ms:AVERAGE:%5.1lf%s Avg,',
140                         'GPRINT:rtime_max_ms:MAX:%5.1lf%s Max,',
141                         'GPRINT:rtime_avg_ms:LAST:%5.1lf%s Last\n',
142                         "LINE1:total_avg_ms#$FullRed:Total",
143                         'GPRINT:total_min_ms:MIN:%5.1lf%s Min,',
144                         'GPRINT:total_avg_ms:AVERAGE:%5.1lf%s Avg,',
145                         'GPRINT:total_max_ms:MAX:%5.1lf%s Max,',
146                         'GPRINT:total_avg_ms:LAST:%5.1lf%s Last'
147                 ],
148                 hddtemp => [
149                         'DEF:temp_avg={file}:value:AVERAGE',
150                         'DEF:temp_min={file}:value:MIN',
151                         'DEF:temp_max={file}:value:MAX',
152                         "AREA:temp_max#$HalfBlue",
153                         "AREA:temp_min#$Canvas",
154                         "LINE1:temp_avg#$FullBlue:Temperature",
155                         'GPRINT:temp_min:MIN:%4.1lf Min,',
156                         'GPRINT:temp_avg:AVERAGE:%4.1lf Avg,',
157                         'GPRINT:temp_max:MAX:%4.1lf Max,',
158                         'GPRINT:temp_avg:LAST:%4.1lf Last\l'
159                 ],
160                 load => ['DEF:s_avg={file}:shortterm:AVERAGE',
161                         'DEF:s_min={file}:shortterm:MIN',
162                         'DEF:s_max={file}:shortterm:MAX',
163                         'DEF:m_avg={file}:midterm:AVERAGE',
164                         'DEF:m_min={file}:midterm:MIN',
165                         'DEF:m_max={file}:midterm:MAX',
166                         'DEF:l_avg={file}:longterm:AVERAGE',
167                         'DEF:l_min={file}:longterm:MIN',
168                         'DEF:l_max={file}:longterm:MAX',
169                         "AREA:s_max#$HalfGreen",
170                         "AREA:s_min#$Canvas",
171                         "LINE1:s_avg#$FullGreen: 1m average",
172                         'GPRINT:s_min:MIN:%4.2lf Min,',
173                         'GPRINT:s_avg:AVERAGE:%4.2lf Avg,',
174                         'GPRINT:s_max:MAX:%4.2lf Max,',
175                         'GPRINT:s_avg:LAST:%4.2lf Last\n',
176                         "LINE1:m_avg#$FullBlue: 5m average",
177                         'GPRINT:m_min:MIN:%4.2lf Min,',
178                         'GPRINT:m_avg:AVERAGE:%4.2lf Avg,',
179                         'GPRINT:m_max:MAX:%4.2lf Max,',
180                         'GPRINT:m_avg:LAST:%4.2lf Last\n',
181                         "LINE1:l_avg#$FullRed:15m average",
182                         'GPRINT:l_min:MIN:%4.2lf Min,',
183                         'GPRINT:l_avg:AVERAGE:%4.2lf Avg,',
184                         'GPRINT:l_max:MAX:%4.2lf Max,',
185                         'GPRINT:l_avg:LAST:%4.2lf Last'
186                 ],
187                 mails => ['DEF:rawgood={file}:good:AVERAGE',
188                         'DEF:rawspam={file}:spam:AVERAGE',
189                         'CDEF:good=rawgood,UN,0,rawgood,IF',
190                         'CDEF:spam=rawspam,UN,0,rawspam,IF',
191                         'CDEF:negspam=spam,-1,*',
192                         "AREA:good#$HalfGreen",
193                         "LINE1:good#$FullGreen:Good mails",
194                         'GPRINT:good:AVERAGE:%4.1lf Avg,',
195                         'GPRINT:good:MAX:%4.1lf Max,',
196                         'GPRINT:good:LAST:%4.1lf Last\n',
197                         "AREA:negspam#$HalfRed",
198                         "LINE1:negspam#$FullRed:Spam mails",
199                         'GPRINT:spam:AVERAGE:%4.1lf Avg,',
200                         'GPRINT:spam:MAX:%4.1lf Max,',
201                         'GPRINT:spam:LAST:%4.1lf Last',
202                         'HRULE:0#000000'],
203                 memory => [
204                         'DEF:used_avg={file}:used:AVERAGE',
205                         'DEF:free_avg={file}:free:AVERAGE',
206                         'DEF:buffers_avg={file}:buffers:AVERAGE',
207                         'DEF:cached_avg={file}:cached:AVERAGE',
208                         'DEF:used_min={file}:used:MIN',
209                         'DEF:free_min={file}:free:MIN',
210                         'DEF:buffers_min={file}:buffers:MIN',
211                         'DEF:cached_min={file}:cached:MIN',
212                         'DEF:used_max={file}:used:MAX',
213                         'DEF:free_max={file}:free:MAX',
214                         'DEF:buffers_max={file}:buffers:MAX',
215                         'DEF:cached_max={file}:cached:MAX',
216                         'CDEF:free_cached_buffers_used=free_avg,cached_avg,+,buffers_avg,+,used_avg,+',
217                         'CDEF:cached_buffers_used=cached_avg,buffers_avg,+,used_avg,+',
218                         'CDEF:buffers_used=buffers_avg,used_avg,+',
219                         "AREA:free_cached_buffers_used#$HalfGreen",
220                         "AREA:cached_buffers_used#$HalfBlue",
221                         "AREA:buffers_used#$HalfYellow",
222                         "AREA:used_avg#$HalfRed",
223                         "LINE1:free_cached_buffers_used#$FullGreen:Free        ",
224                         'GPRINT:free_min:MIN:%5.1lf%s Min,',
225                         'GPRINT:free_avg:AVERAGE:%5.1lf%s Avg,',
226                         'GPRINT:free_max:MAX:%5.1lf%s Max,',
227                         'GPRINT:free_avg:LAST:%5.1lf%s Last\n',
228                         "LINE1:cached_buffers_used#$FullBlue:Page cache  ",
229                         'GPRINT:cached_min:MIN:%5.1lf%s Min,',
230                         'GPRINT:cached_avg:AVERAGE:%5.1lf%s Avg,',
231                         'GPRINT:cached_max:MAX:%5.1lf%s Max,',
232                         'GPRINT:cached_avg:LAST:%5.1lf%s Last\n',
233                         "LINE1:buffers_used#$FullYellow:Buffer cache",
234                         'GPRINT:buffers_min:MIN:%5.1lf%s Min,',
235                         'GPRINT:buffers_avg:AVERAGE:%5.1lf%s Avg,',
236                         'GPRINT:buffers_max:MAX:%5.1lf%s Max,',
237                         'GPRINT:buffers_avg:LAST:%5.1lf%s Last\n',
238                         "LINE1:used_avg#$FullRed:Used        ",
239                         'GPRINT:used_min:MIN:%5.1lf%s Min,',
240                         'GPRINT:used_avg:AVERAGE:%5.1lf%s Avg,',
241                         'GPRINT:used_max:MAX:%5.1lf%s Max,',
242                         'GPRINT:used_avg:LAST:%5.1lf%s Last'
243                 ],
244                 nfs3_procedures => [
245                         "DEF:null_avg={file}:null:AVERAGE",
246                         "DEF:getattr_avg={file}:getattr:AVERAGE",
247                         "DEF:setattr_avg={file}:setattr:AVERAGE",
248                         "DEF:lookup_avg={file}:lookup:AVERAGE",
249                         "DEF:access_avg={file}:access:AVERAGE",
250                         "DEF:readlink_avg={file}:readlink:AVERAGE",
251                         "DEF:read_avg={file}:read:AVERAGE",
252                         "DEF:write_avg={file}:write:AVERAGE",
253                         "DEF:create_avg={file}:create:AVERAGE",
254                         "DEF:mkdir_avg={file}:mkdir:AVERAGE",
255                         "DEF:symlink_avg={file}:symlink:AVERAGE",
256                         "DEF:mknod_avg={file}:mknod:AVERAGE",
257                         "DEF:remove_avg={file}:remove:AVERAGE",
258                         "DEF:rmdir_avg={file}:rmdir:AVERAGE",
259                         "DEF:rename_avg={file}:rename:AVERAGE",
260                         "DEF:link_avg={file}:link:AVERAGE",
261                         "DEF:readdir_avg={file}:readdir:AVERAGE",
262                         "DEF:readdirplus_avg={file}:readdirplus:AVERAGE",
263                         "DEF:fsstat_avg={file}:fsstat:AVERAGE",
264                         "DEF:fsinfo_avg={file}:fsinfo:AVERAGE",
265                         "DEF:pathconf_avg={file}:pathconf:AVERAGE",
266                         "DEF:commit_avg={file}:commit:AVERAGE",
267                         "DEF:null_max={file}:null:MAX",
268                         "DEF:getattr_max={file}:getattr:MAX",
269                         "DEF:setattr_max={file}:setattr:MAX",
270                         "DEF:lookup_max={file}:lookup:MAX",
271                         "DEF:access_max={file}:access:MAX",
272                         "DEF:readlink_max={file}:readlink:MAX",
273                         "DEF:read_max={file}:read:MAX",
274                         "DEF:write_max={file}:write:MAX",
275                         "DEF:create_max={file}:create:MAX",
276                         "DEF:mkdir_max={file}:mkdir:MAX",
277                         "DEF:symlink_max={file}:symlink:MAX",
278                         "DEF:mknod_max={file}:mknod:MAX",
279                         "DEF:remove_max={file}:remove:MAX",
280                         "DEF:rmdir_max={file}:rmdir:MAX",
281                         "DEF:rename_max={file}:rename:MAX",
282                         "DEF:link_max={file}:link:MAX",
283                         "DEF:readdir_max={file}:readdir:MAX",
284                         "DEF:readdirplus_max={file}:readdirplus:MAX",
285                         "DEF:fsstat_max={file}:fsstat:MAX",
286                         "DEF:fsinfo_max={file}:fsinfo:MAX",
287                         "DEF:pathconf_max={file}:pathconf:MAX",
288                         "DEF:commit_max={file}:commit:MAX",
289                         "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,+,+,+,+,+,+,+,+,+,+,+,+,+,+",
290                         "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,+,+,+,+,+,+,+,+,+,+,+,+,+,+",
291                         "CDEF:stack_read=read_avg",
292                         "CDEF:stack_getattr=stack_read,getattr_avg,+",
293                         "CDEF:stack_access=stack_getattr,access_avg,+",
294                         "CDEF:stack_lookup=stack_access,lookup_avg,+",
295                         "CDEF:stack_write=stack_lookup,write_avg,+",
296                         "CDEF:stack_commit=stack_write,commit_avg,+",
297                         "CDEF:stack_setattr=stack_commit,setattr_avg,+",
298                         "CDEF:stack_other=stack_setattr,other_avg,+",
299                         "AREA:stack_other#$HalfRed",
300                         "AREA:stack_setattr#$HalfGreen",
301                         "AREA:stack_commit#$HalfYellow",
302                         "AREA:stack_write#$HalfGreen",
303                         "AREA:stack_lookup#$HalfBlue",
304                         "AREA:stack_access#$HalfMagenta",
305                         "AREA:stack_getattr#$HalfCyan",
306                         "AREA:stack_read#$HalfBlue",
307                         "LINE1:stack_other#$FullRed:Other  ",
308                         'GPRINT:other_max:MAX:%5.1lf Max,',
309                         'GPRINT:other_avg:AVERAGE:%5.1lf Avg,',
310                         'GPRINT:other_avg:LAST:%5.1lf Last\l',
311                         "LINE1:stack_setattr#$FullGreen:setattr",
312                         'GPRINT:setattr_max:MAX:%5.1lf Max,',
313                         'GPRINT:setattr_avg:AVERAGE:%5.1lf Avg,',
314                         'GPRINT:setattr_avg:LAST:%5.1lf Last\l',
315                         "LINE1:stack_commit#$FullYellow:commit ",
316                         'GPRINT:commit_max:MAX:%5.1lf Max,',
317                         'GPRINT:commit_avg:AVERAGE:%5.1lf Avg,',
318                         'GPRINT:commit_avg:LAST:%5.1lf Last\l',
319                         "LINE1:stack_write#$FullGreen:write  ",
320                         'GPRINT:write_max:MAX:%5.1lf Max,',
321                         'GPRINT:write_avg:AVERAGE:%5.1lf Avg,',
322                         'GPRINT:write_avg:LAST:%5.1lf Last\l',
323                         "LINE1:stack_lookup#$FullBlue:lookup ",
324                         'GPRINT:lookup_max:MAX:%5.1lf Max,',
325                         'GPRINT:lookup_avg:AVERAGE:%5.1lf Avg,',
326                         'GPRINT:lookup_avg:LAST:%5.1lf Last\l',
327                         "LINE1:stack_access#$FullMagenta:access ",
328                         'GPRINT:access_max:MAX:%5.1lf Max,',
329                         'GPRINT:access_avg:AVERAGE:%5.1lf Avg,',
330                         'GPRINT:access_avg:LAST:%5.1lf Last\l',
331                         "LINE1:stack_getattr#$FullCyan:getattr",
332                         'GPRINT:getattr_max:MAX:%5.1lf Max,',
333                         'GPRINT:getattr_avg:AVERAGE:%5.1lf Avg,',
334                         'GPRINT:getattr_avg:LAST:%5.1lf Last\l',
335                         "LINE1:stack_read#$FullBlue:read   ",
336                         'GPRINT:read_max:MAX:%5.1lf Max,',
337                         'GPRINT:read_avg:AVERAGE:%5.1lf Avg,',
338                         'GPRINT:read_avg:LAST:%5.1lf Last\l'
339                 ],
340                 partition => [
341                         "DEF:rbyte_avg={file}:rbytes:AVERAGE",
342                         "DEF:rbyte_min={file}:rbytes:MIN",
343                         "DEF:rbyte_max={file}:rbytes:MAX",
344                         "DEF:wbyte_avg={file}:wbytes:AVERAGE",
345                         "DEF:wbyte_min={file}:wbytes:MIN",
346                         "DEF:wbyte_max={file}:wbytes:MAX",
347                         'CDEF:overlap=wbyte_avg,rbyte_avg,GT,rbyte_avg,wbyte_avg,IF',
348                         "AREA:wbyte_avg#$HalfGreen",
349                         "AREA:rbyte_avg#$HalfBlue",
350                         "AREA:overlap#$HalfBlueGreen",
351                         "LINE1:wbyte_avg#$FullGreen:Write",
352                         'GPRINT:wbyte_min:MIN:%5.1lf%s Min,',
353                         'GPRINT:wbyte_avg:AVERAGE:%5.1lf%s Avg,',
354                         'GPRINT:wbyte_max:MAX:%5.1lf%s Max,',
355                         'GPRINT:wbyte_avg:LAST:%5.1lf%s Last\l',
356                         "LINE1:rbyte_avg#$FullBlue:Read ",
357                         'GPRINT:rbyte_min:MIN:%5.1lf%s Min,',
358                         'GPRINT:rbyte_avg:AVERAGE:%5.1lf%s Avg,',
359                         'GPRINT:rbyte_max:MAX:%5.1lf%s Max,',
360                         'GPRINT:rbyte_avg:LAST:%5.1lf%s Last\l'
361                 ],
362                 ping => ['DEF:ping_avg={file}:ping:AVERAGE',
363                         'DEF:ping_min={file}:ping:MIN',
364                         'DEF:ping_max={file}:ping:MAX',
365                         "AREA:ping_max#$HalfBlue",
366                         "AREA:ping_min#$Canvas",
367                         "LINE1:ping_avg#$FullBlue:Ping",
368                         'GPRINT:ping_min:MIN:%4.1lf ms Min,',
369                         'GPRINT:ping_avg:AVERAGE:%4.1lf ms Avg,',
370                         'GPRINT:ping_max:MAX:%4.1lf ms Max,',
371                         'GPRINT:ping_avg:LAST:%4.1lf ms Last'],
372                 processes => [
373                         "DEF:running_avg={file}:running:AVERAGE",
374                         "DEF:running_min={file}:running:MIN",
375                         "DEF:running_max={file}:running:MAX",
376                         "DEF:sleeping_avg={file}:sleeping:AVERAGE",
377                         "DEF:sleeping_min={file}:sleeping:MIN",
378                         "DEF:sleeping_max={file}:sleeping:MAX",
379                         "DEF:zombies_avg={file}:zombies:AVERAGE",
380                         "DEF:zombies_min={file}:zombies:MIN",
381                         "DEF:zombies_max={file}:zombies:MAX",
382                         "DEF:stopped_avg={file}:stopped:AVERAGE",
383                         "DEF:stopped_min={file}:stopped:MIN",
384                         "DEF:stopped_max={file}:stopped:MAX",
385                         "DEF:paging_avg={file}:paging:AVERAGE",
386                         "DEF:paging_min={file}:paging:MIN",
387                         "DEF:paging_max={file}:paging:MAX",
388                         "DEF:blocked_avg={file}:blocked:AVERAGE",
389                         "DEF:blocked_min={file}:blocked:MIN",
390                         "DEF:blocked_max={file}:blocked:MAX",
391                         'CDEF:paging_acc=sleeping_avg,running_avg,stopped_avg,zombies_avg,blocked_avg,paging_avg,+,+,+,+,+',
392                         'CDEF:blocked_acc=sleeping_avg,running_avg,stopped_avg,zombies_avg,blocked_avg,+,+,+,+',
393                         'CDEF:zombies_acc=sleeping_avg,running_avg,stopped_avg,zombies_avg,+,+,+',
394                         'CDEF:stopped_acc=sleeping_avg,running_avg,stopped_avg,+,+',
395                         'CDEF:running_acc=sleeping_avg,running_avg,+',
396                         'CDEF:sleeping_acc=sleeping_avg',
397                         "AREA:paging_acc#$HalfYellow",
398                         "AREA:blocked_acc#$HalfCyan",
399                         "AREA:zombies_acc#$HalfRed",
400                         "AREA:stopped_acc#$HalfMagenta",
401                         "AREA:running_acc#$HalfGreen",
402                         "AREA:sleeping_acc#$HalfBlue",
403                         "LINE1:paging_acc#$FullYellow:Paging  ",
404                         'GPRINT:paging_min:MIN:%5.1lf Min,',
405                         'GPRINT:paging_avg:AVERAGE:%5.1lf Average,',
406                         'GPRINT:paging_max:MAX:%5.1lf Max,',
407                         'GPRINT:paging_avg:LAST:%5.1lf Last\l',
408                         "LINE1:blocked_acc#$FullCyan:Blocked ",
409                         'GPRINT:blocked_min:MIN:%5.1lf Min,',
410                         'GPRINT:blocked_avg:AVERAGE:%5.1lf Average,',
411                         'GPRINT:blocked_max:MAX:%5.1lf Max,',
412                         'GPRINT:blocked_avg:LAST:%5.1lf Last\l',
413                         "LINE1:zombies_acc#$FullRed:Zombies ",
414                         'GPRINT:zombies_min:MIN:%5.1lf Min,',
415                         'GPRINT:zombies_avg:AVERAGE:%5.1lf Average,',
416                         'GPRINT:zombies_max:MAX:%5.1lf Max,',
417                         'GPRINT:zombies_avg:LAST:%5.1lf Last\l',
418                         "LINE1:stopped_acc#$FullMagenta:Stopped ",
419                         'GPRINT:stopped_min:MIN:%5.1lf Min,',
420                         'GPRINT:stopped_avg:AVERAGE:%5.1lf Average,',
421                         'GPRINT:stopped_max:MAX:%5.1lf Max,',
422                         'GPRINT:stopped_avg:LAST:%5.1lf Last\l',
423                         "LINE1:running_acc#$FullGreen:Running ",
424                         'GPRINT:running_min:MIN:%5.1lf Min,',
425                         'GPRINT:running_avg:AVERAGE:%5.1lf Average,',
426                         'GPRINT:running_max:MAX:%5.1lf Max,',
427                         'GPRINT:running_avg:LAST:%5.1lf Last\l',
428                         "LINE1:sleeping_acc#$FullBlue:Sleeping",
429                         'GPRINT:sleeping_min:MIN:%5.1lf Min,',
430                         'GPRINT:sleeping_avg:AVERAGE:%5.1lf Average,',
431                         'GPRINT:sleeping_max:MAX:%5.1lf Max,',
432                         'GPRINT:sleeping_avg:LAST:%5.1lf Last\l'
433                 ],
434                 sensors => [
435                         'DEF:temp_avg={file}:value:AVERAGE',
436                         'DEF:temp_min={file}:value:MIN',
437                         'DEF:temp_max={file}:value:MAX',
438                         "AREA:temp_max#$HalfBlue",
439                         "AREA:temp_min#$Canvas",
440                         "LINE1:temp_avg#$FullBlue:Value",
441                         'GPRINT:temp_min:MIN:%4.1lf Min,',
442                         'GPRINT:temp_avg:AVERAGE:%4.1lf Avg,',
443                         'GPRINT:temp_max:MAX:%4.1lf Max,',
444                         'GPRINT:temp_avg:LAST:%4.1lf Last\l'
445                 ],
446                 swap => [
447                         'DEF:used_avg={file}:used:AVERAGE',
448                         'DEF:used_min={file}:used:MIN',
449                         'DEF:used_max={file}:used:MAX',
450                         'DEF:free_avg={file}:free:AVERAGE',
451                         'DEF:free_min={file}:free:MIN',
452                         'DEF:free_max={file}:free:MAX',
453                         'DEF:cach_avg={file}:cached:AVERAGE',
454                         'DEF:cach_min={file}:cached:MIN',
455                         'DEF:cach_max={file}:cached:MAX',
456                         'DEF:resv_avg={file}:resv:AVERAGE',
457                         'DEF:resv_min={file}:resv:MIN',
458                         'DEF:resv_max={file}:resv:MAX',
459                         'CDEF:cach_avg_notnull=cach_avg,UN,0,cach_avg,IF',
460                         'CDEF:resv_avg_notnull=resv_avg,UN,0,resv_avg,IF',
461                         'CDEF:used_acc=used_avg',
462                         'CDEF:resv_acc=used_acc,resv_avg_notnull,+',
463                         'CDEF:cach_acc=resv_acc,cach_avg_notnull,+',
464                         'CDEF:free_acc=cach_acc,free_avg,+',
465                         "AREA:free_acc#$HalfGreen",
466                         "AREA:cach_acc#$HalfBlue",
467                         "AREA:resv_acc#$HalfYellow",
468                         "AREA:used_acc#$HalfRed",
469                         "LINE1:free_acc#$FullGreen:Free    ",
470                         'GPRINT:free_min:MIN:%5.1lf%s Min,',
471                         'GPRINT:free_avg:AVERAGE:%5.1lf%s Avg,',
472                         'GPRINT:free_max:MAX:%5.1lf%s Max,',
473                         'GPRINT:free_avg:LAST:%5.1lf%s Last\n',
474                         "LINE1:cach_acc#$FullBlue:Cached  ",
475                         'GPRINT:cach_min:MIN:%5.1lf%s Min,',
476                         'GPRINT:cach_avg:AVERAGE:%5.1lf%s Avg,',
477                         'GPRINT:cach_max:MAX:%5.1lf%s Max,',
478                         'GPRINT:cach_avg:LAST:%5.1lf%s Last\l',
479                         "LINE1:resv_acc#$FullYellow:Reserved",
480                         'GPRINT:resv_min:MIN:%5.1lf%s Min,',
481                         'GPRINT:resv_avg:AVERAGE:%5.1lf%s Avg,',
482                         'GPRINT:resv_max:MAX:%5.1lf%s Max,',
483                         'GPRINT:resv_avg:LAST:%5.1lf%s Last\n',
484                         "LINE1:used_acc#$FullRed:Used    ",
485                         'GPRINT:used_min:MIN:%5.1lf%s Min,',
486                         'GPRINT:used_avg:AVERAGE:%5.1lf%s Avg,',
487                         'GPRINT:used_max:MAX:%5.1lf%s Max,',
488                         'GPRINT:used_avg:LAST:%5.1lf%s Last\l'
489                 ],
490                 traffic => ['DEF:out_min_raw={file}:outgoing:MIN',
491                         'DEF:out_avg_raw={file}:outgoing:AVERAGE',
492                         'DEF:out_max_raw={file}:outgoing:MAX',
493                         'DEF:inc_min_raw={file}:incoming:MIN',
494                         'DEF:inc_avg_raw={file}:incoming:AVERAGE',
495                         'DEF:inc_max_raw={file}:incoming:MAX',
496                         'CDEF:out_min=out_min_raw,8,*',
497                         'CDEF:out_avg=out_avg_raw,8,*',
498                         'CDEF:out_max=out_max_raw,8,*',
499                         'CDEF:inc_min=inc_min_raw,8,*',
500                         'CDEF:inc_avg=inc_avg_raw,8,*',
501                         'CDEF:inc_max=inc_max_raw,8,*',
502                         'CDEF:overlap=out_avg,inc_avg,GT,inc_avg,out_avg,IF',
503                         'CDEF:mytime=out_avg_raw,TIME,TIME,IF',
504                         'CDEF:sample_len_raw=mytime,PREV(mytime),-',
505                         'CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF',
506                         'CDEF:out_avg_sample=out_avg_raw,UN,0,out_avg_raw,IF,sample_len,*',
507                         'CDEF:out_avg_sum=PREV,UN,0,PREV,IF,out_avg_sample,+',
508                         'CDEF:inc_avg_sample=inc_avg_raw,UN,0,inc_avg_raw,IF,sample_len,*',
509                         'CDEF:inc_avg_sum=PREV,UN,0,PREV,IF,inc_avg_sample,+',
510                         "AREA:out_avg#$HalfGreen",
511                         "AREA:inc_avg#$HalfBlue",
512                         "AREA:overlap#$HalfBlueGreen",
513                         "LINE1:out_avg#$FullGreen:Outgoing",
514                         'GPRINT:out_avg:AVERAGE:%5.1lf%s Avg,',
515                         'GPRINT:out_max:MAX:%5.1lf%s Max,',
516                         'GPRINT:out_avg:LAST:%5.1lf%s Last',
517                         'GPRINT:out_avg_sum:LAST:(ca. %5.1lf%sB Total)\l',
518                         "LINE1:inc_avg#$FullBlue:Incoming",
519                         #'GPRINT:inc_min:MIN:%5.1lf %s Min,',
520                         'GPRINT:inc_avg:AVERAGE:%5.1lf%s Avg,',
521                         'GPRINT:inc_max:MAX:%5.1lf%s Max,',
522                         'GPRINT:inc_avg:LAST:%5.1lf%s Last',
523                         'GPRINT:inc_avg_sum:LAST:(ca. %5.1lf%sB Total)\l'
524                 ],
525                 cpufreq => [
526                         'DEF:cpufreq_avg={file}:value:AVERAGE',
527                         'DEF:cpufreq_min={file}:value:MIN',
528                         'DEF:cpufreq_max={file}:value:MAX',
529                         "AREA:cpufreq_max#$HalfBlue",
530                         "AREA:cpufreq_min#$Canvas",
531                         "LINE1:cpufreq_avg#$FullBlue:Frequency",
532                         'GPRINT:cpufreq_min:MIN:%5.1lf%s Min,',
533                         'GPRINT:cpufreq_avg:AVERAGE:%5.1lf%s Avg,',
534                         'GPRINT:cpufreq_max:MAX:%5.1lf%s Max,',
535                         'GPRINT:cpufreq_avg:LAST:%5.1lf%s Last\l'
536                 ],
537                 users => [
538                             'DEF:users_avg={file}:users:AVERAGE',
539                             'DEF:users_min={file}:users:MIN',
540                             'DEF:users_max={file}:users:MAX',
541                             "AREA:users_max#$HalfBlue",
542                             "AREA:users_min#$Canvas",
543                             "LINE1:users_avg#$FullBlue:Users",
544                             'GPRINT:users_min:MIN:%4.1lf Min,',
545                             'GPRINT:users_avg:AVERAGE:%4.1lf Average,',
546                             'GPRINT:users_max:MAX:%4.1lf Max,',
547                             'GPRINT:users_avg:LAST:%4.1lf Last\l'
548                 ]
549         };
550         $GraphDefs->{'disk'} = $GraphDefs->{'partition'};
551         $GraphDefs->{'meminfo'} = $GraphDefs->{'memory'};
552 }
553
554 our $GraphArgs =
555 {
556         cpu => ['-t', '{host} cpu{inst} usage', '-v', 'Percent', '-l', '0'],
557         cpufreq => ['-t', '{host} cpu{inst} usage', '-v', 'Mhz'],
558         #disk => ['-t', '{host} disk {inst} IO wait', '-v', 'Seconds'],
559         disk => ['-t', '{host} disk {inst} usage', '-v', 'Byte/s'],
560         hddtemp => ['-t', '{host} hdd temperature {inst}', '-v', '°Celsius'],
561         load => ['-t', '{host} load average', '-v', 'System load', '-X', '0'],
562         mails   => ['-t', '{host} mail count', '-v', 'Amount', '-X', '0'],
563         memory => ['-t', '{host} memory usage', '-v', 'Bytes', '-b', '1024', '-l', '0'],
564         nfs3_procedures => ['-t', '{host} NFSv3 {inst} procedures', '-v', 'Procedures/s' ],
565         partition => ['-t', '{host} partition {inst} usage', '-v', 'Byte/s'],
566         ping => ['-t', '{host} ping to {inst}', '-v', 'ms'],
567         processes => ['-t', '{host} processes', '-v', 'Processes'],
568         sensors => ['-t', '{host} sensor {inst}', '-v', '°Celsius'],
569         swap => ['-t', '{host} swap usage', '-v', 'Bytes', '-b', '1024', '-l', '0'],
570         traffic => ['-t', '{host} {inst} traffic', '-v', 'Bit/s'],
571     users => ['-t', '{host} users', '-v', 'Users'],
572 };
573
574 our $GraphMulti =
575 {
576         cpu     => \&output_graph_cpu,
577         cpufreq => 1,
578         disk    => 1,
579         load    => 0,
580         mails   => 0,
581         memory  => 0,
582         partition => 1,
583         ping    => \&output_graph_ping,
584         sensors => 1,
585         traffic => 1,
586     users => 1
587 };
588
589 our @Info;
590 if (defined ($ENV{'GATEWAY_INTERFACE'}))
591 {
592         @Info = ($ENV{'PATH_INFO'} || '') =~ m#([\w\-\.]+)#g;
593 }
594 else
595 {
596         @Info = @ARGV;
597 }
598
599 parse_pathinfo (@Info);
600
601 if ($TimeSpan)
602 {
603         output_graph ();
604 }
605 else
606 {
607         output_page ();
608 }
609
610 exit (0);
611
612 sub output_graph_cpu
613 {
614         my @inst = @_;
615         my @ret = ();
616
617         die if (@inst < 2);
618
619         for (@inst)
620         {
621                 push (@ret,
622                         "DEF:user_avg_$_=$AbsDir/cpu-$_.rrd:user:AVERAGE",
623                         "DEF:user_min_$_=$AbsDir/cpu-$_.rrd:user:MIN",
624                         "DEF:user_max_$_=$AbsDir/cpu-$_.rrd:user:MAX",
625                         "DEF:nice_avg_$_=$AbsDir/cpu-$_.rrd:nice:AVERAGE",
626                         "DEF:nice_min_$_=$AbsDir/cpu-$_.rrd:nice:MIN",
627                         "DEF:nice_max_$_=$AbsDir/cpu-$_.rrd:nice:MAX",
628                         "DEF:syst_avg_$_=$AbsDir/cpu-$_.rrd:syst:AVERAGE",
629                         "DEF:syst_min_$_=$AbsDir/cpu-$_.rrd:syst:MIN",
630                         "DEF:syst_max_$_=$AbsDir/cpu-$_.rrd:syst:MAX",
631                         "DEF:wait_avg_$_=$AbsDir/cpu-$_.rrd:wait:AVERAGE",
632                         "DEF:wait_min_$_=$AbsDir/cpu-$_.rrd:wait:MIN",
633                         "DEF:wait_max_$_=$AbsDir/cpu-$_.rrd:wait:MAX");
634         }
635
636         for (qw(user nice syst wait))
637         {
638                 my $def = $_;
639                 my $cdef;
640
641                 my $default_value = ($def eq 'user' or $def eq 'syst') ? 'UNKN' : '0';
642
643                 for (qw(avg min max))
644                 {
645                         my $cf = $_;
646
647                         for (@inst)
648                         {
649                                 push (@ret, "CDEF:${def}_${cf}_notnull_${_}=${def}_${cf}_${_},UN,0,${def}_${cf}_${_},IF");
650                                 push (@ret, "CDEF:${def}_${cf}_defined_${_}=${def}_${cf}_${_},UN,0,1,IF");
651                         }
652
653                         $cdef = "CDEF:${def}_${cf}_num=" . join (',', map { "${def}_${cf}_defined_${_}" } (@inst));
654                         $cdef .= ',+' x (scalar (@inst) - 1);
655                         push (@ret, $cdef);
656
657                         $cdef = "CDEF:${def}_${cf}=${def}_${cf}_num," . join (',', map { "${def}_${cf}_notnull_${_}" } (@inst));
658                         $cdef .= ',+' x (scalar (@inst) - 1);
659                         $cdef .= ",${def}_${cf}_num,${def}_${cf}_num,1,IF,/,$default_value,IF";
660                         push (@ret, $cdef);
661                         push (@ret, "CDEF:${def}_${cf}_notnull=${def}_${cf},UN,0,${def}_${cf},IF");
662                 }
663         }
664
665         push (@ret,
666                 "CDEF:nice_acc=syst_avg_notnull,wait_avg_notnull,user_avg_notnull,nice_avg_notnull,+,+,+",
667                 "CDEF:user_acc=syst_avg_notnull,wait_avg_notnull,user_avg_notnull,+,+",
668                 "CDEF:wait_acc=syst_avg_notnull,wait_avg_notnull,+",
669                 "CDEF:syst_acc=syst_avg_notnull");
670
671         push (@ret, grep { $_ !~ m/^C?DEF/ } (@{$GraphDefs->{'cpu'}}));
672
673         return (@ret);
674 }
675
676 sub output_graph_ping
677 {
678         my @inst = @_;
679         my @ret = ();
680
681         die if (@inst < 2);
682
683         my @colors = get_n_colors (scalar (@inst));
684
685         for (my $i = 0; $i < scalar (@inst); $i++)
686         {
687                 my $inst = $inst[$i];
688                 push (@ret,
689                         "DEF:avg_$i=$AbsDir/ping-$inst.rrd:ping:AVERAGE",
690                         "DEF:min_$i=$AbsDir/ping-$inst.rrd:ping:MIN",
691                         "DEF:max_$i=$AbsDir/ping-$inst.rrd:ping:MAX");
692         }
693
694         for (my $i = 0; $i < scalar (@inst); $i++)
695         {
696                 my $inst = $inst[$i];
697                 my $color = $colors[$i];
698
699                 if (length ($inst) > 15)
700                 {
701                         $inst = substr ($inst, 0, 12) . '...';
702                 }
703                 else
704                 {
705                         $inst = sprintf ('%-15s', $inst);
706                 }
707
708                 push (@ret,
709                         "LINE1:avg_$i#$color:$inst",
710                         "GPRINT:min_$i:MIN:%4.1lf ms Min,",
711                         "GPRINT:avg_$i:AVERAGE:%4.1lf ms Avg,",
712                         "GPRINT:max_$i:MAX:%4.1lf ms Max,",
713                         "GPRINT:avg_$i:LAST:%4.1lf ms Last\\l");
714         }
715
716         return (@ret);
717 }
718
719 sub output_graph
720 {
721         die unless (defined ($GraphDefs->{$Type}));
722
723         my $host;
724         my @cmd = ();
725         my $file = $AbsDir . '/';
726         my $files = get_all_files ($AbsDir);
727
728         #
729         # get hostname
730         #
731         if ($RelDir =~ m#([^/]+)$#)
732         {
733                 $host = $1;
734         }
735         else
736         {
737                 $host = $Config->{'HostName'};
738         }
739
740         #
741         # get timespan
742         #
743         if ($TimeSpan =~ m/(\d+)/)
744         {
745                 $TimeSpan = -1 * int ($1);
746         }
747         else
748         {
749                 my %t = (hour => -3600, day => -86400, week => -604800, month => -2678400, year => -31622400);
750                 die unless (defined ($t{$TimeSpan}));
751                 $TimeSpan = $t{$TimeSpan};
752         }
753
754         if (scalar (@{$files->{$Type}}) == 1)
755         {
756                 $Inst = $files->{$Type}[0];
757         }
758
759         push (@cmd, '-', '-a', 'PNG', '-s', $TimeSpan);
760         push (@cmd, @{$GraphArgs->{$Type}}) if (defined ($GraphArgs->{$Type}));
761
762         for (qw(Back ShadeA ShadeB Font Canvas Grid MGrid Frame Arrow))
763         {
764                 push (@cmd, '-c', uc ($_) . '#' . $Config->{'Colors'}{$_});
765         }
766
767         if ((length ($Inst) == 0) and (ref ($GraphMulti->{$Type}) eq 'CODE'))
768         {
769                 push (@cmd, $GraphMulti->{$Type}->(@{$files->{$Type}}));
770         }
771         else
772         {
773                 if (length ("$Inst"))
774                 {
775                         $file .= "$Type-$Inst.rrd";
776                 }
777                 else
778                 {
779                         $file .= "$Type.rrd";
780                 }
781
782                 die ("File not found: $file") unless (-e $file);
783
784                 push (@cmd, @{$GraphDefs->{$Type}});
785         }
786
787         for (@cmd)
788         {
789                 $_ =~ s/{file}/$file/g;
790                 $_ =~ s/{host}/$host/g;
791                 $_ =~ s/{inst}/$Inst/g;
792                 $_ =~ s/{type}/$Type/g;
793         }
794
795         $| = 1;
796
797         print STDOUT <<HEADER if (defined ($ENV{'GATEWAY_INTERFACE'}));
798 Content-Type: image/png
799 Cache-Control: no-cache
800
801 HEADER
802
803         if (1)
804         {
805                 my $fh;
806                 open ($fh, ">/tmp/collection.log") or die ("open: $!");
807                 flock ($fh, LOCK_EX) or die ("flock: $!");
808
809                 print $fh join ("\n\t", @cmd) . "\n";
810
811                 close ($fh);
812         }
813
814         RRDs::graph (@cmd);
815
816         die ('RRDs::error: ' . RRDs::error ()) if (RRDs::error ());
817 }
818
819 sub output_page
820 {
821         my $files = get_all_files ($AbsDir);
822         my $dirs  = get_all_dirs  ($AbsDir);
823
824         print STDOUT <<HEADER if (defined ($ENV{'GATEWAY_INTERFACE'}));
825 Content-Type: text/html
826 Cache-Control: no-cache
827
828 <html>
829         <head>
830                 <title>Collection: $RelDir</title>
831                 <style type="text/css">
832                         img { border: none; display: block; }
833                 </style>
834         </head>
835
836         <body>
837 HEADER
838
839         my $MySelf = defined ($ENV{'GATEWAY_INTERFACE'}) ? $ENV{'SCRIPT_NAME'} : $0;
840
841         if ((length ($Type) != 0) and (length ($Inst) == 0) and (ref ($GraphMulti->{$Type}) eq 'CODE') and (scalar (@{$files->{$Type}}) > 1))
842         {
843                 print qq(\t\t<div><a href="$MySelf$RelDir">Go up</a></div>\n);
844
845                 print "\t\t<ul>\n";
846                 for (@{$files->{$Type}})
847                 {
848                         print qq(\t\t\t<li><a href="$MySelf$RelDir/$Type/$_">$_</a></li>\n);
849                 }
850                 print <<HTML;
851                 </ul>
852
853                 <h3>Daily</h3>
854                 <div><img src="$MySelf$RelDir/$Type/day" /></div>
855                 <h3>Weekly</h3>
856                 <div><img src="$MySelf$RelDir/$Type/week" /></div>
857                 <h3>Monthly</h3>
858                 <div><img src="$MySelf$RelDir/$Type/month" /></div>
859                 <h3>Yearly</h3>
860                 <div><img src="$MySelf$RelDir/$Type/year" /></div>
861 HTML
862         }
863         elsif (length ($Type) != 0)
864         {
865                 my $ext = length ($Inst) ? "$Type/$Inst" : $Type;
866
867                 if ((ref ($GraphMulti->{$Type}) eq 'CODE') and (scalar (@{$files->{$Type}}) > 1))
868                 {
869                         print qq(<div><a href="$MySelf$RelDir/$Type">Go up</a></div>\n);
870                 }
871                 else
872                 {
873                         print qq(<div><a href="$MySelf$RelDir">Go up</a></div>\n);
874                 }
875
876                 print <<HTML;
877                 <h3>Daily</h3>
878                 <div><img src="$MySelf$RelDir/$ext/day" /></div>
879                 <h3>Weekly</h3>
880                 <div><img src="$MySelf$RelDir/$ext/week" /></div>
881                 <h3>Monthly</h3>
882                 <div><img src="$MySelf$RelDir/$ext/month" /></div>
883                 <h3>Yearly</h3>
884                 <div><img src="$MySelf$RelDir/$ext/year" /></div>
885 HTML
886         }
887         else
888         {
889                 if ($RelDir)
890                 {
891                         my ($up) = $RelDir =~ m#(.*)/[^/]+$#;
892                         print qq(\t\t<div><a href="$MySelf$up">Go up</a></div>\n);
893                 }
894
895                 if (@$dirs)
896                 {
897                         print "<ul>\n";
898                         for (@$dirs)
899                         {
900                                 print qq(<li>$AbsDir/<a href="$MySelf$RelDir/$_">$_</a></li>\n);
901                         }
902                         print "</ul>\n";
903                 }
904
905                 for (sort (keys %$files))
906                 {
907                         my $type = $_;
908
909                         if (ref ($GraphMulti->{$type}) eq 'CODE')
910                         {
911                                 print qq(\t\t<a href="$MySelf$RelDir/$type" />),
912                                 qq(<img src="$MySelf$RelDir/$type/day" /></a>\n);
913                                 next;
914                         }
915
916                         for (@{$files->{$type}})
917                         {
918                                 my $inst = "$_";
919
920                                 if (length ($inst))
921                                 {
922                                         print qq(\t\t<a href="$MySelf$RelDir/$type/$inst" />),
923                                         qq(<img src="$MySelf$RelDir/$type/$inst/day" /></a>\n);
924                                 }
925                                 else
926                                 {
927                                         print qq(\t\t<a href="$MySelf$RelDir/$type" />),
928                                         qq(<img src="$MySelf$RelDir/$type/day" /></a>\n);
929                                 }
930                         }
931                 }
932         }
933
934         print STDOUT <<FOOTER if (defined ($ENV{'GATEWAY_INTERFACE'}));
935         </body>
936 </html>
937 FOOTER
938 }
939
940 sub output_xml
941 {
942         my $files = get_all_files ();
943
944         print STDOUT <<HEADER if (defined ($ENV{'GATEWAY_INTERFACE'}));
945 Content-Type: text/xml
946 Cache-Control: no-cache
947
948 HEADER
949         print STDOUT pl2xml ($files);
950 }
951
952 sub read_config
953 {
954         my $file = @_ ? shift : '/etc/collection.conf';
955         my $conf;
956         my $fh;
957
958 #       if (open ($fh, "< $file"))
959 #       {
960 #               my $xml;
961 #               local $/ = undef;
962 #               $xml = <$fh>;
963 #
964 #               eval
965 #               {
966 #                       $conf = xml2pl ($xml);
967 #               };
968 #               close ($fh);
969 #       }
970
971         if (!$conf)
972         {
973                 return ({
974                                 Colors =>
975                                 {
976                                         Back    => 'FFFFFF',
977                                         ShadeA  => 'FFFFFF',
978                                         ShadeB  => 'FFFFFF',
979                                         Font    => '000000',
980                                         Canvas  => 'F5F5F5',
981                                         Grid    => 'D0D0D0',
982                                         MGrid   => 'A0A0A0',
983                                         Frame   => '646464',
984                                         Arrow   => 'FF0000',
985
986                                         FullRed         => 'FF0000',
987                                         FullBlue        => '0000FF',
988                                         FullGreen       => '00E000',
989                                         FullYellow      => 'F0A000',
990                                         FullCyan        => '00A0FF',
991                                         FullMagenta     => 'A000FF',
992                                         Alpha           => 0.25,
993                                         HalfRed         => 'F8B8B8',
994                                         HalfBlue        => 'B8B8F8',
995                                         HalfGreen       => 'B8F0B8',
996                                         HalfYellow      => 'F4F4B8'
997                                 },
998                                 Directory => '/var/lib/collectd',
999                                 HostName  => (defined ($ENV{'SERVER_NAME'}) ? $ENV{'SERVER_NAME'} : 'localhost')
1000                         });
1001         }
1002         else
1003         {
1004                 return ($conf);
1005         }
1006 }
1007
1008 sub parse_pathinfo
1009 {
1010         my @info = @_;
1011
1012         $AbsDir = $Config->{'Directory'};
1013         $RelDir = '';
1014
1015         while (@info and -d $AbsDir . '/' . $Info[0])
1016         {
1017                 my $new = shift (@info);
1018                 next if ($new =~ m/^\./);
1019
1020                 $AbsDir .= '/' . $new;
1021                 $RelDir .= '/' . $new;
1022         }
1023
1024         $Type = '';
1025         $Inst = '';
1026         $TimeSpan = '';
1027
1028         confess ("parse_pathinfo: too many elements in pathinfo") if (scalar (@info) > 3);
1029         return unless (@info);
1030
1031         $Type = shift (@info);
1032         return unless (@info);
1033
1034         if ($info[-1] =~ m/^(hour|day|week|month|year)$/i)
1035         {
1036                 $TimeSpan = pop (@info);
1037         }
1038
1039         $Inst = shift (@info) if (@info);
1040
1041         confess ("unrecognized elements in pathinfo") if (@info);
1042 }
1043
1044 sub get_all_files
1045 {
1046         my $dir = @_ ? shift : $Config->{'Directory'};
1047         my $hash = {};
1048         my $dh;
1049
1050         if (opendir ($dh, $dir))
1051         {
1052                 while (my $thing = readdir ($dh))
1053                 {
1054                         next if ($thing =~ m/^\./);
1055
1056                         my $type;
1057                         my $inst;
1058
1059                         if ($thing =~ m/^(\w+)-([\w\-\.]+)\.rrd$/)
1060                         {
1061                                 $type = $1;
1062                                 $inst = $2;
1063                         }
1064                         elsif ($thing =~ m/^(\w+)\.rrd$/)
1065                         {
1066                                 $type = $1;
1067                                 $inst = '';
1068                         }
1069                         else
1070                         {
1071                                 next;
1072                         }
1073
1074                         # Only load RRD files we can actually display..
1075                         next unless (defined ($GraphDefs->{$type}));
1076
1077                         $hash->{$type} = [] unless (defined ($hash->{$type}));
1078                         push (@{$hash->{$type}}, $inst);
1079                 }
1080
1081                 closedir ($dh);
1082         }
1083
1084         return ($hash);
1085 }
1086
1087 sub get_all_dirs
1088 {
1089         my $dir = @_ ? shift : $Config->{'Directory'};
1090         my @ret = ();
1091         my $dh;
1092
1093         if (opendir ($dh, $dir))
1094         {
1095                 while (my $thing = readdir ($dh))
1096                 {
1097                         next if ($thing =~ m/^\./);
1098
1099                         next if (!-d "$dir/$thing");
1100
1101                         push (@ret, $thing);
1102                 }
1103
1104                 closedir ($dh);
1105         }
1106
1107         return (@ret) if (wantarray ());
1108         return (\@ret);
1109 }
1110
1111 sub color_hex2rgb
1112 {
1113         my $color = shift;
1114
1115         my ($red, $green, $blue) = map { ord (pack ("H2", $_)) } ($color =~ m/([A-Fa-f0-9]{2})/g);
1116         #print STDERR "$color -> rgb($red,$green,$blue)\n";
1117
1118         return ($red, $green, $blue);
1119 }
1120
1121 sub color_rgb2hex
1122 {
1123         croak unless (scalar (@_) == 3);
1124         
1125         my ($red, $green, $blue) = @_;
1126
1127         my $ret = sprintf ("%02X%02X%02X", $red, $green, $blue);
1128         #print STDERR "rgb($red,$green,$blue) -> $ret\n";
1129
1130         return ($ret);
1131 }
1132
1133 sub color_calculate_transparent
1134 {
1135         my $alpha = shift;
1136         my $canvas = [color_hex2rgb (shift)];
1137         my @colors = map { [color_hex2rgb ($_)] } (@_);
1138
1139         if (($alpha < 0.0) or ($alpha > 1.0))
1140         {
1141                 $alpha = 1.0;
1142         }
1143
1144         if ($alpha == 0.0)
1145         {
1146                 return (color_rgb2hex (@$canvas));
1147         }
1148         if ($alpha == 1.0)
1149         {
1150                 return (color_rgb2hex (@{$colors[-1]}));
1151         }
1152
1153         my $ret = _color_calculate_transparent ($alpha, $canvas, @colors);
1154
1155         return (color_rgb2hex (@$ret));
1156 }
1157
1158 sub _color_calculate_transparent
1159 {
1160         my $alpha = shift;
1161         my $canvas = shift;
1162         my $color = shift;
1163         my @colors = @_ ? shift : ();
1164         my $ret = [0, 0, 0];
1165
1166         for (my $i = 0; $i < 3; $i++)
1167         {
1168                 $ret->[$i] = ($alpha * $color->[$i]) + ((1 - $alpha) * $canvas->[$i]);
1169         }
1170
1171         return (_color_calculate_transparent ($alpha, $ret, @colors)) if (@colors);
1172         return ($ret);
1173 }
1174
1175 sub get_n_colors
1176 {
1177         my $num = shift;
1178         my @ret = ();
1179
1180         for (my $i = 0; $i < $num; $i++)
1181         {
1182                 my $pos = 6 * $i / $num;
1183                 my $n = int ($pos);
1184                 my $p = $pos - $n;
1185                 my $q = 1 - $p;
1186
1187                 my $red   = 0;
1188                 my $green = 0;
1189                 my $blue  = 0;
1190
1191                 if ($n == 0)
1192                 {
1193                         $red  = 255;
1194                         $blue = 255 * $p;
1195                 }
1196                 elsif ($n == 1)
1197                 {
1198                         $red  = 255 * $q;
1199                         $blue = 255;
1200                 }
1201                 elsif ($n == 2)
1202                 {
1203                         $green = 255 * $p;
1204                         $blue  = 255;
1205                 }
1206                 elsif ($n == 3)
1207                 {
1208                         $green = 255;
1209                         $blue  = 255 * $q;
1210                 }
1211                 elsif ($n == 4)
1212                 {
1213                         $red   = 255 * $p;
1214                         $green = 255;
1215                 }
1216                 elsif ($n == 5)
1217                 {
1218                         $red   = 255;
1219                         $green = 255 * $q;
1220                 }
1221                 else { die; }
1222
1223                 push (@ret, sprintf ("%02x%02x%02x", $red, $green, $blue));
1224         }
1225
1226         return (@ret);
1227 }