1 /******************************************************************************
3 * Copyright (C) 2012-2018 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 #ifndef LIVESTATUSQUERY_H
21 #define LIVESTATUSQUERY_H
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"
31 using namespace icinga;
38 LivestatusErrorOK = 200,
39 LivestatusErrorNotFound = 404,
40 LivestatusErrorQuery = 452
46 class LivestatusQuery final : public Object
49 DECLARE_PTR_TYPEDEFS(LivestatusQuery);
51 LivestatusQuery(const std::vector<String>& lines, const String& compat_log_path);
53 bool Execute(const Stream::Ptr& stream);
55 static int GetExternalCommands();
62 /* Parameters for GET queries. */
64 std::vector<String> m_Columns;
65 std::vector<String> m_Separators;
68 std::deque<Aggregator::Ptr> m_Aggregators;
70 String m_OutputFormat;
74 String m_ResponseHeader;
76 /* Parameters for COMMAND/SCRIPT queries. */
80 /* Parameters for invalid queries. */
82 String m_ErrorMessage;
84 unsigned long m_LogTimeFrom;
85 unsigned long m_LogTimeUntil;
86 String m_CompatLogPath;
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);
95 void ExecuteGetHelper(const Stream::Ptr& stream);
96 void ExecuteCommandHelper(const Stream::Ptr& stream);
97 void ExecuteErrorHelper(const Stream::Ptr& stream);
99 void SendResponse(const Stream::Ptr& stream, int code, const String& data);
100 void PrintFixed16(const Stream::Ptr& stream, int code, const String& data);
102 static Filter::Ptr ParseFilter(const String& params, unsigned long& from, unsigned long& until);
107 #endif /* LIVESTATUSQUERY_H */