Module: check_mk
Branch: master
Commit: c68acf7cd2c1c509586b623dfa35c557004c5336
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c68acf7cd2c1c5…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Wed Oct 25 13:15:09 2017 +0200
Made LogwatchListColumn a ListColumn.
This unifies things and enables the use of our filtering machinery on this
column type (which was previously not possible).
Change-Id: I003d046d1f3aa549152dc1f1a0a49cdf9222662c
---
livestatus/src/LogwatchListColumn.cc | 61 +++++++++++++++++++++++-------------
livestatus/src/LogwatchListColumn.h | 17 +++++++---
2 files changed, 52 insertions(+), 26 deletions(-)
diff --git a/livestatus/src/LogwatchListColumn.cc b/livestatus/src/LogwatchListColumn.cc
index 18a8f10..c60d473 100644
--- a/livestatus/src/LogwatchListColumn.cc
+++ b/livestatus/src/LogwatchListColumn.cc
@@ -23,6 +23,8 @@
// Boston, MA 02110-1301 USA.
#include "LogwatchListColumn.h"
+#include <algorithm>
+#include <iterator>
#include <ostream>
#include "FileSystem.h"
#include "Logger.h"
@@ -38,35 +40,52 @@
#endif
void LogwatchListColumn::output(Row row, RowRenderer &r,
- const contact * /* auth_user */) const {
+ const contact *auth_user) const {
ListRenderer l(r);
- auto logwatch_path = _mc->mkLogwatchPath();
- if (logwatch_path.empty()) {
- return;
+ for (const auto &filename : getValue(row, auth_user)) {
+ l.output(filename);
}
+}
-#ifdef CMC
- auto hst = columnData<Host>(row);
- if (hst == nullptr) {
- return;
- }
- std::string host_name = hst->name();
-#else
- auto hst = columnData<host>(row);
- if (hst == nullptr) {
- return;
+std::vector<std::string> LogwatchListColumn::getValue(
+ Row row, const contact * /*auth_user*/) const {
+ auto dir = getDirectory(row);
+ if (dir.empty()) {
+ return {};
}
- std::string host_name = hst->name;
-#endif
-
- auto dir = logwatch_path + pnp_cleanup(host_name);
try {
if (fs::exists(dir)) {
- for (const auto &entry : fs::directory_iterator(dir)) {
- l.output(entry.path().filename().string());
- }
+ std::vector<std::string> filenames;
+ auto it = fs::directory_iterator(dir);
+ std::transform(begin(it), end(it), std::back_inserter(filenames),
+ [](const auto &entry) {
+ return entry.path().filename().string();
+ });
+ return filenames;
}
} catch (const fs::filesystem_error &e) {
Warning(logger()) << name() << ": " << e.what();
}
+ return {};
+}
+
+std::string LogwatchListColumn::getDirectory(Row row) const {
+ auto logwatch_path = _mc->mkLogwatchPath();
+ auto host_name = getHostName(row);
+ return logwatch_path.empty() || host_name.empty()
+ ? ""
+ : logwatch_path + pnp_cleanup(host_name);
+}
+
+std::string LogwatchListColumn::getHostName(Row row) const {
+#ifdef CMC
+ if (auto hst = columnData<Host>(row)) {
+ return hst->name();
+ }
+#else
+ if (auto hst = columnData<host>(row)) {
+ return hst->name;
+ }
+#endif
+ return "";
}
diff --git a/livestatus/src/LogwatchListColumn.h b/livestatus/src/LogwatchListColumn.h
index 42c7aeb..67705de 100644
--- a/livestatus/src/LogwatchListColumn.h
+++ b/livestatus/src/LogwatchListColumn.h
@@ -27,26 +27,33 @@
#include "config.h" // IWYU pragma: keep
#include <string>
-#include "Column.h"
+#include <vector>
+#include "ListColumn.h"
#include "contact_fwd.h"
class MonitoringCore;
class Row;
class RowRenderer;
-class LogwatchListColumn : public Column {
+class LogwatchListColumn : public ListColumn {
public:
LogwatchListColumn(const std::string &name, const std::string &description,
int indirect_offset, int extra_offset,
int extra_extra_offset, int offset, MonitoringCore *mc)
- : Column(name, description, indirect_offset, extra_offset,
- extra_extra_offset, offset)
+ : ListColumn(name, description, indirect_offset, extra_offset,
+ extra_extra_offset, offset)
, _mc(mc) {}
- ColumnType type() const override { return ColumnType::list; }
+
void output(Row row, RowRenderer &r,
const contact *auth_user) const override;
+ std::vector<std::string> getValue(Row row,
+ const contact *auth_user) const override;
+
private:
MonitoringCore *_mc;
+
+ std::string getDirectory(Row row) const;
+ std::string getHostName(Row row) const;
};
#endif // LogwatchListColumn_h