]> granicus.if.org Git - icinga2/blob - lib/checker/checkercomponent.hpp
Merge pull request #5929 from Icinga/feature/remove-boost-assign
[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 : 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         CheckerComponent(void);
81
82         virtual void OnConfigLoaded(void) override;
83         virtual void Start(bool runtimeCreated) override;
84         virtual void Stop(bool runtimeRemoved) override;
85
86         static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
87         unsigned long GetIdleCheckables(void);
88         unsigned long GetPendingCheckables(void);
89
90 private:
91         boost::mutex m_Mutex;
92         boost::condition_variable m_CV;
93         bool m_Stopped;
94         std::thread m_Thread;
95
96         CheckableSet m_IdleCheckables;
97         CheckableSet m_PendingCheckables;
98
99         Timer::Ptr m_ResultTimer;
100
101         void CheckThreadProc(void);
102         void ResultTimerHandler(void);
103
104         void ExecuteCheckHelper(const Checkable::Ptr& checkable);
105
106         void AdjustCheckTimer(void);
107
108         void ObjectHandler(const ConfigObject::Ptr& object);
109         void NextCheckChangedHandler(const Checkable::Ptr& checkable);
110
111         void RescheduleCheckTimer(void);
112
113         static CheckableScheduleInfo GetCheckableScheduleInfo(const Checkable::Ptr& checkable);
114 };
115
116 }
117
118 #endif /* CHECKERCOMPONENT_H */