1 /******************************************************************************
3 * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
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.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/configtype.hpp"
27 #include "base/objectlock.hpp"
28 #include "base/convert.hpp"
29 #include <boost/algorithm/string/replace.hpp>
31 using namespace icinga;
33 CommandsTable::CommandsTable(void)
38 void CommandsTable::AddColumns(Table *table, const String& prefix,
39 const Column::ObjectAccessor& objectAccessor)
41 table->AddColumn(prefix + "name", Column(&CommandsTable::NameAccessor, objectAccessor));
42 table->AddColumn(prefix + "line", Column(&CommandsTable::LineAccessor, objectAccessor));
43 table->AddColumn(prefix + "custom_variable_names", Column(&CommandsTable::CustomVariableNamesAccessor, objectAccessor));
44 table->AddColumn(prefix + "custom_variable_values", Column(&CommandsTable::CustomVariableValuesAccessor, objectAccessor));
45 table->AddColumn(prefix + "custom_variables", Column(&CommandsTable::CustomVariablesAccessor, objectAccessor));
46 table->AddColumn(prefix + "modified_attributes", Column(&Table::ZeroAccessor, objectAccessor));
47 table->AddColumn(prefix + "modified_attributes_list", Column(&Table::ZeroAccessor, objectAccessor));
50 String CommandsTable::GetName(void) const
55 String CommandsTable::GetPrefix(void) const
60 void CommandsTable::FetchRows(const AddRowFunction& addRowFn)
62 for (const ConfigObject::Ptr& object : ConfigType::GetObjectsByType<CheckCommand>()) {
63 if (!addRowFn(object, LivestatusGroupByNone, Empty))
67 for (const ConfigObject::Ptr& object : ConfigType::GetObjectsByType<EventCommand>()) {
68 if (!addRowFn(object, LivestatusGroupByNone, Empty))
72 for (const ConfigObject::Ptr& object : ConfigType::GetObjectsByType<NotificationCommand>()) {
73 if (!addRowFn(object, LivestatusGroupByNone, Empty))
78 Value CommandsTable::NameAccessor(const Value& row)
80 Command::Ptr command = static_cast<Command::Ptr>(row);
82 return CompatUtility::GetCommandName(command);
85 Value CommandsTable::LineAccessor(const Value& row)
87 Command::Ptr command = static_cast<Command::Ptr>(row);
92 return CompatUtility::GetCommandLine(command);
95 Value CommandsTable::CustomVariableNamesAccessor(const Value& row)
97 Command::Ptr command = static_cast<Command::Ptr>(row);
102 Dictionary::Ptr vars;
105 ObjectLock olock(command);
106 vars = CompatUtility::GetCustomAttributeConfig(command);
109 Array::Ptr cv = new Array();
115 ObjectLock xlock(vars);
116 for (const auto& kv : vars) {
124 Value CommandsTable::CustomVariableValuesAccessor(const Value& row)
126 Command::Ptr command = static_cast<Command::Ptr>(row);
131 Dictionary::Ptr vars;
134 ObjectLock olock(command);
135 vars = CompatUtility::GetCustomAttributeConfig(command);
138 Array::Ptr cv = new Array();
144 ObjectLock xlock(vars);
145 for (const auto& kv : vars) {
153 Value CommandsTable::CustomVariablesAccessor(const Value& row)
155 Command::Ptr command = static_cast<Command::Ptr>(row);
160 Dictionary::Ptr vars;
163 ObjectLock olock(command);
164 vars = CompatUtility::GetCustomAttributeConfig(command);
167 Array::Ptr cv = new Array();
173 ObjectLock xlock(vars);
174 for (const auto& kv : vars) {
175 Array::Ptr key_val = new Array();
176 key_val->Add(kv.first);
177 key_val->Add(kv.second);