Skip to content
Permalink
Browse files

Add `crashreports` table

The table now has two columns: `id` and `component`.  The rows
return parts of a path that can be configured for both Nagios and CMC.

For now, this is not very useful but it works.

This is part 1 of CMK-853.

Change-Id: I573bf0f2565510cf77eaf28e391531602e7f9379
  • Loading branch information
Synss committed Oct 23, 2019
1 parent 95bd0a8 commit 4ec608c3fd06299534f4bf73ec91b65b6ba73eb3
@@ -1,3 +1,4 @@
.syntastic
*.gcno
*.pyc
*.py.*.py
@@ -0,0 +1,10 @@
#include "CrashReport.h"
#include <string>
#include <utility>

CrashReport::CrashReport(std::string id, std::string component)
: _id(std::move(id)), _component(std::move(component)) {}

std::string CrashReport::id() const { return _id; }

std::string CrashReport::component() const { return _component; }
@@ -0,0 +1,21 @@
#ifndef CrashReport_h
#define CrashReport_h

#include <string>

class TableCrashReports;

class CrashReport {
friend TableCrashReports;

public:
CrashReport(std::string id, std::string component);
[[nodiscard]] std::string id() const;
[[nodiscard]] std::string component() const;

private:
const std::string _id;
const std::string _component;
};

#endif
@@ -62,6 +62,7 @@ unixcat_LDADD = -lpthread
test_neb_SOURCES = \
test/DummyNagios.cc \
test/test_utilities.cc \
test/test_CrashReport.cc \
test/test_CustomVarsDictFilter.cc \
test/test_OffsetStringMacroColumn.cc
$(test_neb_SOURCES): $(ASIO_INCLUDE) $(GOOGLETEST_INCLUDE)
@@ -96,6 +97,7 @@ liblivestatus_a_SOURCES = \
ContactGroupsColumn.cc \
ContactGroupsMemberColumn.cc \
CountAggregator.cc \
CrashReport.cc \
CustomTimeperiodColumn.cc \
CustomVarsDictColumn.cc \
CustomVarsDictFilter.cc \
@@ -173,6 +175,7 @@ liblivestatus_a_SOURCES = \
TableComments.cc \
TableContactGroups.cc \
TableContacts.cc \
TableCrashReports.cc \
TableDowntimes.cc \
TableEventConsole.cc \
TableEventConsoleEvents.cc \
@@ -27,6 +27,7 @@

#include "config.h" // IWYU pragma: keep
#include <chrono>
#include <filesystem>
#include <string>
#include <tuple>
#include <unordered_map>
@@ -123,6 +124,7 @@ class MonitoringCore {
virtual std::string mkLogwatchPath() = 0;
virtual std::string mkInventoryPath() = 0;
virtual std::string structuredStatusPath() = 0;
virtual std::filesystem::path crashReportPath() = 0;
virtual std::string pnpPath() = 0;
virtual std::string historyFilePath() = 0;
virtual std::string logArchivePath() = 0;
@@ -26,6 +26,7 @@
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <filesystem>
#include <memory>
#include <ostream>
#include <utility>
@@ -168,6 +169,9 @@ std::string NagiosCore::mkInventoryPath() { return _paths._mk_inventory; }
std::string NagiosCore::structuredStatusPath() {
return _paths._structured_status;
}
std::filesystem::path NagiosCore::crashReportPath() {
return _paths._crash_reports_path;
}
std::string NagiosCore::pnpPath() { return _paths._pnp; }
std::string NagiosCore::historyFilePath() {
extern char *log_file;
@@ -28,6 +28,7 @@
#include "config.h" // IWYU pragma: keep
#include <chrono>
#include <cstddef>
#include <filesystem>
#include <string>
#include <unordered_map>
#include <vector>
@@ -47,6 +48,7 @@ struct NagiosPaths {
std::string _pnp;
std::string _mk_inventory;
std::string _structured_status;
std::filesystem::path _crash_reports_path;
std::string _mk_logwatch;
std::string _logfile;
std::string _mkeventd_socket;
@@ -102,6 +104,7 @@ class NagiosCore : public MonitoringCore {
std::string mkLogwatchPath() override;
std::string mkInventoryPath() override;
std::string structuredStatusPath() override;
std::filesystem::path crashReportPath() override;
std::string pnpPath() override;
std::string historyFilePath() override;
std::string logArchivePath() override;
@@ -50,6 +50,7 @@ Store::Store(MonitoringCore *mc)
, _table_comments(mc)
, _table_contactgroups(mc)
, _table_contacts(mc)
, _table_crash_reports(mc)
, _table_downtimes(mc)
, _table_eventconsoleevents(mc)
, _table_eventconsolehistory(mc)
@@ -73,6 +74,7 @@ Store::Store(MonitoringCore *mc)
addTable(_table_comments);
addTable(_table_contactgroups);
addTable(_table_contacts);
addTable(_table_crash_reports);
addTable(_table_downtimes);
addTable(_table_hostgroups);
addTable(_table_hostsbygroup);
@@ -41,6 +41,7 @@
#include "TableComments.h"
#include "TableContactGroups.h"
#include "TableContacts.h"
#include "TableCrashReports.h"
#include "TableDowntimes.h"
#include "TableEventConsoleEvents.h"
#include "TableEventConsoleHistory.h"
@@ -134,6 +135,7 @@ class Store {
TableComments _table_comments;
TableContactGroups _table_contactgroups;
TableContacts _table_contacts;
TableCrashReports _table_crash_reports;
TableDowntimes _table_downtimes;
TableEventConsoleEvents _table_eventconsoleevents;
TableEventConsoleHistory _table_eventconsolehistory;
@@ -0,0 +1,30 @@
#include "TableCrashReports.h"
#include <filesystem>
#include <memory>
#include <string>
#include "Column.h"
#include "CrashReport.h"
#include "MonitoringCore.h"
#include "OffsetSStringColumn.h"
#include "Query.h"
#include "Row.h"

TableCrashReports::TableCrashReports(MonitoringCore *mc) : Table(mc) {
addColumn(std::make_unique<OffsetSStringColumn>(
"id", "The ID of a crash report", -1, -1, -1,
DANGEROUS_OFFSETOF(CrashReport, _id)));
addColumn(std::make_unique<OffsetSStringColumn>(
"component", "The component that crashed (gui, agent, check, etc.)", -1,
-1, -1, DANGEROUS_OFFSETOF(CrashReport, _component)));
}

TableCrashReports::~TableCrashReports() = default;

std::string TableCrashReports::name() const { return "crashreports"; }

std::string TableCrashReports::namePrefix() const { return "crashreport_"; }

void TableCrashReports::answerQuery(Query *query) {
CrashReport c{core()->crashReportPath().string(), ""};
query->processDataset(Row(&c));
}
@@ -0,0 +1,20 @@
#ifndef TableCrashReports_h
#define TableCrashReports_h

#include "config.h" // IWYU pragma: keep
#include <string>
#include "Table.h"

class MonitoringCore;
class Query;

class TableCrashReports : public Table {
public:
explicit TableCrashReports(MonitoringCore *mc);
~TableCrashReports();
std::string name() const final;
std::string namePrefix() const final;
void answerQuery(Query *) final;
};

#endif // TableCrashReports_h
@@ -0,0 +1,51 @@
#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>
#include "CrashReport.h"
#include "gtest/gtest.h"

namespace fs = std::filesystem;

class CrashReportTest : public ::testing::Test {
protected:
CrashReportTest() = default;

// Accessors in fixtures make clang tidy happy :-)
[[nodiscard]] fs::path basepath() const { return basepath_; }
[[nodiscard]] fs::path uuid() const { return uuid_; }
[[nodiscard]] fs::path component() const { return component_; }
[[nodiscard]] fs::path crash_info() const { return crash_info_; }
[[nodiscard]] fs::path json() const { return json_; }
[[nodiscard]] fs::path fullpath() const {
return basepath() / component() / uuid() / crash_info();
}

void SetUp() override {
fs::create_directories(fullpath().parent_path());
std::ofstream ofs(fullpath());
ofs << json();
ofs.close();
}
void TearDown() override { fs::remove_all(basepath_); }

private:
fs::path basepath_ = fs::temp_directory_path() / "crash_report_tests";
// This could use `mkstemp()` instead of fixed strings.
const fs::path uuid_{"8966a88e-e369-11e9-981a-acbc328d0e0b"};
const fs::path component_{"gui"};
const fs::path crash_info_{"crash.info"};
const std::string json_{"{}\n"};
};

TEST_F(CrashReportTest, DirectoryAndFileExist) {
ASSERT_TRUE(fs::exists(fullpath()));
EXPECT_TRUE(fs::is_regular_file(fullpath()));
}

TEST_F(CrashReportTest, AccessorsAreCorrect) {
ASSERT_TRUE(fs::exists(fullpath()));
CrashReport cr{uuid(), component()};
EXPECT_EQ(cr.id(), uuid());
EXPECT_EQ(cr.component(), component());
}

0 comments on commit 4ec608c

Please sign in to comment.
You can’t perform that action at this time.