summaryrefslogtreecommitdiffstats
path: root/lib/livestatus/table.cpp
blob: 8e5d85ad23ae0bbeff0ab3ef94a346de4d421cd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "livestatus/table.hpp"
#include "livestatus/statustable.hpp"
#include "livestatus/contactgroupstable.hpp"
#include "livestatus/contactstable.hpp"
#include "livestatus/hostgroupstable.hpp"
#include "livestatus/hoststable.hpp"
#include "livestatus/servicegroupstable.hpp"
#include "livestatus/servicestable.hpp"
#include "livestatus/commandstable.hpp"
#include "livestatus/commentstable.hpp"
#include "livestatus/downtimestable.hpp"
#include "livestatus/endpointstable.hpp"
#include "livestatus/zonestable.hpp"
#include "livestatus/timeperiodstable.hpp"
#include "livestatus/logtable.hpp"
#include "livestatus/statehisttable.hpp"
#include "livestatus/filter.hpp"
#include "base/array.hpp"
#include "base/dictionary.hpp"
#include <boost/algorithm/string/case_conv.hpp>

using namespace icinga;

Table::Table(LivestatusGroupByType type)
	: m_GroupByType(type), m_GroupByObject(Empty)
{ }

Table::Ptr Table::GetByName(const String& name, const String& compat_log_path, const unsigned long& from, const unsigned long& until)
{
	if (name == "status")
		return new StatusTable();
	else if (name == "contactgroups")
		return new ContactGroupsTable();
	else if (name == "contacts")
		return new ContactsTable();
	else if (name == "hostgroups")
		return new HostGroupsTable();
	else if (name == "hosts")
		return new HostsTable();
	else if (name == "hostsbygroup")
		return new HostsTable(LivestatusGroupByHostGroup);
	else if (name == "servicegroups")
		return new ServiceGroupsTable();
	else if (name == "services")
		return new ServicesTable();
	else if (name == "servicesbygroup")
		return new ServicesTable(LivestatusGroupByServiceGroup);
	else if (name == "servicesbyhostgroup")
		return new ServicesTable(LivestatusGroupByHostGroup);
	else if (name == "commands")
		return new CommandsTable();
	else if (name == "comments")
		return new CommentsTable();
	else if (name == "downtimes")
		return new DowntimesTable();
	else if (name == "timeperiods")
		return new TimePeriodsTable();
	else if (name == "log")
		return new LogTable(compat_log_path, from, until);
	else if (name == "statehist")
		return new StateHistTable(compat_log_path, from, until);
	else if (name == "endpoints")
		return new EndpointsTable();
	else if (name == "zones")
		return new ZonesTable();

	return nullptr;
}

void Table::AddColumn(const String& name, const Column& column)
{
	std::pair<String, Column> item = std::make_pair(name, column);

	auto ret = m_Columns.insert(item);

	if (!ret.second)
		ret.first->second = column;
}

Column Table::GetColumn(const String& name) const
{
	String dname = name;
	String prefix = GetPrefix() + "_";

	if (dname.Find(prefix) == 0)
		dname = dname.SubStr(prefix.GetLength());

	auto it = m_Columns.find(dname);

	if (it == m_Columns.end())
		BOOST_THROW_EXCEPTION(std::invalid_argument("Column '" + dname + "' does not exist in table '" + GetName() + "'."));

	return it->second;
}

std::vector<String> Table::GetColumnNames() const
{
	std::vector<String> names;

	for (const auto& kv : m_Columns) {
		names.push_back(kv.first);
	}

	return names;
}

std::vector<LivestatusRowValue> Table::FilterRows(const Filter::Ptr& filter, int limit)
{
	std::vector<LivestatusRowValue> rs;

	FetchRows([this, filter, limit, &rs](const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject) {
		return FilteredAddRow(rs, filter, limit, row, groupByType, groupByObject);
	});

	return rs;
}

bool Table::FilteredAddRow(std::vector<LivestatusRowValue>& rs, const Filter::Ptr& filter, int limit, const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
{
	if (limit != -1 && static_cast<int>(rs.size()) == limit)
		return false;

	if (!filter || filter->Apply(this, row)) {
		LivestatusRowValue rval;
		rval.Row = row;
		rval.GroupByType = groupByType;
		rval.GroupByObject = groupByObject;

		rs.emplace_back(std::move(rval));
	}

	return true;
}

Value Table::ZeroAccessor(const Value&)
{
	return 0;
}

Value Table::OneAccessor(const Value&)
{
	return 1;
}

Value Table::EmptyStringAccessor(const Value&)
{
	return "";
}

Value Table::EmptyArrayAccessor(const Value&)
{
	return new Array();
}

Value Table::EmptyDictionaryAccessor(const Value&)
{
	return new Dictionary();
}

LivestatusGroupByType Table::GetGroupByType() const
{
	return m_GroupByType;
}