diff options
author | Dan Goodliffe <dan.goodliffe@octal.co.uk> | 2025-09-02 16:13:22 +0100 |
---|---|---|
committer | Dan Goodliffe <dan.goodliffe@octal.co.uk> | 2025-09-02 16:13:22 +0100 |
commit | ca9be55a98c094e8026a4003a099573dd4137050 (patch) | |
tree | 1d58e13a84fc6bba25242877859027bdfe8f461e /src | |
parent | 07a98c0ba7d2fdd79cab1884f81bb041741fc18c (diff) | |
download | webstat-ca9be55a98c094e8026a4003a099573dd4137050.tar.bz2 webstat-ca9be55a98c094e8026a4003a099573dd4137050.tar.xz webstat-ca9be55a98c094e8026a4003a099573dd4137050.zip |
Store utsname details in the host's detail field
Diffstat (limited to 'src')
-rw-r--r-- | src/ingestor.cpp | 13 | ||||
-rw-r--r-- | src/ingestor.hpp | 3 | ||||
-rw-r--r-- | src/sql.cpp | 4 | ||||
-rw-r--r-- | src/sql.hpp | 1 | ||||
-rw-r--r-- | src/sql/hostUpsert.sql | 7 | ||||
-rw-r--r-- | src/webstat_logger_main.cpp | 11 |
6 files changed, 25 insertions, 14 deletions
diff --git a/src/ingestor.cpp b/src/ingestor.cpp index ea62d54..87167b5 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -57,13 +57,14 @@ namespace WebStat { } } - Ingestor::Ingestor(const std::string_view hostname, DB::ConnectionPoolPtr dbpl) : - hostnameId {crc32(hostname)}, dbpool {std::move(dbpl)} + Ingestor::Ingestor(const utsname & host, DB::ConnectionPoolPtr dbpl) : + hostnameId {crc32(host.nodename)}, dbpool {std::move(dbpl)} { - storeEntities(dbpool->get().get(), - { - std::make_tuple(hostnameId, EntityType::Host, hostname), - }); + auto dbconn = dbpool->get(); + auto ins = dbconn->modify(SQL::HOST_UPSERT, SQL::HOST_UPSERT_OPTS); + bindMany(ins, 0, hostnameId, host.nodename, host.sysname, host.release, host.version, host.machine, + host.domainname); + ins->execute(); } Ingestor::ScanResult diff --git a/src/ingestor.hpp b/src/ingestor.hpp index ef3ba19..b2a0803 100644 --- a/src/ingestor.hpp +++ b/src/ingestor.hpp @@ -8,11 +8,12 @@ #include <flat_set> #include <scn/scan.h> #include <span> +#include <sys/utsname.h> namespace WebStat { class Ingestor { public: - Ingestor(std::string_view hostname, DB::ConnectionPoolPtr); + Ingestor(const utsname &, DB::ConnectionPoolPtr); virtual ~Ingestor() = default; SPECIAL_MEMBERS_DELETE(Ingestor); diff --git a/src/sql.cpp b/src/sql.cpp index 52e9520..fbfadd3 100644 --- a/src/sql.cpp +++ b/src/sql.cpp @@ -12,9 +12,13 @@ namespace WebStat::SQL { const std::string ENTITY_INSERT { #embed "sql/entityInsert.sql" }; + const std::string HOST_UPSERT { +#embed "sql/hostUpsert.sql" + }; #define HASH_OPTS(VAR) \ const DB::CommandOptionsPtr VAR##_OPTS = std::make_shared<DB::CommandOptions>(std::hash<std::string> {}(VAR)) HASH_OPTS(ACCESS_LOG_INSERT); HASH_OPTS(ENTITY_INSERT); + HASH_OPTS(HOST_UPSERT); #undef HASH_OPTS } diff --git a/src/sql.hpp b/src/sql.hpp index 1426384..8183513 100644 --- a/src/sql.hpp +++ b/src/sql.hpp @@ -10,5 +10,6 @@ namespace WebStat::SQL { EMBED_DECLARE(ACCESS_LOG_INSERT); EMBED_DECLARE(ENTITY_INSERT); + EMBED_DECLARE(HOST_UPSERT); #undef EMBED_DECLARE } diff --git a/src/sql/hostUpsert.sql b/src/sql/hostUpsert.sql new file mode 100644 index 0000000..2b1109c --- /dev/null +++ b/src/sql/hostUpsert.sql @@ -0,0 +1,7 @@ +INSERT INTO entities (id, type, value, detail) + VALUES ($1, 'host', $2, jsonb_build_object('sysname', $3::text, 'release', + $4::text, 'version', $5::text, 'machine', $6::text, 'domainname', $7::text)) +ON CONFLICT ON CONSTRAINT pk_entities + DO UPDATE SET + detail = jsonb_build_object('sysname', $3::text, 'release', + $4::text, 'version', $5::text, 'machine', $6::text, 'domainname', $7::text) diff --git a/src/webstat_logger_main.cpp b/src/webstat_logger_main.cpp index 7a47582..7817dc0 100644 --- a/src/webstat_logger_main.cpp +++ b/src/webstat_logger_main.cpp @@ -7,17 +7,14 @@ namespace { [[nodiscard]] - std::string - getHostname(bool fqdn) + utsname + getHostDetail() { 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; + return uts; } } @@ -60,6 +57,6 @@ main(int argc, char ** argv) po::notify(optVars); auto pool = std::make_shared<DB::ConnectionPool>(dbMax, dbKeep, std::move(dbType), std::move(dbConnStr)); - WebStat::Ingestor {getHostname(false), pool}.ingestLog(stdin); + WebStat::Ingestor {getHostDetail(), pool}.ingestLog(stdin); return EXIT_SUCCESS; } |