1 package Collectd::Graph::Common;
6 use vars (qw($ColorCanvas $ColorFullBlue $ColorHalfBlue));
8 use Collectd::Unixsock;
10 use Carp (qw(confess cluck));
14 $ColorCanvas = 'FFFFFF';
15 $ColorFullBlue = '0000FF';
16 $ColorHalfBlue = 'B7B7F7';
18 @Collectd::Graph::Common::ISA = ('Exporter');
19 @Collectd::Graph::Common::EXPORT_OK = (qw(
25 sanitize_plugin sanitize_plugin_instance
26 sanitize_type sanitize_type_instance
27 group_files_by_plugin_instance
28 get_files_from_directory
36 get_timespan_selection
40 sort_idents_by_type_instance
46 our $DataDir = '/var/lib/collectd/rrd';
50 sub _sanitize_generic_allow_minus
57 # remove all dots and dashes at the beginning and at the end.
64 sub _sanitize_generic_no_minus
66 # Do everything the allow-minus variant does..
67 my $str = _sanitize_generic_allow_minus (@_);
69 # .. and remove the dashes, too
73 } # _sanitize_generic_no_minus
77 return (_sanitize_generic_allow_minus (@_));
82 return (_sanitize_generic_no_minus (@_));
85 sub sanitize_plugin_instance
87 return (_sanitize_generic_allow_minus (@_));
92 return (_sanitize_generic_no_minus (@_));
95 sub sanitize_type_instance
97 return (_sanitize_generic_allow_minus (@_));
100 sub group_files_by_plugin_instance
105 for (my $i = 0; $i < @files; $i++)
107 my $file = $files[$i];
108 my $key = $file->{'plugin_instance'} || '';
110 $data->{$key} ||= [];
111 push (@{$data->{$key}}, $file);
117 sub filename_to_ident
122 if ($file =~ m#([^/]+)/([^/\-]+)(?:-([^/]+))?/([^/\-]+)(?:-([^/]+))?\.rrd$#)
124 $ret = {hostname => $1, plugin => $2, type => $4};
127 $ret->{'plugin_instance'} = $3;
131 $ret->{'type_instance'} = $5;
135 $ret->{'_prefix'} = $`;
144 } # filename_to_ident
146 sub ident_to_filename
152 if (defined ($ident->{'_prefix'}))
154 $ret .= $ident->{'_prefix'};
161 if (!$ident->{'hostname'})
163 cluck ("hostname is undefined")
165 if (!$ident->{'plugin'})
167 cluck ("plugin is undefined")
169 if (!$ident->{'type'})
171 cluck ("type is undefined")
174 $ret .= $ident->{'hostname'} . '/' . $ident->{'plugin'};
175 if (defined ($ident->{'plugin_instance'}))
177 $ret .= '-' . $ident->{'plugin_instance'};
180 $ret .= '/' . $ident->{'type'};
181 if (defined ($ident->{'type_instance'}))
183 $ret .= '-' . $ident->{'type_instance'};
188 } # ident_to_filename
196 $ret .= $ident->{'hostname'} . '/' . $ident->{'plugin'};
197 if (defined ($ident->{'plugin_instance'}))
199 $ret .= '-' . $ident->{'plugin_instance'};
202 $ret .= '/' . $ident->{'type'};
203 if (defined ($ident->{'type_instance'}))
205 $ret .= '-' . $ident->{'type_instance'};
211 sub get_files_from_directory
214 my $recursive = @_ ? shift : 0;
216 my @directories = ();
219 opendir ($dh, $dir) or die ("opendir ($dir): $!");
220 while (my $entry = readdir ($dh))
222 next if ($entry =~ m/^\./);
224 $entry = "$dir/$entry";
228 push (@directories, $entry);
232 my $ident = filename_to_ident ($entry);
235 push (@$ret, $ident);
245 my $temp = get_files_from_directory ($_, $recursive - 1);
248 push (@$ret, @$temp);
254 } # get_files_from_directory
261 opendir ($dh, "$DataDir") or confess ("opendir ($DataDir): $!");
262 while (my $entry = readdir ($dh))
264 next if ($entry =~ m/^\./);
265 next if (!-d "$DataDir/$entry");
266 push (@ret, sanitize_hostname ($entry));
292 @hosts = get_all_hosts ();
298 opendir ($dh, "$DataDir/$host") or next;
299 while (my $entry = readdir ($dh))
302 my $plugin_instance = '';
304 next if ($entry =~ m/^\./);
305 next if (!-d "$DataDir/$host/$entry");
307 if ($entry =~ m#^([^-]+)-(.+)$#)
310 $plugin_instance = $2;
312 elsif ($entry =~ m#^([^-]+)$#)
315 $plugin_instance = '';
322 $ret->{$plugin} ||= {};
323 $ret->{$plugin}{$plugin_instance} = 1;
330 return (sort (keys %$ret));
338 sub get_files_for_host
340 my $host = sanitize_hostname (shift);
341 return (get_files_from_directory ("$DataDir/$host", 2));
342 } # get_files_for_host
349 for (qw(hostname plugin plugin_instance type type_instance))
354 if (!defined ($filter->{$part}))
358 if (!defined ($ident->{$part}))
363 if (ref $filter->{$part})
365 if (!grep { $ident->{$part} eq $_ } (@{$filter->{$part}}))
372 if ($ident->{$part} ne $filter->{$part})
382 sub get_files_by_ident
388 #if ($ident->{'hostname'})
390 #$all_files = get_files_for_host ($ident->{'hostname'});
394 $all_files = get_files_from_directory ($DataDir, 3);
397 @ret = grep { _filter_ident ($ident, $_) == 0 } (@$all_files);
400 } # get_files_by_ident
402 sub get_selected_files
406 for (qw(hostname plugin plugin_instance type type_instance))
409 my @temp = param ($part);
414 elsif (($part eq 'plugin') || ($part eq 'type'))
416 $ident->{$part} = [map { _sanitize_generic_no_minus ($_) } (@temp)];
420 $ident->{$part} = [map { _sanitize_generic_allow_minus ($_) } (@temp)];
424 return (get_files_by_ident ($ident));
425 } # get_selected_files
427 sub get_timespan_selection
430 if (param ('timespan'))
432 my $temp = int (param ('timespan'));
433 if ($temp && ($temp > 0))
440 } # get_timespan_selection
442 sub get_host_selection
446 for (get_all_hosts ())
451 for (param ('hostname'))
453 my $host = _sanitize_generic_allow_minus ($_);
454 if (defined ($ret{$host}))
462 return (grep { $ret{$_} > 0 } (sort (keys %ret)));
468 } # get_host_selection
470 sub get_plugin_selection
473 my @hosts = get_host_selection ();
475 for (get_all_plugins (@hosts))
480 for (param ('plugin'))
482 if (defined ($ret{$_}))
490 return (grep { $ret{$_} > 0 } (sort (keys %ret)));
496 } # get_plugin_selection
501 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])/)
503 return ([hex ($1) / 255.0, hex ($2) / 255.0, hex ($3) / 255.0]);
510 confess ("Wrong number of arguments") if (@_ != 1);
511 return (sprintf ('%02hx%02hx%02hx', map { int (255.0 * $_) } @{$_[0]}));
519 my $ret = [undef, undef, undef];
521 $opts{'background'} ||= [1.0, 1.0, 1.0];
522 $opts{'alpha'} ||= 0.25;
526 $fg = _string_to_color ($fg)
527 or confess ("Cannot parse foreground color $fg");
530 if (!ref ($opts{'background'}))
532 $opts{'background'} = _string_to_color ($opts{'background'})
533 or confess ("Cannot parse background color " . $opts{'background'});
535 $bg = $opts{'background'};
537 for (my $i = 0; $i < 3; $i++)
539 $ret->[$i] = ($opts{'alpha'} * $fg->[$i])
540 + ((1.0 - $opts{'alpha'}) * $bg->[$i]);
543 return (_color_to_string ($ret));
546 sub sort_idents_by_type_instance
549 my $array_sort = shift;
551 my %elements = map { $_->{'type_instance'} => $_ } (@$idents);
552 splice (@$idents, 0);
556 next if (!exists ($elements{$_}));
557 push (@$idents, $elements{$_});
558 delete ($elements{$_});
560 push (@$idents, map { $elements{$_} } (sort (keys %elements)));
561 } # sort_idents_by_type_instance
563 sub type_to_module_name
568 $ret = ucfirst (lc ($type));
570 $ret =~ s/[^A-Za-z_]//g;
571 $ret =~ s/_([A-Za-z])/\U$1\E/g;
573 return ("Collectd::Graph::Type::$ret");
574 } # type_to_module_name
578 my @days = (qw(Sun Mon Tue Wed Thu Fri Sat));
579 my @months = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
581 my $epoch = @_ ? shift : time ();
582 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
583 my $string = sprintf ('%s, %02d %s %4d %02d:%02d:%02d GMT', $days[$wday], $mday,
584 $months[$mon], 1900 + $year, $hour ,$min, $sec);
590 my $all_files = shift;
599 my $files_to_flush = [];
602 if (!defined $opts{'begin'})
604 cluck ("begin is not defined");
607 $begin = $opts{'begin'};
609 if (!defined $opts{'end'})
611 cluck ("end is not defined");
621 $interval = $opts{'interval'} || 10;
623 if (ref ($all_files) eq 'HASH')
625 my @tmp = ($all_files);
630 # Don't flush anything if the timespan is in the future.
631 if (($end > $now) && ($begin > $now))
639 my $file_name = ident_to_filename ($file_orig);
644 @statbuf = stat ($file_name);
649 $mtime = $statbuf[9];
651 # Skip if file is fresh
652 if (($now - $mtime) <= $interval)
656 # or $end is before $mtime
657 elsif (($end != 0) && (($end - $mtime) <= 0))
662 $file_copy->{'host'} = $file_orig->{'hostname'};
663 $file_copy->{'plugin'} = $file_orig->{'plugin'};
664 if (exists $file_orig->{'plugin_instance'})
666 $file_copy->{'plugin_instance'} = $file_orig->{'plugin_instance'}
668 $file_copy->{'type'} = $file_orig->{'type'};
669 if (exists $file_orig->{'type_instance'})
671 $file_copy->{'type_instance'} = $file_orig->{'type_instance'}
674 push (@$files_to_flush, $file_copy);
675 } # for (@$all_files)
677 if (!@$files_to_flush)
682 $sock = Collectd::Unixsock->new ($opts{'addr'});
688 $status = $sock->flush (plugins => ['rrdtool'], identifier => $files_to_flush);
691 cluck ("FLUSH failed: " . $sock->{'error'});
700 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :