summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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
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;
}