Refactoring: Changed all check parameters starting with a 'p' to the new rulespec...
[check_mk.git] / livestatus / src / Table.h
blobdd9dcc3ce59c2cd60ca39a7da34b33a85eeb47fc
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 #ifndef Table_h
26 #define Table_h
28 #include "config.h" // IWYU pragma: keep
29 #include <map>
30 #include <memory>
31 #include <string>
32 #include <utility>
33 #include "Row.h"
34 #include "contact_fwd.h"
35 class Column;
36 class DynamicColumn;
37 class Logger;
38 class MonitoringCore;
39 class Query;
41 // NOTE: This macro leads to undefined behaviour for non-POD/non-standard-layout
42 // classes, e.g. Entity, Host, etc., nevertheless we have to use it below. :-/
43 #define DANGEROUS_OFFSETOF(typename, member) \
44 (reinterpret_cast<size_t>(&(reinterpret_cast<typename *>(32))->member) - 32)
46 /// A table-like view for some underlying data, exposed via LQL.
47 class Table {
48 public:
49 explicit Table(MonitoringCore *mc);
50 virtual ~Table();
52 void addColumn(std::unique_ptr<Column> col);
53 void addDynamicColumn(std::unique_ptr<DynamicColumn> dyncol);
55 template <typename Predicate>
56 bool any_column(Predicate pred) const {
57 for (auto &c : _columns) {
58 if (pred(c.second)) {
59 return true;
62 return false;
65 /// The name of the table, as used in the GET command.
66 virtual std::string name() const = 0;
68 /// \brief An optional prefix for column names.
69 ///
70 /// \todo Due to the way multisite works, column names are sometimes
71 /// prefixed by a variation of the table name (e.g. "hosts" => "host_"), but
72 /// the logic for this really shouldn't live on the cmc side. Furthermore,
73 /// multisite sometimes even seems to use a *sequence* of prefixes, which is
74 /// yet another a bug. Instead of fixing it there, it is currently papered
75 /// over on the cmc side. :-/
76 virtual std::string namePrefix() const = 0;
78 /// \brief Retrieve a column with a give name.
79 ///
80 /// If the name contains a ':' then we have a dynamic column with column
81 /// arguments: The part before the colon is the column name of the dynamic
82 /// column and the part after it is the name of the fresh, dynamically
83 /// created column (up to the 2nd ':') and further arguments. This whole
84 /// mechanism is e.g. used to access RRD metrics data.
85 ///
86 /// \todo This member function is virtual just because TableStateHistory and
87 /// TableLog override it for some dubious reason: They first try the normal
88 /// lookup, and if that didn't find a column, the lookup is retried with a
89 /// "current_" prefix. This logic should probably not live in cmc at all.
90 virtual std::shared_ptr<Column> column(std::string colname) const;
92 // NOTE: We can't make the query argument 'const' right now, because we call
93 // the non-const Query::processDataset() member function on it. This is a
94 // bit ugly, but only a minor issue: Each Query instance is only accessed in
95 // the thread which created it. Splitting up the Query class into a const
96 // and a non-const part can probably fix that wart.
98 // A much bigger problem is that we can't make answerQuery() itself 'const',
99 // because its impementations in TableStateHistory and TableCachedStatehist
100 // are non-const. Tables are shared between threads and the locking in the
101 // problematic answerQuery() implementations is a "bit" chaotic, so this can
102 // be a real correctness problem! This has to be fixed...
103 virtual void answerQuery(Query *query) = 0;
104 virtual bool isAuthorized(Row row, const contact *ctc) const;
105 virtual Row findObject(const std::string &objectspec) const;
107 template <typename T>
108 const T *rowData(Row row) const {
109 return row.rawData<T>();
112 MonitoringCore *core() const { return _mc; }
113 Logger *logger() const;
115 private:
116 MonitoringCore *_mc;
118 std::unique_ptr<Column> dynamicColumn(const std::string &colname,
119 const std::string &rest) const;
121 std::map<std::string, std::shared_ptr<Column>> _columns;
122 std::map<std::string, std::unique_ptr<DynamicColumn>> _dynamic_columns;
125 #endif // Table_h