Merge pull request #402 from bachandi/rrd_locking

Add rrd locking option
This commit is contained in:
Jordi Sanfeliu 2022-02-28 09:48:30 +01:00 committed by GitHub
commit b8314c34f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 1 deletions

View File

@ -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;

View File

@ -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.

View File

@ -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") {

View File

@ -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);

View File

@ -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/