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