| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # gmetric proxy which consumes metric samples consisting of absolute values and | ||
| 4 | # re-emits time-aggregated rate metrics to gmond instead | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Data::Dumper (); | ||
| 10 | use Ganglia::Gmetric::PP ':all'; | ||
| 11 | use Getopt::Long 'GetOptions'; | ||
| 12 | use Time::HiRes 'time'; | ||
| 13 | |||
| 14 | sub usage { | ||
| 15 | (my $me = $0) =~ s,.*/,,; | ||
| 16 | my $error = shift; | ||
| 17 | print "Error: $error\n\n" if $error; | ||
| 18 | print <<EOU; | ||
| 19 | $me $Ganglia::Gmetric::PP::VERSION | ||
| 20 | |||
| 21 | Usage: $me [OPTIONS]... | ||
| 22 | |||
| 23 | --[no-]pp-client Use Ganglia::Gmetric::PP for talking to gmond. | ||
| 24 | Default off, which requires a working gmetric | ||
| 25 | (see --gmetric below) | ||
| 26 | |||
| 27 | -h, --remote-host=STRING Remote host where gmond is running. Default localhost | ||
| 28 | -p, --remote-port=INT Remote port where gmond is running. Default 8649 | ||
| 29 | |||
| 30 | --gmetric=PATH Path to gmetric(1) binary. -h and -p options are | ||
| 31 | ignored; gmetric usse its own gmond.conf for host | ||
| 32 | settings. | ||
| 33 | |||
| 34 | -H, --listen-host=STRING Local interface to listen on. Default 0.0.0.0 | ||
| 35 | -P, --listen-port=INT Local UDP port to listen on. Default 18649 | ||
| 36 | -u, --unit-suffix=STRING Suffix to append to gmetric units. Default "/s" | ||
| 37 | -m, --metric-suffix=STRING Suffix to append to metric names. Default "_rate" | ||
| 38 | -n, --period=INT Time period in seconds between aggregations. Default 60 | ||
| 39 | |||
| 40 | -N, --multiplier=FLOAT Amount to multiply output values by. Default 1 | ||
| 41 | (e.g., use "-N 3600 -u /hour" to emit per-hour | ||
| 42 | metrics). | ||
| 43 | |||
| 44 | -f, --[no]-floating Always use "double" type instead of original metrics' | ||
| 45 | types. Default on | ||
| 46 | |||
| 47 | -d, --daemon Run in daemon mode. | ||
| 48 | -U, --user User to drop to (only if started as root) | ||
| 49 | -F, --pidfile=FILE File to write PID to in daemon mode. | ||
| 50 | -g, --debug Display debugging output | ||
| 51 | --help Print help and exit | ||
| 52 | EOU | ||
| 53 | exit 1; | ||
| 54 | } | ||
| 55 | |||
| 56 | Getopt::Long::Configure('no_ignore_case'); | ||
| 57 | GetOptions( | ||
| 58 | 'pp-client!' => \(my $use_pp_client = 0), | ||
| 59 | 'h|remote-host=s' => \(my $remote_host = '127.0.0.1'), | ||
| 60 | 'p|remote-port=i' => \(my $remote_port = 8649), | ||
| 61 | 'gmetric=s' => \(my $gmetric), | ||
| 62 | 'H|listen-host=s' => \(my $listen_host = '0.0.0.0'), | ||
| 63 | 'P|listen-port=i' => \(my $listen_port = 18649), | ||
| 64 | 'u|unit-suffix=s' => \(my $units_suffix = '/s'), | ||
| 65 | 'm|metric-suffix=s' => \(my $metric_suffix = '_rate'), | ||
| 66 | 'n|period=i' => \(my $period = 60), | ||
| 67 | 'N|multiplier=i' => \(my $multiplier = 1.0), | ||
| 68 | 'f|floating!' => \(my $output_doubles = 1), | ||
| 69 | 'd|daemon!' => \(my $daemonize), | ||
| 70 | 'U|user=s' => \(my $user), | ||
| 71 | 'F|pidfile=s' => \(my $pidfile), | ||
| 72 | 'g|debug' => \(my $debug), | ||
| 73 | 'help!' => \(my $help), | ||
| 74 | ) || usage; | ||
| 75 | |||
| 76 | my $emitter; | ||
| 77 | if ($use_pp_client) { | ||
| 78 | $emitter = Ganglia::Gmetric::PP->new( | ||
| 79 | host => $remote_host, | ||
| 80 | port => $remote_port, | ||
| 81 | ); | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | unless ($gmetric) { | ||
| 85 | $gmetric = qx(which gmetric); | ||
| 86 | die "can't find gmetric\n" unless $gmetric; | ||
| 87 | chomp $gmetric; | ||
| 88 | } | ||
| 89 | die "can't run gmetric\n" unless -x $gmetric; | ||
| 90 | } | ||
| 91 | |||
| 92 | usage if $help; | ||
| 93 | |||
| 94 | my $use_anyevent; | ||
| 95 | if (eval "use AnyEvent; 1") { | ||
| 96 | $debug && warn "Using AnyEvent\n"; | ||
| 97 | $use_anyevent = 1; | ||
| 98 | } | ||
| 99 | elsif (eval "use Danga::Socket; 1") { | ||
| 100 | $debug && warn "Using Danga::Socket\n"; | ||
| 101 | $use_anyevent = 0; | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | die "need either AnyEvent or Danga::Socket module"; | ||
| 105 | } | ||
| 106 | |||
| 107 | if ($daemonize) { | ||
| 108 | die "Proc::Daemon not available" unless eval "use Proc::Daemon (); 1"; | ||
| 109 | Proc::Daemon::Init(); | ||
| 110 | } | ||
| 111 | |||
| 112 | if ($pidfile && open my $pid_fh, '>', $pidfile) { | ||
| 113 | print $pid_fh "$$\n"; | ||
| 114 | close $pid_fh; | ||
| 115 | } | ||
| 116 | END { unlink $pidfile if $pidfile } | ||
| 117 | |||
| 118 | # udp server socket | ||
| 119 | my $gmond = Ganglia::Gmetric::PP->new(listen_host => $listen_host, listen_port => $listen_port); | ||
| 120 | |||
| 121 | if (defined $user) { | ||
| 122 | die "POSIX not available" unless eval "use POSIX (); 1"; | ||
| 123 | my ($uid, $gid) = (getpwnam $user)[2, 3]; | ||
| 124 | die "couldn't find uid/gid for user $user" unless defined $uid && defined $gid; | ||
| 125 | $debug && warn "Dropping to uid=$uid/gid=$gid\n"; | ||
| 126 | POSIX::setgid($gid) or die "failed to set gid to $gid: $!"; | ||
| 127 | POSIX::setuid($uid) or die "failed to set uid to $uid: $!"; | ||
| 128 | } | ||
| 129 | |||
| 130 | # can only aggregate numeric types | ||
| 131 | my %allowed_types = map {$_ => 1} qw/ double float int8 int16 int32 uint8 uint16 uint32 /; | ||
| 132 | |||
| 133 | my $last_time; | ||
| 134 | |||
| 135 | # store gmetric events as they are received | ||
| 136 | my %metric_aggregates; | ||
| 137 | my %metric_templates; | ||
| 138 | sub handle { | ||
| 139 | # receive and parse packet | ||
| 140 | my @sample; | ||
| 141 | eval { @sample = $gmond->receive }; | ||
| 142 | return unless @sample; | ||
| 143 | return unless $allowed_types{ $sample[METRIC_INDEX_TYPE] }; | ||
| 144 | |||
| 145 | # start counting at receipt of first event | ||
| 146 | $last_time ||= time; | ||
| 147 | |||
| 148 | # aggregate sums on the fly | ||
| 149 | $metric_aggregates{ $sample[METRIC_INDEX_NAME] } += $sample[METRIC_INDEX_VALUE]; | ||
| 150 | |||
| 151 | # keep an example copy of this metric to re-emit with aggregated values | ||
| 152 | $metric_templates{ $sample[METRIC_INDEX_NAME] } ||= \@sample; | ||
| 153 | |||
| 154 | $debug && warn Data::Dumper->Dump([\@sample], ['sample']); | ||
| 155 | } | ||
| 156 | my $watcher; | ||
| 157 | if ($use_anyevent) { | ||
| 158 | $watcher = AnyEvent->io(fh => $gmond, poll => 'r', cb => \&handle); | ||
| 159 | } | ||
| 160 | else { | ||
| 161 | Danga::Socket->AddOtherFds(fileno($gmond), \&handle); | ||
| 162 | } | ||
| 163 | |||
| 164 | $SIG{CHLD} = 'IGNORE'; | ||
| 165 | |||
| 166 | # periodically aggregate collected samples and re-emit to target gmond | ||
| 167 | my $timer; | ||
| 168 | sub aggregator { | ||
| 169 | if ($last_time) { | ||
| 170 | my $time = time; | ||
| 171 | my $measured_period = $time - $last_time; | ||
| 172 | $debug && warn "Aggregating at $time ($measured_period elapsed)\n"; | ||
| 173 | |||
| 174 | # emit for any metric seen before, even if it wasn't seen in the last period | ||
| 175 | for my $metric (keys %metric_templates) { | ||
| 176 | my @aggregate = @{ $metric_templates{$metric} }; | ||
| 177 | |||
| 178 | # aggregated value is rate of metric over last period | ||
| 179 | $aggregate[METRIC_INDEX_VALUE] = ($metric_aggregates{$metric}||0) / ($measured_period||1); | ||
| 180 | |||
| 181 | if ($output_doubles) { | ||
| 182 | $aggregate[METRIC_INDEX_TYPE] = GANGLIA_VALUE_DOUBLE; | ||
| 183 | } | ||
| 184 | elsif ($aggregate[METRIC_INDEX_TYPE] =~ /int/) { | ||
| 185 | $aggregate[METRIC_INDEX_VALUE] = int($aggregate[METRIC_INDEX_VALUE]) | ||
| 186 | } | ||
| 187 | |||
| 188 | $aggregate[METRIC_INDEX_NAME] .= $metric_suffix; | ||
| 189 | $aggregate[METRIC_INDEX_UNITS] .= $units_suffix; | ||
| 190 | $aggregate[METRIC_INDEX_VALUE] *= $multiplier; | ||
| 191 | $aggregate[METRIC_INDEX_TMAX] = $period; | ||
| 192 | |||
| 193 | if ($use_pp_client) { | ||
| 194 | $emitter->send(@aggregate); | ||
| 195 | } | ||
| 196 | else { | ||
| 197 | my $pid = fork; | ||
| 198 | if (!defined $pid) { | ||
| 199 | warn "fork failed: $!"; | ||
| 200 | return; | ||
| 201 | } | ||
| 202 | elsif (!$pid) { | ||
| 203 | my %gmetric_args; | ||
| 204 | @gmetric_args{qw/ --type --name --value --units --slope --tmax --dmax /} = @aggregate; | ||
| 205 | $debug && warn "Running $gmetric @{[%gmetric_args]}\n"; | ||
| 206 | exec $gmetric, %gmetric_args; | ||
| 207 | die "exec failed: $!\n"; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | $debug && warn Data::Dumper->Dump([\@aggregate], [$metric]); | ||
| 212 | } | ||
| 213 | %metric_aggregates = (); | ||
| 214 | |||
| 215 | $last_time = $time; | ||
| 216 | } | ||
| 217 | |||
| 218 | if ($use_anyevent) { | ||
| 219 | $timer = AnyEvent->timer(after => $period, cb => \&aggregator); | ||
| 220 | } | ||
| 221 | else { | ||
| 222 | Danga::Socket->AddTimer($period, \&aggregator); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | # run event loop | ||
| 227 | if ($use_anyevent) { | ||
| 228 | $timer = AnyEvent->timer(after => $period, cb => \&aggregator); | ||
| 229 | AnyEvent->condvar->recv; | ||
| 230 | } | ||
| 231 | else { | ||
| 232 | Danga::Socket->AddTimer($period, \&aggregator); | ||
| 233 | Danga::Socket->EventLoop; | ||
| 234 | } |