Commit d0331761 authored by Simon Leinen's avatar Simon Leinen

Make summary fancier, but also more concise

parent dce4a533
......@@ -3,16 +3,107 @@
use strict;
use warnings;
sub read_reports();
sub write_reports($@);
my $DIR = '/var/www/html/reports';
my $script_url = 'https://gitlab.leinen.ch/leinen/usage-report/blob/master/generate-summary.pl';
chdir($DIR) or die "Cannot cd to $DIR: $!";
my ($regions, @reports) = read_reports();
open OUT, ">index.html" or die "Cannot create index.html: $!";
open REPORTS, "ls -t report-*.txt|" or die "Cannot list reports: $!";
while(<REPORTS>) {
chomp;
printf OUT (" <li><a href=\"%s\">%s</a>\n", $_, $_);
}
close REPORTS or die "Error listing reports: $!";
write_reports($regions, @reports);
close OUT or die "Error writing index: $!";
1;
sub parse_totals($ ) {
my ($t) = @_;
my %usage_per_region = ();
while ($t =~ /^(S1|S2|ZH|LS)\s+(.*)/) {
my $region = $1;
my %usage = ();
$t = $2;
while ($t =~ /^([0-9.]+)\s+(\S+)\s*(.*)/) {
my $qty = $1;
my $metric = $2;
$usage{$metric} = $qty;
$t = $3;
}
$usage_per_region{$region} = \%usage;
}
return \%usage_per_region;
}
sub read_reports() {
my @result = ();
my %known_regions = ();
my @regions = ();
foreach my $report_name (<report-*.txt>) {
my ($report);
$report = bless { name => $report_name }, 'Report';
$report->{mtime} = (stat($report_name))[9];
open REPORT, $report_name or die "Cannot open report $report: $!";
while(<REPORT>) {
if (/^TOTAL\s+(.*)/) {
my $totals = parse_totals($1);
$report->{totals} = $totals;
foreach my $region (keys %{$totals}) {
unless (exists $known_regions{$region}) {
$known_regions{$region} = 1;
push @regions, $region;
}
}
last;
}
}
close REPORT or warn "Error closing report $report: $!";
push @result, $report;
}
return (\@regions, @result);
}
sub write_reports($@) {
my ($regions, @reports) = @_;
my $count = 0;
my $nreports = 50;
printf OUT ("<!DOCTYPE html>\n<html>\n <head>\n");
printf OUT (" <link rel=\"stylesheet\" type=\"text/css\" href=\"css/report.css\" />\n");
printf OUT (" </head>\n<body>\n <h1>SWITCHengines Usage Reports</h1>\n <table>\n");
printf OUT (" <tr><th rowspan='2'>Report</th>");
foreach my $region (@{$regions}) {
printf OUT ("<th colspan='4'>%s</th>", $region);
}
printf OUT ("</tr>\n");
printf OUT (" <tr>");
foreach my $region (@{$regions}) {
printf OUT ("<th>#VMs</th><th>#cores</th><th>RAM (GB)</th><th>disk (TB)</th>", $region);
}
printf OUT ("</tr>\n");
foreach my $report (sort { $b->{mtime} <=> $a->{mtime} } @reports) {
my ($name, $totals);
$name = $report->{name};
$totals = $report->{totals};
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($report->{mtime});
printf OUT (" <tr><td><a href=\"%s\">%04d-%02d-%02d %02d:%02d</a></td>",
$name, $year+1900, $mon+1, $mday, $hour, $min);
foreach my $region (@{$regions}) {
if (exists($totals->{$region})) {
printf OUT ("<td>%d</td><td>%d</td><td>%.1f</td><td>%.1f</td>",
$totals->{$region}->{VMs},
$totals->{$region}->{vCPUs},
$totals->{$region}->{MB}/1024,
$totals->{$region}->{GB}/1024);
} else {
printf OUT ("<td></td>");
}
}
printf OUT ("\n");
last if ++$count >= $nreports;
}
printf OUT (" </table>\n");
printf OUT (" <address>Generated by <a href=\"$script_url\">generate-summary.pl</a></address>\n");
printf OUT (" </table>\n </body>\n</html>\n");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment