Fix cache when the same resource is queried with different options

tags/zabbix-agent-addons-0.2.117-1
Daniel Berteaud 5 years ago
parent 4c8ada96a8
commit 0805013d6a
  1. 11
      zabbix_scripts/check_pve_sudo

@ -144,15 +144,16 @@ print to_json($json, { pretty => $pretty }) . "\n";
# the cache if its fresh enough, or query the API # the cache if its fresh enough, or query the API
# and save the result in the cache for later # and save the result in the cache for later
sub get_api_data { sub get_api_data {
my $path = shift; my ($path, $query_opt) = @_;
my $query_opt = shift;
$query_opt ||= ''; $query_opt ||= '';
my $opt_filename = $query_opt;
$opt_filename =~ s/[\-=]/_/g;
my $res; my $res;
# Is the cache existing and fresh enough ? # Is the cache existing and fresh enough ?
if (-f $cache_dir . $path and int((-M $cache_dir . $path)*60*60*24) < $cache){ if (-f $cache_dir . $path . $opt_filename and int((-M $cache_dir . $path . $opt_filename)*60*60*24) < $cache){
{ {
local $/; # Enable slurp local $/; # Enable slurp
open my $fh, "<", $cache_dir . $path; open my $fh, "<", $cache_dir . $path . $opt_filename;
$res = <$fh>; $res = <$fh>;
close $fh; close $fh;
} }
@ -163,7 +164,7 @@ sub get_api_data {
my $dir = (fileparse($path))[1]; my $dir = (fileparse($path))[1];
make_path($cache_dir . $dir, { chmod => 700 }); make_path($cache_dir . $dir, { chmod => 700 });
}; };
open my $fh, ">", $cache_dir . $path; open my $fh, ">", $cache_dir . $path . $opt_filename;
print $fh $res; print $fh $res;
close $fh; close $fh;
} }

Loading…
Cancel
Save