contrib/collection3: Updated the `collection3' stuff in contrib/.
[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   if (defined $obj->{'rrd_vertical'})
53   {
54     push (@ret, '-v', $obj->{'rrd_vertical'});
55   }
56
57   if ($obj->{'custom_order'})
58   {
59     sort_idents_by_type_instance ($idents, $obj->{'custom_order'});
60   }
61
62   $obj->{'ds_names'} ||= {};
63   my @names = map { $obj->{'ds_names'}{$_->{'type_instance'}} || $_->{'type_instance'} } (@$idents);
64
65   for (my $i = 0; $i < @$idents; $i++)
66   {
67     my $ident = $idents->[$i];
68     my $filename = ident_to_filename ($ident);
69
70     if ($ds_name_len < length ($names[$i]))
71     {
72       $ds_name_len = length ($names[$i]);
73     }
74     
75     # Escape colons _after_ the length has been checked.
76     $names[$i] =~ s/:/\\:/g;
77
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   }
83
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   }
98
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     }
107
108     $color = get_faded_color ($color);
109
110     push (@ret,
111       "AREA:cdef${i}#${color}");
112   }
113
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   }
130
131   return (\@ret);
132 }
133
134 sub getGraphArgs
135 {
136   my $obj = shift;
137   my $index = shift;
138
139   my $group = group_files_by_plugin_instance (@{$obj->{'files'}});
140   my @group = sort (keys %$group);
141
142   my $idents = $group->{$group[$index]};
143
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   }
152
153   return (join (';', @args));
154 } # getGraphArgs
155
156
157 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :