| 1 | # vim: ts=2 sw=4 et | ||
| 2 | |||
| 3 | # Flush stats to RRDtool | ||
| 4 | # | ||
| 5 | # To enable this backend, include 'rrd' in the backends | ||
| 6 | # configuration array: | ||
| 7 | # | ||
| 8 | # backends: ['./backends/rrd'] | ||
| 9 | # (if the config file is in the statsd folder) | ||
| 10 | # | ||
| 11 | # A sample configuration can be found in exampleRRDConfig.js | ||
| 12 | # | ||
| 13 | # This backend supports the following config options: | ||
| 14 | # | ||
| 15 | # path: root path of the RRD files (default: '/tmp') | ||
| 16 | # | ||
| 17 | |||
| 18 | package Net::Statsd::Server::Backend::Rrdtool; | ||
| 19 | |||
| 20 | use 5.010; | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use base qw(Net::Statsd::Server::Backend); | ||
| 24 | |||
| 25 | our $VERSION = '0.01'; | ||
| 26 | our $CONFIG = { path => '/tmp' }; | ||
| 27 | |||
| 28 | use Data::Dumper; | ||
| 29 | use File::Basename; | ||
| 30 | use File::Path; | ||
| 31 | use File::Spec; | ||
| 32 | # Not on CPAN, usually provided by 'rrdtool' | ||
| 33 | use RRDs; | ||
| 34 | |||
| 35 | my $debug = 0; | ||
| 36 | my $flushInterval; | ||
| 37 | my $rrdPath; | ||
| 38 | my $stats = {}; | ||
| 39 | |||
| 40 | sub _dir_file { | ||
| 41 | my ($key) = @_; | ||
| 42 | my @key = split m{\.}, $key; | ||
| 43 | my $filename = join('/', @key) . '.rrd'; | ||
| 44 | my $fullfile = File::Spec->catfile($CONFIG->{path}, $filename); | ||
| 45 | my $basename = File::Basename::basename($filename); | ||
| 46 | my $folder = File::Basename::dirname($fullfile); | ||
| 47 | #warn "folder=$folder base=$basename full=$fullfile\n"; | ||
| 48 | return ($folder, $basename, $fullfile); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub write_counter { | ||
| 52 | my ($self, @args) = @_; | ||
| 53 | return $self->_write_rrd('COUNTER', @args); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub write_gauge { | ||
| 57 | my ($self, @args) = @_; | ||
| 58 | return $self->_write_rrd('GAUGE', @args); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _ds_name { | ||
| 62 | my ($self, $name) = @_; | ||
| 63 | if ($name) { | ||
| 64 | $name =~ s{\.rrd$}{}; | ||
| 65 | } | ||
| 66 | my $rrd_name = substr($name, 0, 19); | ||
| 67 | return $rrd_name; | ||
| 68 | } | ||
| 69 | |||
| 70 | # With RRDs, so we can set our statsd-biased defaults | ||
| 71 | sub _rrd_create { | ||
| 72 | my ($self, $full_filename, $ds_name, $type) = @_; | ||
| 73 | |||
| 74 | if (! $ds_name || ! $type || ! $full_filename) { | ||
| 75 | die "Incorrect parameters to _rrd_create()"; | ||
| 76 | } | ||
| 77 | |||
| 78 | if (-e $full_filename) { | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | my $flush_interval = $CONFIG->{flushInterval} / 1000; | ||
| 83 | my %statsd_to_rrd_type = ( | ||
| 84 | COUNTER => 'ABSOLUTE', | ||
| 85 | GAUGE => 'GAUGE', | ||
| 86 | TIMER => 'GAUGE', # Sadly, no timer type in RRD | ||
| 87 | ); | ||
| 88 | |||
| 89 | my $ds_type = $statsd_to_rrd_type{$type}; | ||
| 90 | my $min_value = $ds_type eq 'ABSOLUTE' ? 0 : 'U'; | ||
| 91 | my $max_value = 'U'; | ||
| 92 | my $ds_name = $self->_ds_name($ds_name); | ||
| 93 | |||
| 94 | my @rrd_spec = ( | ||
| 95 | '-b' => "-${flush_interval}s", | ||
| 96 | '-s' => $flush_interval, | ||
| 97 | "DS:${ds_name}:${ds_type}:${flush_interval}:${min_value}:${max_value}", | ||
| 98 | ); | ||
| 99 | my $xff = $CONFIG->{xff} || 0.5; | ||
| 100 | |||
| 101 | my @rra = ( | ||
| 102 | [ 'AVERAGE', $xff, 10, 1 ], # 10=seconds, 1=days | ||
| 103 | [ 'AVERAGE', $xff, 60, 30 ], | ||
| 104 | [ 'AVERAGE', $xff, 300, 2 * 365 ], | ||
| 105 | ); | ||
| 106 | |||
| 107 | sub steps_from_s { | ||
| 108 | my $s = shift; | ||
| 109 | return int($s / ($CONFIG->{flushInterval}/1000)); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub rows_from_days { | ||
| 113 | my ($days, $sec_per_step) = @_; | ||
| 114 | return 86400 * $days / $sec_per_step; | ||
| 115 | } | ||
| 116 | |||
| 117 | for (@rra) { | ||
| 118 | my ($cf, $xff, $seconds_per_step, $days) = @{ $_ }; | ||
| 119 | my $steps = steps_from_s($seconds_per_step); | ||
| 120 | my $rows = rows_from_days($days, $seconds_per_step); | ||
| 121 | push @rrd_spec, "RRA:${cf}:${xff}:${steps}:${rows}"; | ||
| 122 | } | ||
| 123 | |||
| 124 | #warn Data::Dumper::Dumper(\@rrd_spec); | ||
| 125 | RRDs::create($full_filename, @rrd_spec); | ||
| 126 | } | ||
| 127 | |||
| 128 | sub _write_rrd { | ||
| 129 | my ($self, $type, $key, $value, $timestamp) = @_; | ||
| 130 | |||
| 131 | # Statsd server treats timestamp as millisecond values | ||
| 132 | $timestamp /= 1000; | ||
| 133 | |||
| 134 | warn " write_rrd: type=$type key=$key value=$value timestamp=$timestamp\n" | ||
| 135 | if $debug; | ||
| 136 | |||
| 137 | my ($folder, $basename, $full_filename) = _dir_file($key); | ||
| 138 | |||
| 139 | if (! -d $folder) { | ||
| 140 | File::Path::mkpath($folder) or | ||
| 141 | die "Can't create folder '$folder': $!"; | ||
| 142 | } | ||
| 143 | |||
| 144 | my $stat_name = $basename; | ||
| 145 | $stat_name = $self->_ds_name($stat_name); | ||
| 146 | |||
| 147 | $self->_rrd_create($full_filename, $stat_name, $type); | ||
| 148 | |||
| 149 | $timestamp ||= time(); | ||
| 150 | warn "update_rrd: $stat_name = $value (t=$timestamp, $full_filename)\n" | ||
| 151 | if $debug; | ||
| 152 | |||
| 153 | # It is entirely possible that an RRD update fails. | ||
| 154 | # A common reason is that metric names longer than 20 chars | ||
| 155 | # are not accepted by RRD. | ||
| 156 | my $ok; | ||
| 157 | eval { | ||
| 158 | RRDs::update($full_filename, '--template', $stat_name, "${timestamp}:${value}"); | ||
| 159 | if (my $err = RRDs::error()) { | ||
| 160 | die $err; | ||
| 161 | } | ||
| 162 | else { | ||
| 163 | $ok = 1; | ||
| 164 | } | ||
| 165 | } or do { | ||
| 166 | warn "Failed to update $stat_name RRD: $@" | ||
| 167 | if $debug; | ||
| 168 | }; | ||
| 169 | |||
| 170 | return $ok; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub convert_metrics_to_rrd { | ||
| 174 | my ($self, $timestamp, $counters, $timers, $gauges) = @_; | ||
| 175 | |||
| 176 | #say Dumper($counters); | ||
| 177 | #say Dumper($timers); | ||
| 178 | #say Dumper($gauges); | ||
| 179 | |||
| 180 | if ($counters) { | ||
| 181 | for (@{ $counters }) { | ||
| 182 | my ($key, $value) = @{ $_ }; | ||
| 183 | $self->write_counter($key => $value, $timestamp); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | # FIXME Timer_data? | ||
| 188 | #if ($timers) { | ||
| 189 | # for (@{ $timers }) { | ||
| 190 | # my ($key, $value) = @{ $_ }; | ||
| 191 | # $self->write_gauge($key => $value, $timestamp); | ||
| 192 | # } | ||
| 193 | #} | ||
| 194 | |||
| 195 | if ($gauges) { | ||
| 196 | for (@{ $gauges }) { | ||
| 197 | my ($key, $value) = @{ $_ }; | ||
| 198 | $self->write_gauge($key => $value, $timestamp); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | return; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub flush { | ||
| 206 | my ($self, $timestamp, $metrics) = @_; | ||
| 207 | |||
| 208 | my $num_stats = 0; | ||
| 209 | my @counts; | ||
| 210 | my @timers; | ||
| 211 | my @gauges; | ||
| 212 | |||
| 213 | $timestamp *= 1000; | ||
| 214 | |||
| 215 | for my $key (keys %{ $metrics->{counters} }) { | ||
| 216 | my $value = $metrics->{counters}->{$key}; | ||
| 217 | push @counts, [ $key, $value ]; | ||
| 218 | $num_stats++; | ||
| 219 | } | ||
| 220 | |||
| 221 | # timer_data ? | ||
| 222 | for my $key (keys %{ $metrics->{timers} }) { | ||
| 223 | my $series = $metrics->{timers}->{$key}; | ||
| 224 | push @timers, [ $key => $series ]; | ||
| 225 | $num_stats++; | ||
| 226 | } | ||
| 227 | |||
| 228 | for my $key (keys %{ $metrics->{gauges} }) { | ||
| 229 | my $value = $metrics->{gauges}->{$key}; | ||
| 230 | push @gauges, [ $key, $value ]; | ||
| 231 | $num_stats++; | ||
| 232 | } | ||
| 233 | |||
| 234 | if ($num_stats > 0) { | ||
| 235 | $self->convert_metrics_to_rrd($timestamp, \@counts, \@timers, \@gauges); | ||
| 236 | if ($debug) { | ||
| 237 | warn "flushed ${num_stats} stats to RRD\n"; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | } | ||
| 242 | |||
| 243 | sub init { | ||
| 244 | my ($self, $startup_time, $config, $events) = @_; | ||
| 245 | |||
| 246 | $debug = $config->{debug}; | ||
| 247 | my $rrd_config = $config->{rrdtool} || {}; | ||
| 248 | |||
| 249 | my @expected_config_attributes = ('path', 'xff'); | ||
| 250 | for (@expected_config_attributes) { | ||
| 251 | if (exists $rrd_config->{$_} && defined $rrd_config->{$_}) { | ||
| 252 | $CONFIG->{$_} = $rrd_config->{$_}; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | |||
| 256 | # Mirror our flushInterval value, so we can create sensible RRDs | ||
| 257 | $CONFIG->{flushInterval} = $config->{flushInterval}; | ||
| 258 | |||
| 259 | $stats->{last_flush} = $startup_time; | ||
| 260 | $stats->{last_exception} = $startup_time; | ||
| 261 | |||
| 262 | return 1; | ||
| 263 | } | ||
| 264 | |||
| 265 | 1; |