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
41 sort_idents_by_type_instance
47 our $DefaultDataDir = '/var/lib/collectd/rrd';
51 sub _sanitize_generic_allow_minus
58 # remove all dots and dashes at the beginning and at the end.
65 sub _sanitize_generic_no_minus
67 # Do everything the allow-minus variant does..
68 my $str = _sanitize_generic_allow_minus (@_);
70 # .. and remove the dashes, too
74 } # _sanitize_generic_no_minus
78 return (_sanitize_generic_allow_minus (@_));
83 return (_sanitize_generic_no_minus (@_));
86 sub sanitize_plugin_instance
88 return (_sanitize_generic_allow_minus (@_));
93 return (_sanitize_generic_no_minus (@_));
96 sub sanitize_type_instance
98 return (_sanitize_generic_allow_minus (@_));
101 sub group_files_by_plugin_instance
106 for (my $i = 0; $i < @files; $i++)
108 my $file = $files[$i];
109 my $key = $file->{'plugin_instance'} || '';
111 $data->{$key} ||= [];
112 push (@{$data->{$key}}, $file);
118 sub filename_to_ident
123 if ($file =~ m#([^/]+)/([^/\-]+)(?:-([^/]+))?/([^/\-]+)(?:-([^/]+))?\.rrd$#)
125 $ret = {hostname => $1, plugin => $2, type => $4};
128 $ret->{'plugin_instance'} = $3;
132 $ret->{'type_instance'} = $5;
136 $ret->{'_prefix'} = $`;
145 } # filename_to_ident
147 sub ident_to_filename
150 my $data_dir = gc_get_scalar ('DataDir', $DefaultDataDir);
154 if (defined ($ident->{'_prefix'}))
156 $ret .= $ident->{'_prefix'};
160 $ret .= "$data_dir/";
163 if (!$ident->{'hostname'})
165 cluck ("hostname is undefined")
167 if (!$ident->{'plugin'})
169 cluck ("plugin is undefined")
171 if (!$ident->{'type'})
173 cluck ("type is undefined")
176 $ret .= $ident->{'hostname'} . '/' . $ident->{'plugin'};
177 if (defined ($ident->{'plugin_instance'}))
179 $ret .= '-' . $ident->{'plugin_instance'};
182 $ret .= '/' . $ident->{'type'};
183 if (defined ($ident->{'type_instance'}))
185 $ret .= '-' . $ident->{'type_instance'};
190 } # ident_to_filename
198 $ret .= $ident->{'hostname'} . '/' . $ident->{'plugin'};
199 if (defined ($ident->{'plugin_instance'}))
201 $ret .= '-' . $ident->{'plugin_instance'};
204 $ret .= '/' . $ident->{'type'};
205 if (defined ($ident->{'type_instance'}))
207 $ret .= '-' . $ident->{'type_instance'};
213 sub get_files_from_directory
216 my $recursive = @_ ? shift : 0;
218 my @directories = ();
222 opendir ($dh, $dir) or die ("opendir ($dir): $!");
223 while (my $entry = readdir ($dh))
225 next if ($entry =~ m/^\./);
227 $entry = "$dir/$entry";
231 push (@directories, $entry);
235 push (@files, $entry);
240 push (@$ret, map { filename_to_ident ($_) } sort (@files));
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 next if (!-r "$data_dir/$entry" or !-x "$data_dir/$entry");
269 push (@ret, sanitize_hostname ($entry));
292 my $data_dir = gc_get_scalar ('DataDir', $DefaultDataDir);
296 @hosts = get_all_hosts ();
302 opendir ($dh, "$data_dir/$host") or next;
303 while (my $entry = readdir ($dh))
306 my $plugin_instance = '';
308 next if ($entry =~ m/^\./);
309 next if (!-d "$data_dir/$host/$entry");
311 if ($entry =~ m#^([^-]+)-(.+)$#)
314 $plugin_instance = $2;
316 elsif ($entry =~ m#^([^-]+)$#)
319 $plugin_instance = '';
326 $ret->{$plugin} ||= {};
327 $ret->{$plugin}{$plugin_instance} = 1;
334 return (sort (keys %$ret));
342 sub get_files_for_host
344 my $host = sanitize_hostname (shift);
345 my $data_dir = gc_get_scalar ('DataDir', $DefaultDataDir);
346 return (get_files_from_directory ("$data_dir/$host", 2));
347 } # get_files_for_host
354 for (qw(hostname plugin plugin_instance type type_instance))
359 if (!defined ($filter->{$part}))
363 if (!defined ($ident->{$part}))
368 if (ref $filter->{$part})
370 if (!grep { $ident->{$part} eq $_ } (@{$filter->{$part}}))
377 if ($ident->{$part} ne $filter->{$part})
387 sub get_files_by_ident
392 my $data_dir = gc_get_scalar ('DataDir', $DefaultDataDir);
394 #if ($ident->{'hostname'})
396 #$all_files = get_files_for_host ($ident->{'hostname'});
400 $all_files = get_files_from_directory ($data_dir, 3);
403 @ret = grep { _filter_ident ($ident, $_) == 0 } (@$all_files);
406 } # get_files_by_ident
408 sub get_selected_files
412 for (qw(hostname plugin plugin_instance type type_instance))
415 my @temp = param ($part);
420 elsif (($part eq 'plugin') || ($part eq 'type'))
422 $ident->{$part} = [map { _sanitize_generic_no_minus ($_) } (@temp)];
426 $ident->{$part} = [map { _sanitize_generic_allow_minus ($_) } (@temp)];
430 return (get_files_by_ident ($ident));
431 } # get_selected_files
433 sub get_timespan_selection
436 if (param ('timespan'))
438 my $temp = int (param ('timespan'));
439 if ($temp && ($temp > 0))
446 } # get_timespan_selection
448 sub get_host_selection
452 for (get_all_hosts ())
457 for (param ('hostname'))
459 my $host = _sanitize_generic_allow_minus ($_);
460 if (defined ($ret{$host}))
468 return (grep { $ret{$_} > 0 } (sort (keys %ret)));
474 } # get_host_selection
476 sub get_plugin_selection
479 my @hosts = get_host_selection ();
481 for (get_all_plugins (@hosts))
486 for (param ('plugin'))
488 if (defined ($ret{$_}))
496 return (grep { $ret{$_} > 0 } (sort (keys %ret)));
502 } # get_plugin_selection
507 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])/)
509 return ([hex ($1) / 255.0, hex ($2) / 255.0, hex ($3) / 255.0]);
516 confess ("Wrong number of arguments") if (@_ != 1);
517 return (sprintf ('%02hx%02hx%02hx', map { int (255.0 * $_) } @{$_[0]}));
522 my ($r, $g, $b) = (rand (), rand ());
528 $min = 1.0 - ($r + $g);
532 $max = 2.0 - ($r + $g);
535 $b = $min + (rand () * ($max - $min));
537 return (_color_to_string ([$r, $g, $b]));
545 my $ret = [undef, undef, undef];
547 $opts{'background'} ||= [1.0, 1.0, 1.0];
548 $opts{'alpha'} ||= 0.25;
552 $fg = _string_to_color ($fg)
553 or confess ("Cannot parse foreground color $fg");
556 if (!ref ($opts{'background'}))
558 $opts{'background'} = _string_to_color ($opts{'background'})
559 or confess ("Cannot parse background color " . $opts{'background'});
561 $bg = $opts{'background'};
563 for (my $i = 0; $i < 3; $i++)
565 $ret->[$i] = ($opts{'alpha'} * $fg->[$i])
566 + ((1.0 - $opts{'alpha'}) * $bg->[$i]);
569 return (_color_to_string ($ret));
572 sub sort_idents_by_type_instance
575 my $array_sort = shift;
577 my %elements = map { $_->{'type_instance'} => $_ } (@$idents);
578 splice (@$idents, 0);
582 next if (!exists ($elements{$_}));
583 push (@$idents, $elements{$_});
584 delete ($elements{$_});
586 push (@$idents, map { $elements{$_} } (sort (keys %elements)));
587 } # sort_idents_by_type_instance
589 sub type_to_module_name
594 $ret = ucfirst (lc ($type));
596 $ret =~ s/[^A-Za-z_]//g;
597 $ret =~ s/_([A-Za-z])/\U$1\E/g;
599 return ("Collectd::Graph::Type::$ret");
600 } # type_to_module_name
604 my @days = (qw(Sun Mon Tue Wed Thu Fri Sat));
605 my @months = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
607 my $epoch = @_ ? shift : time ();
608 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
609 my $string = sprintf ('%s, %02d %s %4d %02d:%02d:%02d GMT', $days[$wday], $mday,
610 $months[$mon], 1900 + $year, $hour ,$min, $sec);
616 my $all_files = shift;
625 my $files_to_flush = [];
628 if (!defined $opts{'begin'})
630 cluck ("begin is not defined");
633 $begin = $opts{'begin'};
635 if (!defined $opts{'end'})
637 cluck ("end is not defined");
647 $interval = $opts{'interval'} || 10;
649 if (ref ($all_files) eq 'HASH')
651 my @tmp = ($all_files);
656 # Don't flush anything if the timespan is in the future.
657 if (($end > $now) && ($begin > $now))
665 my $file_name = ident_to_filename ($file_orig);
670 @statbuf = stat ($file_name);
675 $mtime = $statbuf[9];
677 # Skip if file is fresh
678 if (($now - $mtime) <= $interval)
682 # or $end is before $mtime
683 elsif (($end != 0) && (($end - $mtime) <= 0))
688 $file_copy->{'host'} = $file_orig->{'hostname'};
689 $file_copy->{'plugin'} = $file_orig->{'plugin'};
690 if (exists $file_orig->{'plugin_instance'})
692 $file_copy->{'plugin_instance'} = $file_orig->{'plugin_instance'}
694 $file_copy->{'type'} = $file_orig->{'type'};
695 if (exists $file_orig->{'type_instance'})
697 $file_copy->{'type_instance'} = $file_orig->{'type_instance'}
700 push (@$files_to_flush, $file_copy);
701 } # for (@$all_files)
703 if (!@$files_to_flush)
708 $sock = Collectd::Unixsock->new ($opts{'addr'});
714 $status = $sock->flush (plugins => ['rrdtool'], identifier => $files_to_flush);
717 cluck ("FLUSH failed: " . $sock->{'error'});
726 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :