]> granicus.if.org Git - icinga2/blob - lib/livestatus/livestatusquery.hpp
Remove more redundant wrappers from CompatUtility class
[icinga2] / lib / livestatus / livestatusquery.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 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 #ifndef LIVESTATUSQUERY_H
21 #define LIVESTATUSQUERY_H
22
23 #include "livestatus/filter.hpp"
24 #include "livestatus/aggregator.hpp"
25 #include "base/object.hpp"
26 #include "base/array.hpp"
27 #include "base/stream.hpp"
28 #include "base/scriptframe.hpp"
29 #include <deque>
30
31 using namespace icinga;
32
33 namespace icinga
34 {
35
36 enum LivestatusError
37 {
38         LivestatusErrorOK = 200,
39         LivestatusErrorNotFound = 404,
40         LivestatusErrorQuery = 452
41 };
42
43 /**
44  * @ingroup livestatus
45  */
46 class LivestatusQuery final : public Object
47 {
48 public:
49         DECLARE_PTR_TYPEDEFS(LivestatusQuery);
50
51         LivestatusQuery(const std::vector<String>& lines, const String& compat_log_path);
52
53         bool Execute(const Stream::Ptr& stream);
54
55         static int GetExternalCommands();
56
57 private:
58         String m_Verb;
59
60         bool m_KeepAlive;
61
62         /* Parameters for GET queries. */
63         String m_Table;
64         std::vector<String> m_Columns;
65         std::vector<String> m_Separators;
66
67         Filter::Ptr m_Filter;
68         std::deque<Aggregator::Ptr> m_Aggregators;
69
70         String m_OutputFormat;
71         bool m_ColumnHeaders;
72         int m_Limit;
73
74         String m_ResponseHeader;
75
76         /* Parameters for COMMAND/SCRIPT queries. */
77         String m_Command;
78         String m_Session;
79
80         /* Parameters for invalid queries. */
81         int m_ErrorCode;
82         String m_ErrorMessage;
83
84         unsigned long m_LogTimeFrom;
85         unsigned long m_LogTimeUntil;
86         String m_CompatLogPath;
87
88         void BeginResultSet(std::ostream& fp) const;
89         void EndResultSet(std::ostream& fp) const;
90         void AppendResultRow(std::ostream& fp, const Array::Ptr& row, bool& first_row) const;
91         void PrintCsvArray(std::ostream& fp, const Array::Ptr& array, int level) const;
92         void PrintPythonArray(std::ostream& fp, const Array::Ptr& array) const;
93         static String QuoteStringPython(const String& str);
94
95         void ExecuteGetHelper(const Stream::Ptr& stream);
96         void ExecuteCommandHelper(const Stream::Ptr& stream);
97         void ExecuteErrorHelper(const Stream::Ptr& stream);
98
99         void SendResponse(const Stream::Ptr& stream, int code, const String& data);
100         void PrintFixed16(const Stream::Ptr& stream, int code, const String& data);
101
102         static Filter::Ptr ParseFilter(const String& params, unsigned long& from, unsigned long& until);
103 };
104
105 }
106
107 #endif /* LIVESTATUSQUERY_H */