From 61689a58908b4741f83b4aa2f9faf676bcecf163 Mon Sep 17 00:00:00 2001 From: Tobi Oetiker Date: Mon, 13 Aug 2007 16:54:30 +0000 Subject: make slave code work --- lib/Smokeping.pm | 30 ++++++++++++++++++++++++++---- lib/Smokeping/Slave.pm | 48 ++++++++++++++++++------------------------------ 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/lib/Smokeping.pm b/lib/Smokeping.pm index 6de126f..413a039 100644 --- a/lib/Smokeping.pm +++ b/lib/Smokeping.pm @@ -12,6 +12,7 @@ use POSIX; use Config::Grammar; use RRDs; use Sys::Syslog qw(:DEFAULT setlogsock); +use Sys::Hostname; use Smokeping::Colorspace; use Smokeping::Master; use Smokeping::Slave; @@ -85,13 +86,18 @@ sub load_probes ($){ return \%prbs; }; -sub load_probe ($$$$) { +sub load_probe ($$$$) { my $modname = shift; my $properties = shift; my $cfg = shift; my $name = shift; $name = $modname unless defined $name; - my $rv; + # just in case, make sure we have the module loaded. unless + # we are running as slave, this will already be the case + # after reading the config file + eval 'require Smokeping::probes::'.$modname; + die "$@\n" if $@; + my $rv; eval '$rv = Smokeping::probes::'.$modname.'->new( $properties,$cfg,$name);'; die "$@\n" if $@; die "Failed to load Probe $name (module $modname)\n" unless defined $rv; @@ -3511,9 +3517,17 @@ sub main (;$) { master_url => $opt{'master-url'}, cache_dir => $opt{'cache-dir'}, shared_secret => $secret, + slave_name => hostname, }; # this should get us a config set from the server - Smokeping::Slave::submit_results($slave_cfg,$cfg); + my $new_conf = Smokeping::Slave::submit_results($slave_cfg,$cfg); + if ($new_conf){ + $cfg=$new_conf; + $probes = undef; + $probes = load_probes $cfg; + $cfg->{__probes} = $probes; + add_targets($cfg, $probes, $cfg->{Targets}, $cfg->{General}{datadir}); + } } else { if(defined $opt{'check'}) { verify_cfg($cfgfile); exit 0; } if($opt{reload}) { @@ -3710,7 +3724,15 @@ KID: run_probes $probes, $myprobe; # $myprobe is undef if running without 'concurrentprobes' my %sortercache; if ($opt{'master-url'}){ - Smokeping::Slave::get_results $slave_cfg,$cfg,$probes,$cfg->{Targets},"",$myprobe; + my $new_conf = Smokeping::Slave::submit_results $slave_cfg,$cfg,$myprobe,$probes; + if ($new_conf){ + $cfg=$new_conf; + $probes = undef; + $probes = load_probes $cfg; + $cfg->{__probes} = $probes; + add_targets($cfg, $probes, $cfg->{Targets}, $cfg->{General}{datadir}); + goto RESTART; + } } else { update_rrds $cfg, $probes, $cfg->{Targets}, $cfg->{General}{datadir}, $myprobe, \%sortercache; save_sortercache($cfg,\%sortercache,$myprobe); diff --git a/lib/Smokeping/Slave.pm b/lib/Smokeping/Slave.pm index 533201e..fb0c52c 100644 --- a/lib/Smokeping/Slave.pm +++ b/lib/Smokeping/Slave.pm @@ -67,64 +67,52 @@ sub submit_results { my $restore = retrieve $store if -f $store; my $data = get_results($slave_cfg, $cfg, $probes, $cfg->{Targets}, '', $myprobe); push @$data, @$restore if $restore; - my $data_dump = join "\n",@{$data}; + my $data_dump = join("\n",@{$data}) || ""; my $ua = LWP::UserAgent->new( agent => 'smokeping-slave/1.0', timeout => 10, env_proxy => 1 ); + my $response = $ua->post( $slave_cfg->{master_url}, Content_Type => 'form-data', Content => [ slave => $slave_cfg->{slave_name}, - key => md5_base_64($slave_cfg->{shared_secret}.$data_dump), + key => md5_base64($slave_cfg->{shared_secret}.$data_dump), data => $data_dump, config_time => $cfg->{__last} || 0, ], ); if ($response->is_success){ - my $data = $response->decoded_content; + my $data = $response->content; my $key = $response->header('Key'); - if (md5_base_64($slave_cfg->{shared_secret}.$data) ne $key){ - warn "Warning: $slave_cfg->{master_url} sent data with wrong key"; + if ($response->header('Content-Type') ne 'application/smokeping-config'){ + warn "$data\n" unless $data =~ /OK/; + Smokeping::do_debuglog("Sent data to Server. Server said $data"); + return undef; + }; + if (md5_base64($slave_cfg->{shared_secret}.$data) ne $key){ + warn "WARNING $slave_cfg->{master_url} sent data with wrong key"; return undef; } my $VAR1; eval $data; if ($@){ - warn "evaluating new config from server failed: $@"; - } elsif (definded $VAR1 and ref $VAR1 eq 'HASH'){ - update_config($cfg,$VAR1); + warn "WARNING evaluating new config from server failed: $@"; + } elsif (defined $VAR1 and ref $VAR1 eq 'HASH'){ + $VAR1->{General}{pidir} = $slave_cfg->{cache_dir}; + Smokeping::do_debuglog("Sent data to Server and got new config"); + return $VAR1; } } else { # ok did not manage to get our data to the server. # we store the result so that we can try again later. - nstore $store; - warn $response->status_line(); + warn "WARNING Master said ".$response->status_line()."\n"; + nstore $data, $store; } return undef; } -=head3 update_config - -Update the config information based on the latest input form the server. - -=cut - -sub update_config { - my $cfg = shift; - my $data = shift; - $cfg->{General} = $data->{General}; - $cfg->{Probes} = $data->{Probes}; - $cfg->{Database} = $data->{Database}; - $cfg->{Targets} = $data->{Targets}; - $cfg->{__last} = $data->{__last}; - my $probes = Smokeping::load_probes $cfg; - $cfg->{__probes} = $probes; - add_targets($cfg, $probes, $cfg->{Targets}, $cfg->{General}{datadir}); - return $probes; -} - 1; __END__ -- cgit v1.2.3-24-g4f1b