Changeset 3855

Show
Ignore:
Timestamp:
07/08/10 22:42:29 (2 years ago)
Author:
steve.schnepp
Message:

- Ask the config each time : no need to restart munin-async-server if a field appears

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/node/_bin/munin-async-server.in

    r3852 r3855  
    5858); 
    5959 
    60  
    61 # Read whole configuration 
     60my $process_name = "main"; 
     61 
     62my $plugin_rate_filename = "$SPOOLDIR/plugin_rates"; 
     63 
    6264my @plugins; 
    63 my %plugins_rate; 
    64 
    65         print STDERR "Reading config from $host\n" if $verbose; 
     65
     66        print STDERR "[$$][$process_name] Reading config from $host\n" if $verbose; 
    6667        my $sock = new IO::Socket::INET(  
    6768                PeerAddr        => "$host",  
     
    7980                        "w", 
    8081                ); 
    81          
     82 
    8283                print $fh_list $plugins_line; 
    8384                print $fh_list "\n"; 
     
    8586 
    8687        @plugins = split(/ /, $plugins_line); 
    87  
    88         # Ask for config & search update_rate for each plugins 
    89         foreach my $plugin (@plugins) { 
    90                 print STDERR "Asking config for $plugin\n" if $verbose; 
    91                 $plugins_rate{$plugin} = 300; # default : 5 min 
    92                 print $sock "config $plugin\n"; 
    93                 open (OUTFILE, "> $SPOOLDIR/munin-daemon.$plugin.config"); 
    94                 while(my $line = <$sock>) { 
    95                         print OUTFILE $line; 
    96                         if ($line =~ m/^update_rate (\d+)/) { 
    97                                 # The plugin has a special update_rate: overriding it 
    98                                 # XXX - Doesn't take into account a per field update_rate 
    99                                 $plugins_rate{$plugin} = $1; 
    100                         } 
    101                         if ($line =~ m/^\./) { 
    102                                 # Starting with . => end 
    103                                 last; 
    104                         } 
    105                 } 
    106                 close (OUTFILE); 
    107         } 
    10888} 
    10989 
     
    11898        PLUGIN: foreach my $plugin (@plugins) { 
    11999                # See if this plugin should be updated 
    120                 my $plugin_rate = $plugins_rate{$plugin} || 300; 
     100                my $plugin_rate = get_hash($plugin, $plugin_rate_filename) || 300; 
    121101                if ($when < ($last_updated{$plugin} || 0) + $plugin_rate) { 
    122102                        # not yet, next plugin 
     
    127107                $last_updated{$plugin} = $when; 
    128108                if (fork()) { 
    129                         # parent, return directly       
     109                        # parent, return directly 
    130110                        next PLUGIN; 
    131111                } 
     112                # Setting the command name for a useful top information 
     113                $process_name = "plugin:$plugin"; 
     114                $0 = "munin-async-server [$process_name]"; 
    132115                 
    133                 print STDERR "[$$][$plugin] asking for data\n" if $debug; 
    134  
    135                 # Setting the command name for a useful top information 
    136                 $0 = "munin-async-server [$plugin]"; 
    137  
    138116                fetch_data($plugin, $when); 
    139117 
     
    145123        sleep 1;  
    146124} 
    147  
    148 print STDERR "[$$] Exiting\n" if $debug
     125                 
     126print STDERR "[$$][$process_name] Exiting\n" if $verbose
    149127 
    150128sub fetch_data 
     
    159137                <$sock>; # skip header 
    160138 
    161                 print $sock "fetch $plugin\n"; 
    162                  
    163                 my $fh_data = IO::File->new("$SPOOLDIR/munin-daemon.$plugin.data", "a+"); 
     139                print STDERR "[$$][$process_name] asking for config\n" if $verbose; 
     140 
     141                print $sock "config $plugin\n"; 
     142 
     143                my $output_rows = []; 
     144 
    164145                while(my $line = <$sock>) { 
    165146                        print STDERR "[sock] $line" if $debug; 
     147                        chomp($line); 
     148 
    166149                        if ($line =~ m/^\./) { 
    167150                                # Starting with . => end 
     
    169152                        } 
    170153 
    171                         if ($line =~ m/^(\w+)\.value (\d+):(-?\d+\.?\d*)/) { 
    172                                 # the plugin already outputed a time 
    173                                 # keeping it 
    174                                 print $fh_data "$1.value $2:$3\n"; 
    175                                 print STDERR "[$plugin] $1.value $2:$3\n" if $debug; 
    176                         } elsif ($line =~ m/^(\w+)\.value (-?\d+\.?\d*)/) { 
    177                                 # the plugin doesn't ouput a time,  
    178                                 # using now() 
    179                                 print $fh_data "$1.value $when:$2\n"; 
    180                                 print STDERR "[$plugin] $1.value $when:$2\n" if $debug; 
     154                        push @$output_rows, $line; 
     155                        if ($line =~ m/^update_rate (\d+)/) { 
     156                                # The plugin has a special update_rate: overriding it 
     157                                # XXX - Doesn't take into account a per field update_rate 
     158 
     159                                # This has to be sent back to the master 
     160                                set_hash($plugin, $1, $plugin_rate_filename); 
    181161                        } 
    182162                } 
    183 
     163 
     164                print STDERR "[$$][$process_name] asking for data\n" if $verbose; 
     165                print $sock "fetch $plugin\n"; 
     166                 
     167                while(my $line = <$sock>) { 
     168                        print STDERR "[sock] $line" if $debug; 
     169                        chomp($line); 
     170                         
     171                        if ($line =~ m/^\./) { 
     172                                # Starting with . => end 
     173                                last; 
     174                        } 
     175 
     176                        # Save the line 
     177                        push @$output_rows, $line; 
     178                } 
     179 
     180                # Write the whole load into the spool 
     181                $spoolwriter->write($when, $plugin, $output_rows); 
     182
     183 
     184sub get_hash 
     185
     186        my ($key, $filename) = @_; 
     187        my %hash; 
     188 
     189        use Fcntl;   # For O_RDWR, O_CREAT, etc. 
     190        use DB_File; 
     191        tie (%hash, 'DB_File', $filename, O_RDWR|O_CREAT, 0666)  
     192                or die "$!"; 
     193        my $value = $hash{$key}; 
     194        untie(%hash); 
     195 
     196        return $value; 
     197
     198 
     199sub set_hash 
     200
     201        my ($key, $value, $filename) = @_; 
     202        my %hash; 
     203 
     204        use Fcntl;   # For O_RDWR, O_CREAT, etc. 
     205        use DB_File; 
     206        tie (%hash, 'DB_File', $filename, O_RDWR|O_CREAT, 0666)  
     207                or die "$!"; 
     208        $hash{$key} = $value; 
     209        untie(%hash); 
     210
     211 
     212 
    184213 
    185214__END__