]> granicus.if.org Git - icinga2/blob - lib/perfdata/graphitewriter.hpp
Implement ScheduledDowntime::AllConfigIsLoaded()
[icinga2] / lib / perfdata / graphitewriter.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://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 GRAPHITEWRITER_H
21 #define GRAPHITEWRITER_H
22
23 #include "perfdata/graphitewriter-ti.hpp"
24 #include "icinga/service.hpp"
25 #include "base/configobject.hpp"
26 #include "base/tcpsocket.hpp"
27 #include "base/timer.hpp"
28 #include "base/workqueue.hpp"
29 #include <fstream>
30 #include <boost/thread/mutex.hpp>
31
32 namespace icinga
33 {
34
35 /**
36  * An Icinga graphite writer.
37  *
38  * @ingroup perfdata
39  */
40 class GraphiteWriter final : public ObjectImpl<GraphiteWriter>
41 {
42 public:
43         DECLARE_OBJECT(GraphiteWriter);
44         DECLARE_OBJECTNAME(GraphiteWriter);
45
46         static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
47
48         void ValidateHostNameTemplate(const Lazy<String>& lvalue, const ValidationUtils& utils) override;
49         void ValidateServiceNameTemplate(const Lazy<String>& lvalue, const ValidationUtils& utils) override;
50
51 protected:
52         void OnConfigLoaded() override;
53         void Resume() override;
54         void Pause() override;
55
56 private:
57         Stream::Ptr m_Stream;
58         boost::mutex m_StreamMutex;
59         WorkQueue m_WorkQueue{10000000, 1};
60
61         Timer::Ptr m_ReconnectTimer;
62
63         void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
64         void CheckResultHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
65         void SendMetric(const String& prefix, const String& name, double value, double ts);
66         void SendPerfdata(const String& prefix, const CheckResult::Ptr& cr, double ts);
67         static String EscapeMetric(const String& str);
68         static String EscapeMetricLabel(const String& str);
69         static Value EscapeMacroMetric(const Value& value);
70
71         void ReconnectTimerHandler();
72
73         void Disconnect();
74         void Reconnect();
75
76         void AssertOnWorkQueue();
77
78         void ExceptionHandler(boost::exception_ptr exp);
79 };
80
81 }
82
83 #endif /* GRAPHITEWRITER_H */