]> granicus.if.org Git - icinga2/blob - lib/livestatus/commentstable.cpp
add some object locking to the Dump method (which could theoreticylly suffer from...
[icinga2] / lib / livestatus / commentstable.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "livestatus/commentstable.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 CommentsTable::CommentsTable()
14 {
15         AddColumns(this);
16 }
17
18 void CommentsTable::AddColumns(Table *table, const String& prefix,
19         const Column::ObjectAccessor& objectAccessor)
20 {
21         table->AddColumn(prefix + "author", Column(&CommentsTable::AuthorAccessor, objectAccessor));
22         table->AddColumn(prefix + "comment", Column(&CommentsTable::CommentAccessor, objectAccessor));
23         table->AddColumn(prefix + "id", Column(&CommentsTable::IdAccessor, objectAccessor));
24         table->AddColumn(prefix + "entry_time", Column(&CommentsTable::EntryTimeAccessor, objectAccessor));
25         table->AddColumn(prefix + "type", Column(&CommentsTable::TypeAccessor, objectAccessor));
26         table->AddColumn(prefix + "is_service", Column(&CommentsTable::IsServiceAccessor, objectAccessor));
27         table->AddColumn(prefix + "persistent", Column(&Table::OneAccessor, objectAccessor));
28         table->AddColumn(prefix + "source", Column(&Table::OneAccessor, objectAccessor));
29         table->AddColumn(prefix + "entry_type", Column(&CommentsTable::EntryTypeAccessor, objectAccessor));
30         table->AddColumn(prefix + "expires", Column(&CommentsTable::ExpiresAccessor, objectAccessor));
31         table->AddColumn(prefix + "expire_time", Column(&CommentsTable::ExpireTimeAccessor, objectAccessor));
32
33         /* order is important - host w/o services must not be empty */
34         ServicesTable::AddColumns(table, "service_", std::bind(&CommentsTable::ServiceAccessor, _1, objectAccessor));
35         HostsTable::AddColumns(table, "host_", std::bind(&CommentsTable::HostAccessor, _1, objectAccessor));
36 }
37
38 String CommentsTable::GetName() const
39 {
40         return "comments";
41 }
42
43 String CommentsTable::GetPrefix() const
44 {
45         return "comment";
46 }
47
48 void CommentsTable::FetchRows(const AddRowFunction& addRowFn)
49 {
50         for (const Comment::Ptr& comment : ConfigType::GetObjectsByType<Comment>()) {
51                 if (!addRowFn(comment, LivestatusGroupByNone, Empty))
52                         return;
53         }
54 }
55
56 Object::Ptr CommentsTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
57 {
58         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
59
60         Checkable::Ptr checkable = comment->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 CommentsTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
70 {
71         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
72
73         Checkable::Ptr checkable = comment->GetCheckable();
74
75         Host::Ptr host;
76         Service::Ptr service;
77         tie(host, service) = GetHostService(checkable);
78
79         return service;
80 }
81
82 Value CommentsTable::AuthorAccessor(const Value& row)
83 {
84         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
85
86         if (!comment)
87                 return Empty;
88
89         return comment->GetAuthor();
90 }
91
92 Value CommentsTable::CommentAccessor(const Value& row)
93 {
94         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
95
96         if (!comment)
97                 return Empty;
98
99         return comment->GetText();
100 }
101
102 Value CommentsTable::IdAccessor(const Value& row)
103 {
104         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
105
106         if (!comment)
107                 return Empty;
108
109         return comment->GetLegacyId();
110 }
111
112 Value CommentsTable::EntryTimeAccessor(const Value& row)
113 {
114         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
115
116         if (!comment)
117                 return Empty;
118
119         return static_cast<int>(comment->GetEntryTime());
120 }
121
122 Value CommentsTable::TypeAccessor(const Value& row)
123 {
124         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
125         Checkable::Ptr checkable = comment->GetCheckable();
126
127         if (!checkable)
128                 return Empty;
129
130         if (dynamic_pointer_cast<Host>(checkable))
131                 return 1;
132         else
133                 return 2;
134 }
135
136 Value CommentsTable::IsServiceAccessor(const Value& row)
137 {
138         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
139         Checkable::Ptr checkable = comment->GetCheckable();
140
141         if (!checkable)
142                 return Empty;
143
144         return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
145 }
146
147 Value CommentsTable::EntryTypeAccessor(const Value& row)
148 {
149         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
150
151         if (!comment)
152                 return Empty;
153
154         return comment->GetEntryType();
155 }
156
157 Value CommentsTable::ExpiresAccessor(const Value& row)
158 {
159         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
160
161         if (!comment)
162                 return Empty;
163
164         return comment->GetExpireTime() != 0;
165 }
166
167 Value CommentsTable::ExpireTimeAccessor(const Value& row)
168 {
169         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
170
171         if (!comment)
172                 return Empty;
173
174         return static_cast<int>(comment->GetExpireTime());
175 }