contrib/collection3: Add an basic, extensible, modular graphing front-end.
[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 $rrd_title = $obj->getTitle ($idents->[0]);
36
37   my $colors = $obj->{'rrd_colors'} || {};
38   my @ret = ('-t', $rrd_title, @$rrd_opts);
39
40   if (defined $obj->{'rrd_vertical'})
41   {
42     push (@ret, '-v', $obj->{'rrd_vertical'});
43   }
44
45   if ($obj->{'custom_order'})
46   {
47     sort_idents_by_type_instance ($idents, $obj->{'custom_order'});
48   }
49
50   $obj->{'ds_names'} ||= {};
51   my @names = map { $obj->{'ds_names'}{$_->{'type_instance'}} || $_->{'type_instance'} } (@$idents);
52
53   for (my $i = 0; $i < @$idents; $i++)
54   {
55     my $ident = $idents->[$i];
56     my $filename = ident_to_filename ($ident);
57
58     if ($ds_name_len < length ($names[$i]))
59     {
60       $ds_name_len = length ($names[$i]);
61     }
62     
63     # Escape colons _after_ the length has been checked.
64     $names[$i] =~ s/:/\\:/g;
65
66     push (@ret,
67       "DEF:min${i}=${filename}:value:MIN",
68       "DEF:avg${i}=${filename}:value:AVERAGE",
69       "DEF:max${i}=${filename}:value:MAX");
70   }
71
72   for (my $i = @$idents - 1; $i >= 0; $i--)
73   {
74     if ($i == (@$idents - 1))
75     {
76       push (@ret,
77         "CDEF:cdef${i}=avg${i}");
78     }
79     else
80     {
81       my $j = $i + 1;
82       push (@ret,
83         "CDEF:cdef${i}=cdef${j},avg${i},+");
84     }
85   }
86
87   for (my $i = 0; $i < @$idents; $i++)
88   {
89     my $type_instance = $idents->[$i]{'type_instance'};
90     my $color = '000000';
91     if (exists $colors->{$type_instance})
92     {
93       $color = $colors->{$type_instance};
94     }
95
96     $color = get_faded_color ($color);
97
98     push (@ret,
99       "AREA:cdef${i}#${color}");
100   }
101
102   for (my $i = 0; $i < @$idents; $i++)
103   {
104     my $type_instance = $idents->[$i]{'type_instance'};
105     my $ds_name = sprintf ("%-*s", $ds_name_len, $names[$i]);
106     my $color = '000000';
107     if (exists $colors->{$type_instance})
108     {
109       $color = $colors->{$type_instance};
110     }
111     push (@ret,
112       "LINE1:cdef${i}#${color}:${ds_name}",
113       "GPRINT:min${i}:MIN:${format} Min,",
114       "GPRINT:avg${i}:AVERAGE:${format} Avg,",
115       "GPRINT:max${i}:MAX:${format} Max,",
116       "GPRINT:avg${i}:LAST:${format} Last\\l");
117   }
118
119   return (\@ret);
120 }
121
122 sub getGraphArgs
123 {
124   my $obj = shift;
125   my $index = shift;
126
127   my $group = group_files_by_plugin_instance (@{$obj->{'files'}});
128   my @group = sort (keys %$group);
129
130   my $idents = $group->{$group[$index]};
131
132   my @args = ();
133   for (qw(hostname plugin plugin_instance type))
134   {
135     if (defined ($idents->[0]{$_}))
136     {
137       push (@args, $_ . '=' . $idents->[0]{$_});
138     }
139   }
140
141   return (join (';', @args));
142 } # getGraphArgs
143
144
145 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :