notes = "Interface check for Port " + string(vars.port) + " in VLAN " + vars.vlan + " on Address " + vars.address + " QoS " + vars.qos
notes_url = "http://foreman.company.com/hosts/" + host.name
action_url = "http://snmp.checker.company.com/" + host.name + "if-" + if_name
-
- assign where host.vars.interfaces
}
Note that numbers must be explicitely casted to string when adding to strings.
notes_url = "http://foreman.company.com/hosts/" + host.name
action_url = "http://snmp.checker.company.com/" + host.name + "/" + vars.customer_id
-
- assign where host.vars.hosting
}
### <a id="groups"></a> Groups
vars.disk_wfree = 10
vars.disk_cfree = 5
-
- assign where host.vars.local_disks
}
check_command = "disk"
vars += config
-
- assign where host.vars.disks
}
A similar example is used for the `http` services. That way you can make your
check_command = "http"
vars += config
-
- assign where host.vars.http_vhosts
}
apply Service for (disk => config in host.vars.disks) {
check_command = "disk"
vars += config
-
- assign where host.vars.disks
}
apply Service "icinga" {
CopyTo(clone);
return clone;
}
+
+/**
+ * Returns an array containing all keys
+ * which are currently set in this directory.
+ *
+ * @returns an array of key names
+ */
+std::vector<String> Dictionary::GetKeys(void) const
+{
+ ASSERT(!OwnsLock());
+ ObjectLock olock(this);
+
+ std::vector<String> keys;
+
+ BOOST_FOREACH(const Dictionary::Pair& kv, m_Data) {
+ keys.push_back(kv.first);
+ }
+
+ return keys;
+}
#include "base/value.hpp"
#include <boost/range/iterator.hpp>
#include <map>
+#include <vector>
namespace icinga
{
void CopyTo(const Dictionary::Ptr& dest) const;
Dictionary::Ptr ShallowClone(void) const;
+ std::vector<String> GetKeys(void) const;
+
static Object::Ptr GetPrototype(void);
private:
DictExpression *exprl = dynamic_cast<DictExpression *>($8);
exprl->MakeInline();
- // assign && !ignore
- if (!context->m_SeenAssign.top())
- BOOST_THROW_EXCEPTION(ScriptError("'apply' is missing 'assign'", DebugInfoRange(@2, @3)));
-
+ bool seen_assign = context->m_SeenAssign.top();
context->m_SeenAssign.pop();
+ // assign && !ignore
+ if (!seen_assign && !context->m_FTerm.top())
+ BOOST_THROW_EXCEPTION(ScriptError("'apply' is missing 'assign'/'for'", DebugInfoRange(@2, @3)));
+
Expression *ignore = context->m_Ignore.top();
context->m_Ignore.pop();
- Expression *assign = context->m_Assign.top();
+ Expression *assign;
+
+ if (!seen_assign)
+ assign = MakeLiteral(true);
+ else
+ assign = context->m_Assign.top();
+
context->m_Assign.pop();
Expression *filter;
ApplyRule::RegisterType("Dependency", targets);
}
-void Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
+bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
{
+ if (!rule.EvaluateFilter(frame))
+ return false;
+
DebugInfo di = rule.GetDebugInfo();
Log(LogDebug, "Dependency")
ConfigItem::Ptr dependencyItem = builder->Compile();
dependencyItem->Commit();
+
+ return true;
}
bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
if (service)
frame.Locals->Set("service", service);
- if (!rule.EvaluateFilter(frame))
- return false;
-
Value vinstances;
if (rule.GetFTerm()) {
- vinstances = rule.GetFTerm()->Evaluate(frame);
+ try {
+ vinstances = rule.GetFTerm()->Evaluate(frame);
+ } catch (const std::exception&) {
+ /* Silently ignore errors here and assume there are no instances. */
+ return false;
+ }
} else {
Array::Ptr instances = new Array();
instances->Add("");
vinstances = instances;
}
+ bool match = false;
+
if (vinstances.IsObjectType<Array>()) {
if (!rule.GetFVVar().IsEmpty())
BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
Array::Ptr arr = vinstances;
+ Array::Ptr arrclone = arr->ShallowClone();
- ObjectLock olock(arr);
- BOOST_FOREACH(const String& instance, arr) {
+ ObjectLock olock(arrclone);
+ BOOST_FOREACH(const String& instance, arrclone) {
String name = rule.GetName();
if (!rule.GetFKVar().IsEmpty()) {
name += instance;
}
- EvaluateApplyRuleInstance(checkable, name, frame, rule);
+ if (EvaluateApplyRuleInstance(checkable, name, frame, rule))
+ match = true;
}
} else if (vinstances.IsObjectType<Dictionary>()) {
if (rule.GetFVVar().IsEmpty())
Dictionary::Ptr dict = vinstances;
- ObjectLock olock(dict);
- BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
- frame.Locals->Set(rule.GetFKVar(), kv.first);
- frame.Locals->Set(rule.GetFVVar(), kv.second);
+ BOOST_FOREACH(const String& key, dict->GetKeys()) {
+ frame.Locals->Set(rule.GetFKVar(), key);
+ frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
- EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule);
+ if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule))
+ match = true;
}
}
- return true;
+ return match;
}
void Dependency::EvaluateApplyRules(const Host::Ptr& host)
Checkable::Ptr m_Parent;
Checkable::Ptr m_Child;
- static void EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule);
+ static bool EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule);
static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule);
};
ApplyRule::RegisterType("Notification", targets);
}
-void Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
+bool Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
{
+ if (!rule.EvaluateFilter(frame))
+ return false;
+
DebugInfo di = rule.GetDebugInfo();
Log(LogDebug, "Notification")
ConfigItem::Ptr notificationItem = builder->Compile();
notificationItem->Commit();
+
+ return true;
}
bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
if (service)
frame.Locals->Set("service", service);
- if (!rule.EvaluateFilter(frame))
- return false;
-
Value vinstances;
if (rule.GetFTerm()) {
- vinstances = rule.GetFTerm()->Evaluate(frame);
+ try {
+ vinstances = rule.GetFTerm()->Evaluate(frame);
+ } catch (const std::exception&) {
+ /* Silently ignore errors here and assume there are no instances. */
+ return false;
+ }
} else {
Array::Ptr instances = new Array();
instances->Add("");
vinstances = instances;
}
+ bool match = false;
+
if (vinstances.IsObjectType<Array>()) {
if (!rule.GetFVVar().IsEmpty())
BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
name += instance;
}
- EvaluateApplyRuleInstance(checkable, name, frame, rule);
+ if (EvaluateApplyRuleInstance(checkable, name, frame, rule))
+ match = true;
}
} else if (vinstances.IsObjectType<Dictionary>()) {
if (rule.GetFVVar().IsEmpty())
Dictionary::Ptr dict = vinstances;
- ObjectLock olock(dict);
- BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
- frame.Locals->Set(rule.GetFKVar(), kv.first);
- frame.Locals->Set(rule.GetFVVar(), kv.second);
+ BOOST_FOREACH(const String& key, dict->GetKeys()) {
+ frame.Locals->Set(rule.GetFKVar(), key);
+ frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
- EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule);
+ if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule))
+ match = true;
}
}
- return true;
+ return match;
}
void Notification::EvaluateApplyRules(const Host::Ptr& host)
private:
void ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author = "", const String& text = "");
- static void EvaluateApplyRuleInstance(const intrusive_ptr<Checkable>& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule);
+ static bool EvaluateApplyRuleInstance(const intrusive_ptr<Checkable>& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule);
static bool EvaluateApplyRule(const intrusive_ptr<Checkable>& checkable, const ApplyRule& rule);
};
ApplyRule::RegisterType("ScheduledDowntime", targets);
}
-void ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
+bool ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
{
+ if (!rule.EvaluateFilter(frame))
+ return false;
+
DebugInfo di = rule.GetDebugInfo();
Log(LogDebug, "ScheduledDowntime")
ConfigItem::Ptr downtimeItem = builder->Compile();
downtimeItem->Commit();
+
+ return true;
}
bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
if (service)
frame.Locals->Set("service", service);
- if (!rule.EvaluateFilter(frame))
- return false;
-
Value vinstances;
if (rule.GetFTerm()) {
- vinstances = rule.GetFTerm()->Evaluate(frame);
+ try {
+ vinstances = rule.GetFTerm()->Evaluate(frame);
+ } catch (const std::exception&) {
+ /* Silently ignore errors here and assume there are no instances. */
+ return false;
+ }
} else {
Array::Ptr instances = new Array();
instances->Add("");
vinstances = instances;
}
+ bool match = false;
+
if (vinstances.IsObjectType<Array>()) {
if (!rule.GetFVVar().IsEmpty())
BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
Array::Ptr arr = vinstances;
+ Array::Ptr arrclone = arr->ShallowClone();
- ObjectLock olock(arr);
- BOOST_FOREACH(const String& instance, arr) {
+ ObjectLock olock(arrclone);
+ BOOST_FOREACH(const String& instance, arrclone) {
String name = rule.GetName();
if (!rule.GetFKVar().IsEmpty()) {
name += instance;
}
- EvaluateApplyRuleInstance(checkable, name, frame, rule);
+ if (EvaluateApplyRuleInstance(checkable, name, frame, rule))
+ match = true;
}
} else if (vinstances.IsObjectType<Dictionary>()) {
if (rule.GetFVVar().IsEmpty())
Dictionary::Ptr dict = vinstances;
- ObjectLock olock(dict);
- BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
- frame.Locals->Set(rule.GetFKVar(), kv.first);
- frame.Locals->Set(rule.GetFVVar(), kv.second);
+ BOOST_FOREACH(const String& key, dict->GetKeys()) {
+ frame.Locals->Set(rule.GetFKVar(), key);
+ frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
- EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule);
+ if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule))
+ match = true;
}
}
- return true;
+ return match;
}
void ScheduledDowntime::EvaluateApplyRules(const Host::Ptr& host)
std::pair<double, double> FindNextSegment(void);
void CreateNextDowntime(void);
- static void EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule);
+ static bool EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule);
static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule);
};
ApplyRule::RegisterType("Service", targets);
}
-void Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& name, ScriptFrame& frame, const ApplyRule& rule)
+bool Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& name, ScriptFrame& frame, const ApplyRule& rule)
{
+ if (!rule.EvaluateFilter(frame))
+ return false;
+
DebugInfo di = rule.GetDebugInfo();
Log(LogDebug, "Service")
ConfigItem::Ptr serviceItem = builder->Compile();
serviceItem->Commit();
+
+ return true;
}
bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule)
rule.GetScope()->CopyTo(frame.Locals);
frame.Locals->Set("host", host);
- if (!rule.EvaluateFilter(frame))
- return false;
-
Value vinstances;
if (rule.GetFTerm()) {
- vinstances = rule.GetFTerm()->Evaluate(frame);
+ try {
+ vinstances = rule.GetFTerm()->Evaluate(frame);
+ } catch (const std::exception&) {
+ /* Silently ignore errors here and assume there are no instances. */
+ return false;
+ }
} else {
Array::Ptr instances = new Array();
instances->Add("");
vinstances = instances;
}
+ bool match = false;
+
if (vinstances.IsObjectType<Array>()) {
if (!rule.GetFVVar().IsEmpty())
BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
Array::Ptr arr = vinstances;
+ Array::Ptr arrclone = arr->ShallowClone();
- ObjectLock olock(arr);
- BOOST_FOREACH(const String& instance, arr) {
+ ObjectLock olock(arrclone);
+ BOOST_FOREACH(const String& instance, arrclone) {
String name = rule.GetName();
if (!rule.GetFKVar().IsEmpty()) {
name += instance;
}
- EvaluateApplyRuleInstance(host, name, frame, rule);
+ if (EvaluateApplyRuleInstance(host, name, frame, rule))
+ match = true;
}
} else if (vinstances.IsObjectType<Dictionary>()) {
if (rule.GetFVVar().IsEmpty())
Dictionary::Ptr dict = vinstances;
- ObjectLock olock(dict);
- BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
- frame.Locals->Set(rule.GetFKVar(), kv.first);
- frame.Locals->Set(rule.GetFVVar(), kv.second);
+ BOOST_FOREACH(const String& key, dict->GetKeys()) {
+ frame.Locals->Set(rule.GetFKVar(), key);
+ frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
- EvaluateApplyRuleInstance(host, rule.GetName() + kv.first, frame, rule);
+ if (EvaluateApplyRuleInstance(host, rule.GetName() + key, frame, rule))
+ match = true;
}
}
- return true;
+ return match;
}
void Service::EvaluateApplyRules(const Host::Ptr& host)
private:
Host::Ptr m_Host;
- static void EvaluateApplyRuleInstance(const Host::Ptr& host, const String& name, ScriptFrame& frame, const ApplyRule& rule);
+ static bool EvaluateApplyRuleInstance(const Host::Ptr& host, const String& name, ScriptFrame& frame, const ApplyRule& rule);
static bool EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule);
};