Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats fix #1544

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/Munin/Master/Update.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sub run {
my ($self) = @_;

$self->_create_rundir_if_missing();
$self->{runid} = time();

$self->_do_with_timing(sub {
INFO "[INFO]: Starting munin-update";
Expand Down Expand Up @@ -147,14 +148,22 @@ sub _do_with_timing {
sub _db_stats {
my ($self, $type, $name, $duration) = @_;

$self->{runid} = time() unless $self->{runid};
my $runid = $self->{runid};
my $dbh = $self->{dbh} || get_dbh(); # Reuse any existing connection, or open a temporary one
my $dbh_driver = $dbh->{Driver}->{Name};
my $sql_to_timestamp = "";
$sql_to_timestamp = "TO_TIMESTAMP" if $dbh_driver eq "Pg";
my $sth_i = $dbh->prepare_cached("INSERT INTO stats (runid, tstp, type, name, duration) VALUES (?, $sql_to_timestamp(?), ?, ?, ?);");
$sth_i->execute($runid, time(), $type, $name, $duration);
if ($type eq 'UT') {
# One day retention policy
my $sth_d= $dbh->prepare_cached("DELETE FROM stats where tstp < $sql_to_timestamp(?)");
$sth_d->execute(time() - 86400);
# Only retain last stats
#my $sth_d= $dbh->prepare_cached("DELETE FROM stats where runid < ?");
#$sth_d->execute($runid);
}

$dbh->commit();
}

Expand Down
12 changes: 12 additions & 0 deletions t/munin_master_update_spoolfetch.t
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ ok($update->run() == 5);
kill('TERM', $pid_debug_node);
wait();

# Verify stats table has 3 runs of 5 hosts + total
my $dbh = $update->get_dbh("readonly");
my $sth = $dbh->prepare("select count(1) from stats group by runid");
$sth -> execute();
my $res = $sth->fetchall_arrayref();
ok(scalar(@{$res}) == 3);
for my $row (@{$res}) {
ok(${$row}[0] == 6);
}
$dbh->disconnect();


done_testing();

1;