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