]> granicus.if.org Git - icinga2/blob - lib/livestatus/downtimestable.cpp
Remove more redundant wrappers from CompatUtility class
[icinga2] / lib / livestatus / downtimestable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "livestatus/downtimestable.hpp"
21 #include "livestatus/hoststable.hpp"
22 #include "livestatus/servicestable.hpp"
23 #include "icinga/service.hpp"
24 #include "base/configtype.hpp"
25 #include "base/objectlock.hpp"
26 #include <boost/tuple/tuple.hpp>
27
28 using namespace icinga;
29
30 DowntimesTable::DowntimesTable()
31 {
32         AddColumns(this);
33 }
34
35 void DowntimesTable::AddColumns(Table *table, const String& prefix,
36         const Column::ObjectAccessor& objectAccessor)
37 {
38         table->AddColumn(prefix + "author", Column(&DowntimesTable::AuthorAccessor, objectAccessor));
39         table->AddColumn(prefix + "comment", Column(&DowntimesTable::CommentAccessor, objectAccessor));
40         table->AddColumn(prefix + "id", Column(&DowntimesTable::IdAccessor, objectAccessor));
41         table->AddColumn(prefix + "entry_time", Column(&DowntimesTable::EntryTimeAccessor, objectAccessor));
42         table->AddColumn(prefix + "type", Column(&DowntimesTable::TypeAccessor, objectAccessor));
43         table->AddColumn(prefix + "is_service", Column(&DowntimesTable::IsServiceAccessor, objectAccessor));
44         table->AddColumn(prefix + "start_time", Column(&DowntimesTable::StartTimeAccessor, objectAccessor));
45         table->AddColumn(prefix + "end_time", Column(&DowntimesTable::EndTimeAccessor, objectAccessor));
46         table->AddColumn(prefix + "fixed", Column(&DowntimesTable::FixedAccessor, objectAccessor));
47         table->AddColumn(prefix + "duration", Column(&DowntimesTable::DurationAccessor, objectAccessor));
48         table->AddColumn(prefix + "triggered_by", Column(&DowntimesTable::TriggeredByAccessor, objectAccessor));
49
50         /* order is important - host w/o services must not be empty */
51         ServicesTable::AddColumns(table, "service_", std::bind(&DowntimesTable::ServiceAccessor, _1, objectAccessor));
52         HostsTable::AddColumns(table, "host_", std::bind(&DowntimesTable::HostAccessor, _1, objectAccessor));
53 }
54
55 String DowntimesTable::GetName() const
56 {
57         return "downtimes";
58 }
59
60 String DowntimesTable::GetPrefix() const
61 {
62         return "downtime";
63 }
64
65 void DowntimesTable::FetchRows(const AddRowFunction& addRowFn)
66 {
67         for (const Downtime::Ptr& downtime : ConfigType::GetObjectsByType<Downtime>()) {
68                 if (!addRowFn(downtime, LivestatusGroupByNone, Empty))
69                         return;
70         }
71 }
72
73 Object::Ptr DowntimesTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
74 {
75         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
76
77         Checkable::Ptr checkable = downtime->GetCheckable();
78
79         Host::Ptr host;
80         Service::Ptr service;
81         tie(host, service) = GetHostService(checkable);
82
83         return host;
84 }
85
86 Object::Ptr DowntimesTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
87 {
88         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
89
90         Checkable::Ptr checkable = downtime->GetCheckable();
91
92         Host::Ptr host;
93         Service::Ptr service;
94         tie(host, service) = GetHostService(checkable);
95
96         return service;
97 }
98
99 Value DowntimesTable::AuthorAccessor(const Value& row)
100 {
101         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
102
103         return downtime->GetAuthor();
104 }
105
106 Value DowntimesTable::CommentAccessor(const Value& row)
107 {
108         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
109
110         return downtime->GetComment();
111 }
112
113 Value DowntimesTable::IdAccessor(const Value& row)
114 {
115         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
116
117         return downtime->GetLegacyId();
118 }
119
120 Value DowntimesTable::EntryTimeAccessor(const Value& row)
121 {
122         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
123
124         return static_cast<int>(downtime->GetEntryTime());
125 }
126
127 Value DowntimesTable::TypeAccessor(const Value& row)
128 {
129         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
130         // 1 .. active, 0 .. pending
131         return (downtime->IsInEffect() ? 1 : 0);
132 }
133
134 Value DowntimesTable::IsServiceAccessor(const Value& row)
135 {
136         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
137         Checkable::Ptr checkable = downtime->GetCheckable();
138
139         return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
140 }
141
142 Value DowntimesTable::StartTimeAccessor(const Value& row)
143 {
144         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
145
146         return static_cast<int>(downtime->GetStartTime());
147 }
148
149 Value DowntimesTable::EndTimeAccessor(const Value& row)
150 {
151         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
152
153         return static_cast<int>(downtime->GetEndTime());
154 }
155
156 Value DowntimesTable::FixedAccessor(const Value& row)
157 {
158         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
159
160         return downtime->GetFixed();
161 }
162
163 Value DowntimesTable::DurationAccessor(const Value& row)
164 {
165         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
166
167         return downtime->GetDuration();
168 }
169
170 Value DowntimesTable::TriggeredByAccessor(const Value& row)
171 {
172         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
173
174         String triggerDowntimeName = downtime->GetTriggeredBy();
175
176         Downtime::Ptr triggerDowntime = Downtime::GetByName(triggerDowntimeName);
177
178         if (triggerDowntime)
179                 return triggerDowntime->GetLegacyId();
180
181         return Empty;
182 }