]> granicus.if.org Git - icinga2/blob - lib/livestatus/commandstable.cpp
Merge pull request #5746 from sni/livestatus_should_return_empty_lists
[icinga2] / lib / livestatus / commandstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
30
31 using namespace icinga;
32
33 CommandsTable::CommandsTable(void)
34 {
35         AddColumns(this);
36 }
37
38 void CommandsTable::AddColumns(Table *table, const String& prefix,
39     const Column::ObjectAccessor& objectAccessor)
40 {
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));
48 }
49
50 String CommandsTable::GetName(void) const
51 {
52         return "commands";
53 }
54
55 String CommandsTable::GetPrefix(void) const
56 {
57         return "command";
58 }
59
60 void CommandsTable::FetchRows(const AddRowFunction& addRowFn)
61 {
62         for (const ConfigObject::Ptr& object : ConfigType::GetObjectsByType<CheckCommand>()) {
63                 if (!addRowFn(object, LivestatusGroupByNone, Empty))
64                         return;
65         }
66
67         for (const ConfigObject::Ptr& object : ConfigType::GetObjectsByType<EventCommand>()) {
68                 if (!addRowFn(object, LivestatusGroupByNone, Empty))
69                         return;
70         }
71
72         for (const ConfigObject::Ptr& object : ConfigType::GetObjectsByType<NotificationCommand>()) {
73                 if (!addRowFn(object, LivestatusGroupByNone, Empty))
74                         return;
75         }
76 }
77
78 Value CommandsTable::NameAccessor(const Value& row)
79 {
80         Command::Ptr command = static_cast<Command::Ptr>(row);
81
82         return CompatUtility::GetCommandName(command);
83 }
84
85 Value CommandsTable::LineAccessor(const Value& row)
86 {
87         Command::Ptr command = static_cast<Command::Ptr>(row);
88
89         if (!command)
90                 return Empty;
91
92         return CompatUtility::GetCommandLine(command);
93 }
94
95 Value CommandsTable::CustomVariableNamesAccessor(const Value& row)
96 {
97         Command::Ptr command = static_cast<Command::Ptr>(row);
98
99         if (!command)
100                 return Empty;
101
102         Dictionary::Ptr vars;
103
104         {
105                 ObjectLock olock(command);
106                 vars = CompatUtility::GetCustomAttributeConfig(command);
107         }
108
109         Array::Ptr cv = new Array();
110
111         if (!vars)
112                 return cv;
113
114         {
115                 ObjectLock xlock(vars);
116                 for (const auto& kv : vars) {
117                         cv->Add(kv.first);
118                 }
119         }
120
121         return cv;
122 }
123
124 Value CommandsTable::CustomVariableValuesAccessor(const Value& row)
125 {
126         Command::Ptr command = static_cast<Command::Ptr>(row);
127
128         if (!command)
129                 return Empty;
130
131         Dictionary::Ptr vars;
132
133         {
134                 ObjectLock olock(command);
135                 vars = CompatUtility::GetCustomAttributeConfig(command);
136         }
137
138         Array::Ptr cv = new Array();
139
140         if (!vars)
141                 return cv;
142
143         {
144                 ObjectLock xlock(vars);
145                 for (const auto& kv : vars) {
146                         cv->Add(kv.second);
147                 }
148         }
149
150         return cv;
151 }
152
153 Value CommandsTable::CustomVariablesAccessor(const Value& row)
154 {
155         Command::Ptr command = static_cast<Command::Ptr>(row);
156
157         if (!command)
158                 return Empty;
159
160         Dictionary::Ptr vars;
161
162         {
163                 ObjectLock olock(command);
164                 vars = CompatUtility::GetCustomAttributeConfig(command);
165         }
166
167         Array::Ptr cv = new Array();
168
169         if (!vars)
170                 return cv;
171
172         {
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);
178                         cv->Add(key_val);
179                 }
180         }
181
182         return cv;
183 }