diff --git a/lib/Monitorix.pm b/lib/Monitorix.pm index 1aa37af..d3b3ff3 100644 --- a/lib/Monitorix.pm +++ b/lib/Monitorix.pm @@ -24,8 +24,9 @@ use strict; use warnings; use Exporter 'import'; use POSIX qw(setuid setgid setsid getgid getuid ceil); +use Fcntl qw(:flock); use Socket; -our @EXPORT = qw(logger trim min max celsius_to img_element picz_a_element picz_js_a_element uptime2str setup_riglim httpd_setup get_nvidia_data get_ati_data flush_accounting_rules); +our @EXPORT = qw(logger trim min max celsius_to img_element picz_a_element picz_js_a_element uptime2str setup_riglim httpd_setup get_nvidia_data get_ati_data flush_accounting_rules lockfile_handler global_flock); sub logger { my ($msg) = @_; @@ -511,4 +512,26 @@ sub flush_accounting_rules { } } +sub lockfile_handler { + my ($config) = @_; + if (lc($config->{enable_rrd_lock} || "") eq "y") { + my $lock_file = "/tmp/monitorix.lock"; + + my $lockfile_was_available = (-e $lock_file); + open(my $fh, ($lockfile_was_available ? "+<" : ">>"), $lock_file) or die "Can't open $lock_file: $!"; # If the file already exists we open it without the O_CREATE flag due to the limitations introduced by fs.protected_regular. + if (!$lockfile_was_available) { + chmod(0666, $lock_file); + } + return $fh; + } + return undef; +} + +sub global_flock { + my ($fh, $option) = @_; + if (defined($fh)) { + flock($fh, $option) or die "flock error: $!\n"; + } +} + 1; diff --git a/man/man5/monitorix.conf.5 b/man/man5/monitorix.conf.5 index 9dec156..0893f5a 100644 --- a/man/man5/monitorix.conf.5 +++ b/man/man5/monitorix.conf.5 @@ -169,6 +169,13 @@ This option will fork an independent process for each graph in order to speed up Default value: \fIy\fP .RE .P +.BI enable_rrd_lock +.RS +This option will synchronise the rrd file access by creating the file \fI/tmp/monitorix.lock\fP and use it via flock. +.P +Default value: \fIn\fP +.RE +.P .BI include_dir .RS The main configuration file is usually called \fImonitorix.conf\fP and its location is provided as part of the command line arguments. In addition, other configuration files may be loaded placing them in the directory pointed by this option. The names must end with .conf to be included. diff --git a/monitorix b/monitorix index 39c41e3..beda540 100755 --- a/monitorix +++ b/monitorix @@ -31,6 +31,7 @@ use POSIX qw(WNOHANG LC_TIME setlocale uname pause setsid); use Config::General; use Getopt::Std; use Cwd qw(abs_path); +use Fcntl qw(:flock); # Force a standard locale $ENV{LANG} = ""; @@ -767,6 +768,8 @@ while(1) { # call to all enabled graphs on every minute if($sec == 0) { + my $lockfile_handler = lockfile_handler(\%config); + global_flock($lockfile_handler, LOCK_EX); foreach my $f (@{$config{func_update}}) { my $update = $f . "_update"; my $d = $f; @@ -785,6 +788,7 @@ while(1) { } } } + global_flock($lockfile_handler, LOCK_UN); # TRAFFACCT graph daily reports if(lc($config{traffacct}->{enabled} || "") eq "y") { diff --git a/monitorix.cgi b/monitorix.cgi index 48133d6..16e7749 100755 --- a/monitorix.cgi +++ b/monitorix.cgi @@ -31,6 +31,7 @@ use Config::General; use POSIX; use RRDs; use Encode; +use Fcntl qw(:flock); my %config; my %cgi; @@ -577,6 +578,8 @@ if($mode eq "localhost") { my @writers; # array of file descriptors my $children = 0; + my $lockfile_handler = lockfile_handler(\%config); + global_flock($lockfile_handler, LOCK_SH); foreach (split(',', $config{graph_name})) { my $gn = trim($_); my $g = ""; @@ -652,6 +655,7 @@ if($mode eq "localhost") { print @{$outputs{$n}} if $outputs{$n}; } } + global_flock($lockfile_handler, LOCK_UN); } elsif($mode eq "multihost") { multihost(\%config, \%colors, \%cgi); diff --git a/monitorix.conf b/monitorix.conf index be68d08..2e89fcb 100644 --- a/monitorix.conf +++ b/monitorix.conf @@ -19,6 +19,7 @@ max_historic_years = 1 accept_selfsigned_certs = y image_format = PNG enable_parallelizing = y +enable_rrd_lock = n include_dir = /etc/monitorix/conf.d base_dir = /var/lib/monitorix/www/