From 73f0b597dec0705db01ed4eec7abaebc0295a243 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 20 Dec 2020 16:52:47 +0100 Subject: webserver 1.12: Added graphical statistics page (gnuplot generated) --- plugins/statistics/Makefile | 4 ++-- plugins/statistics/statistics.cpp | 48 ++++++++++++++++++++++++++++++++++++++- plugins/webbox/webbox.cpp | 3 +-- plugins/weblog/weblog.cpp | 2 +- 4 files changed, 51 insertions(+), 6 deletions(-) (limited to 'plugins') diff --git a/plugins/statistics/Makefile b/plugins/statistics/Makefile index 8e8a6f7..34c0229 100644 --- a/plugins/statistics/Makefile +++ b/plugins/statistics/Makefile @@ -28,8 +28,8 @@ SRC=$(PROGSRC) all: $(PROJECTNAME).so -$(PROJECTNAME).so: $(SRC:.cpp=.o) - $(CXX) $(CXXFLAGS) $^ -shared $(LIBS) -o $@ +$(PROJECTNAME).so: ../../libcommon/libcommon.a $(SRC:.cpp=.o) + $(CXX) $(LDFLAGS) $^ -shared $(LDLIBS) $(LIBS) -o $@ ../../libcommon/libcommon.a: cd ../.. && $(MAKE) libcommon/libcommon.a diff --git a/plugins/statistics/statistics.cpp b/plugins/statistics/statistics.cpp index b1778f7..415369d 100644 --- a/plugins/statistics/statistics.cpp +++ b/plugins/statistics/statistics.cpp @@ -1,5 +1,8 @@ #include "statistics.h" +#include "libcommon/file.h" +#include "libcommon/tempfile.h" + #include #include #include @@ -23,9 +26,48 @@ namespace { { SetResponseHeader("status", status); SetResponseHeader("content_type", "text/html"); + return status + " " + message; } + std::string statsPng(std::function& GetServerParam, + std::function& SetResponseHeader) + { + // Create PNG statistics via Gnuplot + std::string statistics{GetServerParam("statistics")}; + + Tempfile tempStats{".txt"}; // input data + File::setFile(tempStats.GetPath(), statistics); + + Tempfile tempPng{".png"}; // output + + Tempfile tempGnuplot{".gnuplot"}; + std::stringstream s; + s << "set terminal png truecolor\n"; + s << "set output " << tempPng.GetPath() << "\n"; // quotes added automatically for paths + s << "set title \"Webserver Throughput\"\n"; + s << "set xlabel \"Time\"\n"; + s << "set timefmt \"%s\"\n"; + s << "set format x \"%Y-%m-%d\"\n"; + s << "set xdata time\n"; + s << "set xtics rotate by 45 right\n"; + s << "set ylabel \"Bytes\"\n"; + s << "set datafile separator \",\"\n"; + s << "set grid\n"; + s << "set style fill solid\n"; + s << "plot " << tempStats.GetPath() << " using 1:($4+$5) with boxes title \"Bytes I/O\" lt rgb \"#1132A7\"\n"; + File::setFile(tempGnuplot.GetPath(), s.str()); + + int exit_code{system(("gnuplot "s + tempGnuplot.GetPath().generic_string()).c_str())}; + if (exit_code) { + std::cerr << "Error: gnuplot returned " << std::to_string(exit_code) << std::endl; + return HttpStatus("500", "Statistics error"s, SetResponseHeader); + } + + SetResponseHeader("content_type", "image/png"); + return File::getFile(tempPng.GetPath()); + } + // returns sum over specified column uint64_t getSum(const std::string& stats, size_t column) { @@ -95,9 +137,12 @@ std::string statistics_plugin::generate_page( if (target.size() && target.back() != '/' && fs::is_directory(path)) { std::string location{GetRequestParam("location") + "/"s}; SetResponseHeader("location", location); - return HttpStatus("301", "Correcting directory path", SetResponseHeader); + return HttpStatus("301", "Correcting directory path", SetResponseHeader); // 301 Moved Permanently } + if (path.filename() == "stats.png") + return statsPng(GetServerParam, SetResponseHeader); + try { SetResponseHeader("content_type", "text/html"); @@ -122,6 +167,7 @@ std::string statistics_plugin::generate_page( result += "

IPv6 fraction by bytes: "s + std::to_string(ipv6_fraction_by_bytes * 100) + "%

"; result += "

HTTPS fraction by requests: "s + std::to_string(https_fraction_by_requests * 100) + "%

"; result += "

HTTPS fraction by bytes: "s + std::to_string(https_fraction_by_bytes * 100) + "%

"; + result += "

"; result += "
"s + statistics + "
"s; result += footer; diff --git a/plugins/webbox/webbox.cpp b/plugins/webbox/webbox.cpp index d19f428..26d49c8 100644 --- a/plugins/webbox/webbox.cpp +++ b/plugins/webbox/webbox.cpp @@ -5,7 +5,6 @@ #include "libcommon/file.h" #include "libcommon/stringutil.h" - #include #include #include @@ -511,7 +510,7 @@ protected: return HttpStatus("500", "Change path error: "s + ex.what(), p); } - Tempfile tempfile; // guards this path, removing file afterwards via RAII + Tempfile tempfile{".zip"}; // guards this path, removing file afterwards via RAII arglist = "/usr/bin/zip -r - "s + arglist + " > "s + tempfile.GetPath().string(); diff --git a/plugins/weblog/weblog.cpp b/plugins/weblog/weblog.cpp index a64ee1b..c26a4fe 100644 --- a/plugins/weblog/weblog.cpp +++ b/plugins/weblog/weblog.cpp @@ -302,7 +302,7 @@ namespace { HtmlPage htmlPage{GetRequestParam, "

"s + metaData.at("Subject") + "

" "
" + metaData.at("Date") + "
" - "

"s + data}; + "

"s + data + "
□"}; return htmlPage; } catch (const std::exception& ex) { -- cgit v1.2.3