~ubuntu-branches/ubuntu/saucy/rrdtool/saucy-proposed

« back to all changes in this revision

Viewing changes to examples/rrdcached/RRDCached.pm

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2010-07-22 08:07:01 UTC
  • mfrom: (1.2.8 upstream) (3.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100722080701-k46mgdfz6euxwqsm
Tags: 1.4.3-1ubuntu1
* Merge from debian unstable, Remaining changes:
  - debian/control: Don't build against ruby1.9 as we don't want
    it in main.
* require libdbi >= 0.8.3 to prevent aborts when using dbi datasources

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
use strict;
 
3
use warnings;
 
4
 
 
5
package RRDCached;
 
6
 
 
7
=head1 RRDCached
 
8
 
 
9
This module implements the B<RRDCached> client protocol for bulk updates.
 
10
 
 
11
=head1 SYNOPSIS
 
12
 
 
13
    my $cache = RRDCached->new('unix:/var/run/rrdcached.sock')
 
14
        or die "Cannot connect to RRDCached";
 
15
 
 
16
    $cache->update('file1.rrd', 'N:10:2:78');
 
17
    $cache->update('file2.rrd', '1222973760:30:0:9', 'N:68:1:55');
 
18
    ...
 
19
 
 
20
    $cache->done();
 
21
 
 
22
=cut
 
23
 
 
24
use IO::Socket;
 
25
 
 
26
#################################################################
 
27
 
 
28
sub new {
 
29
    my ($class, $daemon) = @_;
 
30
    my $this = {};
 
31
 
 
32
    $daemon ||= $ENV{RRDCACHED_ADDRESS};
 
33
    defined $daemon or return undef;
 
34
 
 
35
    my $sock_family = "INET";
 
36
 
 
37
    if ($daemon =~ m{^unix: | ^/ }x)
 
38
    {
 
39
        $sock_family = "UNIX";
 
40
        $daemon =~ s/^unix://;
 
41
    }
 
42
 
 
43
    my $sock = "IO::Socket::$sock_family"->new($daemon)
 
44
        or die "Cannot connect to daemon";
 
45
 
 
46
    $sock->printflush("BATCH\n");
 
47
 
 
48
    my $go = $sock->getline;
 
49
    warn "We didn't get go-ahead from rrdcached" unless $go =~ /^0/;
 
50
 
 
51
    $sock->autoflush(0);
 
52
 
 
53
    bless { sock => $sock,
 
54
            daemon => $daemon,
 
55
        }, $class;
 
56
}
 
57
 
 
58
sub update {
 
59
    my $this = shift;
 
60
    my $file = shift;
 
61
    ## @updates = @_;
 
62
 
 
63
    @_ or warn "No updates for $file!";
 
64
 
 
65
    ## rrdcached doesn't handle N: timestamps
 
66
    my $now = time();
 
67
    s/^N(?=:)/$now/ for (@_);
 
68
 
 
69
    $this->{sock}->print("update $file @_\n");
 
70
}
 
71
 
 
72
sub done {
 
73
    my ($this) = @_;
 
74
 
 
75
    my $sock = delete $this->{sock};
 
76
 
 
77
    $sock->printflush(".\n");
 
78
    my $errs = $sock->getline;
 
79
 
 
80
    my ($num_err) = $errs =~ /^(\d+)/;
 
81
    return unless $num_err;
 
82
 
 
83
    $sock->getline for (1..$num_err);
 
84
 
 
85
    $sock->close;
 
86
}
 
87
 
 
88
#################################################################
 
89
 
 
90
1;