Use flock to make sure only one gluster command run at a time

This commit is contained in:
Daniel Berteaud 2016-03-21 10:16:28 +01:00
parent 4b73b1f8df
commit c43bdff1a9
2 changed files with 12 additions and 0 deletions

View File

@ -10,12 +10,16 @@ my $peer = undef;
my $bricks = undef;
my $gluster = which('gluster');
my $lock = '/var/lock/gluster-zabbix.lock';
unless($gluster){
# Gluster is not installed, exit with an error
die "gluster command not found";
}
# Get an exclusive lock
open(LOCK, ">$lock") || die "Can't open $lock";
flock(LOCK, 2);
GetOptions(
"what=s" => \$what,
@ -150,3 +154,5 @@ elsif ($what eq 'peer'){
print $status;
}
close(LOCK);
exit(0);

View File

@ -9,6 +9,7 @@ my $json;
@{$json->{data}} = ();
my $gluster = which('gluster');
my $lock = '/var/lock/gluster-zabbix.lock';
unless($gluster){
# Gluster is not installed, just return an empty JSON object
@ -16,6 +17,10 @@ unless($gluster){
exit(0);
}
# Get an exclusive lock
open(LOCK, ">$lock") || die "Can't open $lock";
flock(LOCK, 2);
my $what = 'volumes';
GetOptions(
"what=s" => \$what,
@ -110,6 +115,7 @@ else{
exit(1);
}
close(LOCK);
print to_json($json);
exit(0);