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"
10 using namespace icinga;
12 DowntimesTable::DowntimesTable()
17 void DowntimesTable::AddColumns(Table *table, const String& prefix,
18 const Column::ObjectAccessor& objectAccessor)
20 table->AddColumn(prefix + "author", Column(&DowntimesTable::AuthorAccessor, objectAccessor));
21 table->AddColumn(prefix + "comment", Column(&DowntimesTable::CommentAccessor, objectAccessor));
22 table->AddColumn(prefix + "id", Column(&DowntimesTable::IdAccessor, objectAccessor));
23 table->AddColumn(prefix + "entry_time", Column(&DowntimesTable::EntryTimeAccessor, objectAccessor));
24 table->AddColumn(prefix + "type", Column(&DowntimesTable::TypeAccessor, objectAccessor));
25 table->AddColumn(prefix + "is_service", Column(&DowntimesTable::IsServiceAccessor, objectAccessor));
26 table->AddColumn(prefix + "start_time", Column(&DowntimesTable::StartTimeAccessor, objectAccessor));
27 table->AddColumn(prefix + "end_time", Column(&DowntimesTable::EndTimeAccessor, objectAccessor));
28 table->AddColumn(prefix + "fixed", Column(&DowntimesTable::FixedAccessor, objectAccessor));
29 table->AddColumn(prefix + "duration", Column(&DowntimesTable::DurationAccessor, objectAccessor));
30 table->AddColumn(prefix + "triggered_by", Column(&DowntimesTable::TriggeredByAccessor, objectAccessor));
32 /* order is important - host w/o services must not be empty */
33 ServicesTable::AddColumns(table, "service_", std::bind(&DowntimesTable::ServiceAccessor, _1, objectAccessor));
34 HostsTable::AddColumns(table, "host_", std::bind(&DowntimesTable::HostAccessor, _1, objectAccessor));
37 String DowntimesTable::GetName() const
42 String DowntimesTable::GetPrefix() const
47 void DowntimesTable::FetchRows(const AddRowFunction& addRowFn)
49 for (const Downtime::Ptr& downtime : ConfigType::GetObjectsByType<Downtime>()) {
50 if (!addRowFn(downtime, LivestatusGroupByNone, Empty))
55 Object::Ptr DowntimesTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
57 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
59 Checkable::Ptr checkable = downtime->GetCheckable();
63 tie(host, service) = GetHostService(checkable);
68 Object::Ptr DowntimesTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
70 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
72 Checkable::Ptr checkable = downtime->GetCheckable();
76 tie(host, service) = GetHostService(checkable);
81 Value DowntimesTable::AuthorAccessor(const Value& row)
83 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
85 return downtime->GetAuthor();
88 Value DowntimesTable::CommentAccessor(const Value& row)
90 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
92 return downtime->GetComment();
95 Value DowntimesTable::IdAccessor(const Value& row)
97 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
99 return downtime->GetLegacyId();
102 Value DowntimesTable::EntryTimeAccessor(const Value& row)
104 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
106 return static_cast<int>(downtime->GetEntryTime());
109 Value DowntimesTable::TypeAccessor(const Value& row)
111 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
112 // 1 .. active, 0 .. pending
113 return (downtime->IsInEffect() ? 1 : 0);
116 Value DowntimesTable::IsServiceAccessor(const Value& row)
118 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
119 Checkable::Ptr checkable = downtime->GetCheckable();
121 return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
124 Value DowntimesTable::StartTimeAccessor(const Value& row)
126 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
128 return static_cast<int>(downtime->GetStartTime());
131 Value DowntimesTable::EndTimeAccessor(const Value& row)
133 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
135 return static_cast<int>(downtime->GetEndTime());
138 Value DowntimesTable::FixedAccessor(const Value& row)
140 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
142 return downtime->GetFixed();
145 Value DowntimesTable::DurationAccessor(const Value& row)
147 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
149 return downtime->GetDuration();
152 Value DowntimesTable::TriggeredByAccessor(const Value& row)
154 Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
156 String triggerDowntimeName = downtime->GetTriggeredBy();
158 Downtime::Ptr triggerDowntime = Downtime::GetByName(triggerDowntimeName);
161 return triggerDowntime->GetLegacyId();