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