1 package Collectd::Graph::Type::GenericStacked;
3 # Copyright (C) 2008,2009 Florian octo Forster <octo at verplant.org>
5 # This program is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free Software
7 # Foundation; only version 2 of the License is applicable.
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 # You should have received a copy of the GNU General Public License along with
15 # this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 use base ('Collectd::Graph::Type');
22 use Collectd::Graph::Common (qw($ColorCanvas $ColorFullBlue $ColorHalfBlue
23 group_files_by_plugin_instance ident_to_filename sanitize_type_instance
24 get_faded_color sort_idents_by_type_instance));
31 my $group = group_files_by_plugin_instance (@{$obj->{'files'}});
33 return (scalar (keys %$group));
41 my $group = group_files_by_plugin_instance (@{$obj->{'files'}});
42 my @group = sort (keys %$group);
44 my $rrd_opts = $obj->{'rrd_opts'} || [];
45 my $format = $obj->{'rrd_format'} || '%5.1lf';
47 my $idents = $group->{$group[$index]};
50 my $ds = $obj->getDataSources ();
53 confess ("obj->getDataSources failed.");
57 confess ("I can only work with RRD files that have "
58 . "exactly one data source!");
60 my $data_source = $ds->[0];
62 my $rrd_title = $obj->getTitle ($idents->[0]);
64 my $colors = $obj->{'rrd_colors'} || {};
65 my @ret = ('-t', $rrd_title, @$rrd_opts);
67 my $ignore_unknown = $obj->{'ignore_unknown'} || 0;
70 if ($ignore_unknown =~ m/^(yes|true|on)$/i)
80 my $stacking = $obj->{'stacking'};
83 if ($stacking =~ m/^(no|false|off|none)$/i)
93 if (defined $obj->{'rrd_vertical'})
95 push (@ret, '-v', $obj->{'rrd_vertical'});
98 if ($obj->{'custom_order'})
100 sort_idents_by_type_instance ($idents, $obj->{'custom_order'});
108 if (exists ($obj->{'ds_names'}{$_->{'type_instance'}}))
110 push (@$new_idents, $_);
116 $idents = $new_idents;
120 $obj->{'ds_names'} ||= {};
121 my @names = map { $obj->{'ds_names'}{$_->{'type_instance'}} || $_->{'type_instance'} } (@$idents);
123 for (my $i = 0; $i < @$idents; $i++)
125 my $ident = $idents->[$i];
126 my $filename = ident_to_filename ($ident);
128 if ($ds_name_len < length ($names[$i]))
130 $ds_name_len = length ($names[$i]);
133 # Escape colons _after_ the length has been checked.
134 $names[$i] =~ s/:/\\:/g;
137 "DEF:min${i}=${filename}:${data_source}:MIN",
138 "DEF:avg${i}=${filename}:${data_source}:AVERAGE",
139 "DEF:max${i}=${filename}:${data_source}:MAX");
143 for (my $i = @$idents - 1; $i >= 0; $i--)
145 if ($i == (@$idents - 1))
148 "CDEF:cdef${i}=avg${i},UN,0,avg${i},IF");
154 "CDEF:cdef${i}=avg${i},UN,0,avg${i},IF,cdef${j},+");
158 for (my $i = 0; $i < @$idents; $i++)
160 my $type_instance = $idents->[$i]{'type_instance'};
161 my $color = '000000';
162 if (exists $colors->{$type_instance})
164 $color = $colors->{$type_instance};
167 $color = get_faded_color ($color);
170 "AREA:cdef${i}#${color}");
175 for (my $i = @$idents - 1; $i >= 0; $i--)
178 "CDEF:cdef${i}=avg${i}");
183 for (my $i = 0; $i < @$idents; $i++)
185 my $type_instance = $idents->[$i]{'type_instance'};
186 my $ds_name = sprintf ("%-*s", $ds_name_len, $names[$i]);
187 my $color = '000000';
188 if (exists $colors->{$type_instance})
190 $color = $colors->{$type_instance};
193 "LINE1:cdef${i}#${color}:${ds_name}",
194 "GPRINT:min${i}:MIN:${format} Min,",
195 "GPRINT:avg${i}:AVERAGE:${format} Avg,",
196 "GPRINT:max${i}:MAX:${format} Max,",
197 "GPRINT:avg${i}:LAST:${format} Last\\l");
208 my $group = group_files_by_plugin_instance (@{$obj->{'files'}});
209 my @group = sort (keys %$group);
211 my $idents = $group->{$group[$index]};
214 for (qw(hostname plugin plugin_instance type))
216 if (defined ($idents->[0]{$_}))
218 push (@args, $_ . '=' . $idents->[0]{$_});
222 return (join (';', @args));
226 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :