]> granicus.if.org Git - icinga2/blob - lib/livestatus/downtimestable.cpp
Merge pull request #7204 from episodeiv/master
[icinga2] / lib / livestatus / downtimestable.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
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
10 using namespace icinga;
11
12 DowntimesTable::DowntimesTable()
13 {
14         AddColumns(this);
15 }
16
17 void DowntimesTable::AddColumns(Table *table, const String& prefix,
18         const Column::ObjectAccessor& objectAccessor)
19 {
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));
31
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));
35 }
36
37 String DowntimesTable::GetName() const
38 {
39         return "downtimes";
40 }
41
42 String DowntimesTable::GetPrefix() const
43 {
44         return "downtime";
45 }
46
47 void DowntimesTable::FetchRows(const AddRowFunction& addRowFn)
48 {
49         for (const Downtime::Ptr& downtime : ConfigType::GetObjectsByType<Downtime>()) {
50                 if (!addRowFn(downtime, LivestatusGroupByNone, Empty))
51                         return;
52         }
53 }
54
55 Object::Ptr DowntimesTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
56 {
57         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
58
59         Checkable::Ptr checkable = downtime->GetCheckable();
60
61         Host::Ptr host;
62         Service::Ptr service;
63         tie(host, service) = GetHostService(checkable);
64
65         return host;
66 }
67
68 Object::Ptr DowntimesTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
69 {
70         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
71
72         Checkable::Ptr checkable = downtime->GetCheckable();
73
74         Host::Ptr host;
75         Service::Ptr service;
76         tie(host, service) = GetHostService(checkable);
77
78         return service;
79 }
80
81 Value DowntimesTable::AuthorAccessor(const Value& row)
82 {
83         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
84
85         return downtime->GetAuthor();
86 }
87
88 Value DowntimesTable::CommentAccessor(const Value& row)
89 {
90         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
91
92         return downtime->GetComment();
93 }
94
95 Value DowntimesTable::IdAccessor(const Value& row)
96 {
97         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
98
99         return downtime->GetLegacyId();
100 }
101
102 Value DowntimesTable::EntryTimeAccessor(const Value& row)
103 {
104         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
105
106         return static_cast<int>(downtime->GetEntryTime());
107 }
108
109 Value DowntimesTable::TypeAccessor(const Value& row)
110 {
111         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
112         // 1 .. active, 0 .. pending
113         return (downtime->IsInEffect() ? 1 : 0);
114 }
115
116 Value DowntimesTable::IsServiceAccessor(const Value& row)
117 {
118         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
119         Checkable::Ptr checkable = downtime->GetCheckable();
120
121         return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
122 }
123
124 Value DowntimesTable::StartTimeAccessor(const Value& row)
125 {
126         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
127
128         return static_cast<int>(downtime->GetStartTime());
129 }
130
131 Value DowntimesTable::EndTimeAccessor(const Value& row)
132 {
133         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
134
135         return static_cast<int>(downtime->GetEndTime());
136 }
137
138 Value DowntimesTable::FixedAccessor(const Value& row)
139 {
140         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
141
142         return downtime->GetFixed();
143 }
144
145 Value DowntimesTable::DurationAccessor(const Value& row)
146 {
147         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
148
149         return downtime->GetDuration();
150 }
151
152 Value DowntimesTable::TriggeredByAccessor(const Value& row)
153 {
154         Downtime::Ptr downtime = static_cast<Downtime::Ptr>(row);
155
156         String triggerDowntimeName = downtime->GetTriggeredBy();
157
158         Downtime::Ptr triggerDowntime = Downtime::GetByName(triggerDowntimeName);
159
160         if (triggerDowntime)
161                 return triggerDowntime->GetLegacyId();
162
163         return Empty;
164 }