From 6c7f9e31f779e1d2a0cdbc0dba170e074016e551 Mon Sep 17 00:00:00 2001 From: Mike Tillberg Date: Fri, 23 Jun 2023 14:43:11 -0400 Subject: [PATCH 1/2] Fix runid for saving stats, add 1 day retention policy --- lib/Munin/Master/Update.pm | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/Munin/Master/Update.pm b/lib/Munin/Master/Update.pm index f2df2c9dc..096faa48f 100644 --- a/lib/Munin/Master/Update.pm +++ b/lib/Munin/Master/Update.pm @@ -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"; @@ -147,7 +148,6 @@ 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}; @@ -155,6 +155,15 @@ sub _db_stats { $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(); } From f17ef614ea0335efa21fa5c92cb7a7395ce1e66a Mon Sep 17 00:00:00 2001 From: Mike Tillberg Date: Fri, 23 Jun 2023 14:43:51 -0400 Subject: [PATCH 2/2] Add test for stats table to verify contents --- t/munin_master_update_spoolfetch.t | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/t/munin_master_update_spoolfetch.t b/t/munin_master_update_spoolfetch.t index 6bd5ffc69..239240553 100644 --- a/t/munin_master_update_spoolfetch.t +++ b/t/munin_master_update_spoolfetch.t @@ -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;