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