]> granicus.if.org Git - icinga2/blob - lib/livestatus/downtimestable.cpp
Implement support for the 'Limit' column in Livestatus
[icinga2] / lib / livestatus / downtimestable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
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/dynamictype.hpp"
25 #include "base/objectlock.hpp"
26 #include <boost/tuple/tuple.hpp>
27 #include <boost/foreach.hpp>
28
29 using namespace icinga;
30
31 DowntimesTable::DowntimesTable(void)
32 {
33         AddColumns(this);
34 }
35
36 void DowntimesTable::AddColumns(Table *table, const String& prefix,
37     const Column::ObjectAccessor& objectAccessor)
38 {
39         table->AddColumn(prefix + "author", Column(&DowntimesTable::AuthorAccessor, objectAccessor));
40         table->AddColumn(prefix + "comment", Column(&DowntimesTable::CommentAccessor, objectAccessor));
41         table->AddColumn(prefix + "id", Column(&DowntimesTable::IdAccessor, objectAccessor));
42         table->AddColumn(prefix + "entry_time", Column(&DowntimesTable::EntryTimeAccessor, objectAccessor));
43         table->AddColumn(prefix + "type", Column(&DowntimesTable::TypeAccessor, objectAccessor));
44         table->AddColumn(prefix + "is_service", Column(&DowntimesTable::IsServiceAccessor, objectAccessor));
45         table->AddColumn(prefix + "start_time", Column(&DowntimesTable::StartTimeAccessor, objectAccessor));
46         table->AddColumn(prefix + "end_time", Column(&DowntimesTable::EndTimeAccessor, objectAccessor));
47         table->AddColumn(prefix + "fixed", Column(&DowntimesTable::FixedAccessor, objectAccessor));
48         table->AddColumn(prefix + "duration", Column(&DowntimesTable::DurationAccessor, objectAccessor));
49         table->AddColumn(prefix + "triggered_by", Column(&DowntimesTable::TriggeredByAccessor, objectAccessor));
50
51         /* order is important - host w/o services must not be empty */
52         ServicesTable::AddColumns(table, "service_", boost::bind(&DowntimesTable::ServiceAccessor, _1, objectAccessor));
53         HostsTable::AddColumns(table, "host_", boost::bind(&DowntimesTable::HostAccessor, _1, objectAccessor));
54 }
55
56 String DowntimesTable::GetName(void) const
57 {
58         return "downtimes";
59 }
60
61 String DowntimesTable::GetPrefix(void) const
62 {
63         return "downtime";
64 }
65
66 void DowntimesTable::FetchRows(const AddRowFunction& addRowFn)
67 {
68         BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjectsByType<Host>()) {
69                 Dictionary::Ptr downtimes = host->GetDowntimes();
70
71                 ObjectLock olock(downtimes);
72
73                 String id;
74                 Downtime::Ptr downtime;
75                 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
76                         if (Host::GetOwnerByDowntimeID(id) == host) {
77                                 if (!addRowFn(downtime, LivestatusGroupByNone, Empty))
78                                         return;
79                         }
80                 }
81         }
82
83         BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType<Service>()) {
84                 Dictionary::Ptr downtimes = service->GetDowntimes();
85
86                 ObjectLock olock(downtimes);
87
88                 String id;
89                 Downtime::Ptr downtime;
90                 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
91                         if (Service::GetOwnerByDowntimeID(id) == service) {
92                                 if (!addRowFn(downtime, LivestatusGroupByNone, Empty))
93                                         return;
94                         }
95                 }
96         }
97 }
98
99 Object::Ptr DowntimesTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
100 {
101         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
102
103         Checkable::Ptr checkable = Checkable::GetOwnerByDowntimeID(downtime->GetId());
104
105         Host::Ptr host;
106         Service::Ptr service;
107         tie(host, service) = GetHostService(checkable);
108
109         return host;
110 }
111
112 Object::Ptr DowntimesTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
113 {
114         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
115
116         Checkable::Ptr checkable = Checkable::GetOwnerByDowntimeID(downtime->GetId());
117
118         Host::Ptr host;
119         Service::Ptr service;
120         tie(host, service) = GetHostService(checkable);
121
122         return service;
123 }
124
125 Value DowntimesTable::AuthorAccessor(const Value& row)
126 {
127         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
128
129         return downtime->GetAuthor();
130 }
131
132 Value DowntimesTable::CommentAccessor(const Value& row)
133 {
134         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
135
136         return downtime->GetComment();
137 }
138
139 Value DowntimesTable::IdAccessor(const Value& row)
140 {
141         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
142
143         return downtime->GetLegacyId();
144 }
145
146 Value DowntimesTable::EntryTimeAccessor(const Value& row)
147 {
148         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
149
150         return static_cast<int>(downtime->GetEntryTime());
151 }
152
153 Value DowntimesTable::TypeAccessor(const Value& row)
154 {
155         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
156         // 1 .. active, 0 .. pending
157         return (downtime->IsActive() ? 1 : 0);
158 }
159
160 Value DowntimesTable::IsServiceAccessor(const Value& row)
161 {
162         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
163         Checkable::Ptr checkable = Checkable::GetOwnerByDowntimeID(downtime->GetId());
164
165         return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
166 }
167
168 Value DowntimesTable::StartTimeAccessor(const Value& row)
169 {
170         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
171
172         return static_cast<int>(downtime->GetStartTime());
173 }
174
175 Value DowntimesTable::EndTimeAccessor(const Value& row)
176 {
177         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
178
179         return static_cast<int>(downtime->GetEndTime());
180 }
181
182 Value DowntimesTable::FixedAccessor(const Value& row)
183 {
184         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
185
186         return downtime->GetFixed();
187 }
188
189 Value DowntimesTable::DurationAccessor(const Value& row)
190 {
191         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
192
193         return downtime->GetDuration();
194 }
195
196 Value DowntimesTable::TriggeredByAccessor(const Value& row)
197 {
198         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
199
200         return downtime->GetTriggeredByLegacyId();
201 }