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