]> granicus.if.org Git - icinga2/blob - lib/livestatus/commandstable.cpp
add some object locking to the Dump method (which could theoreticylly suffer from...
[icinga2] / lib / livestatus / commandstable.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
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>
13
14 using namespace icinga;
15
16 CommandsTable::CommandsTable()
17 {
18         AddColumns(this);
19 }
20
21 void CommandsTable::AddColumns(Table *table, const String& prefix,
22         const Column::ObjectAccessor& objectAccessor)
23 {
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));
31 }
32
33 String CommandsTable::GetName() const
34 {
35         return "commands";
36 }
37
38 String CommandsTable::GetPrefix() const
39 {
40         return "command";
41 }
42
43 void CommandsTable::FetchRows(const AddRowFunction& addRowFn)
44 {
45         for (const ConfigObject::Ptr& object : ConfigType::GetObjectsByType<CheckCommand>()) {
46                 if (!addRowFn(object, LivestatusGroupByNone, Empty))
47                         return;
48         }
49
50         for (const ConfigObject::Ptr& object : ConfigType::GetObjectsByType<EventCommand>()) {
51                 if (!addRowFn(object, LivestatusGroupByNone, Empty))
52                         return;
53         }
54
55         for (const ConfigObject::Ptr& object : ConfigType::GetObjectsByType<NotificationCommand>()) {
56                 if (!addRowFn(object, LivestatusGroupByNone, Empty))
57                         return;
58         }
59 }
60
61 Value CommandsTable::NameAccessor(const Value& row)
62 {
63         Command::Ptr command = static_cast<Command::Ptr>(row);
64
65         return CompatUtility::GetCommandName(command);
66 }
67
68 Value CommandsTable::LineAccessor(const Value& row)
69 {
70         Command::Ptr command = static_cast<Command::Ptr>(row);
71
72         if (!command)
73                 return Empty;
74
75         return CompatUtility::GetCommandLine(command);
76 }
77
78 Value CommandsTable::CustomVariableNamesAccessor(const Value& row)
79 {
80         Command::Ptr command = static_cast<Command::Ptr>(row);
81
82         if (!command)
83                 return Empty;
84
85         Dictionary::Ptr vars = command->GetVars();
86
87         ArrayData keys;
88
89         if (vars) {
90                 ObjectLock xlock(vars);
91                 for (const auto& kv : vars) {
92                         keys.push_back(kv.first);
93                 }
94         }
95
96         return new Array(std::move(keys));
97 }
98
99 Value CommandsTable::CustomVariableValuesAccessor(const Value& row)
100 {
101         Command::Ptr command = static_cast<Command::Ptr>(row);
102
103         if (!command)
104                 return Empty;
105
106         Dictionary::Ptr vars = command->GetVars();
107
108         ArrayData keys;
109
110         if (vars) {
111                 ObjectLock xlock(vars);
112                 for (const auto& kv : vars) {
113                         keys.push_back(kv.second);
114                 }
115         }
116
117         return new Array(std::move(keys));
118 }
119
120 Value CommandsTable::CustomVariablesAccessor(const Value& row)
121 {
122         Command::Ptr command = static_cast<Command::Ptr>(row);
123
124         if (!command)
125                 return Empty;
126
127         Dictionary::Ptr vars = command->GetVars();
128
129         ArrayData result;
130
131         if (vars) {
132                 ObjectLock xlock(vars);
133                 for (const auto& kv : vars) {
134                         result.push_back(new Array({
135                                 kv.first,
136                                 kv.second
137                         }));
138                 }
139         }
140
141         return new Array(std::move(result));
142 }