]> granicus.if.org Git - icinga2/blob - lib/livestatus/table.cpp
Merge pull request #7001 from Icinga/bugfix/doc-assignment-5430
[icinga2] / lib / livestatus / table.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "livestatus/table.hpp"
4 #include "livestatus/statustable.hpp"
5 #include "livestatus/contactgroupstable.hpp"
6 #include "livestatus/contactstable.hpp"
7 #include "livestatus/hostgroupstable.hpp"
8 #include "livestatus/hoststable.hpp"
9 #include "livestatus/servicegroupstable.hpp"
10 #include "livestatus/servicestable.hpp"
11 #include "livestatus/commandstable.hpp"
12 #include "livestatus/commentstable.hpp"
13 #include "livestatus/downtimestable.hpp"
14 #include "livestatus/endpointstable.hpp"
15 #include "livestatus/zonestable.hpp"
16 #include "livestatus/timeperiodstable.hpp"
17 #include "livestatus/logtable.hpp"
18 #include "livestatus/statehisttable.hpp"
19 #include "livestatus/filter.hpp"
20 #include "base/array.hpp"
21 #include "base/dictionary.hpp"
22 #include <boost/algorithm/string/case_conv.hpp>
23 #include <boost/tuple/tuple.hpp>
24
25 using namespace icinga;
26
27 Table::Table(LivestatusGroupByType type)
28         : m_GroupByType(type), m_GroupByObject(Empty)
29 { }
30
31 Table::Ptr Table::GetByName(const String& name, const String& compat_log_path, const unsigned long& from, const unsigned long& until)
32 {
33         if (name == "status")
34                 return new StatusTable();
35         else if (name == "contactgroups")
36                 return new ContactGroupsTable();
37         else if (name == "contacts")
38                 return new ContactsTable();
39         else if (name == "hostgroups")
40                 return new HostGroupsTable();
41         else if (name == "hosts")
42                 return new HostsTable();
43         else if (name == "hostsbygroup")
44                 return new HostsTable(LivestatusGroupByHostGroup);
45         else if (name == "servicegroups")
46                 return new ServiceGroupsTable();
47         else if (name == "services")
48                 return new ServicesTable();
49         else if (name == "servicesbygroup")
50                 return new ServicesTable(LivestatusGroupByServiceGroup);
51         else if (name == "servicesbyhostgroup")
52                 return new ServicesTable(LivestatusGroupByHostGroup);
53         else if (name == "commands")
54                 return new CommandsTable();
55         else if (name == "comments")
56                 return new CommentsTable();
57         else if (name == "downtimes")
58                 return new DowntimesTable();
59         else if (name == "timeperiods")
60                 return new TimePeriodsTable();
61         else if (name == "log")
62                 return new LogTable(compat_log_path, from, until);
63         else if (name == "statehist")
64                 return new StateHistTable(compat_log_path, from, until);
65         else if (name == "endpoints")
66                 return new EndpointsTable();
67         else if (name == "zones")
68                 return new ZonesTable();
69
70         return nullptr;
71 }
72
73 void Table::AddColumn(const String& name, const Column& column)
74 {
75         std::pair<String, Column> item = std::make_pair(name, column);
76
77         auto ret = m_Columns.insert(item);
78
79         if (!ret.second)
80                 ret.first->second = column;
81 }
82
83 Column Table::GetColumn(const String& name) const
84 {
85         String dname = name;
86         String prefix = GetPrefix() + "_";
87
88         if (dname.Find(prefix) == 0)
89                 dname = dname.SubStr(prefix.GetLength());
90
91         auto it = m_Columns.find(dname);
92
93         if (it == m_Columns.end())
94                 BOOST_THROW_EXCEPTION(std::invalid_argument("Column '" + dname + "' does not exist in table '" + GetName() + "'."));
95
96         return it->second;
97 }
98
99 std::vector<String> Table::GetColumnNames() const
100 {
101         std::vector<String> names;
102
103         for (const auto& kv : m_Columns) {
104                 names.push_back(kv.first);
105         }
106
107         return names;
108 }
109
110 std::vector<LivestatusRowValue> Table::FilterRows(const Filter::Ptr& filter, int limit)
111 {
112         std::vector<LivestatusRowValue> rs;
113
114         FetchRows(std::bind(&Table::FilteredAddRow, this, std::ref(rs), filter, limit, _1, _2, _3));
115
116         return rs;
117 }
118
119 bool Table::FilteredAddRow(std::vector<LivestatusRowValue>& rs, const Filter::Ptr& filter, int limit, const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
120 {
121         if (limit != -1 && static_cast<int>(rs.size()) == limit)
122                 return false;
123
124         if (!filter || filter->Apply(this, row)) {
125                 LivestatusRowValue rval;
126                 rval.Row = row;
127                 rval.GroupByType = groupByType;
128                 rval.GroupByObject = groupByObject;
129
130                 rs.emplace_back(std::move(rval));
131         }
132
133         return true;
134 }
135
136 Value Table::ZeroAccessor(const Value&)
137 {
138         return 0;
139 }
140
141 Value Table::OneAccessor(const Value&)
142 {
143         return 1;
144 }
145
146 Value Table::EmptyStringAccessor(const Value&)
147 {
148         return "";
149 }
150
151 Value Table::EmptyArrayAccessor(const Value&)
152 {
153         return new Array();
154 }
155
156 Value Table::EmptyDictionaryAccessor(const Value&)
157 {
158         return new Dictionary();
159 }
160
161 LivestatusGroupByType Table::GetGroupByType() const
162 {
163         return m_GroupByType;
164 }