Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing static generation #1590

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
16 changes: 16 additions & 0 deletions lib/Munin/Master/Graph.pm
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ sub handle_request

DEBUG "found node=$id, type=$type";

my $dbdir = get_param("dbdir");

# Here's the most common case: only plain plugins
my $sth;

Expand Down Expand Up @@ -453,6 +455,8 @@ sub handle_request
($_rrdfile, $_rrdfield, $_lastupdated) = get_alias_rrdfile($dbh, $_rrdalias);
}

$_rrdfile = File::Spec->catfile($dbdir, $_rrdfile);

# Fetch the data from the RRDs
my $rrd_is_virtual = is_virtual($_rrdname, $_rrdcdef);
my $rrd_is_cdef = defined $_rrdcdef && $_rrdcdef ne "";
Expand Down Expand Up @@ -987,4 +991,16 @@ sub get_alias_rrdfile
return ($_rrdfile, $_rrdfield, $_lastupdated);
}

sub get_param
{
my ($param) = @_;

# Ok, now SQL is needed to go further
use DBI;
my $datafilename = $ENV{MUNIN_DBURL} || "$Munin::Common::Defaults::MUNIN_DBDIR/datafile.sqlite";
my $dbh = Munin::Master::Update::get_dbh(1);
my ($value) = $dbh->selectrow_array("SELECT value FROM param WHERE name = ?", undef, ($param));
return $value;
}

1;
3 changes: 2 additions & 1 deletion lib/Munin/Master/HTML.pm
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,9 @@ RENDERING:
print $cgi->header( "-Content-Type" => "text/html",
-Cache_Control => "public, max-age=3600", # 1h for HTML pages
);
my $tmpldir = get_param("tmpldir");
my $template = HTML::Template::Pro->new(
filename => "$Munin::Common::Defaults::MUNIN_CONFDIR/templates/$template_filename",
filename => "$tmpldir/$template_filename",
loop_context_vars => 1,
);

Expand Down
40 changes: 40 additions & 0 deletions lib/Munin/Master/Static/CGI.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package Munin::Master::Static::HTML::CGI;

# empty, it is just to have Perl loading this file to be able to override the
# standard ::CGI namespace to mock it

package CGI;

sub new {
my $c = shift;
my $params = shift;
return bless $params, $c;
}

sub path_info {
my $self = shift;
return $self->{path_info};
}

sub url_param {
my $self = shift;
my $param = shift;
die("undef") unless defined $param;
return $self->{url_param}{$param};
}

my %header;
sub header {
my $self = shift;
%header = @_;

# No return, so nothing is printed
return "";
}

sub url {
my $self = shift;
return "";
}

1;
73 changes: 73 additions & 0 deletions lib/Munin/Master/Static/Graph.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package Munin::Master::Static::Graph;

use strict;
use warnings;

use Getopt::Long;
use Parallel::ForkManager;
use File::Basename;
use File::Path qw(make_path remove_tree);

use Munin::Master::Static::CGI;

use Munin::Master::Graph;

sub create
{
my ($jobs, $destination) = @_;

# Get the list of png to create
my @paths;
{
use Munin::Master::Update;
my $dbh = Munin::Master::Update::get_dbh(1);
my $row_ref = $dbh->selectall_arrayref("SELECT path FROM url WHERE type = ?", {}, "service");

# Process results
@paths = map {
my $path = @{$_}[0];
(
"$path-year.png",
"$path-month.png",
"$path-week.png",
"$path-day.png",
"$path-hour.png",
);
} @$row_ref;
}

my $pm = Parallel::ForkManager->new($jobs);
for my $path (@paths) {
my $worker_id = $pm->start();
next if $worker_id;

# Mock CGI interface, in order to reuse most of munin-httpd internals
my $cgi = CGI->new({
path_info => "/$path",
});

$destination = $1 if $destination =~ m/(.*)/;
my $filepath = "$destination/$path";

# redirect STDOUT to the file
print "s: $filepath\n";

my $dirpath = dirname($filepath);
make_path($dirpath);

do {
local *STDOUT;
open (STDOUT, '>', $filepath);

Munin::Master::Graph::handle_request($cgi);
};

$pm->finish();
}

# wait for every worker to finish
$pm->wait_all_children();

}

1;
67 changes: 67 additions & 0 deletions lib/Munin/Master/Static/HTML.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package Munin::Master::Static::HTML;

use strict;
use warnings;

use Getopt::Long;
use Parallel::ForkManager;

use Munin::Master::Static::CGI;

sub create
{
my ($jobs, $destination) = @_;

# Get the list of HTML pages to create
my @paths;
{
use Munin::Master::Update;
my $dbh = Munin::Master::Update::get_dbh(1);
my $row_ref = $dbh->selectall_arrayref("SELECT path FROM url");

# Process results
@paths = map {
my $path = @{$_}[0] . ".html";
} @$row_ref;
push @paths, "/";
}

my $pm = Parallel::ForkManager->new($jobs);
for my $path (@paths) {
my $worker_id = $pm->start();
next if $worker_id;

# Mock CGI interface, in order to reuse most of munin-httpd internals
my $cgi = CGI->new({
path_info => $path,
});

$destination = $1 if $destination =~ m/(.*)/;
my $filepath = "$destination/$path";
$filepath = "$destination/index.html" if $path eq "/";

# redirect STDOUT to the file
print "s: $filepath\n";

use File::Basename;
use File::Path qw(make_path remove_tree);

my $dirpath = dirname($filepath);
make_path($dirpath);

do {
local *STDOUT;
open (STDOUT, '>', $filepath);

use Munin::Master::HTML;
Munin::Master::HTML::handle_request($cgi);
};

$pm->finish();
}

# wait for every worker to finish
$pm->wait_all_children();
}

1;
7 changes: 4 additions & 3 deletions lib/Munin/Master/UpdateWorker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -845,9 +845,6 @@ sub _get_rrd_file_name {
$ds_name,
$type_id);

$file = File::Spec->catfile($config->{dbdir},
$file);

DEBUG "[DEBUG] rrd filename: $file\n";

return $file;
Expand All @@ -859,6 +856,8 @@ sub _create_rrd_file {

INFO "[INFO] creating rrd-file for $service->$ds_name: '$rrd_file'";

$rrd_file = File::Spec->catfile($config->{dbdir}, $rrd_file);

munin_mkdir_p(dirname($rrd_file), oct(777));

my @args;
Expand Down Expand Up @@ -1018,6 +1017,8 @@ sub to_sec {
sub _update_rrd_file {
my ($self, $rrd_file, $ds_name, $ds_values) = @_;

$rrd_file = File::Spec->catfile($config->{dbdir}, $rrd_file);

my $values = $ds_values->{value};

# Some kind of mismatch between fetch and config can cause this.
Expand Down
Loading