1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "icinga/notification.hpp"
4 #include "icinga/service.hpp"
5 #include "config/configitembuilder.hpp"
6 #include "config/applyrule.hpp"
7 #include "base/initialize.hpp"
8 #include "base/configtype.hpp"
9 #include "base/logger.hpp"
10 #include "base/context.hpp"
11 #include "base/workqueue.hpp"
12 #include "base/exception.hpp"
13
14 using namespace icinga;
15
__anon349169960102() 16 INITIALIZE_ONCE([]() {
17 ApplyRule::RegisterType("Notification", { "Host", "Service" });
18 });
19
EvaluateApplyRuleInstance(const Checkable::Ptr & checkable,const String & name,ScriptFrame & frame,const ApplyRule & rule)20 bool Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
21 {
22 if (!rule.EvaluateFilter(frame))
23 return false;
24
25 DebugInfo di = rule.GetDebugInfo();
26
27 #ifdef _DEBUG
28 Log(LogDebug, "Notification")
29 << "Applying notification '" << name << "' to object '" << checkable->GetName() << "' for rule " << di;
30 #endif /* _DEBUG */
31
32 ConfigItemBuilder builder{di};
33 builder.SetType(Notification::TypeInstance);
34 builder.SetName(name);
35 builder.SetScope(frame.Locals->ShallowClone());
36 builder.SetIgnoreOnError(rule.GetIgnoreOnError());
37
38 Host::Ptr host;
39 Service::Ptr service;
40 tie(host, service) = GetHostService(checkable);
41
42 builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
43
44 if (service)
45 builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di));
46
47 String zone = checkable->GetZoneName();
48
49 if (!zone.IsEmpty())
50 builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "zone"), OpSetLiteral, MakeLiteral(zone), di));
51
52 builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "package"), OpSetLiteral, MakeLiteral(rule.GetPackage()), di));
53
54 builder.AddExpression(new OwnedExpression(rule.GetExpression()));
55
56 builder.AddExpression(new ImportDefaultTemplatesExpression());
57
58 ConfigItem::Ptr notificationItem = builder.Compile();
59 notificationItem->Register();
60
61 return true;
62 }
63
EvaluateApplyRule(const Checkable::Ptr & checkable,const ApplyRule & rule)64 bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
65 {
66 DebugInfo di = rule.GetDebugInfo();
67
68 std::ostringstream msgbuf;
69 msgbuf << "Evaluating 'apply' rule (" << di << ")";
70 CONTEXT(msgbuf.str());
71
72 Host::Ptr host;
73 Service::Ptr service;
74 tie(host, service) = GetHostService(checkable);
75
76 ScriptFrame frame(true);
77 if (rule.GetScope())
78 rule.GetScope()->CopyTo(frame.Locals);
79 frame.Locals->Set("host", host);
80 if (service)
81 frame.Locals->Set("service", service);
82
83 Value vinstances;
84
85 if (rule.GetFTerm()) {
86 try {
87 vinstances = rule.GetFTerm()->Evaluate(frame);
88 } catch (const std::exception&) {
89 /* Silently ignore errors here and assume there are no instances. */
90 return false;
91 }
92 } else {
93 vinstances = new Array({ "" });
94 }
95
96 bool match = false;
97
98 if (vinstances.IsObjectType<Array>()) {
99 if (!rule.GetFVVar().IsEmpty())
100 BOOST_THROW_EXCEPTION(ScriptError("Dictionary iterator requires value to be a dictionary.", di));
101
102 Array::Ptr arr = vinstances;
103
104 ObjectLock olock(arr);
105 for (const Value& instance : arr) {
106 String name = rule.GetName();
107
108 if (!rule.GetFKVar().IsEmpty()) {
109 frame.Locals->Set(rule.GetFKVar(), instance);
110 name += instance;
111 }
112
113 if (EvaluateApplyRuleInstance(checkable, name, frame, rule))
114 match = true;
115 }
116 } else if (vinstances.IsObjectType<Dictionary>()) {
117 if (rule.GetFVVar().IsEmpty())
118 BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
119
120 Dictionary::Ptr dict = vinstances;
121
122 for (const String& key : dict->GetKeys()) {
123 frame.Locals->Set(rule.GetFKVar(), key);
124 frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
125
126 if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule))
127 match = true;
128 }
129 }
130
131 return match;
132 }
133
EvaluateApplyRules(const Host::Ptr & host)134 void Notification::EvaluateApplyRules(const Host::Ptr& host)
135 {
136 CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
137
138 for (ApplyRule& rule : ApplyRule::GetRules("Notification"))
139 {
140 if (rule.GetTargetType() != "Host")
141 continue;
142
143 if (EvaluateApplyRule(host, rule))
144 rule.AddMatch();
145 }
146 }
147
EvaluateApplyRules(const Service::Ptr & service)148 void Notification::EvaluateApplyRules(const Service::Ptr& service)
149 {
150 CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
151
152 for (ApplyRule& rule : ApplyRule::GetRules("Notification")) {
153 if (rule.GetTargetType() != "Service")
154 continue;
155
156 if (EvaluateApplyRule(service, rule))
157 rule.AddMatch();
158 }
159 }
160