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
44 our $DataDir = '/var/lib/collectd/rrd';
48 sub _sanitize_generic_allow_minus
55 # remove all dots and dashes at the beginning and at the end.
62 sub _sanitize_generic_no_minus
64 # Do everything the allow-minus variant does..
65 my $str = _sanitize_generic_allow_minus (@_);
67 # .. and remove the dashes, too
71 } # _sanitize_generic_no_minus
75 return (_sanitize_generic_allow_minus (@_));
80 return (_sanitize_generic_no_minus (@_));
83 sub sanitize_plugin_instance
85 return (_sanitize_generic_allow_minus (@_));
90 return (_sanitize_generic_no_minus (@_));
93 sub sanitize_type_instance
95 return (_sanitize_generic_allow_minus (@_));
98 sub group_files_by_plugin_instance
103 for (my $i = 0; $i < @files; $i++)
105 my $file = $files[$i];
106 my $key = $file->{'plugin_instance'} || '';
108 $data->{$key} ||= [];
109 push (@{$data->{$key}}, $file);
115 sub filename_to_ident
120 if ($file =~ m#([^/]+)/([^/\-]+)(?:-([^/]+))?/([^/\-]+)(?:-([^/]+))?\.rrd$#)
122 $ret = {hostname => $1, plugin => $2, type => $4};
125 $ret->{'plugin_instance'} = $3;
129 $ret->{'type_instance'} = $5;
133 $ret->{'_prefix'} = $`;
142 } # filename_to_ident
144 sub ident_to_filename
150 if (defined ($ident->{'_prefix'}))
152 $ret .= $ident->{'_prefix'};
159 if (!$ident->{'hostname'})
161 cluck ("hostname is undefined")
163 if (!$ident->{'plugin'})
165 cluck ("plugin is undefined")
167 if (!$ident->{'type'})
169 cluck ("type is undefined")
172 $ret .= $ident->{'hostname'} . '/' . $ident->{'plugin'};
173 if (defined ($ident->{'plugin_instance'}))
175 $ret .= '-' . $ident->{'plugin_instance'};
178 $ret .= '/' . $ident->{'type'};
179 if (defined ($ident->{'type_instance'}))
181 $ret .= '-' . $ident->{'type_instance'};
186 } # ident_to_filename
194 $ret .= $ident->{'hostname'} . '/' . $ident->{'plugin'};
195 if (defined ($ident->{'plugin_instance'}))
197 $ret .= '-' . $ident->{'plugin_instance'};
200 $ret .= '/' . $ident->{'type'};
201 if (defined ($ident->{'type_instance'}))
203 $ret .= '-' . $ident->{'type_instance'};
209 sub get_files_from_directory
212 my $recursive = @_ ? shift : 0;
214 my @directories = ();
217 opendir ($dh, $dir) or die ("opendir ($dir): $!");
218 while (my $entry = readdir ($dh))
220 next if ($entry =~ m/^\./);
222 $entry = "$dir/$entry";
226 push (@directories, $entry);
230 my $ident = filename_to_ident ($entry);
233 push (@$ret, $ident);
243 my $temp = get_files_from_directory ($_, $recursive - 1);
246 push (@$ret, @$temp);
252 } # get_files_from_directory
259 opendir ($dh, "$DataDir") or confess ("opendir ($DataDir): $!");
260 while (my $entry = readdir ($dh))
262 next if ($entry =~ m/^\./);
263 next if (!-d "$DataDir/$entry");
264 push (@ret, sanitize_hostname ($entry));
290 @hosts = get_all_hosts ();
296 opendir ($dh, "$DataDir/$host") or next;
297 while (my $entry = readdir ($dh))
300 my $plugin_instance = '';
302 next if ($entry =~ m/^\./);
303 next if (!-d "$DataDir/$host/$entry");
305 if ($entry =~ m#^([^-]+)-(.+)$#)
308 $plugin_instance = $2;
310 elsif ($entry =~ m#^([^-]+)$#)
313 $plugin_instance = '';
320 $ret->{$plugin} ||= {};
321 $ret->{$plugin}{$plugin_instance} = 1;
328 return (sort (keys %$ret));
336 sub get_files_for_host
338 my $host = sanitize_hostname (shift);
339 return (get_files_from_directory ("$DataDir/$host", 2));
340 } # get_files_for_host
347 for (qw(hostname plugin plugin_instance type type_instance))
352 if (!defined ($filter->{$part}))
356 if (!defined ($ident->{$part}))
361 if (ref $filter->{$part})
363 if (!grep { $ident->{$part} eq $_ } (@{$filter->{$part}}))
370 if ($ident->{$part} ne $filter->{$part})
380 sub get_files_by_ident
386 #if ($ident->{'hostname'})
388 #$all_files = get_files_for_host ($ident->{'hostname'});
392 $all_files = get_files_from_directory ($DataDir, 3);
395 @ret = grep { _filter_ident ($ident, $_) == 0 } (@$all_files);
398 } # get_files_by_ident
400 sub get_selected_files
404 for (qw(hostname plugin plugin_instance type type_instance))
407 my @temp = param ($part);
412 elsif (($part eq 'plugin') || ($part eq 'type'))
414 $ident->{$part} = [map { _sanitize_generic_no_minus ($_) } (@temp)];
418 $ident->{$part} = [map { _sanitize_generic_allow_minus ($_) } (@temp)];
422 return (get_files_by_ident ($ident));
423 } # get_selected_files
425 sub get_timespan_selection
428 if (param ('timespan'))
430 my $temp = int (param ('timespan'));
431 if ($temp && ($temp > 0))
438 } # get_timespan_selection
440 sub get_host_selection
444 for (get_all_hosts ())
449 for (param ('hostname'))
451 my $host = _sanitize_generic_allow_minus ($_);
452 if (defined ($ret{$host}))
460 return (grep { $ret{$_} > 0 } (sort (keys %ret)));
466 } # get_host_selection
468 sub get_plugin_selection
471 my @hosts = get_host_selection ();
473 for (get_all_plugins (@hosts))
478 for (param ('plugin'))
480 if (defined ($ret{$_}))
488 return (grep { $ret{$_} > 0 } (sort (keys %ret)));
494 } # get_plugin_selection
499 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])/)
501 return ([hex ($1) / 255.0, hex ($2) / 255.0, hex ($3) / 255.0]);
508 confess ("Wrong number of arguments") if (@_ != 1);
509 return (sprintf ('%02hx%02hx%02hx', map { int (255.0 * $_) } @{$_[0]}));
517 my $ret = [undef, undef, undef];
519 $opts{'background'} ||= [1.0, 1.0, 1.0];
520 $opts{'alpha'} ||= 0.25;
524 $fg = _string_to_color ($fg)
525 or confess ("Cannot parse foreground color $fg");
528 if (!ref ($opts{'background'}))
530 $opts{'background'} = _string_to_color ($opts{'background'})
531 or confess ("Cannot parse background color " . $opts{'background'});
533 $bg = $opts{'background'};
535 for (my $i = 0; $i < 3; $i++)
537 $ret->[$i] = ($opts{'alpha'} * $fg->[$i])
538 + ((1.0 - $opts{'alpha'}) * $bg->[$i]);
541 return (_color_to_string ($ret));
544 sub sort_idents_by_type_instance
547 my $array_sort = shift;
549 my %elements = map { $_->{'type_instance'} => $_ } (@$idents);
550 splice (@$idents, 0);
554 next if (!exists ($elements{$_}));
555 push (@$idents, $elements{$_});
556 delete ($elements{$_});
558 push (@$idents, map { $elements{$_} } (sort (keys %elements)));
559 } # sort_idents_by_type_instance
561 sub type_to_module_name
566 $ret = ucfirst (lc ($type));
568 $ret =~ s/[^A-Za-z_]//g;
569 $ret =~ s/_([A-Za-z])/\U$1\E/g;
571 return ("Collectd::Graph::Type::$ret");
572 } # type_to_module_name
576 my @days = (qw(Sun Mon Tue Wed Thu Fri Sat));
577 my @months = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
579 my $epoch = @_ ? shift : time ();
580 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
581 my $string = sprintf ('%s, %02d %s %4d %02d:%02d:%02d GMT', $days[$wday], $mday,
582 $months[$mon], 1900 + $year, $hour ,$min, $sec);
588 my $all_files = shift;
597 my $files_to_flush = [];
600 if (!defined $opts{'begin'})
602 cluck ("begin is not defined");
605 $begin = $opts{'begin'};
607 if (!defined $opts{'end'})
609 cluck ("end is not defined");
619 $interval = $opts{'interval'} || 10;
621 if (ref ($all_files) eq 'HASH')
623 my @tmp = ($all_files);
628 # Don't flush anything if the timespan is in the future.
629 if (($end > $now) && ($begin > $now))
637 my $file_name = ident_to_filename ($file_orig);
642 @statbuf = stat ($file_name);
647 $mtime = $statbuf[9];
649 # Skip if file is fresh
650 if (($now - $mtime) <= $interval)
654 # or $end is before $mtime
655 elsif (($end != 0) && (($end - $mtime) <= 0))
660 $file_copy->{'host'} = $file_orig->{'hostname'};
661 $file_copy->{'plugin'} = $file_orig->{'plugin'};
662 if (exists $file_orig->{'plugin_instance'})
664 $file_copy->{'plugin_instance'} = $file_orig->{'plugin_instance'}
666 $file_copy->{'type'} = $file_orig->{'type'};
667 if (exists $file_orig->{'type_instance'})
669 $file_copy->{'type_instance'} = $file_orig->{'type_instance'}
672 push (@$files_to_flush, $file_copy);
673 } # for (@$all_files)
675 if (!@$files_to_flush)
680 $sock = Collectd::Unixsock->new ($opts{'addr'});
686 $status = $sock->flush (plugins => ['rrdtool'], identifier => $files_to_flush);
689 cluck ("FLUSH failed: " . $sock->{'error'});
698 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :