hydra/src/script/hydra-send-stats
2018-06-12 13:00:39 +01:00

80 lines
3.1 KiB
Perl
Executable file

#! /usr/bin/env perl
use strict;
use utf8;
use Net::Statsd;
use File::Slurp;
use JSON;
STDERR->autoflush(1);
binmode STDERR, ":encoding(utf8)";
sub gauge {
my ($name, $val) = @_;
die unless defined $val;
Net::Statsd::gauge($name, $val);
}
sub sendQueueRunnerStats {
my $s = `hydra-queue-runner --status`;
die "cannot get queue runner stats\n" if $? != 0;
my $json = decode_json($s) or die "cannot decode queue runner status";
gauge("hydra.queue.up", $json->{status} eq "up" ? 1 : 0);
return if $json->{status} ne "up";
gauge("hydra.queue.steps.active", $json->{nrActiveSteps});
gauge("hydra.queue.steps.building", $json->{nrStepsBuilding});
gauge("hydra.queue.steps.copying_to", $json->{nrStepsCopyingTo});
gauge("hydra.queue.steps.copying_from", $json->{nrStepsCopyingFrom});
gauge("hydra.queue.steps.waiting", $json->{nrStepsWaiting});
gauge("hydra.queue.steps.runnable", $json->{nrRunnableSteps});
gauge("hydra.queue.steps.unfinished", $json->{nrUnfinishedSteps});
gauge("hydra.queue.steps.finished", $json->{nrStepsDone});
gauge("hydra.queue.steps.retries", $json->{nrRetries});
gauge("hydra.queue.steps.max_retries", $json->{maxNrRetries});
if ($json->{nrStepsDone}) {
gauge("hydra.queue.steps.avg_total_time", $json->{avgStepTime});
gauge("hydra.queue.steps.avg_build_time", $json->{avgStepBuildTime});
}
foreach my $machine (keys %{$json->{machineTypes}}) {
my $machineType = $machine =~ s/:|,/_/r;
gauge("hydra.queue.$machineType.runnable", $json->{machineTypes}->{$machine}->{runnable});
gauge("hydra.queue.$machineType.running", $json->{machineTypes}->{$machine}->{running});
}
gauge("hydra.queue.builds.read", $json->{nrBuildsRead});
gauge("hydra.queue.builds.unfinished", $json->{nrQueuedBuilds});
gauge("hydra.queue.builds.finished", $json->{nrBuildsDone});
gauge("hydra.queue.checks", $json->{nrQueueWakeups});
gauge("hydra.queue.bytes_sent", $json->{bytesSent});
gauge("hydra.queue.bytes_received", $json->{bytesReceived});
gauge("hydra.queue.machines.total", scalar(grep { $_->{enabled} } (values %{$json->{machines}})));
gauge("hydra.queue.machines.in_use", scalar(grep { $_->{currentJobs} > 0 } (values %{$json->{machines}})));
gauge("hydra.queue.notification.time_avg_ms", $json->{nrNotificationTimeAvgMs});
gauge("hydra.queue.notification.time_ms", $json->{nrNotificationTimeMs});
gauge("hydra.queue.notification.done", $json->{nrNotificationsDone});
gauge("hydra.queue.notification.failed", $json->{nrNotificationsFailed});
gauge("hydra.queue.notification.in_progress", $json->{nrNotificationsInProgress});
gauge("hydra.queue.notification.pending", $json->{nrNotificationsPending});
}
while (1) {
eval { sendQueueRunnerStats(); };
if ($@) { warn "$@"; }
my $meminfo = read_file("/proc/meminfo", err_mode => 'quiet') // "";
$meminfo =~ m/Dirty:\s*(\d+) kB/;
if (defined $1) {
my $dirty = $1 / (1024.0 * 1024.0);
gauge("hydra.mem.dirty", $dirty);
}
sleep(30);
}