summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ingestor.cpp13
-rw-r--r--src/ingestor.hpp3
-rw-r--r--src/sql.cpp4
-rw-r--r--src/sql.hpp1
-rw-r--r--src/sql/hostUpsert.sql7
-rw-r--r--src/webstat_logger_main.cpp11
-rw-r--r--test/perf-ingest.cpp3
-rw-r--r--test/test-ingest.cpp2
-rw-r--r--test/test-util.cpp8
-rw-r--r--test/test-util.hpp3
10 files changed, 39 insertions, 16 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;
}
diff --git a/test/perf-ingest.cpp b/test/perf-ingest.cpp
index 36a3b49..ed87192 100644
--- a/test/perf-ingest.cpp
+++ b/test/perf-ingest.cpp
@@ -102,7 +102,8 @@ namespace {
void
doIngestFile(benchmark::State & state)
{
- WebStat::Ingestor ingestor {"perf-hostname", std::make_shared<WebStat::MockDBPool>("webstat")};
+ WebStat::Ingestor ingestor {
+ WebStat::getTestUtsName("perf-hostname"), std::make_shared<WebStat::MockDBPool>("webstat")};
for (auto loop : state) {
WebStat::FilePtr logFile {fopen(TMP_LOG.c_str(), "r")};
ingestor.ingestLog(logFile.get());
diff --git a/test/test-ingest.cpp b/test/test-ingest.cpp
index fb35915..5f29806 100644
--- a/test/test-ingest.cpp
+++ b/test/test-ingest.cpp
@@ -178,6 +178,6 @@ BOOST_DATA_TEST_CASE(StoreLogLine,
}),
line)
{
- WebStat::Ingestor {"test-hostname", std::make_shared<MockDBPool>("webstat")}.ingestLogLine(
+ WebStat::Ingestor {WebStat::getTestUtsName("test-hostname"), std::make_shared<MockDBPool>("webstat")}.ingestLogLine(
DB::MockDatabase::openConnectionTo("webstat").get(), line);
}
diff --git a/test/test-util.cpp b/test/test-util.cpp
index 4648498..b9011a4 100644
--- a/test/test-util.cpp
+++ b/test/test-util.cpp
@@ -11,4 +11,12 @@ namespace WebStat {
{
return DB::MockDatabase::openConnectionTo(name);
}
+
+ utsname
+ getTestUtsName(const std::string_view nodename)
+ {
+ utsname uts {};
+ nodename.copy(uts.nodename, sizeof(uts.nodename));
+ return uts;
+ }
}
diff --git a/test/test-util.hpp b/test/test-util.hpp
index 5b2a968..3338c5e 100644
--- a/test/test-util.hpp
+++ b/test/test-util.hpp
@@ -3,6 +3,7 @@
#include <connectionPool.h>
#include <filesystem>
#include <pq-mock.h>
+#include <sys/utsname.h>
namespace WebStat {
#define XSTR(s) STR(s)
@@ -33,4 +34,6 @@ namespace WebStat {
};
template<typename Out> using ParseData = std::tuple<std::string_view, Out>;
+
+ utsname getTestUtsName(std::string_view);
}