]> granicus.if.org Git - icinga2/blob - lib/livestatus/commandstable.cpp
Implement support for the 'Limit' column in Livestatus
[icinga2] / lib / livestatus / commandstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "livestatus/commandstable.hpp"
21 #include "icinga/icingaapplication.hpp"
22 #include "icinga/checkcommand.hpp"
23 #include "icinga/eventcommand.hpp"
24 #include "icinga/notificationcommand.hpp"
25 #include "icinga/compatutility.hpp"
26 #include "base/dynamictype.hpp"
27 #include "base/objectlock.hpp"
28 #include "base/convert.hpp"
29 #include <boost/foreach.hpp>
30 #include <boost/algorithm/string/replace.hpp>
31
32 using namespace icinga;
33
34 CommandsTable::CommandsTable(void)
35 {
36         AddColumns(this);
37 }
38
39 void CommandsTable::AddColumns(Table *table, const String& prefix,
40     const Column::ObjectAccessor& objectAccessor)
41 {
42         table->AddColumn(prefix + "name", Column(&CommandsTable::NameAccessor, objectAccessor));
43         table->AddColumn(prefix + "line", Column(&CommandsTable::LineAccessor, objectAccessor));
44         table->AddColumn(prefix + "custom_variable_names", Column(&CommandsTable::CustomVariableNamesAccessor, objectAccessor));
45         table->AddColumn(prefix + "custom_variable_values", Column(&CommandsTable::CustomVariableValuesAccessor, objectAccessor));
46         table->AddColumn(prefix + "custom_variables", Column(&CommandsTable::CustomVariablesAccessor, objectAccessor));
47         table->AddColumn(prefix + "modified_attributes", Column(&CommandsTable::ModifiedAttributesAccessor, objectAccessor));
48         table->AddColumn(prefix + "modified_attributes_list", Column(&CommandsTable::ModifiedAttributesListAccessor, objectAccessor));
49 }
50
51 String CommandsTable::GetName(void) const
52 {
53         return "commands";
54 }
55
56 String CommandsTable::GetPrefix(void) const
57 {
58         return "command";
59 }
60
61 void CommandsTable::FetchRows(const AddRowFunction& addRowFn)
62 {
63         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjectsByType<CheckCommand>()) {
64                 if (!addRowFn(object, LivestatusGroupByNone, Empty))
65                         return;
66         }
67
68         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjectsByType<EventCommand>()) {
69                 if (!addRowFn(object, LivestatusGroupByNone, Empty))
70                         return;
71         }
72
73         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjectsByType<NotificationCommand>()) {
74                 if (!addRowFn(object, LivestatusGroupByNone, Empty))
75                         return;
76         }
77 }
78
79 Value CommandsTable::NameAccessor(const Value& row)
80 {
81         Command::Ptr command = static_cast<Command::Ptr>(row);
82
83         return CompatUtility::GetCommandName(command);
84 }
85
86 Value CommandsTable::LineAccessor(const Value& row)
87 {
88         Command::Ptr command = static_cast<Command::Ptr>(row);
89
90         if (!command)
91                 return Empty;
92
93         return CompatUtility::GetCommandLine(command);
94 }
95
96 Value CommandsTable::CustomVariableNamesAccessor(const Value& row)
97 {
98         Command::Ptr command = static_cast<Command::Ptr>(row);
99
100         if (!command)
101                 return Empty;
102
103         Dictionary::Ptr vars;
104
105         {
106                 ObjectLock olock(command);
107                 vars = CompatUtility::GetCustomAttributeConfig(command);
108         }
109
110         if (!vars)
111                 return Empty;
112
113         Array::Ptr cv = new Array();
114
115         String key;
116         Value value;
117
118         ObjectLock xlock(vars);
119         BOOST_FOREACH(tie(key, value), vars) {
120                 cv->Add(key);
121         }
122
123         return cv;
124 }
125
126 Value CommandsTable::CustomVariableValuesAccessor(const Value& row)
127 {
128         Command::Ptr command = static_cast<Command::Ptr>(row);
129
130         if (!command)
131                 return Empty;
132
133         Dictionary::Ptr vars;
134
135         {
136                 ObjectLock olock(command);
137                 vars = CompatUtility::GetCustomAttributeConfig(command);
138         }
139
140         if (!vars)
141                 return Empty;
142
143         Array::Ptr cv = new Array();
144
145         String key;
146         Value value;
147
148         ObjectLock xlock(vars);
149         BOOST_FOREACH(tie(key, value), vars) {
150                 cv->Add(value);
151         }
152
153         return cv;
154 }
155
156 Value CommandsTable::CustomVariablesAccessor(const Value& row)
157 {
158         Command::Ptr command = static_cast<Command::Ptr>(row);
159
160         if (!command)
161                 return Empty;
162
163         Dictionary::Ptr vars;
164
165         {
166                 ObjectLock olock(command);
167                 vars = CompatUtility::GetCustomAttributeConfig(command);
168         }
169
170         if (!vars)
171                 return Empty;
172
173         Array::Ptr cv = new Array();
174
175         String key;
176         Value value;
177
178         ObjectLock xlock(vars);
179         BOOST_FOREACH(tie(key, value), vars) {
180                 Array::Ptr key_val = new Array();
181                 key_val->Add(key);
182                 key_val->Add(value);
183                 cv->Add(key_val);
184         }
185
186         return cv;
187 }
188
189 Value CommandsTable::ModifiedAttributesAccessor(const Value& row)
190 {
191         Command::Ptr command = static_cast<Command::Ptr>(row);
192
193         if (!command)
194                 return Empty;
195
196         /* not supported */
197         return command->GetModifiedAttributes();
198 }
199
200 Value CommandsTable::ModifiedAttributesListAccessor(const Value& row)
201 {
202         Command::Ptr command = static_cast<Command::Ptr>(row);
203
204         if (!command)
205                 return Empty;
206
207         return CompatUtility::GetModifiedAttributesList(command);
208 }