]> granicus.if.org Git - icinga2/blob - lib/icinga/notification-apply.cpp
Implement apply support for scheduled downtimes and notifications.
[icinga2] / lib / icinga / notification-apply.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-present 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 "icinga/service.h"
21 #include "config/configitembuilder.h"
22 #include "base/initialize.h"
23 #include "base/dynamictype.h"
24 #include "base/convert.h"
25 #include "base/logger_fwd.h"
26 #include "base/context.h"
27 #include <boost/foreach.hpp>
28
29 using namespace icinga;
30
31 INITIALIZE_ONCE(&Notification::RegisterApplyRuleHandler);
32
33 void Notification::RegisterApplyRuleHandler(void)
34 {
35         ApplyRule::RegisterType("Notification", &Notification::EvaluateApplyRules, 2);
36 }
37
38 void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
39 {
40         BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
41                 CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
42
43                 Dictionary::Ptr locals = make_shared<Dictionary>();
44                 locals->Set("host", service->GetHost());
45                 locals->Set("service", service);
46
47                 BOOST_FOREACH(const ApplyRule& rule, rules) {
48                         DebugInfo di = rule.GetDebugInfo();
49
50                         std::ostringstream msgbuf;
51                         msgbuf << "Evaluating 'apply' rule (" << di << ")";
52                         CONTEXT(msgbuf.str());
53
54                         if (!rule.EvaluateFilter(locals))
55                                 continue;
56
57                         std::ostringstream msgbuf2;
58                         msgbuf2 << "Applying notification '" << rule.GetName() << "' to service '" << service->GetName() << "' for rule " << di;
59                         Log(LogDebug, "icinga", msgbuf2.str());
60
61                         std::ostringstream namebuf;
62                         namebuf << service->GetName() << "!" << rule.GetName();
63                         String name = namebuf.str();
64
65                         ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
66                         builder->SetType("Notification");
67                         builder->SetName(name);
68                         builder->SetScope(rule.GetScope());
69
70                         builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet, "host", make_shared<AExpression>(&AExpression::OpLiteral, service->GetHost()->GetName(), di), di));
71                         builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet, "service", make_shared<AExpression>(&AExpression::OpLiteral, service->GetShortName(), di), di));
72                         builder->AddExpression(rule.GetExpression());
73
74                         ConfigItem::Ptr serviceItem = builder->Compile();
75                         serviceItem->Register();
76                         DynamicObject::Ptr dobj = serviceItem->Commit();
77                         dobj->OnConfigLoaded();
78                 }
79         }
80 }