1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2 
3 #ifndef LIVESTATUSQUERY_H
4 #define LIVESTATUSQUERY_H
5 
6 #include "livestatus/filter.hpp"
7 #include "livestatus/aggregator.hpp"
8 #include "base/object.hpp"
9 #include "base/array.hpp"
10 #include "base/stream.hpp"
11 #include "base/scriptframe.hpp"
12 #include <deque>
13 
14 using namespace icinga;
15 
16 namespace icinga
17 {
18 
19 enum LivestatusError
20 {
21 	LivestatusErrorOK = 200,
22 	LivestatusErrorNotFound = 404,
23 	LivestatusErrorQuery = 452
24 };
25 
26 /**
27  * @ingroup livestatus
28  */
29 class LivestatusQuery final : public Object
30 {
31 public:
32 	DECLARE_PTR_TYPEDEFS(LivestatusQuery);
33 
34 	LivestatusQuery(const std::vector<String>& lines, const String& compat_log_path);
35 
36 	bool Execute(const Stream::Ptr& stream);
37 
38 	static int GetExternalCommands();
39 
40 private:
41 	String m_Verb;
42 
43 	bool m_KeepAlive;
44 
45 	/* Parameters for GET queries. */
46 	String m_Table;
47 	std::vector<String> m_Columns;
48 	std::vector<String> m_Separators;
49 
50 	Filter::Ptr m_Filter;
51 	std::deque<Aggregator::Ptr> m_Aggregators;
52 
53 	String m_OutputFormat;
54 	bool m_ColumnHeaders;
55 	int m_Limit;
56 
57 	String m_ResponseHeader;
58 
59 	/* Parameters for COMMAND/SCRIPT queries. */
60 	String m_Command;
61 	String m_Session;
62 
63 	/* Parameters for invalid queries. */
64 	int m_ErrorCode;
65 	String m_ErrorMessage;
66 
67 	unsigned long m_LogTimeFrom;
68 	unsigned long m_LogTimeUntil;
69 	String m_CompatLogPath;
70 
71 	void BeginResultSet(std::ostream& fp) const;
72 	void EndResultSet(std::ostream& fp) const;
73 	void AppendResultRow(std::ostream& fp, const Array::Ptr& row, bool& first_row) const;
74 	void PrintCsvArray(std::ostream& fp, const Array::Ptr& array, int level) const;
75 	void PrintPythonArray(std::ostream& fp, const Array::Ptr& array) const;
76 	static String QuoteStringPython(const String& str);
77 
78 	void ExecuteGetHelper(const Stream::Ptr& stream);
79 	void ExecuteCommandHelper(const Stream::Ptr& stream);
80 	void ExecuteErrorHelper(const Stream::Ptr& stream);
81 
82 	void SendResponse(const Stream::Ptr& stream, int code, const String& data);
83 	void PrintFixed16(const Stream::Ptr& stream, int code, const String& data);
84 
85 	static Filter::Ptr ParseFilter(const String& params, unsigned long& from, unsigned long& until);
86 };
87 
88 }
89 
90 #endif /* LIVESTATUSQUERY_H */
91