Value NotInExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { Value right = m_Operand2->Evaluate(frame); if (right.IsEmpty()) return true; else if (!right.IsObjectType<Array>()) BOOST_THROW_EXCEPTION(ScriptError("Invalid right side argument for 'in' operator: " + JsonEncode(right), m_DebugInfo)); Value left = m_Operand1->Evaluate(frame); Array::Ptr arr = right; return !arr->Contains(left); }
void User::AddGroup(const String& name) { boost::mutex::scoped_lock lock(m_UserMutex); Array::Ptr groups = GetGroups(); if (groups && groups->Contains(name)) return; if (!groups) groups = new Array(); groups->Add(name); }
ExpressionResult NotInExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { ExpressionResult operand2 = m_Operand2->Evaluate(frame); CHECK_RESULT(operand2); if (operand2.GetValue().IsEmpty()) return true; else if (!operand2.GetValue().IsObjectType<Array>()) BOOST_THROW_EXCEPTION(ScriptError("Invalid right side argument for 'in' operator: " + JsonEncode(operand2.GetValue()), m_DebugInfo)); ExpressionResult operand1 = m_Operand1->Evaluate(frame); CHECK_RESULT(operand1); Array::Ptr arr = operand2.GetValue(); return !arr->Contains(operand1.GetValue()); }
void Checkable::AddGroup(const String& name) { boost::mutex::scoped_lock lock(m_CheckableMutex); Array::Ptr groups; auto *host = dynamic_cast<Host *>(this); if (host) groups = host->GetGroups(); else groups = static_cast<Service *>(this)->GetGroups(); if (groups && groups->Contains(name)) return; if (!groups) groups = new Array(); groups->Add(name); }
bool HostGroup::EvaluateObjectRule(const Host::Ptr& host, const ConfigItem::Ptr& group) { String groupName = group->GetName(); CONTEXT("Evaluating rule for group '" + groupName + "'"); ScriptFrame frame(true); if (group->GetScope()) group->GetScope()->CopyTo(frame.Locals); frame.Locals->Set("host", host); if (!group->GetFilter()->Evaluate(frame).GetValue().ToBool()) return false; Log(LogDebug, "HostGroup") << "Assigning membership for group '" << groupName << "' to host '" << host->GetName() << "'"; Array::Ptr groups = host->GetGroups(); if (groups && !groups->Contains(groupName)) groups->Add(groupName); return true; }
static bool ArrayContains(const Value& value) { ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); Array::Ptr self = static_cast<Array::Ptr>(vframe->Self); return self->Contains(value); }
void Notification::BeginExecuteNotification(NotificationType type, const CheckResult::Ptr& cr, bool force, bool reminder, const String& author, const String& text) { Log(LogNotice, "Notification") << "Attempting to send " << (reminder ? "reminder " : " ") << "notifications for notification object '" << GetName() << "'."; Checkable::Ptr checkable = GetCheckable(); if (!force) { TimePeriod::Ptr tp = GetPeriod(); if (tp && !tp->IsInside(Utility::GetTime())) { Log(LogNotice, "Notification") << "Not sending " << (reminder ? "reminder " : " ") << "notifications for notification object '" << GetName() << "': not in timeperiod '" << tp->GetName() << "'"; return; } double now = Utility::GetTime(); Dictionary::Ptr times = GetTimes(); if (times && type == NotificationProblem) { Value timesBegin = times->Get("begin"); Value timesEnd = times->Get("end"); if (timesBegin != Empty && timesBegin >= 0 && now < checkable->GetLastHardStateChange() + timesBegin) { Log(LogNotice, "Notification") << "Not sending " << (reminder ? "reminder " : " ") << "notifications for notification object '" << GetName() << "': before specified begin time (" << Utility::FormatDuration(timesBegin) << ")"; /* we need to adjust the next notification time * to now + begin delaying the first notification */ double nextProposedNotification = now + timesBegin + 1.0; if (GetNextNotification() > nextProposedNotification) SetNextNotification(nextProposedNotification); return; } if (timesEnd != Empty && timesEnd >= 0 && now > checkable->GetLastHardStateChange() + timesEnd) { Log(LogNotice, "Notification") << "Not sending " << (reminder ? "reminder " : " ") << "notifications for notification object '" << GetName() << "': after specified end time (" << Utility::FormatDuration(timesEnd) << ")"; return; } } unsigned long ftype = type; Log(LogDebug, "Notification") << "Type '" << NotificationTypeToStringInternal(type) << "', TypeFilter: " << NotificationFilterToString(GetTypeFilter(), GetTypeFilterMap()) << " (FType=" << ftype << ", TypeFilter=" << GetTypeFilter() << ")"; if (!(ftype & GetTypeFilter())) { Log(LogNotice, "Notification") << "Not sending " << (reminder ? "reminder " : " ") << "notifications for notification object '" << GetName() << "': type '" << NotificationTypeToStringInternal(type) << "' does not match type filter: " << NotificationFilterToString(GetTypeFilter(), GetTypeFilterMap()) << "."; return; } /* ensure that recovery notifications are always sent, no state filter checks necessary */ if (type != NotificationRecovery) { Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); unsigned long fstate; String stateStr; if (service) { fstate = ServiceStateToFilter(service->GetState()); stateStr = NotificationServiceStateToString(service->GetState()); } else { fstate = HostStateToFilter(host->GetState()); stateStr = NotificationHostStateToString(host->GetState()); } Log(LogDebug, "Notification") << "State '" << stateStr << "', StateFilter: " << NotificationFilterToString(GetStateFilter(), GetStateFilterMap()) << " (FState=" << fstate << ", StateFilter=" << GetStateFilter() << ")"; if (!(fstate & GetStateFilter())) { Log(LogNotice, "Notification") << "Not sending " << (reminder ? "reminder " : " ") << "notifications for notification object '" << GetName() << "': state '" << stateStr << "' does not match state filter: " << NotificationFilterToString(GetStateFilter(), GetStateFilterMap()) << "."; return; } } } else { Log(LogNotice, "Notification") << "Not checking " << (reminder ? "reminder " : " ") << "notification filters for notification object '" << GetName() << "': Notification was forced."; } { ObjectLock olock(this); UpdateNotificationNumber(); double now = Utility::GetTime(); SetLastNotification(now); if (type == NotificationProblem && GetInterval() <= 0) SetNoMoreNotifications(true); else SetNoMoreNotifications(false); if (type == NotificationProblem && GetInterval() > 0) SetNextNotification(now + GetInterval()); if (type == NotificationProblem) SetLastProblemNotification(now); } std::set<User::Ptr> allUsers; std::set<User::Ptr> users = GetUsers(); std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin())); for (const UserGroup::Ptr& ug : GetUserGroups()) { std::set<User::Ptr> members = ug->GetMembers(); std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin())); } std::set<User::Ptr> allNotifiedUsers; Array::Ptr notifiedProblemUsers = GetNotifiedProblemUsers(); for (const User::Ptr& user : allUsers) { String userName = user->GetName(); if (!user->GetEnableNotifications()) { Log(LogNotice, "Notification") << "Disabled notifications for user '" << userName << "'. Not sending notification."; continue; } if (!CheckNotificationUserFilters(type, user, force, reminder)) { Log(LogNotice, "Notification") << "Notification filters for user '" << userName << "' not matched. Not sending notification."; continue; } /* on recovery, check if user was notified before */ if (type == NotificationRecovery) { if (!notifiedProblemUsers->Contains(userName)) { Log(LogNotice, "Notification") << "We did not notify user '" << userName << "' for a problem before. Not sending recovery notification."; continue; } } Log(LogInformation, "Notification") << "Sending " << (reminder ? "reminder " : "") << "'" << NotificationTypeToStringInternal(type) << "' notification '" << GetName() << " for user '" << userName << "'"; Utility::QueueAsyncCallback(boost::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force, author, text)); /* collect all notified users */ allNotifiedUsers.insert(user); /* store all notified users for later recovery checks */ if (type == NotificationProblem && !notifiedProblemUsers->Contains(userName)) notifiedProblemUsers->Add(userName); } /* if this was a recovery notification, reset all notified users */ if (type == NotificationRecovery) notifiedProblemUsers->Clear(); /* used in db_ido for notification history */ Service::OnNotificationSentToAllUsers(this, checkable, allNotifiedUsers, type, cr, author, text, MessageOrigin::Ptr()); }
String Downtime::AddDowntime(const Checkable::Ptr& checkable, const String& author, const String& comment, double startTime, double endTime, bool fixed, const String& triggeredBy, double duration, const String& scheduledDowntime, const String& scheduledBy, const String& id, const MessageOrigin::Ptr& origin) { String fullName; if (id.IsEmpty()) fullName = checkable->GetName() + "!" + Utility::NewUniqueID(); else fullName = id; Dictionary::Ptr attrs = new Dictionary(); attrs->Set("author", author); attrs->Set("comment", comment); attrs->Set("start_time", startTime); attrs->Set("end_time", endTime); attrs->Set("fixed", fixed); attrs->Set("duration", duration); attrs->Set("triggered_by", triggeredBy); attrs->Set("scheduled_by", scheduledBy); attrs->Set("config_owner", scheduledDowntime); attrs->Set("entry_time", Utility::GetTime()); Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); attrs->Set("host_name", host->GetName()); if (service) attrs->Set("service_name", service->GetShortName()); String zone = checkable->GetZoneName(); if (!zone.IsEmpty()) attrs->Set("zone", zone); String config = ConfigObjectUtility::CreateObjectConfig(Downtime::TypeInstance, fullName, true, Array::Ptr(), attrs); Array::Ptr errors = new Array(); if (!ConfigObjectUtility::CreateObject(Downtime::TypeInstance, fullName, config, errors)) { ObjectLock olock(errors); for (const String& error : errors) { Log(LogCritical, "Downtime", error); } BOOST_THROW_EXCEPTION(std::runtime_error("Could not create downtime.")); } if (!triggeredBy.IsEmpty()) { Downtime::Ptr parentDowntime = Downtime::GetByName(triggeredBy); Array::Ptr triggers = parentDowntime->GetTriggers(); ObjectLock olock(triggers); if (!triggers->Contains(fullName)) triggers->Add(fullName); } Downtime::Ptr downtime = Downtime::GetByName(fullName); if (!downtime) BOOST_THROW_EXCEPTION(std::runtime_error("Could not create downtime object.")); Log(LogNotice, "Downtime") << "Added downtime '" << downtime->GetName() << "' between '" << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", startTime) << "' and '" << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", endTime) << "'."; return fullName; }