1 package Collectd::Graph::Type::GenericStacked;
3 use strict;
4 use warnings;
5 use base ('Collectd::Graph::Type');
7 use Collectd::Graph::Common (qw($ColorCanvas $ColorFullBlue $ColorHalfBlue
8 group_files_by_plugin_instance ident_to_filename sanitize_type_instance
9 get_faded_color sort_idents_by_type_instance));
11 return (1);
13 sub getGraphsNum
14 {
15 my $obj = shift;
16 my $group = group_files_by_plugin_instance (@{$obj->{'files'}});
18 return (scalar (keys %$group));
19 }
21 sub getRRDArgs
22 {
23 my $obj = shift;
24 my $index = shift;
26 my $group = group_files_by_plugin_instance (@{$obj->{'files'}});
27 my @group = sort (keys %$group);
29 my $rrd_opts = $obj->{'rrd_opts'} || [];
30 my $format = $obj->{'rrd_format'} || '%5.1lf';
32 my $idents = $group->{$group[$index]};
33 my $ds_name_len = 0;
35 my $ds = $obj->getDataSources ();
36 if (!$ds)
37 {
38 confess ("obj->getDataSources failed.");
39 }
40 if (@$ds != 1)
41 {
42 confess ("I can only work with RRD files that have "
43 . "exactly one data source!");
44 }
45 my $data_source = $ds->[0];
47 my $rrd_title = $obj->getTitle ($idents->[0]);
49 my $colors = $obj->{'rrd_colors'} || {};
50 my @ret = ('-t', $rrd_title, @$rrd_opts);
52 if (defined $obj->{'rrd_vertical'})
53 {
54 push (@ret, '-v', $obj->{'rrd_vertical'});
55 }
57 if ($obj->{'custom_order'})
58 {
59 sort_idents_by_type_instance ($idents, $obj->{'custom_order'});
60 }
62 $obj->{'ds_names'} ||= {};
63 my @names = map { $obj->{'ds_names'}{$_->{'type_instance'}} || $_->{'type_instance'} } (@$idents);
65 for (my $i = 0; $i < @$idents; $i++)
66 {
67 my $ident = $idents->[$i];
68 my $filename = ident_to_filename ($ident);
70 if ($ds_name_len < length ($names[$i]))
71 {
72 $ds_name_len = length ($names[$i]);
73 }
75 # Escape colons _after_ the length has been checked.
76 $names[$i] =~ s/:/\\:/g;
78 push (@ret,
79 "DEF:min${i}=${filename}:${data_source}:MIN",
80 "DEF:avg${i}=${filename}:${data_source}:AVERAGE",
81 "DEF:max${i}=${filename}:${data_source}:MAX");
82 }
84 for (my $i = @$idents - 1; $i >= 0; $i--)
85 {
86 if ($i == (@$idents - 1))
87 {
88 push (@ret,
89 "CDEF:cdef${i}=avg${i}");
90 }
91 else
92 {
93 my $j = $i + 1;
94 push (@ret,
95 "CDEF:cdef${i}=cdef${j},avg${i},+");
96 }
97 }
99 for (my $i = 0; $i < @$idents; $i++)
100 {
101 my $type_instance = $idents->[$i]{'type_instance'};
102 my $color = '000000';
103 if (exists $colors->{$type_instance})
104 {
105 $color = $colors->{$type_instance};
106 }
108 $color = get_faded_color ($color);
110 push (@ret,
111 "AREA:cdef${i}#${color}");
112 }
114 for (my $i = 0; $i < @$idents; $i++)
115 {
116 my $type_instance = $idents->[$i]{'type_instance'};
117 my $ds_name = sprintf ("%-*s", $ds_name_len, $names[$i]);
118 my $color = '000000';
119 if (exists $colors->{$type_instance})
120 {
121 $color = $colors->{$type_instance};
122 }
123 push (@ret,
124 "LINE1:cdef${i}#${color}:${ds_name}",
125 "GPRINT:min${i}:MIN:${format} Min,",
126 "GPRINT:avg${i}:AVERAGE:${format} Avg,",
127 "GPRINT:max${i}:MAX:${format} Max,",
128 "GPRINT:avg${i}:LAST:${format} Last\\l");
129 }
131 return (\@ret);
132 }
134 sub getGraphArgs
135 {
136 my $obj = shift;
137 my $index = shift;
139 my $group = group_files_by_plugin_instance (@{$obj->{'files'}});
140 my @group = sort (keys %$group);
142 my $idents = $group->{$group[$index]};
144 my @args = ();
145 for (qw(hostname plugin plugin_instance type))
146 {
147 if (defined ($idents->[0]{$_}))
148 {
149 push (@args, $_ . '=' . $idents->[0]{$_});
150 }
151 }
153 return (join (';', @args));
154 } # getGraphArgs
157 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :