contrib/collection3: Add the "IgnoreUnknown" config option.
[collectd.git] / contrib / collection3 / lib / Collectd / Graph / Type / GenericStacked.pm
1 package Collectd::Graph::Type::GenericStacked;
2
3 use strict;
4 use warnings;
5 use base ('Collectd::Graph::Type');
6
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));
10
11 return (1);
12
13 sub getGraphsNum
14 {
15   my $obj = shift;
16   my $group = group_files_by_plugin_instance (@{$obj->{'files'}});
17
18   return (scalar (keys %$group));
19 }
20
21 sub getRRDArgs
22 {
23   my $obj = shift;
24   my $index = shift;
25
26   my $group = group_files_by_plugin_instance (@{$obj->{'files'}});
27   my @group = sort (keys %$group);
28
29   my $rrd_opts = $obj->{'rrd_opts'} || [];
30   my $format = $obj->{'rrd_format'} || '%5.1lf';
31
32   my $idents = $group->{$group[$index]};
33   my $ds_name_len = 0;
34
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];
46
47   my $rrd_title = $obj->getTitle ($idents->[0]);
48
49   my $colors = $obj->{'rrd_colors'} || {};
50   my @ret = ('-t', $rrd_title, @$rrd_opts);
51
52   my $ignore_unknown = $obj->{'ignore_unknown'} || 0;
53   if ($ignore_unknown)
54   {
55     if ($ignore_unknown =~ m/^(yes|true|on)$/i)
56     {
57       $ignore_unknown = 1;
58     }
59     else
60     {
61       $ignore_unknown = 0;
62     }
63   }
64
65   if (defined $obj->{'rrd_vertical'})
66   {
67     push (@ret, '-v', $obj->{'rrd_vertical'});
68   }
69
70   if ($obj->{'custom_order'})
71   {
72     sort_idents_by_type_instance ($idents, $obj->{'custom_order'});
73   }
74
75   if ($ignore_unknown)
76   {
77     my $new_idents = [];
78     for (@$idents)
79     {
80       if (exists ($obj->{'ds_names'}{$_->{'type_instance'}}))
81       {
82         push (@$new_idents, $_);
83       }
84     }
85
86     if (@$new_idents)
87     {
88       $idents = $new_idents;
89     }
90   }
91
92   $obj->{'ds_names'} ||= {};
93   my @names = map { $obj->{'ds_names'}{$_->{'type_instance'}} || $_->{'type_instance'} } (@$idents);
94
95   for (my $i = 0; $i < @$idents; $i++)
96   {
97     my $ident = $idents->[$i];
98     my $filename = ident_to_filename ($ident);
99
100     if ($ds_name_len < length ($names[$i]))
101     {
102       $ds_name_len = length ($names[$i]);
103     }
104     
105     # Escape colons _after_ the length has been checked.
106     $names[$i] =~ s/:/\\:/g;
107
108     push (@ret,
109       "DEF:min${i}=${filename}:${data_source}:MIN",
110       "DEF:avg${i}=${filename}:${data_source}:AVERAGE",
111       "DEF:max${i}=${filename}:${data_source}:MAX");
112   }
113
114   for (my $i = @$idents - 1; $i >= 0; $i--)
115   {
116     if ($i == (@$idents - 1))
117     {
118       push (@ret,
119         "CDEF:cdef${i}=avg${i}");
120     }
121     else
122     {
123       my $j = $i + 1;
124       push (@ret,
125         "CDEF:cdef${i}=cdef${j},avg${i},+");
126     }
127   }
128
129   for (my $i = 0; $i < @$idents; $i++)
130   {
131     my $type_instance = $idents->[$i]{'type_instance'};
132     my $color = '000000';
133     if (exists $colors->{$type_instance})
134     {
135       $color = $colors->{$type_instance};
136     }
137
138     $color = get_faded_color ($color);
139
140     push (@ret,
141       "AREA:cdef${i}#${color}");
142   }
143
144   for (my $i = 0; $i < @$idents; $i++)
145   {
146     my $type_instance = $idents->[$i]{'type_instance'};
147     my $ds_name = sprintf ("%-*s", $ds_name_len, $names[$i]);
148     my $color = '000000';
149     if (exists $colors->{$type_instance})
150     {
151       $color = $colors->{$type_instance};
152     }
153     push (@ret,
154       "LINE1:cdef${i}#${color}:${ds_name}",
155       "GPRINT:min${i}:MIN:${format} Min,",
156       "GPRINT:avg${i}:AVERAGE:${format} Avg,",
157       "GPRINT:max${i}:MAX:${format} Max,",
158       "GPRINT:avg${i}:LAST:${format} Last\\l");
159   }
160
161   return (\@ret);
162 }
163
164 sub getGraphArgs
165 {
166   my $obj = shift;
167   my $index = shift;
168
169   my $group = group_files_by_plugin_instance (@{$obj->{'files'}});
170   my @group = sort (keys %$group);
171
172   my $idents = $group->{$group[$index]};
173
174   my @args = ();
175   for (qw(hostname plugin plugin_instance type))
176   {
177     if (defined ($idents->[0]{$_}))
178     {
179       push (@args, $_ . '=' . $idents->[0]{$_});
180     }
181   }
182
183   return (join (';', @args));
184 } # getGraphArgs
185
186
187 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :