summaryrefslogtreecommitdiffstats
path: root/lib/livestatus/zonestable.cpp
blob: d86cc7256dfb2106504088d7a385b2c8967ea97c (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
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "livestatus/zonestable.hpp"
#include "remote/zone.hpp"
#include "base/configtype.hpp"

using namespace icinga;

ZonesTable::ZonesTable()
{
	AddColumns(this);
}

void ZonesTable::AddColumns(Table *table, const String& prefix,
	const Column::ObjectAccessor& objectAccessor)
{
	table->AddColumn(prefix + "name", Column(&ZonesTable::NameAccessor, objectAccessor));
	table->AddColumn(prefix + "parent", Column(&ZonesTable::ParentAccessor, objectAccessor));
	table->AddColumn(prefix + "endpoints", Column(&ZonesTable::EndpointsAccessor, objectAccessor));
	table->AddColumn(prefix + "global", Column(&ZonesTable::GlobalAccessor, objectAccessor));
}

String ZonesTable::GetName() const
{
	return "zones";
}

String ZonesTable::GetPrefix() const
{
	return "zone";
}

void ZonesTable::FetchRows(const AddRowFunction& addRowFn)
{
	for (const Zone::Ptr& zone : ConfigType::GetObjectsByType<Zone>()) {
		if (!addRowFn(zone, LivestatusGroupByNone, Empty))
			return;
	}
}

Value ZonesTable::NameAccessor(const Value& row)
{
	Zone::Ptr zone = static_cast<Zone::Ptr>(row);

	if (!zone)
		return Empty;

	return zone->GetName();
}

Value ZonesTable::ParentAccessor(const Value& row)
{
	Zone::Ptr zone = static_cast<Zone::Ptr>(row);

	if (!zone)
		return Empty;

	Zone::Ptr parent_zone = zone->GetParent();

	if (!parent_zone)
		return Empty;

	return parent_zone->GetName();
}

Value ZonesTable::EndpointsAccessor(const Value& row)
{
	Zone::Ptr zone = static_cast<Zone::Ptr>(row);

	if (!zone)
		return Empty;

	std::set<Endpoint::Ptr> endpoints = zone->GetEndpoints();

	ArrayData result;

	for (const Endpoint::Ptr& endpoint : endpoints) {
		result.push_back(endpoint->GetName());
	}

	return new Array(std::move(result));
}

Value ZonesTable::GlobalAccessor(const Value& row)
{
	Zone::Ptr zone = static_cast<Zone::Ptr>(row);

	if (!zone)
		return Empty;

	return zone->GetGlobal() ? 1 : 0;
}