summaryrefslogtreecommitdiffstats
path: root/lib/Smokeping/Master.pm
diff options
context:
space:
mode:
authorTobi Oetiker <tobi@oetiker.ch>2007-07-27 21:54:11 +0200
committerTobi Oetiker <tobi@oetiker.ch>2007-07-27 21:54:11 +0200
commit4b147a40568beb0fb8ae6cfb8e5e1c024d669cd0 (patch)
treea0b7fedbc7e424db693a646cb17c1308128d8128 /lib/Smokeping/Master.pm
parent6f6466013b64af7e906dc12b5a49a39c127f0ac5 (diff)
downloadsmokeping-4b147a40568beb0fb8ae6cfb8e5e1c024d669cd0.tar.gz
smokeping-4b147a40568beb0fb8ae6cfb8e5e1c024d669cd0.tar.xz
* completed master/slave infrastructure ...
* updated documentation * presentation is pending
Diffstat (limited to 'lib/Smokeping/Master.pm')
-rw-r--r--lib/Smokeping/Master.pm68
1 files changed, 63 insertions, 5 deletions
diff --git a/lib/Smokeping/Master.pm b/lib/Smokeping/Master.pm
index 11f61e9..85ef5ce 100644
--- a/lib/Smokeping/Master.pm
+++ b/lib/Smokeping/Master.pm
@@ -93,7 +93,7 @@ sub save_updates {
# [ name, time, updatestring ] ]
for my $update (split /\n/, $updates){
my ($name, $time, $updatestring) = split /\t/, $update;
- my $file = $cfg->{General}{datadir}."/${name}.slaves";
+ my $file = $cfg->{General}{datadir}."/${name}.slave_cache";
if ( ! -f $cfg->{General}{datadir}."/${name}.rrd" ){
warn "Skipping update for $name since it does not exist in the local data structure ($cfg->{General}{datadir})\n";
} elsif ( open (my $hand, '+>>', $file) ) {
@@ -114,11 +114,60 @@ sub save_updates {
}
close $hand;
} else {
- warn "Could not write to $file: $!";
+ warn "Could not update $file: $!";
}
}
};
+=head3 get_slaveupdates
+
+Read in all updates provided by slaves and return an array reference.
+
+=cut
+
+sub get_slaveupdates {
+ my $name = shift;
+ my $file = $name.".slave_cache";
+ my $data;
+ if ( open (my $hand, '<', $file) ) {
+ if ( flock $hand, LOCK_EX ){
+ eval { $data = fd_retreive $hand };
+ if ($@) { #error
+ warn "Loading $file: $@";
+ return;
+ }
+ unlink $file;
+ flock $hand, LOCK_UN;
+ } else {
+ warn "Could not lock $file. Can't load data.\n";
+ }
+ close $hand;
+ return $data;
+ }
+ return;
+}
+
+
+=head3 get_secret
+
+Read the secrtes file and figure the secret for the slave which is talking to us.
+
+=cut
+
+sub get_secret {
+ my $cfg = shift;
+ my $slave = shift;
+ if (open my $hand, "<", $cfg->{Slaves}{secrets}){
+ while (<$hand>){
+ next unless /^${slave}:(\S+)/;
+ close $hand;
+ return $1;
+ }
+ }
+ warn "WARNING: Opening $cfg->{Slaves}{secrets}: $!\n";
+ return;
+}
+
=head3 answer_slave
Answer the requests from the slave by accepting the data, verifying the secrets
@@ -130,16 +179,24 @@ sub anwer_slave {
my $cfg = shift;
my $q = shift;
my $slave = $q->param('slave');
- my $secret = get_secret($slave);
+ my $secret = get_secret($cfg,$slave);
+ if (not $secret){
+ warn "WARNING: No secret found for slave ${slave}\n";
+ return;
+ }
my $key = $q->param('key');
my $data = $q->param('data');
my $config_time = $q->param('config_time');
-
+ if (not ref $cfg->{Slaves}{$slave} eq 'HASH'){
+ warn "WARNING: I don't know the slave ${slave} ignoring it";
+ return;
+ }
# lets make sure the she share a secret
if (md5_base64($secret.$data) eq $key){
save_updates $cfg, $slave, $data;
} else {
- warn "Data from $slave was signed with $key which does not match our expectation\n";
+ warn "WARNING: Data from $slave was signed with $key which does not match our expectation\n";
+ return;
}
# does the client need new config ?
if ($config_time < $cfg->{__last}){
@@ -149,6 +206,7 @@ sub anwer_slave {
};
}
+
1;
__END__