]> granicus.if.org Git - icinga2/blob - lib/checker/checkercomponent.hpp
Merge pull request #5993 from Icinga/feature/cmake-object-libs
[icinga2] / lib / checker / checkercomponent.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 CHECKERCOMPONENT_H
21 #define CHECKERCOMPONENT_H
22
23 #include "checker/checkercomponent.thpp"
24 #include "icinga/service.hpp"
25 #include "base/configobject.hpp"
26 #include "base/timer.hpp"
27 #include "base/utility.hpp"
28 #include <boost/thread/mutex.hpp>
29 #include <boost/thread/condition_variable.hpp>
30 #include <boost/multi_index_container.hpp>
31 #include <boost/multi_index/ordered_index.hpp>
32 #include <boost/multi_index/key_extractors.hpp>
33 #include <thread>
34
35 namespace icinga
36 {
37
38 /**
39  * @ingroup checker
40  */
41 struct CheckableScheduleInfo
42 {
43         Checkable::Ptr Object;
44         double NextCheck;
45 };
46
47 /**
48  * @ingroup checker
49  */
50 struct CheckableNextCheckExtractor
51 {
52         typedef double result_type;
53
54         /**
55          * @threadsafety Always.
56          */
57         double operator()(const CheckableScheduleInfo& csi)
58         {
59                 return csi.NextCheck;
60         }
61 };
62
63 /**
64  * @ingroup checker
65  */
66 class CheckerComponent final : public ObjectImpl<CheckerComponent>
67 {
68 public:
69         DECLARE_OBJECT(CheckerComponent);
70         DECLARE_OBJECTNAME(CheckerComponent);
71
72         typedef boost::multi_index_container<
73                 CheckableScheduleInfo,
74                 boost::multi_index::indexed_by<
75                         boost::multi_index::ordered_unique<boost::multi_index::member<CheckableScheduleInfo, Checkable::Ptr, &CheckableScheduleInfo::Object> >,
76                         boost::multi_index::ordered_non_unique<CheckableNextCheckExtractor>
77                 >
78         > CheckableSet;
79
80         void OnConfigLoaded() override;
81         void Start(bool runtimeCreated) override;
82         void Stop(bool runtimeRemoved) override;
83
84         static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
85         unsigned long GetIdleCheckables();
86         unsigned long GetPendingCheckables();
87
88 private:
89         boost::mutex m_Mutex;
90         boost::condition_variable m_CV;
91         bool m_Stopped{false};
92         std::thread m_Thread;
93
94         CheckableSet m_IdleCheckables;
95         CheckableSet m_PendingCheckables;
96
97         Timer::Ptr m_ResultTimer;
98
99         void CheckThreadProc();
100         void ResultTimerHandler();
101
102         void ExecuteCheckHelper(const Checkable::Ptr& checkable);
103
104         void AdjustCheckTimer();
105
106         void ObjectHandler(const ConfigObject::Ptr& object);
107         void NextCheckChangedHandler(const Checkable::Ptr& checkable);
108
109         void RescheduleCheckTimer();
110
111         static CheckableScheduleInfo GetCheckableScheduleInfo(const Checkable::Ptr& checkable);
112 };
113
114 }
115
116 #endif /* CHECKERCOMPONENT_H */