1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "livestatus/commandstable.hpp"
4 #include "icinga/icingaapplication.hpp"
5 #include "icinga/checkcommand.hpp"
6 #include "icinga/eventcommand.hpp"
7 #include "icinga/notificationcommand.hpp"
8 #include "icinga/compatutility.hpp"
9 #include "base/configtype.hpp"
10 #include "base/objectlock.hpp"
11 #include "base/convert.hpp"
12 #include <boost/algorithm/string/replace.hpp>
14 using namespace icinga;
16 CommandsTable::CommandsTable()
21 void CommandsTable::AddColumns(Table *table, const String& prefix,
22 const Column::ObjectAccessor& objectAccessor)
24 table->AddColumn(prefix + "name", Column(&CommandsTable::NameAccessor, objectAccessor));
25 table->AddColumn(prefix + "line", Column(&CommandsTable::LineAccessor, objectAccessor));
26 table->AddColumn(prefix + "custom_variable_names", Column(&CommandsTable::CustomVariableNamesAccessor, objectAccessor));
27 table->AddColumn(prefix + "custom_variable_values", Column(&CommandsTable::CustomVariableValuesAccessor, objectAccessor));
28 table->AddColumn(prefix + "custom_variables", Column(&CommandsTable::CustomVariablesAccessor, objectAccessor));
29 table->AddColumn(prefix + "modified_attributes", Column(&Table::ZeroAccessor, objectAccessor));
30 table->AddColumn(prefix + "modified_attributes_list", Column(&Table::ZeroAccessor, objectAccessor));
33 String CommandsTable::GetName() const
38 String CommandsTable::GetPrefix() const
43 void CommandsTable::FetchRows(const AddRowFunction& addRowFn)
45 for (const ConfigObject::Ptr& object : ConfigType::GetObjectsByType<CheckCommand>()) {
46 if (!addRowFn(object, LivestatusGroupByNone, Empty))
50 for (const ConfigObject::Ptr& object : ConfigType::GetObjectsByType<EventCommand>()) {
51 if (!addRowFn(object, LivestatusGroupByNone, Empty))
55 for (const ConfigObject::Ptr& object : ConfigType::GetObjectsByType<NotificationCommand>()) {
56 if (!addRowFn(object, LivestatusGroupByNone, Empty))
61 Value CommandsTable::NameAccessor(const Value& row)
63 Command::Ptr command = static_cast<Command::Ptr>(row);
65 return CompatUtility::GetCommandName(command);
68 Value CommandsTable::LineAccessor(const Value& row)
70 Command::Ptr command = static_cast<Command::Ptr>(row);
75 return CompatUtility::GetCommandLine(command);
78 Value CommandsTable::CustomVariableNamesAccessor(const Value& row)
80 Command::Ptr command = static_cast<Command::Ptr>(row);
85 Dictionary::Ptr vars = command->GetVars();
90 ObjectLock xlock(vars);
91 for (const auto& kv : vars) {
92 keys.push_back(kv.first);
96 return new Array(std::move(keys));
99 Value CommandsTable::CustomVariableValuesAccessor(const Value& row)
101 Command::Ptr command = static_cast<Command::Ptr>(row);
106 Dictionary::Ptr vars = command->GetVars();
111 ObjectLock xlock(vars);
112 for (const auto& kv : vars) {
113 keys.push_back(kv.second);
117 return new Array(std::move(keys));
120 Value CommandsTable::CustomVariablesAccessor(const Value& row)
122 Command::Ptr command = static_cast<Command::Ptr>(row);
127 Dictionary::Ptr vars = command->GetVars();
132 ObjectLock xlock(vars);
133 for (const auto& kv : vars) {
134 result.push_back(new Array({
141 return new Array(std::move(result));