]> granicus.if.org Git - icinga2/blob - components/livestatus/commandstable.cpp
Add CompatUtility::GetModifiedAttributesList() for Livestatus.
[icinga2] / components / livestatus / commandstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 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.h"
21 #include "icinga/icingaapplication.h"
22 #include "icinga/checkcommand.h"
23 #include "icinga/eventcommand.h"
24 #include "icinga/notificationcommand.h"
25 #include "icinga/compatutility.h"
26 #include "base/dynamictype.h"
27 #include "base/objectlock.h"
28 #include "base/convert.h"
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 "command";
54 }
55
56 void CommandsTable::FetchRows(const AddRowFunction& addRowFn)
57 {
58         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<CheckCommand>()) {
59                 addRowFn(object);
60         }
61         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<EventCommand>()) {
62                 addRowFn(object);
63         }
64         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<NotificationCommand>()) {
65                 addRowFn(object);
66         }
67 }
68
69 Value CommandsTable::NameAccessor(const Value& row)
70 {
71         String buf;
72         Command::Ptr command = static_cast<Command::Ptr>(row);
73         
74         if (!command)
75                 return Empty;
76
77         if (command->GetType() == DynamicType::GetByName("CheckCommand"))
78                 buf += "check_";
79         if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
80                 buf += "notification_";
81         if (command->GetType() == DynamicType::GetByName("EventCommand"))
82                 buf += "event_";
83
84         buf += command->GetName();
85
86         return buf;
87 }
88
89 Value CommandsTable::LineAccessor(const Value& row)
90 {
91         String buf;
92         Command::Ptr command = static_cast<Command::Ptr>(row);
93         
94         if (!command)
95                 return Empty;
96
97         Value commandLine = command->GetCommandLine();
98
99         if (commandLine.IsObjectType<Array>()) {
100                 Array::Ptr args = commandLine;
101
102                 ObjectLock olock(args);
103                 String arg;
104                 BOOST_FOREACH(arg, args) {
105                         // This is obviously incorrect for non-trivial cases.
106                         String argitem = " \"" + arg + "\"";
107                         boost::algorithm::replace_all(argitem, "\n", "\\n");
108                         buf += argitem;
109                 }
110         } else if (!commandLine.IsEmpty()) {
111                 String args = Convert::ToString(commandLine);
112                 boost::algorithm::replace_all(args, "\n", "\\n");
113                 buf += args;
114         } else {
115                 buf += "<internal>";
116         }
117
118         return buf;
119 }
120
121 Value CommandsTable::CustomVariableNamesAccessor(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 = make_shared<Array>();
139
140         String key;
141         Value value;
142         BOOST_FOREACH(tie(key, value), vars) {
143                 cv->Add(key);
144         }
145
146         return cv;
147 }
148
149 Value CommandsTable::CustomVariableValuesAccessor(const Value& row)
150 {
151         Command::Ptr command = static_cast<Command::Ptr>(row);
152
153         if (!command)
154                 return Empty;
155
156         Dictionary::Ptr vars;
157
158         {
159                 ObjectLock olock(command);
160                 vars = CompatUtility::GetCustomAttributeConfig(command);
161         }
162
163         if (!vars)
164                 return Empty;
165
166         Array::Ptr cv = make_shared<Array>();
167
168         String key;
169         Value value;
170         BOOST_FOREACH(tie(key, value), vars) {
171                 cv->Add(value);
172         }
173
174         return cv;
175 }
176
177 Value CommandsTable::CustomVariablesAccessor(const Value& row)
178 {
179         Command::Ptr command = static_cast<Command::Ptr>(row);
180
181         if (!command)
182                 return Empty;
183
184         Dictionary::Ptr vars;
185
186         {
187                 ObjectLock olock(command);
188                 vars = CompatUtility::GetCustomAttributeConfig(command);
189         }
190
191         if (!vars)
192                 return Empty;
193
194         Array::Ptr cv = make_shared<Array>();
195
196         String key;
197         Value value;
198         BOOST_FOREACH(tie(key, value), vars) {
199                 Array::Ptr key_val = make_shared<Array>();
200                 key_val->Add(key);
201                 key_val->Add(value);
202                 cv->Add(key_val);
203         }
204
205         return cv;
206 }
207
208 Value CommandsTable::ModifiedAttributesAccessor(const Value& row)
209 {
210         Command::Ptr command = static_cast<Command::Ptr>(row);
211
212         if (!command)
213                 return Empty;
214
215         /* not supported */
216         return command->GetModifiedAttributes();
217 }
218
219 Value CommandsTable::ModifiedAttributesListAccessor(const Value& row)
220 {
221         Command::Ptr command = static_cast<Command::Ptr>(row);
222
223         if (!command)
224                 return Empty;
225
226         return CompatUtility::GetModifiedAttributesList(command);
227 }