]> granicus.if.org Git - icinga2/blob - components/checker/checkercomponent.cpp
Replaced custom event code with Boost.Signals.
[icinga2] / components / checker / checkercomponent.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
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 #include "i2-checker.h"
21
22 using namespace icinga;
23
24 string CheckerComponent::GetName(void) const
25 {
26         return "configcomponent";
27 }
28
29 void CheckerComponent::Start(void)
30 {
31         m_CheckerEndpoint = make_shared<VirtualEndpoint>();
32         m_CheckerEndpoint->RegisterTopicHandler("checker::AssignService",
33                 bind(&CheckerComponent::AssignServiceRequestHandler, this, _1));
34         m_CheckerEndpoint->RegisterTopicHandler("checker::RevokeService",
35                 bind(&CheckerComponent::AssignServiceRequestHandler, this, _1));
36         m_CheckerEndpoint->RegisterPublication("checker::CheckResult");
37         GetEndpointManager()->RegisterEndpoint(m_CheckerEndpoint);
38
39         RequestMessage rm;
40         rm.SetMethod("checker::AssignService");
41         GetEndpointManager()->SendAPIMessage(m_CheckerEndpoint, rm, bind(&CheckerComponent::TestResponseHandler, this, _1));
42
43         // TODO: get rid of this
44         ConfigObject::GetAllObjects()->OnObjectAdded.connect(bind(&CheckerComponent::NewServiceHandler, this, _1));
45
46         m_CheckTimer = make_shared<Timer>();
47         m_CheckTimer->SetInterval(10);
48         m_CheckTimer->OnTimerExpired.connect(bind(&CheckerComponent::CheckTimerHandler, this, _1));
49         m_CheckTimer->Start();
50
51         CheckTask::RegisterType("nagios", NagiosCheckTask::CreateTask);
52
53         ConfigObject::TMap::Range range = ConfigObject::GetObjects("service");
54
55         for (ConfigObject::TMap::Iterator it = range.first; it != range.second; it++) {
56                 Service svc(it->second);
57                 CheckTask::Ptr ct = CheckTask::CreateTask(svc);
58                 CheckResult cr = ct->Execute();
59         }
60 }
61
62 int CheckerComponent::TestResponseHandler(const NewResponseEventArgs& ea)
63 {
64         return 0;
65 }
66
67 void CheckerComponent::Stop(void)
68 {
69
70 }
71
72 int CheckerComponent::NewServiceHandler(const ObjectSetEventArgs<ConfigObject::Ptr>& ea)
73 {
74         if (ea.Target->GetType() == "service")
75                 m_Services.push(ea.Target);
76
77         return 0;
78 }
79
80 int CheckerComponent::CheckTimerHandler(const TimerEventArgs& ea)
81 {
82         time_t now;
83         time(&now);
84
85         if (m_Services.size() == 0)
86                 return 0;
87
88         for (;;) {
89
90                 Service service = m_Services.top();
91
92                 if (service.GetNextCheck() > now)
93                         break;
94
95                 CheckTask::Ptr ct = CheckTask::CreateTask(service);
96                 CheckResult cr = ct->Execute();
97
98                 m_Services.pop();
99                 service.SetNextCheck(now + service.GetCheckInterval());
100                 m_Services.push(service);
101         }
102
103         /* adjust next call time for the check timer */
104         Service service = m_Services.top();
105         static_pointer_cast<Timer>(ea.Source)->SetInterval(service.GetNextCheck() - now);
106
107         return 0;
108 }
109
110 int CheckerComponent::AssignServiceRequestHandler(const NewRequestEventArgs& nrea)
111 {
112         string id;
113         if (!nrea.Request.GetID(&id))
114                 return 0;
115
116         ResponseMessage rm;
117         rm.SetID(id);
118
119         MessagePart result;
120         rm.SetResult(result);
121         GetEndpointManager()->SendUnicastMessage(m_CheckerEndpoint, nrea.Sender, rm);
122
123         return 0;
124 }
125
126 int CheckerComponent::RevokeServiceRequestHandler(const NewRequestEventArgs& nrea)
127 {
128         return 0;
129 }
130
131 EXPORT_COMPONENT(checker, CheckerComponent);