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