Refactoring: Changed all check parameters starting with a 'p' to the new rulespec...
[check_mk.git] / livestatus / src / Table.cc
blob3f5a946677c6509ca19738aaff4dcfbe6e536519
1 // +------------------------------------------------------------------+
2 // | ____ _ _ __ __ _ __ |
3 // | / ___| |__ ___ ___| | __ | \/ | |/ / |
4 // | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
5 // | | |___| | | | __/ (__| < | | | | . \ |
6 // | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
7 // | |
8 // | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
9 // +------------------------------------------------------------------+
11 // This file is part of Check_MK.
12 // The official homepage is at http://mathias-kettner.de/check_mk.
14 // check_mk is free software; you can redistribute it and/or modify it
15 // under the terms of the GNU General Public License as published by
16 // the Free Software Foundation in version 2. check_mk is distributed
17 // in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
18 // out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 // PARTICULAR PURPOSE. See the GNU General Public License for more de-
20 // tails. You should have received a copy of the GNU General Public
21 // License along with GNU Make; see the file COPYING. If not, write
22 // to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 // Boston, MA 02110-1301 USA.
25 #include "Table.h"
26 #include <stdexcept>
27 #include "Column.h"
28 #include "DynamicColumn.h"
29 #include "MonitoringCore.h"
30 #include "StringUtils.h"
31 #include "nagios.h"
33 Table::Table(MonitoringCore *mc) : _mc(mc) {}
35 Table::~Table() = default;
37 void Table::addColumn(std::unique_ptr<Column> col) {
38 _columns.emplace(col->name(), std::move(col));
41 void Table::addDynamicColumn(std::unique_ptr<DynamicColumn> dyncol) {
42 _dynamic_columns.emplace(dyncol->name(), std::move(dyncol));
45 std::shared_ptr<Column> Table::column(std::string colname) const {
46 // Strip away a sequence of prefixes.
47 while (mk::starts_with(colname, namePrefix())) {
48 colname = colname.substr(namePrefix().size());
51 auto sep = colname.find(':');
52 if (sep != std::string::npos) {
53 // TODO(sp) Use shared_ptr
54 return dynamicColumn(colname.substr(0, sep), colname.substr(sep + 1));
57 // First try exact match...
58 auto it = _columns.find(colname);
59 if (it != _columns.end()) {
60 return it->second;
63 // ... then try to match with the prefix.
64 it = _columns.find(namePrefix() + colname);
65 if (it != _columns.end()) {
66 return it->second;
69 throw std::runtime_error("table '" + name() + "' has no column '" +
70 colname + "'");
73 std::unique_ptr<Column> Table::dynamicColumn(const std::string &colname,
74 const std::string &rest) const {
75 auto it = _dynamic_columns.find(colname);
76 if (it == _dynamic_columns.end()) {
77 throw std::runtime_error("table '" + name() +
78 "' has no dynamic column '" + colname + "'");
80 auto sep_pos = rest.find(':');
81 if (sep_pos == std::string::npos) {
82 throw std::runtime_error("missing separator in dynamic column '" +
83 colname + "'");
85 std::string colname2 = rest.substr(0, sep_pos);
86 if (colname2.empty()) {
87 throw std::runtime_error("empty column name for dynamic column '" +
88 colname + "'");
90 return it->second->createColumn(colname2, rest.substr(sep_pos + 1));
93 bool Table::isAuthorized(Row /*unused*/, const contact * /*unused*/) const {
94 return true;
97 Row Table::findObject(const std::string & /*unused*/) const {
98 return Row(nullptr);
101 Logger *Table::logger() const { return _mc->loggerLivestatus(); }