Changeset 3322

Show
Ignore:
Timestamp:
01/19/10 22:00:10 (2 years ago)
Author:
steve.schnepp
Message:

Initial add of update_rate. Still quite rough (uses tie() and files in /tmp)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • people/snide/pre_1.5/master/lib/Munin/Master/UpdateWorker.pm

    r3254 r3322  
    8181                    WARN "[WARNING] Service $plugin on $nodedesignation ". 
    8282                        "returned no config"; 
     83                    next; 
     84                } 
     85 
     86                # Check if this plugin has to be updated 
     87                my $update_rate_in_seconds = get_global_service_value(\%service_config, $plugin, "update_rate", 0); # default is 0 sec : always update when asked 
     88                DEBUG "[DEBUG] update_rate $update_rate_in_seconds for $plugin on $nodedesignation"; 
     89                if ($update_rate_in_seconds  
     90                        && is_fresh_enough($nodedesignation, $plugin, $update_rate_in_seconds)) { 
     91                    # It's fresh enough, skip this $service 
     92                    DEBUG "[DEBUG] $plugin is fresh enough, not updating it"; 
    8393                    next; 
    8494                } 
     
    148158        service_configs => \%all_service_configs, 
    149159    } 
     160} 
     161 
     162sub get_global_service_value { 
     163        my ($service_config, $service, $conf_field_name, $default) = @_; 
     164        foreach my $array (@{$service_config->{global}{$service}}) { 
     165                my ($field_name, $field_value) = @$array; 
     166                if ($field_name eq $conf_field_name) { 
     167                        return $field_value; 
     168                } 
     169        } 
     170 
     171        return $default; 
     172} 
     173 
     174sub is_fresh_enough { 
     175        my ($nodedesignation, $service, $update_rate_in_seconds) = @_; 
     176 
     177        my $key = "$nodedesignation/$service"; 
     178        DEBUG "is_fresh_enough asked for $key with a rate of $update_rate_in_seconds"; 
     179 
     180        my %last_updated; 
     181        # XXX - ugly hack. Should be refactored to use a a common state provider 
     182 
     183        use Fcntl;   # For O_RDWR, O_CREAT, etc. 
     184        use NDBM_File; 
     185        tie(%last_updated, 'NDBM_File', '/tmp/munin_plugins_last_updated', O_RDWR|O_CREAT, 0666) or ERROR "$!"; 
     186        DEBUG "last_updated{$key}: " . $last_updated{$key}; 
     187        my @last = split(/ /, $last_updated{$key}); 
     188    
     189        use Time::HiRes qw(gettimeofday tv_interval);    
     190        my $now = [ gettimeofday ]; 
     191 
     192        my $age = tv_interval(\@last, $now);     
     193        DEBUG "last: " . Dumper(\@last) . ", now: " . Dumper($now) . ", age: $age"; 
     194        my $is_fresh_enough = ($age < $update_rate_in_seconds); 
     195        DEBUG "is_fresh_enough  $is_fresh_enough"; 
     196 
     197        if (! $is_fresh_enough) { 
     198                DEBUG "new value: " . join(" ", @$now); 
     199                $last_updated{$key} = join(" ", @$now); 
     200        } 
     201 
     202        untie(%last_updated); 
     203 
     204        return $is_fresh_enough; 
    150205} 
    151206