]> granicus.if.org Git - icinga2/blob - lib/livestatus/commandstable.cpp
Livestatus: Add GroupBy tables: hostsbygroup, servicesbygroup, servicesbyhostgroup
[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                 addRowFn(object, LivestatusGroupByNone, Empty);
65         }
66         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjectsByType<EventCommand>()) {
67                 addRowFn(object, LivestatusGroupByNone, Empty);
68         }
69         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjectsByType<NotificationCommand>()) {
70                 addRowFn(object, LivestatusGroupByNone, Empty);
71         }
72 }
73
74 Value CommandsTable::NameAccessor(const Value& row)
75 {
76         Command::Ptr command = static_cast<Command::Ptr>(row);
77
78         return CompatUtility::GetCommandName(command);
79 }
80
81 Value CommandsTable::LineAccessor(const Value& row)
82 {
83         Command::Ptr command = static_cast<Command::Ptr>(row);
84
85         if (!command)
86                 return Empty;
87
88         return CompatUtility::GetCommandLine(command);
89 }
90
91 Value CommandsTable::CustomVariableNamesAccessor(const Value& row)
92 {
93         Command::Ptr command = static_cast<Command::Ptr>(row);
94
95         if (!command)
96                 return Empty;
97
98         Dictionary::Ptr vars;
99
100         {
101                 ObjectLock olock(command);
102                 vars = CompatUtility::GetCustomAttributeConfig(command);
103         }
104
105         if (!vars)
106                 return Empty;
107
108         Array::Ptr cv = new Array();
109
110         String key;
111         Value value;
112
113         ObjectLock xlock(vars);
114         BOOST_FOREACH(tie(key, value), vars) {
115                 cv->Add(key);
116         }
117
118         return cv;
119 }
120
121 Value CommandsTable::CustomVariableValuesAccessor(const Value& row)
122 {
123         Command::Ptr command = static_cast<Command::Ptr>(row);
124
125         if (!command)
126                 return Empty;
127
128         Dictionary::Ptr vars;
129
130         {
131                 ObjectLock olock(command);
132                 vars = CompatUtility::GetCustomAttributeConfig(command);
133         }
134
135         if (!vars)
136                 return Empty;
137
138         Array::Ptr cv = new Array();
139
140         String key;
141         Value value;
142
143         ObjectLock xlock(vars);
144         BOOST_FOREACH(tie(key, value), vars) {
145                 cv->Add(value);
146         }
147
148         return cv;
149 }
150
151 Value CommandsTable::CustomVariablesAccessor(const Value& row)
152 {
153         Command::Ptr command = static_cast<Command::Ptr>(row);
154
155         if (!command)
156                 return Empty;
157
158         Dictionary::Ptr vars;
159
160         {
161                 ObjectLock olock(command);
162                 vars = CompatUtility::GetCustomAttributeConfig(command);
163         }
164
165         if (!vars)
166                 return Empty;
167
168         Array::Ptr cv = new Array();
169
170         String key;
171         Value value;
172
173         ObjectLock xlock(vars);
174         BOOST_FOREACH(tie(key, value), vars) {
175                 Array::Ptr key_val = new Array();
176                 key_val->Add(key);
177                 key_val->Add(value);
178                 cv->Add(key_val);
179         }
180
181         return cv;
182 }
183
184 Value CommandsTable::ModifiedAttributesAccessor(const Value& row)
185 {
186         Command::Ptr command = static_cast<Command::Ptr>(row);
187
188         if (!command)
189                 return Empty;
190
191         /* not supported */
192         return command->GetModifiedAttributes();
193 }
194
195 Value CommandsTable::ModifiedAttributesListAccessor(const Value& row)
196 {
197         Command::Ptr command = static_cast<Command::Ptr>(row);
198
199         if (!command)
200                 return Empty;
201
202         return CompatUtility::GetModifiedAttributesList(command);
203 }