1 package Collectd::Graph::Common;
6 use vars (qw($ColorCanvas $ColorFullBlue $ColorHalfBlue));
8 use Collectd::Unixsock ();
9 use Carp (qw(confess cluck));
12 use Collectd::Graph::Config (qw(gc_get_scalar));
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 $DefaultDataDir = '/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
149 my $data_dir = gc_get_scalar ('DataDir', $DefaultDataDir);
153 if (defined ($ident->{'_prefix'}))
155 $ret .= $ident->{'_prefix'};
159 $ret .= "$data_dir/";
162 if (!$ident->{'hostname'})
164 cluck ("hostname is undefined")
166 if (!$ident->{'plugin'})
168 cluck ("plugin is undefined")
170 if (!$ident->{'type'})
172 cluck ("type is undefined")
175 $ret .= $ident->{'hostname'} . '/' . $ident->{'plugin'};
176 if (defined ($ident->{'plugin_instance'}))
178 $ret .= '-' . $ident->{'plugin_instance'};
181 $ret .= '/' . $ident->{'type'};
182 if (defined ($ident->{'type_instance'}))
184 $ret .= '-' . $ident->{'type_instance'};
189 } # ident_to_filename
197 $ret .= $ident->{'hostname'} . '/' . $ident->{'plugin'};
198 if (defined ($ident->{'plugin_instance'}))
200 $ret .= '-' . $ident->{'plugin_instance'};
203 $ret .= '/' . $ident->{'type'};
204 if (defined ($ident->{'type_instance'}))
206 $ret .= '-' . $ident->{'type_instance'};
212 sub get_files_from_directory
215 my $recursive = @_ ? shift : 0;
217 my @directories = ();
220 opendir ($dh, $dir) or die ("opendir ($dir): $!");
221 while (my $entry = readdir ($dh))
223 next if ($entry =~ m/^\./);
225 $entry = "$dir/$entry";
229 push (@directories, $entry);
233 my $ident = filename_to_ident ($entry);
236 push (@$ret, $ident);
246 my $temp = get_files_from_directory ($_, $recursive - 1);
249 push (@$ret, @$temp);
255 } # get_files_from_directory
261 my $data_dir = gc_get_scalar ('DataDir', $DefaultDataDir);
263 opendir ($dh, "$data_dir") or confess ("opendir ($data_dir): $!");
264 while (my $entry = readdir ($dh))
266 next if ($entry =~ m/^\./);
267 next if (!-d "$data_dir/$entry");
268 push (@ret, sanitize_hostname ($entry));
291 my $data_dir = gc_get_scalar ('DataDir', $DefaultDataDir);
295 @hosts = get_all_hosts ();
301 opendir ($dh, "$data_dir/$host") or next;
302 while (my $entry = readdir ($dh))
305 my $plugin_instance = '';
307 next if ($entry =~ m/^\./);
308 next if (!-d "$data_dir/$host/$entry");
310 if ($entry =~ m#^([^-]+)-(.+)$#)
313 $plugin_instance = $2;
315 elsif ($entry =~ m#^([^-]+)$#)
318 $plugin_instance = '';
325 $ret->{$plugin} ||= {};
326 $ret->{$plugin}{$plugin_instance} = 1;
333 return (sort (keys %$ret));
341 sub get_files_for_host
343 my $host = sanitize_hostname (shift);
344 my $data_dir = gc_get_scalar ('DataDir', $DefaultDataDir);
345 return (get_files_from_directory ("$data_dir/$host", 2));
346 } # get_files_for_host
353 for (qw(hostname plugin plugin_instance type type_instance))
358 if (!defined ($filter->{$part}))
362 if (!defined ($ident->{$part}))
367 if (ref $filter->{$part})
369 if (!grep { $ident->{$part} eq $_ } (@{$filter->{$part}}))
376 if ($ident->{$part} ne $filter->{$part})
386 sub get_files_by_ident
391 my $data_dir = gc_get_scalar ('DataDir', $DefaultDataDir);
393 #if ($ident->{'hostname'})
395 #$all_files = get_files_for_host ($ident->{'hostname'});
399 $all_files = get_files_from_directory ($data_dir, 3);
402 @ret = grep { _filter_ident ($ident, $_) == 0 } (@$all_files);
405 } # get_files_by_ident
407 sub get_selected_files
411 for (qw(hostname plugin plugin_instance type type_instance))
414 my @temp = param ($part);
419 elsif (($part eq 'plugin') || ($part eq 'type'))
421 $ident->{$part} = [map { _sanitize_generic_no_minus ($_) } (@temp)];
425 $ident->{$part} = [map { _sanitize_generic_allow_minus ($_) } (@temp)];
429 return (get_files_by_ident ($ident));
430 } # get_selected_files
432 sub get_timespan_selection
435 if (param ('timespan'))
437 my $temp = int (param ('timespan'));
438 if ($temp && ($temp > 0))
445 } # get_timespan_selection
447 sub get_host_selection
451 for (get_all_hosts ())
456 for (param ('hostname'))
458 my $host = _sanitize_generic_allow_minus ($_);
459 if (defined ($ret{$host}))
467 return (grep { $ret{$_} > 0 } (sort (keys %ret)));
473 } # get_host_selection
475 sub get_plugin_selection
478 my @hosts = get_host_selection ();
480 for (get_all_plugins (@hosts))
485 for (param ('plugin'))
487 if (defined ($ret{$_}))
495 return (grep { $ret{$_} > 0 } (sort (keys %ret)));
501 } # get_plugin_selection
506 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])/)
508 return ([hex ($1) / 255.0, hex ($2) / 255.0, hex ($3) / 255.0]);
515 confess ("Wrong number of arguments") if (@_ != 1);
516 return (sprintf ('%02hx%02hx%02hx', map { int (255.0 * $_) } @{$_[0]}));
524 my $ret = [undef, undef, undef];
526 $opts{'background'} ||= [1.0, 1.0, 1.0];
527 $opts{'alpha'} ||= 0.25;
531 $fg = _string_to_color ($fg)
532 or confess ("Cannot parse foreground color $fg");
535 if (!ref ($opts{'background'}))
537 $opts{'background'} = _string_to_color ($opts{'background'})
538 or confess ("Cannot parse background color " . $opts{'background'});
540 $bg = $opts{'background'};
542 for (my $i = 0; $i < 3; $i++)
544 $ret->[$i] = ($opts{'alpha'} * $fg->[$i])
545 + ((1.0 - $opts{'alpha'}) * $bg->[$i]);
548 return (_color_to_string ($ret));
551 sub sort_idents_by_type_instance
554 my $array_sort = shift;
556 my %elements = map { $_->{'type_instance'} => $_ } (@$idents);
557 splice (@$idents, 0);
561 next if (!exists ($elements{$_}));
562 push (@$idents, $elements{$_});
563 delete ($elements{$_});
565 push (@$idents, map { $elements{$_} } (sort (keys %elements)));
566 } # sort_idents_by_type_instance
568 sub type_to_module_name
573 $ret = ucfirst (lc ($type));
575 $ret =~ s/[^A-Za-z_]//g;
576 $ret =~ s/_([A-Za-z])/\U$1\E/g;
578 return ("Collectd::Graph::Type::$ret");
579 } # type_to_module_name
583 my @days = (qw(Sun Mon Tue Wed Thu Fri Sat));
584 my @months = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
586 my $epoch = @_ ? shift : time ();
587 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
588 my $string = sprintf ('%s, %02d %s %4d %02d:%02d:%02d GMT', $days[$wday], $mday,
589 $months[$mon], 1900 + $year, $hour ,$min, $sec);
595 my $all_files = shift;
604 my $files_to_flush = [];
607 if (!defined $opts{'begin'})
609 cluck ("begin is not defined");
612 $begin = $opts{'begin'};
614 if (!defined $opts{'end'})
616 cluck ("end is not defined");
626 $interval = $opts{'interval'} || 10;
628 if (ref ($all_files) eq 'HASH')
630 my @tmp = ($all_files);
635 # Don't flush anything if the timespan is in the future.
636 if (($end > $now) && ($begin > $now))
644 my $file_name = ident_to_filename ($file_orig);
649 @statbuf = stat ($file_name);
654 $mtime = $statbuf[9];
656 # Skip if file is fresh
657 if (($now - $mtime) <= $interval)
661 # or $end is before $mtime
662 elsif (($end != 0) && (($end - $mtime) <= 0))
667 $file_copy->{'host'} = $file_orig->{'hostname'};
668 $file_copy->{'plugin'} = $file_orig->{'plugin'};
669 if (exists $file_orig->{'plugin_instance'})
671 $file_copy->{'plugin_instance'} = $file_orig->{'plugin_instance'}
673 $file_copy->{'type'} = $file_orig->{'type'};
674 if (exists $file_orig->{'type_instance'})
676 $file_copy->{'type_instance'} = $file_orig->{'type_instance'}
679 push (@$files_to_flush, $file_copy);
680 } # for (@$all_files)
682 if (!@$files_to_flush)
687 $sock = Collectd::Unixsock->new ($opts{'addr'});
693 $status = $sock->flush (plugins => ['rrdtool'], identifier => $files_to_flush);
696 cluck ("FLUSH failed: " . $sock->{'error'});
705 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :