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 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]}));
521 my ($r, $g, $b) = (rand (), rand ());
527 $min = 1.0 - ($r + $g);
531 $max = 2.0 - ($r + $g);
534 $b = $min + (rand () * ($max - $min));
536 return (_color_to_string ([$r, $g, $b]));
544 my $ret = [undef, undef, undef];
546 $opts{'background'} ||= [1.0, 1.0, 1.0];
547 $opts{'alpha'} ||= 0.25;
551 $fg = _string_to_color ($fg)
552 or confess ("Cannot parse foreground color $fg");
555 if (!ref ($opts{'background'}))
557 $opts{'background'} = _string_to_color ($opts{'background'})
558 or confess ("Cannot parse background color " . $opts{'background'});
560 $bg = $opts{'background'};
562 for (my $i = 0; $i < 3; $i++)
564 $ret->[$i] = ($opts{'alpha'} * $fg->[$i])
565 + ((1.0 - $opts{'alpha'}) * $bg->[$i]);
568 return (_color_to_string ($ret));
571 sub sort_idents_by_type_instance
574 my $array_sort = shift;
576 my %elements = map { $_->{'type_instance'} => $_ } (@$idents);
577 splice (@$idents, 0);
581 next if (!exists ($elements{$_}));
582 push (@$idents, $elements{$_});
583 delete ($elements{$_});
585 push (@$idents, map { $elements{$_} } (sort (keys %elements)));
586 } # sort_idents_by_type_instance
588 sub type_to_module_name
593 $ret = ucfirst (lc ($type));
595 $ret =~ s/[^A-Za-z_]//g;
596 $ret =~ s/_([A-Za-z])/\U$1\E/g;
598 return ("Collectd::Graph::Type::$ret");
599 } # type_to_module_name
603 my @days = (qw(Sun Mon Tue Wed Thu Fri Sat));
604 my @months = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
606 my $epoch = @_ ? shift : time ();
607 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
608 my $string = sprintf ('%s, %02d %s %4d %02d:%02d:%02d GMT', $days[$wday], $mday,
609 $months[$mon], 1900 + $year, $hour ,$min, $sec);
615 my $all_files = shift;
624 my $files_to_flush = [];
627 if (!defined $opts{'begin'})
629 cluck ("begin is not defined");
632 $begin = $opts{'begin'};
634 if (!defined $opts{'end'})
636 cluck ("end is not defined");
646 $interval = $opts{'interval'} || 10;
648 if (ref ($all_files) eq 'HASH')
650 my @tmp = ($all_files);
655 # Don't flush anything if the timespan is in the future.
656 if (($end > $now) && ($begin > $now))
664 my $file_name = ident_to_filename ($file_orig);
669 @statbuf = stat ($file_name);
674 $mtime = $statbuf[9];
676 # Skip if file is fresh
677 if (($now - $mtime) <= $interval)
681 # or $end is before $mtime
682 elsif (($end != 0) && (($end - $mtime) <= 0))
687 $file_copy->{'host'} = $file_orig->{'hostname'};
688 $file_copy->{'plugin'} = $file_orig->{'plugin'};
689 if (exists $file_orig->{'plugin_instance'})
691 $file_copy->{'plugin_instance'} = $file_orig->{'plugin_instance'}
693 $file_copy->{'type'} = $file_orig->{'type'};
694 if (exists $file_orig->{'type_instance'})
696 $file_copy->{'type_instance'} = $file_orig->{'type_instance'}
699 push (@$files_to_flush, $file_copy);
700 } # for (@$all_files)
702 if (!@$files_to_flush)
707 $sock = Collectd::Unixsock->new ($opts{'addr'});
713 $status = $sock->flush (plugins => ['rrdtool'], identifier => $files_to_flush);
716 cluck ("FLUSH failed: " . $sock->{'error'});
725 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :