1
package Munin::Node::SpoolWriter;
3
# $Id: SpoolWriter.pm 4288 2011-07-19 12:02:48Z steve.schnepp $
11
use Munin::Common::Defaults;
12
use Munin::Node::Logger;
15
use constant TIME => 86_400; # put 1 day of results into a spool file
16
use constant MAXIMUM_AGE => TIME * 7; # remove spool files more than a week old
18
sub _snap_to_epoch_boundary { return $_[0] - ($_[0] % TIME) }
23
my ($class, %args) = @_;
25
$args{spooldir} or croak "no spooldir provided";
27
opendir $args{spooldirhandle}, $args{spooldir}
28
or croak "Could not open spooldir '$args{spooldir}': $!";
30
# TODO: paranoia check? except dir doesn't (currently) have to be
35
return bless \%args, $class;
39
# writes the results of a plugin run to the appropriate spool-file
40
# need to remove any lines containing only a '.'
43
my ($self, $timestamp, $service, $data) = @_;
45
my $fmtTimestamp = _snap_to_epoch_boundary($timestamp);
47
open my $fh , '>>', "$self->{spooldir}/munin-daemon.$service.$fmtTimestamp"
48
or die "Unable to open spool file: $!";
50
print {$fh} "timestamp $timestamp\n";
51
print {$fh} "multigraph $service\n" unless $data->[0] =~ m{^multigraph};
53
foreach my $line (@$data) {
54
# Ignore blank lines and "."-ones.
55
next if (!defined($line) || $line eq '' || $line eq '.');
57
print {$fh} $line, "\n" or logger("Error writing results: $!");
64
# removes files from the spooldir older than MAXIMUM_AGE
69
opendir my $dir, $self->{spooldir} or die $!;
71
foreach my $file (readdir $dir) {
73
next unless ($timestamp) = ($file =~ m{munin-daemon\.\w+\.(\d+)$})
74
and (time - $timestamp) > MAXIMUM_AGE;
76
my $filename = "$self->{spooldir}/$file";
78
unlink $filename or die "Unable to unlink '$filename': $!\n";
91
Munin::Node::SpoolWriter - Writing side of the spool functionality
95
my $spool = Munin::Node::SpoolWriter->new(spooldir => $spooldir);
96
$spool->write(1234567890, 'cpu', \@results);
104
Constructor. 'spooldir' key should be the directory
105
L<Munin::Node::SpoolReader> is reading from.
107
=item B<write($timestamp, $service, \@results)>
109
Takes a timestamp, service name, and the results of running config and fetch on
110
it. Writes it to the spool directory for L<Munin::Node::SpoolReader> to read.
112
=item B<cleanup($timestamp)>
114
Removes any items in the spool directory older than $timestamp.
120
# vim: sw=4 : ts=4 : et