From b446381dfa3f4298262ad8c7ef942d4ec64bcbea Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 23 Aug 2025 17:06:01 +0100 Subject: Provide hostname to Ingestor Store its Entity immediately for later use. --- src/ingestor.cpp | 6 +++++- src/ingestor.hpp | 3 ++- src/webstat_logger_main.cpp | 20 +++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/ingestor.cpp b/src/ingestor.cpp index a5a2e57..4084d2e 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -41,7 +41,11 @@ namespace WebStat { } } - Ingestor::Ingestor(DB::ConnectionPtr dbconn) : dbconn {std::move(dbconn)} { } + Ingestor::Ingestor(const std::string_view hostname, DB::ConnectionPtr dbconn) : + hostnameId {crc32(hostname)}, dbconn {std::move(dbconn)} + { + storeEntity({hostnameId, hostname}); + } Ingestor::ScanResult Ingestor::scanLogLine(std::string_view input) diff --git a/src/ingestor.hpp b/src/ingestor.hpp index a7a57a6..892cc9c 100644 --- a/src/ingestor.hpp +++ b/src/ingestor.hpp @@ -9,7 +9,7 @@ namespace WebStat { class Ingestor { public: - Ingestor(DB::ConnectionPtr dbconn); + Ingestor(std::string_view hostname, DB::ConnectionPtr dbconn); virtual ~Ingestor() = default; SPECIAL_MEMBERS_DEFAULT_MOVE_NO_COPY(Ingestor); @@ -33,6 +33,7 @@ namespace WebStat { size_t linesDiscarded = 0; private: + uint32_t hostnameId; DB::ConnectionPtr dbconn; }; } diff --git a/src/webstat_logger_main.cpp b/src/webstat_logger_main.cpp index 8f2d45a..e4f871e 100644 --- a/src/webstat_logger_main.cpp +++ b/src/webstat_logger_main.cpp @@ -1,9 +1,27 @@ #include "ingestor.hpp" +#include #include +#include + +namespace { + [[nodiscard]] + std::string + getHostname(bool fqdn) + { + utsname uts {}; + if (uname(&uts)) { + throw std::runtime_error(std::format("Failed to get hostname (uts: {}:{})", errno, strerror(errno))); + } + if (fqdn) { + return std::format("{}.{}", uts.nodename, uts.domainname); + } + return uts.nodename; + } +} int main(int, char **) { auto dbconn = std::make_shared("dbname=webstat user=webstat"); - WebStat::Ingestor {dbconn}.ingestLog(stdin); + WebStat::Ingestor {getHostname(false), dbconn}.ingestLog(stdin); } -- cgit v1.2.3