1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "livestatus/downtimestable.hpp"
4 #include "livestatus/hoststable.hpp"
5 #include "livestatus/servicestable.hpp"
6 #include "icinga/service.hpp"
7 #include "base/configtype.hpp"
8 #include "base/objectlock.hpp"
9 #include <boost/tuple/tuple.hpp>
11 using namespace icinga;
13 DowntimesTable::DowntimesTable()
18 void DowntimesTable::AddColumns(Table *table, const String& prefix,
19 const Column::ObjectAccessor& objectAccessor)
21 table->AddColumn(prefix + "author", Column(&DowntimesTable::AuthorAccessor, objectAccessor));
22 table->AddColumn(prefix + "comment", Column(&DowntimesTable::CommentAccessor, objectAccessor));
23 table->AddColumn(prefix + "id", Column(&DowntimesTable::IdAccessor, objectAccessor));
24 table->AddColumn(prefix + "entry_time", Column(&DowntimesTable::EntryTimeAccessor, objectAccessor));
25 table->AddColumn(prefix + "type", Column(&DowntimesTable::TypeAccessor, objectAccessor));
26 table->AddColumn(prefix + "is_service", Column(&DowntimesTable::IsServiceAccessor, objectAccessor));
27 table->AddColumn(prefix + "start_time", Column(&DowntimesTable::StartTimeAccessor, objectAccessor));
28 table->AddColumn(prefix + "end_time", Column(&DowntimesTable::EndTimeAccessor, objectAccessor));
29 table->AddColumn(prefix + "fixed", Column(&DowntimesTable::FixedAccessor, objectAccessor));
30 table->AddColumn(prefix + "duration", Column(&DowntimesTable::DurationAccessor, objectAccessor));
31 table->AddColumn(prefix + "triggered_by", Column(&DowntimesTable::TriggeredByAccessor, objectAccessor));
33 /* order is important - host w/o services must not be empty */
34 ServicesTable::AddColumns(table, "service_", std::bind(&DowntimesTable::ServiceAccessor, _1, objectAccessor));
35 HostsTable::AddColumns(table, "host_", std::bind(&DowntimesTable::HostAccessor, _1, objectAccessor));
38 String DowntimesTable::GetName() const
43 String DowntimesTable::GetPrefix() const
48 void DowntimesTable::FetchRows(const AddRowFunction& addRowFn)
50 for (const Downtime::Ptr& downtime : ConfigType::GetObjectsByType<Downtime>()) {
51 if (!addRowFn(downtime, LivestatusGroupByNone, Empty))
56 Object::Ptr DowntimesTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
58 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
60 Checkable::Ptr checkable = downtime->GetCheckable();
64 tie(host, service) = GetHostService(checkable);
69 Object::Ptr DowntimesTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
71 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
73 Checkable::Ptr checkable = downtime->GetCheckable();
77 tie(host, service) = GetHostService(checkable);
82 Value DowntimesTable::AuthorAccessor(const Value& row)
84 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
86 return downtime->GetAuthor();
89 Value DowntimesTable::CommentAccessor(const Value& row)
91 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
93 return downtime->GetComment();
96 Value DowntimesTable::IdAccessor(const Value& row)
98 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
100 return downtime->GetLegacyId();
103 Value DowntimesTable::EntryTimeAccessor(const Value& row)
105 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
107 return static_cast<int>(downtime->GetEntryTime());
110 Value DowntimesTable::TypeAccessor(const Value& row)
112 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
113 // 1 .. active, 0 .. pending
114 return (downtime->IsInEffect() ? 1 : 0);
117 Value DowntimesTable::IsServiceAccessor(const Value& row)
119 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
120 Checkable::Ptr checkable = downtime->GetCheckable();
122 return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
125 Value DowntimesTable::StartTimeAccessor(const Value& row)
127 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
129 return static_cast<int>(downtime->GetStartTime());
132 Value DowntimesTable::EndTimeAccessor(const Value& row)
134 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
136 return static_cast<int>(downtime->GetEndTime());
139 Value DowntimesTable::FixedAccessor(const Value& row)
141 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
143 return downtime->GetFixed();
146 Value DowntimesTable::DurationAccessor(const Value& row)
148 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
150 return downtime->GetDuration();
153 Value DowntimesTable::TriggeredByAccessor(const Value& row)
155 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
157 String triggerDowntimeName = downtime->GetTriggeredBy();
159 Downtime::Ptr triggerDowntime = Downtime::GetByName(triggerDowntimeName);
162 return triggerDowntime->GetLegacyId();