1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
20 #include "i2-checker.h"
22 using namespace icinga;
24 string CheckerComponent::GetName(void) const
26 return "configcomponent";
29 void CheckerComponent::Start(void)
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);
40 rm.SetMethod("checker::AssignService");
41 GetEndpointManager()->SendAPIMessage(m_CheckerEndpoint, rm, bind(&CheckerComponent::TestResponseHandler, this, _1));
43 // TODO: get rid of this
44 ConfigObject::GetAllObjects()->OnObjectAdded.connect(bind(&CheckerComponent::NewServiceHandler, this, _1));
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();
51 CheckTask::RegisterType("nagios", NagiosCheckTask::CreateTask);
53 ConfigObject::TMap::Range range = ConfigObject::GetObjects("service");
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();
62 int CheckerComponent::TestResponseHandler(const NewResponseEventArgs& ea)
67 void CheckerComponent::Stop(void)
72 int CheckerComponent::NewServiceHandler(const ObjectSetEventArgs<ConfigObject::Ptr>& ea)
74 if (ea.Target->GetType() == "service")
75 m_Services.push(ea.Target);
80 int CheckerComponent::CheckTimerHandler(const TimerEventArgs& ea)
85 if (m_Services.size() == 0)
90 Service service = m_Services.top();
92 if (service.GetNextCheck() > now)
95 CheckTask::Ptr ct = CheckTask::CreateTask(service);
96 CheckResult cr = ct->Execute();
99 service.SetNextCheck(now + service.GetCheckInterval());
100 m_Services.push(service);
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);
110 int CheckerComponent::AssignServiceRequestHandler(const NewRequestEventArgs& nrea)
113 if (!nrea.Request.GetID(&id))
120 rm.SetResult(result);
121 GetEndpointManager()->SendUnicastMessage(m_CheckerEndpoint, nrea.Sender, rm);
126 int CheckerComponent::RevokeServiceRequestHandler(const NewRequestEventArgs& nrea)
131 EXPORT_COMPONENT(checker, CheckerComponent);