From 7bab74885aba3a419503e9776699d239db6600c9 Mon Sep 17 00:00:00 2001 From: oetiker Date: Wed, 3 Dec 2008 08:47:56 +0000 Subject: [PATCH] add rrdcached examples -- kevin git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk/program@1685 a5681a0c-68f1-0310-ab6d-d61299d08faa --- examples/rrdcached/RRDCached.pm | 90 +++++++++++++++++++++ examples/rrdcached/rrdcached-size.pl | 153 +++++++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+) create mode 100644 examples/rrdcached/RRDCached.pm create mode 100644 examples/rrdcached/rrdcached-size.pl diff --git a/examples/rrdcached/RRDCached.pm b/examples/rrdcached/RRDCached.pm new file mode 100644 index 0000000..d776e32 --- /dev/null +++ b/examples/rrdcached/RRDCached.pm @@ -0,0 +1,90 @@ + +use strict; +use warnings; + +package RRDCached; + +=head1 RRDCached + +This module implements the B client protocol for bulk updates. + +=head1 SYNOPSIS + + my $cache = RRDCached->new('unix:/var/run/rrdcached.sock') + or die "Cannot connect to RRDCached"; + + $cache->update('file1.rrd', 'N:10:2:78'); + $cache->update('file2.rrd', '1222973760:30:0:9', 'N:68:1:55'); + ... + + $cache->done(); + +=cut + +use IO::Socket; + +################################################################# + +sub new { + my ($class, $daemon) = @_; + my $this = {}; + + $daemon ||= $ENV{RRDCACHED_ADDRESS}; + defined $daemon or return undef; + + my $sock_family = "INET"; + + if ($daemon =~ m{^unix: | ^/ }x) + { + $sock_family = "UNIX"; + $daemon =~ s/^unix://; + } + + my $sock = "IO::Socket::$sock_family"->new($daemon) + or die "Cannot connect to daemon"; + + $sock->printflush("BATCH\n"); + + my $go = $sock->getline; + warn "We didn't get go-ahead from rrdcached" unless $go =~ /^0/; + + $sock->autoflush(0); + + bless { sock => $sock, + daemon => $daemon, + }, $class; +} + +sub update { + my $this = shift; + my $file = shift; + ## @updates = @_; + + @_ or warn "No updates for $file!"; + + ## rrdcached doesn't handle N: timestamps + my $now = time(); + s/^N(?=:)/$now/ for (@_); + + $this->{sock}->print("update $file @_\n"); +} + +sub done { + my ($this) = @_; + + my $sock = delete $this->{sock}; + + $sock->printflush(".\n"); + my $errs = $sock->getline; + + my ($num_err) = $errs =~ /^(\d+)/; + return unless $num_err; + + $sock->getline for (1..$num_err); + + $sock->close; +} + +################################################################# + +1; diff --git a/examples/rrdcached/rrdcached-size.pl b/examples/rrdcached/rrdcached-size.pl new file mode 100644 index 0000000..df6aaf6 --- /dev/null +++ b/examples/rrdcached/rrdcached-size.pl @@ -0,0 +1,153 @@ +#!/usr/bin/perl + +=head1 NAME + +rrdcached-size.pl - estimate the IO and memory requirements for rrdcached + +=head1 SYNOPSIS + +B +[B<-rrds>EI] +[B<-step>EI] +[B<-update>EI] +[B<-file>EI] +[B<-io>EI] +[B<-w>EI] +[B<-f>EI] +[B<-pagesize>EI] + +=head1 OPTIONS + +=over 4 + +=item B<-rrds> I + +Specify the number of RRDs in the working set. + +=item B<-step> I + +Specify the RRD step value for each file. + +=item B<-update> I + +Average update string length. For this calculation, the time value must +be specified as a C, not C. For example, this update string +would lead to B<-update>EI<43> : + + 1226936851:0:0:101113914:0:0:0:25814373:0:0 + +=item B<-file> I + +Specify the average file name length. For this calculation, use the full +path of the file. + +=item B<-io> I + +Specify the number of RRD files that your system can write per second. + +=item B<-w> I + +Specifies the B<-w> timer used with rrdcached. For more information, see +the B documentation. + +=item B<-f> I + +Specifies the B<-f> timer used with rrdcached. For more information, see +the B documentation. + +=item B<-pagesize> I + +Manually specify the system page size, in case it is not detected +properly. + +=back + +=cut + +use strict; +use warnings; + +my $filename_len = 60; +my $update_len = 128; +my $rrds = 100; +my $step = 300; +my $rrd_per_sec = 200; +my $rrdc_write = 300; +my $rrdc_flush = 3600; +my $pagesize = `pagesize` || 4096; + +################################################################# + +use Getopt::Long; +GetOptions('rrds=i' => \$rrds, + 'step=i' => \$step, + 'update=i' => \$update_len, + 'file=i' => \$filename_len, + 'io=i' => \$rrd_per_sec, + 'w=i' => \$rrdc_write, + 'f=i' => \$rrdc_flush, + 'pagesize=i' => \$pagesize, + 'h' => \&usage, + ) + or die "Options failure"; + +@ARGV and die "Extra args: @ARGV\n"; + +################################################################# + +my $MEG = 1024*1024; + +my $write_time = int($rrds / $rrd_per_sec); +my $write_busy = int(100 * $write_time / $rrdc_write); +my $buffered_pdp = $rrdc_write / $step; + +my $max_ram + = $rrds + * ($filename_len + + ( $rrdc_write / $step ) * $update_len) + / $MEG; + +my $journal_size + = $rrds + * (length("update") + $filename_len + $update_len + 3) + * ($rrdc_flush/$step) + * 2 # 2 logs + / $MEG; + +my $journal_rate = (($journal_size*$MEG/2))/$rrdc_flush; +my $journal_page_rate = $journal_rate / $pagesize; + +$_ = sprintf("%.1f", $_) + for ($write_time, + $write_busy, + $buffered_pdp, + $max_ram, + $journal_size, + $journal_rate, + $journal_page_rate, + ); + +print <<"EOF"; +RRD files : $rrds files +RRD step : $step seconds +Update length : $update_len bytes +IO writes/sec : $rrd_per_sec rrd/sec +write timer : $rrdc_write seconds +flush timer : $rrdc_flush seconds +----------------------------------------------------------------- + +Time to write all RRDs: $write_time sec ($write_busy\% busy) + +$buffered_pdp PDPs will be buffered per file + +RAM usage: $max_ram MB + +Journal size: $journal_size MB (total size for two journals) + +Journal write rate: $journal_page_rate page/sec ($journal_rate byte/sec) +EOF + +sub usage { + system("perldoc $0"); + exit(1); +} -- 2.11.0