~ubuntu-branches/ubuntu/saucy/munin/saucy

« back to all changes in this revision

Viewing changes to node/lib/Munin/Node/SpoolWriter.pm

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 12:54:28 UTC
  • mfrom: (8.1.30 sid)
  • Revision ID: package-import@ubuntu.com-20120611125428-k8z25s77rp755vxe
Tags: 2.0.0-1ubuntu1
* Resync with Debian unstable.
* d/munin-node.upstart,munin.upstart: Add upstart configurations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Munin::Node::SpoolWriter;
 
2
 
 
3
# $Id: SpoolWriter.pm 4288 2011-07-19 12:02:48Z steve.schnepp $
 
4
 
 
5
use strict;
 
6
use warnings;
 
7
 
 
8
use Carp;
 
9
use IO::File;
 
10
 
 
11
use Munin::Common::Defaults;
 
12
use Munin::Node::Logger;
 
13
 
 
14
 
 
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
 
17
 
 
18
sub _snap_to_epoch_boundary { return $_[0] - ($_[0] % TIME) }
 
19
 
 
20
 
 
21
sub new
 
22
{
 
23
    my ($class, %args) = @_;
 
24
 
 
25
    $args{spooldir} or croak "no spooldir provided";
 
26
 
 
27
    opendir $args{spooldirhandle}, $args{spooldir}
 
28
        or croak "Could not open spooldir '$args{spooldir}': $!";
 
29
 
 
30
    # TODO: paranoia check?  except dir doesn't (currently) have to be
 
31
    # root-owned.
 
32
 
 
33
    # TODO: set umask
 
34
 
 
35
    return bless \%args, $class;
 
36
}
 
37
 
 
38
 
 
39
# writes the results of a plugin run to the appropriate spool-file
 
40
# need to remove any lines containing only a '.'
 
41
sub write
 
42
{
 
43
    my ($self, $timestamp, $service, $data) = @_;
 
44
 
 
45
    my $fmtTimestamp = _snap_to_epoch_boundary($timestamp);
 
46
 
 
47
    open my $fh , '>>', "$self->{spooldir}/munin-daemon.$service.$fmtTimestamp"
 
48
        or die "Unable to open spool file: $!";
 
49
 
 
50
    print {$fh} "timestamp $timestamp\n";
 
51
    print {$fh} "multigraph $service\n" unless $data->[0] =~ m{^multigraph};
 
52
 
 
53
    foreach my $line (@$data) {
 
54
        # Ignore blank lines and "."-ones.
 
55
        next if (!defined($line) || $line eq '' || $line eq '.');
 
56
 
 
57
        print {$fh} $line, "\n" or logger("Error writing results: $!");
 
58
    }
 
59
 
 
60
    return;
 
61
}
 
62
 
 
63
 
 
64
# removes files from the spooldir older than MAXIMUM_AGE
 
65
sub cleanup
 
66
{
 
67
    my ($self) = @_;
 
68
 
 
69
    opendir my $dir, $self->{spooldir} or die $!;
 
70
 
 
71
    foreach my $file (readdir $dir) {
 
72
        my $timestamp;
 
73
        next unless ($timestamp) = ($file =~ m{munin-daemon\.\w+\.(\d+)$})
 
74
                and (time - $timestamp) > MAXIMUM_AGE;
 
75
 
 
76
        my $filename = "$self->{spooldir}/$file";
 
77
 
 
78
        unlink $filename or die "Unable to unlink '$filename': $!\n";
 
79
    }
 
80
 
 
81
    return;
 
82
}
 
83
 
 
84
 
 
85
1;
 
86
 
 
87
__END__
 
88
 
 
89
=head1 NAME
 
90
 
 
91
Munin::Node::SpoolWriter - Writing side of the spool functionality
 
92
 
 
93
=head1 SYNOPSIS
 
94
 
 
95
  my $spool = Munin::Node::SpoolWriter->new(spooldir => $spooldir);
 
96
  $spool->write(1234567890, 'cpu', \@results);
 
97
 
 
98
=head1 METHODS
 
99
 
 
100
=over 4
 
101
 
 
102
=item B<new(%args)>
 
103
 
 
104
Constructor.  'spooldir' key should be the directory
 
105
L<Munin::Node::SpoolReader> is reading from.
 
106
 
 
107
=item B<write($timestamp, $service, \@results)>
 
108
 
 
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.
 
111
 
 
112
=item B<cleanup($timestamp)>
 
113
 
 
114
Removes any items in the spool directory older than $timestamp.
 
115
 
 
116
=back
 
117
 
 
118
=cut
 
119
 
 
120
# vim: sw=4 : ts=4 : et