Skip to content

Commit

Permalink
limits: use the SQL db instead of the storable
Browse files Browse the repository at this point in the history
  • Loading branch information
steveschnepp committed Feb 14, 2016
1 parent 8eb3ff1 commit c59fc69
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
39 changes: 23 additions & 16 deletions lib/Munin/Master/LimitsOld.pm
Expand Up @@ -64,7 +64,6 @@ my $verbose = 0;
my $force_run_as_root = 0;
my %notes = ();
my $config;
my $oldnotes;
my $modified = 0;
my %default_text = (
"default" =>
Expand Down Expand Up @@ -128,8 +127,6 @@ sub limits_main {

munin_runlock("$config->{rundir}/munin-limits.lock");

$oldnotes = &munin_readconfig_part('limits', 1);

initialize_for_nagios();

initialize_contacts();
Expand All @@ -138,9 +135,6 @@ sub limits_main {

close_pipes();

&munin_writeconfig("$config->{dbdir}/limits", \%notes);
&munin_writeconfig_storable("$config->{dbdir}/limits.storable", \%notes);

$update_time = sprintf("%.2f", (Time::HiRes::time - $update_time));

munin_removelock("$config->{rundir}/munin-limits.lock");
Expand Down Expand Up @@ -317,9 +311,16 @@ sub process_service {
$hash->{'worstid'} = 0 unless defined $hash->{'worstid'};
$hash->{'recovered'} = {};

my $state_file = sprintf ('%s/state-%s-%s.storable', $config->{dbdir}, $hash->{group}, $host);
DEBUG "[DEBUG] state_file: $state_file";
my $state = munin_read_storable($state_file) || {};
my $service_url = sprintf ('%s/%s', $hash->{group}, $host);
DEBUG "[DEBUG] service_url: $service_url";

use DBI;
my $datafilename = $ENV{MUNIN_DBURL} || $config->{dbdir}."/datafile.sqlite";
DEBUG "[DEBUG] opening sql $datafilename";
my $dbh = DBI->connect("dbi:SQLite:dbname=$datafilename","","") or die $DBI::errstr;
my $sth_state = $dbh->prepare('SELECT last_epoch, last_value, prev_epoch, prev_value, alarm FROM state WHERE id = ? and type = ?');
my $sth_state_upt = $dbh->prepare('UPDATE state SET alarm = ? WHERE id = ? and type = ?');
my $sth_ds = $dbh->prepare('SELECT id FROM url WHERE path = ? and type = ?');

foreach my $field (@$children) {
next if (!defined $field or ref($field) ne "HASH");
Expand All @@ -328,15 +329,14 @@ sub process_service {
my $onfield = munin_get_node($oldnotes, $fpath);
my $oldstate = 'ok';


# Test directly here as get_limits is in truth recursive and
# that fools us when processing multigraphs.
next if (!defined($field->{warning}) and !defined($field->{critical}));

# get the old state if there is one, or leave it empty.
if ( defined($onfield) and
defined($onfield->{"state"}) ) {
$oldstate = $onfield->{"state"};
}
$sth_ds->execute("$service_url/$fname", "ds");
my ($ds_id) = $sth_ds->fetchrow_array;

my ($warn, $crit, $unknown_limit) = get_limits($field);

Expand All @@ -347,9 +347,12 @@ sub process_service {
DEBUG "[DEBUG] field: " . munin_dumpconfig_as_str($field);
my $value;
{
my $rrd_filename = munin_get_rrd_filename($field);
my ($current_updated_timestamp, $current_updated_value) = @{ $state->{value}{"$rrd_filename:42"}{current} || [ ] };
my ($previous_updated_timestamp, $previous_updated_value) = @{ $state->{value}{"$rrd_filename:42"}{previous} || [ ] };
$sth_state->execute($ds_id, "ds");
my ($current_updated_timestamp, $current_updated_value,
$previous_updated_timestamp, $previous_updated_value,
$old_alarm) = $sth_state->fetchrow_array;

my $oldstate = $old_alarm || 'ok';

my $heartbeat = 600; # XXX - $heartbeat is a fixed 10 min (2 runs of 5 min).
if (! $field->{type} || $field->{type} eq "GAUGE") {
Expand Down Expand Up @@ -551,6 +554,10 @@ sub process_service {
}
}
}

# Replicate the state into the SQL DB
my $new_state = $onfield->{"state"};
$sth_state_upt->execute($new_state, $ds_id, "ds");
}
generate_service_message($hash);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Munin/Master/Update.pm
Expand Up @@ -431,7 +431,7 @@ sub _dump_into_sql {
$dbh->do("CREATE TABLE IF NOT EXISTS state (id INTEGER, type VARCHAR,
last_epoch INTEGER, last_value VARCHAR,
prev_epoch INTEGER, prev_value VARCHAR,
alarm VARCHAR
alarm VARCHAR, num_unknowns INTEGER
)");
$dbh->do("CREATE UNIQUE INDEX IF NOT EXISTS pk_state ON state (type, id)");
my $sth_state = $dbh->prepare('INSERT INTO state (id, type, last_epoch, last_value, prev_epoch, prev_value) VALUES (?, ?, ?, ?, ?, ?)');
Expand Down

0 comments on commit c59fc69

Please sign in to comment.