]> granicus.if.org Git - icinga2/blobdiff - lib/icinga/notification.cpp
Remove the HostUnreachable state.
[icinga2] / lib / icinga / notification.cpp
index 922e765cd0adfd603050dfec5d13541c99ceee15..c143ee4d787958edfb15f65812ef688156a5e80a 100644 (file)
@@ -21,6 +21,7 @@
 #include "icinga/notificationcommand.h"
 #include "icinga/macroprocessor.h"
 #include "icinga/service.h"
+#include "config/configcompilercontext.h"
 #include "base/dynamictype.h"
 #include "base/objectlock.h"
 #include "base/logger_fwd.h"
 #include "base/exception.h"
 #include "base/initialize.h"
 #include "base/scriptvariable.h"
+#include "base/scriptfunction.h"
 #include <boost/foreach.hpp>
 
 using namespace icinga;
 
 REGISTER_TYPE(Notification);
+REGISTER_SCRIPTFUNCTION(ValidateNotificationFilters, &Notification::ValidateFilters);
 INITIALIZE_ONCE(&Notification::StaticInitialize);
 
 boost::signals2::signal<void (const Notification::Ptr&, double, const String&)> Notification::OnNextNotificationChanged;
 
+String NotificationNameComposer::MakeName(const String& shortName, const Dictionary::Ptr props) const
+{
+       if (!props)
+               return "";
+
+       String name = props->Get("host_name");
+
+       if (props->Contains("service_name"))
+               name += "!" + props->Get("service_name");
+
+       name += "!" + shortName;
+
+       return name;
+}
+
 void Notification::StaticInitialize(void)
 {
-       ScriptVariable::Set("NotificationDowntimeStart", NotificationDowntimeStart, true, true);
-       ScriptVariable::Set("NotificationDowntimeEnd", NotificationDowntimeEnd, true, true);
-       ScriptVariable::Set("NotificationDowntimeRemoved", NotificationDowntimeRemoved, true, true);
-       ScriptVariable::Set("NotificationCustom", NotificationCustom, true, true);
-       ScriptVariable::Set("NotificationAcknowledgement", NotificationAcknowledgement, true, true);
-       ScriptVariable::Set("NotificationProblem", NotificationProblem, true, true);
-       ScriptVariable::Set("NotificationRecovery", NotificationRecovery, true, true);
-       ScriptVariable::Set("NotificationFlappingStart", NotificationFlappingStart, true, true);
-       ScriptVariable::Set("NotificationFlappingEnd", NotificationFlappingEnd, true, true);
-
-       ScriptVariable::Set("NotificationFilterDowntimeStart", 1 << NotificationDowntimeStart, true, true);
-       ScriptVariable::Set("NotificationFilterDowntimeEnd", 1 << NotificationDowntimeEnd, true, true);
-       ScriptVariable::Set("NotificationFilterDowntimeRemoved", 1 << NotificationDowntimeRemoved, true, true);
-       ScriptVariable::Set("NotificationFilterCustom", 1 << NotificationCustom, true, true);
-       ScriptVariable::Set("NotificationFilterAcknowledgement", 1 << NotificationAcknowledgement, true, true);
-       ScriptVariable::Set("NotificationFilterProblem", 1 << NotificationProblem, true, true);
-       ScriptVariable::Set("NotificationFilterRecovery", 1 << NotificationRecovery, true, true);
-       ScriptVariable::Set("NotificationFilterFlappingStart", 1 << NotificationFlappingStart, true, true);
-       ScriptVariable::Set("NotificationFilterFlappingEnd", 1 << NotificationFlappingEnd, true, true);
+       ScriptVariable::Set("OK", StateFilterOK, true, true);
+       ScriptVariable::Set("Warning", StateFilterWarning, true, true);
+       ScriptVariable::Set("Critical", StateCritical, true, true);
+       ScriptVariable::Set("Unknown", StateUnknown, true, true);
+       ScriptVariable::Set("Up", StateFilterUp, true, true);
+       ScriptVariable::Set("Down", StateFilterDown, true, true);
+
+       ScriptVariable::Set("DowntimeStart", 1 << NotificationDowntimeStart, true, true);
+       ScriptVariable::Set("DowntimeEnd", 1 << NotificationDowntimeEnd, true, true);
+       ScriptVariable::Set("DowntimeRemoved", 1 << NotificationDowntimeRemoved, true, true);
+       ScriptVariable::Set("Custom", 1 << NotificationCustom, true, true);
+       ScriptVariable::Set("Acknowledgement", 1 << NotificationAcknowledgement, true, true);
+       ScriptVariable::Set("Problem", 1 << NotificationProblem, true, true);
+       ScriptVariable::Set("Recovery", 1 << NotificationRecovery, true, true);
+       ScriptVariable::Set("FlappingStart", 1 << NotificationFlappingStart, true, true);
+       ScriptVariable::Set("FlappingEnd", 1 << NotificationFlappingEnd, true, true);
+}
+
+void Notification::OnConfigLoaded(void)
+{
+       SetNotificationTypeFilter(FilterArrayToInt(GetNotificationTypeFilterRaw(), 0));
+       SetNotificationStateFilter(FilterArrayToInt(GetNotificationStateFilterRaw(), 0));
+
+       GetCheckable()->AddNotification(GetSelf());
 }
 
 void Notification::Start(void)
 {
        DynamicObject::Start();
 
-       GetService()->AddNotification(GetSelf());
+       GetCheckable()->AddNotification(GetSelf());
 }
 
 void Notification::Stop(void)
 {
        DynamicObject::Stop();
 
-       GetService()->RemoveNotification(GetSelf());
+       GetCheckable()->RemoveNotification(GetSelf());
 }
 
-Service::Ptr Notification::GetService(void) const
+Checkable::Ptr Notification::GetCheckable(void) const
 {
-       Host::Ptr host = Host::GetByName(GetHostRaw());
+       Host::Ptr host = Host::GetByName(GetHostName());
 
-       if (GetServiceRaw().IsEmpty())
-               return host->GetCheckService();
+       if (GetServiceName().IsEmpty())
+               return host;
        else
-               return host->GetServiceByShortName(GetServiceRaw());
+               return host->GetServiceByShortName(GetServiceName());
 }
 
 NotificationCommand::Ptr Notification::GetNotificationCommand(void) const
@@ -195,6 +218,8 @@ void Notification::BeginExecuteNotification(NotificationType type, const CheckRe
 {
        ASSERT(!OwnsLock());
 
+       Checkable::Ptr checkable = GetCheckable();
+
        if (!force) {
                TimePeriod::Ptr tp = GetNotificationPeriod();
 
@@ -205,15 +230,14 @@ void Notification::BeginExecuteNotification(NotificationType type, const CheckRe
 
                double now = Utility::GetTime();
                Dictionary::Ptr times = GetTimes();
-               Service::Ptr service = GetService();
 
                if (type == NotificationProblem) {
-                       if (times && times->Contains("begin") && now < service->GetLastHardStateChange() + times->Get("begin")) {
+                       if (times && times->Contains("begin") && now < checkable->GetLastHardStateChange() + times->Get("begin")) {
                                Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': before escalation range");
                                return;
                        }
 
-                       if (times && times->Contains("end") && now > service->GetLastHardStateChange() + times->Get("end")) {
+                       if (times && times->Contains("end") && now > checkable->GetLastHardStateChange() + times->Get("end")) {
                                Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': after escalation range");
                                return;
                        }
@@ -228,7 +252,20 @@ void Notification::BeginExecuteNotification(NotificationType type, const CheckRe
                        return;
                }
 
-               unsigned long fstate = 1 << GetService()->GetState();
+               Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
+               Host::Ptr host;
+
+               if (service)
+                       host = service->GetHost();
+               else
+                       host = static_pointer_cast<Host>(checkable);
+
+               unsigned long fstate;
+
+               if (service)
+                       fstate = ServiceStateToFilter(service->GetState());
+               else
+                       fstate = HostStateToFilter(host->GetState());
 
                if (!(fstate & GetNotificationStateFilter())) {
                        Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': state filter does not match");
@@ -256,7 +293,7 @@ void Notification::BeginExecuteNotification(NotificationType type, const CheckRe
                std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
        }
 
-       Service::OnNotificationSendStart(GetSelf(), GetService(), allUsers, type, cr, author, text);
+       Service::OnNotificationSendStart(GetSelf(), checkable, allUsers, type, cr, author, text);
 
        std::set<User::Ptr> allNotifiedUsers;
        BOOST_FOREACH(const User::Ptr& user, allUsers) {
@@ -271,7 +308,7 @@ void Notification::BeginExecuteNotification(NotificationType type, const CheckRe
        }
 
        /* used in db_ido for notification history */
-       Service::OnNotificationSentToAllUsers(GetSelf(), GetService(), allNotifiedUsers, type, cr, author, text);
+       Service::OnNotificationSentToAllUsers(GetSelf(), checkable, allNotifiedUsers, type, cr, author, text);
 }
 
 bool Notification::CheckNotificationUserFilters(NotificationType type, const User::Ptr& user, bool force)
@@ -295,7 +332,21 @@ bool Notification::CheckNotificationUserFilters(NotificationType type, const Use
                        return false;
                }
 
-               unsigned long fstate = 1 << GetService()->GetState();
+               Checkable::Ptr checkable = GetCheckable();
+               Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
+               Host::Ptr host;
+
+               if (service)
+                               host = service->GetHost();
+               else
+                               host = static_pointer_cast<Host>(checkable);
+
+               unsigned long fstate;
+
+               if (service)
+                               fstate = ServiceStateToFilter(service->GetState());
+               else
+                               fstate = HostStateToFilter(host->GetState());
 
                if (!(fstate & user->GetNotificationStateFilter())) {
                        Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
@@ -328,30 +379,97 @@ void Notification::ExecuteNotificationHelper(NotificationType type, const User::
                }
 
                /* required by compatlogger */
-               Service::OnNotificationSentToUser(GetSelf(), GetService(), user, type, cr, author, text, command->GetName());
+               Service::OnNotificationSentToUser(GetSelf(), GetCheckable(), user, type, cr, author, text, command->GetName());
 
-               Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
+               Log(LogInformation, "icinga", "Completed sending notification for object '" + GetCheckable()->GetName() + "'");
        } catch (const std::exception& ex) {
                std::ostringstream msgbuf;
-               msgbuf << "Exception occured during notification for service '"
-                      << GetService()->GetName() << "': " << DiagnosticInformation(ex);
+               msgbuf << "Exception occured during notification for object '"
+                      << GetCheckable()->GetName() << "': " << DiagnosticInformation(ex);
                Log(LogWarning, "icinga", msgbuf.str());
        }
 }
 
-bool Notification::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
+int icinga::ServiceStateToFilter(ServiceState state)
+{
+       switch (state) {
+               case StateOK:
+                       return StateFilterOK;
+               case StateWarning:
+                       return StateFilterWarning;
+               case StateCritical:
+                       return StateFilterCritical;
+               case StateUnknown:
+                       return StateFilterUnknown;
+               default:
+                       VERIFY(!"Invalid state type.");
+       }
+}
+
+int icinga::HostStateToFilter(HostState state)
 {
-       if (macro.SubStr(0, 13) == "_NOTIFICATION") {
-               Dictionary::Ptr custom = GetCustom();
-               *result = custom ? custom->Get(macro.SubStr(13)) : "";
-               return true;
+       switch (state) {
+               case HostUp:
+                       return StateFilterUp;
+               case HostDown:
+                       return StateFilterDown;
+               default:
+                       VERIFY(!"Invalid state type.");
+       }
+}
+
+int icinga::FilterArrayToInt(const Array::Ptr& typeFilters, int defaultValue)
+{
+       Value resultTypeFilter;
+
+       if (!typeFilters || typeFilters->GetLength() == 0)
+               return defaultValue;
+
+       resultTypeFilter = 0;
+
+       ObjectLock olock(typeFilters);
+       BOOST_FOREACH(const Value& typeFilter, typeFilters) {
+               resultTypeFilter = resultTypeFilter | typeFilter;
+       }
+
+       return resultTypeFilter;
+}
+
+void Notification::ValidateFilters(const String& location, const Dictionary::Ptr& attrs)
+{
+       int sfilter = FilterArrayToInt(attrs->Get("notification_state_filter"), 0);
+
+       if (!attrs->Contains("service_name") && (sfilter & ~(StateFilterUp | StateFilterDown)) != 0) {
+               ConfigCompilerContext::GetInstance()->AddMessage(true, "Validation failed for " +
+                   location + ": State filter is invalid.");
+       }
+
+       if (attrs->Contains("service_name") && (sfilter & ~(StateFilterOK | StateFilterWarning | StateFilterCritical | StateFilterUnknown)) != 0) {
+               ConfigCompilerContext::GetInstance()->AddMessage(true, "Validation failed for " +
+                   location + ": State filter is invalid.");
+       }
+
+       int tfilter = FilterArrayToInt(attrs->Get("notification_type_filter"), 0);
+
+       if ((tfilter & ~(1 << NotificationDowntimeStart | 1 << NotificationDowntimeEnd | 1 << NotificationDowntimeRemoved |
+           1 << NotificationCustom | 1 << NotificationAcknowledgement | 1 << NotificationProblem | 1 << NotificationRecovery |
+           1 << NotificationFlappingStart | 1 << NotificationFlappingEnd)) != 0) {
+               ConfigCompilerContext::GetInstance()->AddMessage(true, "Validation failed for " +
+                   location + ": Type filter is invalid.");
        }
+}
+
+bool Notification::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
+{
+       Dictionary::Ptr vars = GetVars();
 
-       Dictionary::Ptr macros = GetMacros();
+       if (macro.SubStr(0, 13) == "notification.") {
+               String key = vars->Get(macro.SubStr(13));
 
-       if (macros && macros->Contains(macro)) {
-               *result = macros->Get(macro);
-               return true;
+               if (vars && vars->Contains(key)) {
+                       *result = vars->Get(key);
+                       return true;
+               }
        }
 
        return false;