Refactoring: Changed all check parameters starting with a 'p' to the new rulespec...
[check_mk.git] / livestatus / src / TableHostGroups.cc
blobf668aea083b6cb5b40932f3547cac2e8605148cc
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 "TableHostGroups.h"
26 #include <memory>
27 #include "Column.h"
28 #include "HostListColumn.h"
29 #include "HostListStateColumn.h"
30 #include "OffsetStringColumn.h"
31 #include "Query.h"
32 #include "auth.h"
33 #include "nagios.h"
35 /* this might be a hack (accessing Nagios' internal structures.
36 Hi Ethan: please help me here: how should this be code to be
37 portable? */
38 extern hostgroup *hostgroup_list;
40 TableHostGroups::TableHostGroups(MonitoringCore *mc) : Table(mc) {
41 addColumns(this, "", -1);
44 std::string TableHostGroups::name() const { return "hostgroups"; }
46 std::string TableHostGroups::namePrefix() const { return "hostgroup_"; }
48 // static
49 void TableHostGroups::addColumns(Table *table, const std::string &prefix,
50 int indirect_offset) {
51 table->addColumn(std::make_unique<OffsetStringColumn>(
52 prefix + "name", "Name of the hostgroup", indirect_offset, -1, -1,
53 DANGEROUS_OFFSETOF(hostgroup, group_name)));
54 table->addColumn(std::make_unique<OffsetStringColumn>(
55 prefix + "alias", "An alias of the hostgroup", indirect_offset, -1, -1,
56 DANGEROUS_OFFSETOF(hostgroup, alias)));
57 table->addColumn(std::make_unique<OffsetStringColumn>(
58 prefix + "notes", "Optional notes to the hostgroup", indirect_offset,
59 -1, -1, DANGEROUS_OFFSETOF(hostgroup, notes)));
60 table->addColumn(std::make_unique<OffsetStringColumn>(
61 prefix + "notes_url",
62 "An optional URL with further information about the hostgroup",
63 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, notes_url)));
64 table->addColumn(std::make_unique<OffsetStringColumn>(
65 prefix + "action_url",
66 "An optional URL to custom actions or information about the hostgroup",
67 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, action_url)));
68 table->addColumn(std::make_unique<HostListColumn>(
69 prefix + "members",
70 "A list of all host names that are members of the hostgroup",
71 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
72 table->core(), false));
73 table->addColumn(std::make_unique<HostListColumn>(
74 prefix + "members_with_state",
75 "A list of all host names that are members of the hostgroup together with state and has_been_checked",
76 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
77 table->core(), true));
79 table->addColumn(std::make_unique<HostListStateColumn>(
80 prefix + "worst_host_state",
81 "The worst state of all of the groups' hosts (UP <= UNREACHABLE <= DOWN)",
82 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
83 table->core(), HostListStateColumn::Type::worst_hst_state));
84 table->addColumn(std::make_unique<HostListStateColumn>(
85 prefix + "num_hosts", "The total number of hosts in the group",
86 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
87 table->core(), HostListStateColumn::Type::num_hst));
88 table->addColumn(std::make_unique<HostListStateColumn>(
89 prefix + "num_hosts_pending",
90 "The number of hosts in the group that are pending", indirect_offset,
91 -1, -1, DANGEROUS_OFFSETOF(hostgroup, members), table->core(),
92 HostListStateColumn::Type::num_hst_pending));
93 table->addColumn(std::make_unique<HostListStateColumn>(
94 prefix + "num_hosts_up", "The number of hosts in the group that are up",
95 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
96 table->core(), HostListStateColumn::Type::num_hst_up));
97 table->addColumn(std::make_unique<HostListStateColumn>(
98 prefix + "num_hosts_down",
99 "The number of hosts in the group that are down", indirect_offset, -1,
100 -1, DANGEROUS_OFFSETOF(hostgroup, members), table->core(),
101 HostListStateColumn::Type::num_hst_down));
102 table->addColumn(std::make_unique<HostListStateColumn>(
103 prefix + "num_hosts_unreach",
104 "The number of hosts in the group that are unreachable",
105 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
106 table->core(), HostListStateColumn::Type::num_hst_unreach));
108 table->addColumn(std::make_unique<HostListStateColumn>(
109 prefix + "num_services",
110 "The total number of services of hosts in this group", indirect_offset,
111 -1, -1, DANGEROUS_OFFSETOF(hostgroup, members), table->core(),
112 HostListStateColumn::Type::num_svc));
114 // soft states
115 table->addColumn(std::make_unique<HostListStateColumn>(
116 prefix + "worst_service_state",
117 "The worst state of all services that belong to a host of this group (OK <= WARN <= UNKNOWN <= CRIT)",
118 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
119 table->core(), HostListStateColumn::Type::worst_svc_state));
120 table->addColumn(std::make_unique<HostListStateColumn>(
121 prefix + "num_services_pending",
122 "The total number of services with the state Pending of hosts in this group",
123 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
124 table->core(), HostListStateColumn::Type::num_svc_pending));
125 table->addColumn(std::make_unique<HostListStateColumn>(
126 prefix + "num_services_ok",
127 "The total number of services with the state OK of hosts in this group",
128 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
129 table->core(), HostListStateColumn::Type::num_svc_ok));
130 table->addColumn(std::make_unique<HostListStateColumn>(
131 prefix + "num_services_warn",
132 "The total number of services with the state WARN of hosts in this group",
133 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
134 table->core(), HostListStateColumn::Type::num_svc_warn));
135 table->addColumn(std::make_unique<HostListStateColumn>(
136 prefix + "num_services_crit",
137 "The total number of services with the state CRIT of hosts in this group",
138 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
139 table->core(), HostListStateColumn::Type::num_svc_crit));
140 table->addColumn(std::make_unique<HostListStateColumn>(
141 prefix + "num_services_unknown",
142 "The total number of services with the state UNKNOWN of hosts in this group",
143 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
144 table->core(), HostListStateColumn::Type::num_svc_unknown));
146 // hard state
147 table->addColumn(std::make_unique<HostListStateColumn>(
148 prefix + "worst_service_hard_state",
149 "The worst state of all services that belong to a host of this group (OK <= WARN <= UNKNOWN <= CRIT)",
150 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
151 table->core(), HostListStateColumn::Type::worst_svc_hard_state));
152 table->addColumn(std::make_unique<HostListStateColumn>(
153 prefix + "num_services_hard_ok",
154 "The total number of services with the state OK of hosts in this group",
155 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
156 table->core(), HostListStateColumn::Type::num_svc_hard_ok));
157 table->addColumn(std::make_unique<HostListStateColumn>(
158 prefix + "num_services_hard_warn",
159 "The total number of services with the state WARN of hosts in this group",
160 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
161 table->core(), HostListStateColumn::Type::num_svc_hard_warn));
162 table->addColumn(std::make_unique<HostListStateColumn>(
163 prefix + "num_services_hard_crit",
164 "The total number of services with the state CRIT of hosts in this group",
165 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
166 table->core(), HostListStateColumn::Type::num_svc_hard_crit));
167 table->addColumn(std::make_unique<HostListStateColumn>(
168 prefix + "num_services_hard_unknown",
169 "The total number of services with the state UNKNOWN of hosts in this group",
170 indirect_offset, -1, -1, DANGEROUS_OFFSETOF(hostgroup, members),
171 table->core(), HostListStateColumn::Type::num_svc_hard_unknown));
174 void TableHostGroups::answerQuery(Query *query) {
175 for (hostgroup *hg = hostgroup_list; hg != nullptr; hg = hg->next) {
176 if (!query->processDataset(Row(hg))) {
177 break;
182 Row TableHostGroups::findObject(const std::string &objectspec) const {
183 return Row(find_hostgroup(const_cast<char *>(objectspec.c_str())));
186 bool TableHostGroups::isAuthorized(Row row, const contact *ctc) const {
187 return is_authorized_for_host_group(core(), rowData<hostgroup>(row), ctc);