1 package Collectd::Graph::Common;
6 use vars (qw($ColorCanvas $ColorFullBlue $ColorHalfBlue));
8 use Carp (qw(confess cluck));
12 $ColorCanvas = 'FFFFFF';
13 $ColorFullBlue = '0000FF';
14 $ColorHalfBlue = 'B7B7F7';
16 @Collectd::Graph::Common::ISA = ('Exporter');
17 @Collectd::Graph::Common::EXPORT_OK = (qw(
23 sanitize_plugin sanitize_plugin_instance
24 sanitize_type sanitize_type_instance
25 group_files_by_plugin_instance
26 get_files_from_directory
34 get_timespan_selection
38 sort_idents_by_type_instance
43 our $DataDir = '/var/lib/collectd/rrd';
47 sub _sanitize_generic_allow_minus
54 # remove all dots and dashes at the beginning and at the end.
61 sub _sanitize_generic_no_minus
63 # Do everything the allow-minus variant does..
64 my $str = _sanitize_generic_allow_minus (@_);
66 # .. and remove the dashes, too
70 } # _sanitize_generic_no_minus
74 return (_sanitize_generic_allow_minus (@_));
79 return (_sanitize_generic_no_minus (@_));
82 sub sanitize_plugin_instance
84 return (_sanitize_generic_allow_minus (@_));
89 return (_sanitize_generic_no_minus (@_));
92 sub sanitize_type_instance
94 return (_sanitize_generic_allow_minus (@_));
97 sub group_files_by_plugin_instance
102 for (my $i = 0; $i < @files; $i++)
104 my $file = $files[$i];
105 my $key = $file->{'plugin_instance'} || '';
107 $data->{$key} ||= [];
108 push (@{$data->{$key}}, $file);
114 sub filename_to_ident
119 if ($file =~ m#([^/]+)/([^/\-]+)(?:-([^/]+))?/([^/\-]+)(?:-([^/]+))?\.rrd$#)
121 $ret = {hostname => $1, plugin => $2, type => $4};
124 $ret->{'plugin_instance'} = $3;
128 $ret->{'type_instance'} = $5;
132 $ret->{'_prefix'} = $`;
141 } # filename_to_ident
143 sub ident_to_filename
149 if (defined ($ident->{'_prefix'}))
151 $ret .= $ident->{'_prefix'};
158 if (!$ident->{'hostname'})
160 cluck ("hostname is undefined")
162 if (!$ident->{'plugin'})
164 cluck ("plugin is undefined")
166 if (!$ident->{'type'})
168 cluck ("type is undefined")
171 $ret .= $ident->{'hostname'} . '/' . $ident->{'plugin'};
172 if (defined ($ident->{'plugin_instance'}))
174 $ret .= '-' . $ident->{'plugin_instance'};
177 $ret .= '/' . $ident->{'type'};
178 if (defined ($ident->{'type_instance'}))
180 $ret .= '-' . $ident->{'type_instance'};
185 } # ident_to_filename
193 $ret .= $ident->{'hostname'} . '/' . $ident->{'plugin'};
194 if (defined ($ident->{'plugin_instance'}))
196 $ret .= '-' . $ident->{'plugin_instance'};
199 $ret .= '/' . $ident->{'type'};
200 if (defined ($ident->{'type_instance'}))
202 $ret .= '-' . $ident->{'type_instance'};
208 sub get_files_from_directory
211 my $recursive = @_ ? shift : 0;
213 my @directories = ();
216 opendir ($dh, $dir) or die ("opendir ($dir): $!");
217 while (my $entry = readdir ($dh))
219 next if ($entry =~ m/^\./);
221 $entry = "$dir/$entry";
225 push (@directories, $entry);
229 my $ident = filename_to_ident ($entry);
232 push (@$ret, $ident);
242 my $temp = get_files_from_directory ($_, $recursive - 1);
245 push (@$ret, @$temp);
251 } # get_files_from_directory
258 opendir ($dh, "$DataDir") or confess ("opendir ($DataDir): $!");
259 while (my $entry = readdir ($dh))
261 next if ($entry =~ m/^\./);
262 next if (!-d "$DataDir/$entry");
263 push (@ret, sanitize_hostname ($entry));
289 @hosts = get_all_hosts ();
295 opendir ($dh, "$DataDir/$host") or next;
296 while (my $entry = readdir ($dh))
299 my $plugin_instance = '';
301 next if ($entry =~ m/^\./);
302 next if (!-d "$DataDir/$host/$entry");
304 if ($entry =~ m#^([^-]+)-(.+)$#)
307 $plugin_instance = $2;
309 elsif ($entry =~ m#^([^-]+)$#)
312 $plugin_instance = '';
319 $ret->{$plugin} ||= {};
320 $ret->{$plugin}{$plugin_instance} = 1;
327 return (sort (keys %$ret));
335 sub get_files_for_host
337 my $host = sanitize_hostname (shift);
338 return (get_files_from_directory ("$DataDir/$host", 2));
339 } # get_files_for_host
346 for (qw(hostname plugin plugin_instance type type_instance))
351 if (!defined ($filter->{$part}))
355 if (!defined ($ident->{$part}))
360 if (ref $filter->{$part})
362 if (!grep { $ident->{$part} eq $_ } (@{$filter->{$part}}))
369 if ($ident->{$part} ne $filter->{$part})
379 sub get_files_by_ident
385 #if ($ident->{'hostname'})
387 #$all_files = get_files_for_host ($ident->{'hostname'});
391 $all_files = get_files_from_directory ($DataDir, 3);
394 @ret = grep { _filter_ident ($ident, $_) == 0 } (@$all_files);
397 } # get_files_by_ident
399 sub get_selected_files
403 for (qw(hostname plugin plugin_instance type type_instance))
406 my @temp = param ($part);
411 elsif (($part eq 'plugin') || ($part eq 'type'))
413 $ident->{$part} = [map { _sanitize_generic_no_minus ($_) } (@temp)];
417 $ident->{$part} = [map { _sanitize_generic_allow_minus ($_) } (@temp)];
421 return (get_files_by_ident ($ident));
422 } # get_selected_files
424 sub get_timespan_selection
427 if (param ('timespan'))
429 my $temp = int (param ('timespan'));
430 if ($temp && ($temp > 0))
437 } # get_timespan_selection
439 sub get_host_selection
443 for (get_all_hosts ())
448 for (param ('hostname'))
450 my $host = _sanitize_generic_allow_minus ($_);
451 if (defined ($ret{$host}))
459 return (grep { $ret{$_} > 0 } (sort (keys %ret)));
465 } # get_host_selection
467 sub get_plugin_selection
470 my @hosts = get_host_selection ();
472 for (get_all_plugins (@hosts))
477 for (param ('plugin'))
479 if (defined ($ret{$_}))
487 return (grep { $ret{$_} > 0 } (sort (keys %ret)));
493 } # get_plugin_selection
498 if ($color =~ m/([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])/)
500 return ([hex ($1) / 255.0, hex ($2) / 255.0, hex ($3) / 255.0]);
507 confess ("Wrong number of arguments") if (@_ != 1);
508 return (sprintf ('%02hx%02hx%02hx', map { int (255.0 * $_) } @{$_[0]}));
516 my $ret = [undef, undef, undef];
518 $opts{'background'} ||= [1.0, 1.0, 1.0];
519 $opts{'alpha'} ||= 0.25;
523 $fg = _string_to_color ($fg)
524 or confess ("Cannot parse foreground color $fg");
527 if (!ref ($opts{'background'}))
529 $opts{'background'} = _string_to_color ($opts{'background'})
530 or confess ("Cannot parse background color " . $opts{'background'});
532 $bg = $opts{'background'};
534 for (my $i = 0; $i < 3; $i++)
536 $ret->[$i] = ($opts{'alpha'} * $fg->[$i])
537 + ((1.0 - $opts{'alpha'}) * $bg->[$i]);
540 return (_color_to_string ($ret));
543 sub sort_idents_by_type_instance
546 my $array_sort = shift;
548 my %elements = map { $_->{'type_instance'} => $_ } (@$idents);
549 splice (@$idents, 0);
553 next if (!exists ($elements{$_}));
554 push (@$idents, $elements{$_});
555 delete ($elements{$_});
557 push (@$idents, map { $elements{$_} } (sort (keys %elements)));
558 } # sort_idents_by_type_instance
560 sub type_to_module_name
565 $ret = ucfirst (lc ($type));
567 $ret =~ s/[^A-Za-z_]//g;
568 $ret =~ s/_([A-Za-z])/\U$1\E/g;
570 return ("Collectd::Graph::Type::$ret");
571 } # type_to_module_name
575 my @days = (qw(Sun Mon Tue Wed Thu Fri Sat));
576 my @months = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
578 my $epoch = @_ ? shift : time ();
579 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
580 my $string = sprintf ('%s, %02d %s %4d %02d:%02d:%02d GMT', $days[$wday], $mday,
581 $months[$mon], 1900 + $year, $hour ,$min, $sec);
585 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :