]> granicus.if.org Git - icinga2/blob - lib/checker/checkercomponent.hpp
add some object locking to the Dump method (which could theoreticylly suffer from...
[icinga2] / lib / checker / checkercomponent.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef CHECKERCOMPONENT_H
4 #define CHECKERCOMPONENT_H
5
6 #include "checker/checkercomponent-ti.hpp"
7 #include "icinga/service.hpp"
8 #include "base/configobject.hpp"
9 #include "base/timer.hpp"
10 #include "base/utility.hpp"
11 #include <boost/thread/mutex.hpp>
12 #include <boost/thread/condition_variable.hpp>
13 #include <boost/multi_index_container.hpp>
14 #include <boost/multi_index/ordered_index.hpp>
15 #include <boost/multi_index/key_extractors.hpp>
16 #include <thread>
17
18 namespace icinga
19 {
20
21 /**
22  * @ingroup checker
23  */
24 struct CheckableScheduleInfo
25 {
26         Checkable::Ptr Object;
27         double NextCheck;
28 };
29
30 /**
31  * @ingroup checker
32  */
33 struct CheckableNextCheckExtractor
34 {
35         typedef double result_type;
36
37         /**
38          * @threadsafety Always.
39          */
40         double operator()(const CheckableScheduleInfo& csi)
41         {
42                 return csi.NextCheck;
43         }
44 };
45
46 /**
47  * @ingroup checker
48  */
49 class CheckerComponent final : public ObjectImpl<CheckerComponent>
50 {
51 public:
52         DECLARE_OBJECT(CheckerComponent);
53         DECLARE_OBJECTNAME(CheckerComponent);
54
55         typedef boost::multi_index_container<
56                 CheckableScheduleInfo,
57                 boost::multi_index::indexed_by<
58                         boost::multi_index::ordered_unique<boost::multi_index::member<CheckableScheduleInfo, Checkable::Ptr, &CheckableScheduleInfo::Object> >,
59                         boost::multi_index::ordered_non_unique<CheckableNextCheckExtractor>
60                 >
61         > CheckableSet;
62
63         void OnConfigLoaded() override;
64         void Start(bool runtimeCreated) override;
65         void Stop(bool runtimeRemoved) override;
66
67         static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
68         unsigned long GetIdleCheckables();
69         unsigned long GetPendingCheckables();
70
71 private:
72         boost::mutex m_Mutex;
73         boost::condition_variable m_CV;
74         bool m_Stopped{false};
75         std::thread m_Thread;
76
77         CheckableSet m_IdleCheckables;
78         CheckableSet m_PendingCheckables;
79
80         Timer::Ptr m_ResultTimer;
81
82         void CheckThreadProc();
83         void ResultTimerHandler();
84
85         void ExecuteCheckHelper(const Checkable::Ptr& checkable);
86
87         void AdjustCheckTimer();
88
89         void ObjectHandler(const ConfigObject::Ptr& object);
90         void NextCheckChangedHandler(const Checkable::Ptr& checkable);
91
92         void RescheduleCheckTimer();
93
94         static CheckableScheduleInfo GetCheckableScheduleInfo(const Checkable::Ptr& checkable);
95 };
96
97 }
98
99 #endif /* CHECKERCOMPONENT_H */