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));
49 String CommandsTable::GetName(void) const
54 void CommandsTable::FetchRows(const AddRowFunction& addRowFn)
56 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<CheckCommand>()) {
59 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<EventCommand>()) {
62 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<NotificationCommand>()) {
67 Value CommandsTable::NameAccessor(const Value& row)
70 Command::Ptr command = static_cast<Command::Ptr>(row);
75 if (command->GetType() == DynamicType::GetByName("CheckCommand"))
77 if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
78 buf += "notification_";
79 if (command->GetType() == DynamicType::GetByName("EventCommand"))
82 buf += command->GetName();
87 Value CommandsTable::LineAccessor(const Value& row)
90 Command::Ptr command = static_cast<Command::Ptr>(row);
95 Value commandLine = command->GetCommandLine();
97 if (commandLine.IsObjectType<Array>()) {
98 Array::Ptr args = commandLine;
100 ObjectLock olock(args);
102 BOOST_FOREACH(arg, args) {
103 // This is obviously incorrect for non-trivial cases.
104 String argitem = " \"" + arg + "\"";
105 boost::algorithm::replace_all(argitem, "\n", "\\n");
108 } else if (!commandLine.IsEmpty()) {
109 String args = Convert::ToString(commandLine);
110 boost::algorithm::replace_all(args, "\n", "\\n");
119 Value CommandsTable::CustomVariableNamesAccessor(const Value& row)
121 Command::Ptr command = static_cast<Command::Ptr>(row);
126 Dictionary::Ptr vars;
129 ObjectLock olock(command);
130 vars = CompatUtility::GetCustomAttributeConfig(command);
136 Array::Ptr cv = make_shared<Array>();
140 BOOST_FOREACH(tie(key, value), vars) {
147 Value CommandsTable::CustomVariableValuesAccessor(const Value& row)
149 Command::Ptr command = static_cast<Command::Ptr>(row);
154 Dictionary::Ptr vars;
157 ObjectLock olock(command);
158 vars = CompatUtility::GetCustomAttributeConfig(command);
164 Array::Ptr cv = make_shared<Array>();
168 BOOST_FOREACH(tie(key, value), vars) {
175 Value CommandsTable::CustomVariablesAccessor(const Value& row)
177 Command::Ptr command = static_cast<Command::Ptr>(row);
182 Dictionary::Ptr vars;
185 ObjectLock olock(command);
186 vars = CompatUtility::GetCustomAttributeConfig(command);
192 Array::Ptr cv = make_shared<Array>();
196 BOOST_FOREACH(tie(key, value), vars) {
197 Array::Ptr key_val = make_shared<Array>();