1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
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. *
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 ******************************************************************************/
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>
32 using namespace icinga;
34 CommandsTable::CommandsTable(void)
39 void CommandsTable::AddColumns(Table *table, const String& prefix,
40 const Column::ObjectAccessor& objectAccessor)
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));
51 String CommandsTable::GetName(void) const
56 void CommandsTable::FetchRows(const AddRowFunction& addRowFn)
58 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<CheckCommand>()) {
61 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<EventCommand>()) {
64 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<NotificationCommand>()) {
69 Value CommandsTable::NameAccessor(const Value& row)
72 Command::Ptr command = static_cast<Command::Ptr>(row);
77 if (command->GetType() == DynamicType::GetByName("CheckCommand"))
79 if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
80 buf += "notification_";
81 if (command->GetType() == DynamicType::GetByName("EventCommand"))
84 buf += command->GetName();
89 Value CommandsTable::LineAccessor(const Value& row)
92 Command::Ptr command = static_cast<Command::Ptr>(row);
97 Value commandLine = command->GetCommandLine();
99 if (commandLine.IsObjectType<Array>()) {
100 Array::Ptr args = commandLine;
102 ObjectLock olock(args);
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");
110 } else if (!commandLine.IsEmpty()) {
111 String args = Convert::ToString(commandLine);
112 boost::algorithm::replace_all(args, "\n", "\\n");
121 Value CommandsTable::CustomVariableNamesAccessor(const Value& row)
123 Command::Ptr command = static_cast<Command::Ptr>(row);
128 Dictionary::Ptr vars;
131 ObjectLock olock(command);
132 vars = CompatUtility::GetCustomAttributeConfig(command);
138 Array::Ptr cv = make_shared<Array>();
142 BOOST_FOREACH(tie(key, value), vars) {
149 Value CommandsTable::CustomVariableValuesAccessor(const Value& row)
151 Command::Ptr command = static_cast<Command::Ptr>(row);
156 Dictionary::Ptr vars;
159 ObjectLock olock(command);
160 vars = CompatUtility::GetCustomAttributeConfig(command);
166 Array::Ptr cv = make_shared<Array>();
170 BOOST_FOREACH(tie(key, value), vars) {
177 Value CommandsTable::CustomVariablesAccessor(const Value& row)
179 Command::Ptr command = static_cast<Command::Ptr>(row);
184 Dictionary::Ptr vars;
187 ObjectLock olock(command);
188 vars = CompatUtility::GetCustomAttributeConfig(command);
194 Array::Ptr cv = make_shared<Array>();
198 BOOST_FOREACH(tie(key, value), vars) {
199 Array::Ptr key_val = make_shared<Array>();
208 Value CommandsTable::ModifiedAttributesAccessor(const Value& row)
210 Command::Ptr command = static_cast<Command::Ptr>(row);
216 return command->GetModifiedAttributes();
219 Value CommandsTable::ModifiedAttributesListAccessor(const Value& row)
221 Command::Ptr command = static_cast<Command::Ptr>(row);
226 return CompatUtility::GetModifiedAttributesList(command);