Implement the 'notification_period' attribute.

This commit is contained in:
Gunnar Beutner 2013-03-21 13:42:46 +01:00
parent 8037612e72
commit 99bd0f0189
7 changed files with 69 additions and 22 deletions

View File

@ -92,12 +92,14 @@ void NotificationComponent::NotificationTimerHandler(void)
send_notification = reachable && !service->IsInDowntime() && !service->IsAcknowledged(); send_notification = reachable && !service->IsInDowntime() && !service->IsAcknowledged();
} }
notification->SetNextNotification(Utility::GetTime() + notification->GetNotificationInterval());
if (!send_notification) if (!send_notification)
continue; continue;
try { try {
Log(LogInformation, "notification", "Sending reminder notification for service '" + service->GetName() + "'"); Log(LogInformation, "notification", "Sending reminder notification for service '" + service->GetName() + "'");
notification->BeginExecuteNotification(NotificationProblem, service->GetLastCheckResult()); notification->BeginExecuteNotification(NotificationProblem, service->GetLastCheckResult(), false);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
std::ostringstream msgbuf; std::ostringstream msgbuf;
msgbuf << "Exception occured during notification for service '" msgbuf << "Exception occured during notification for service '"
@ -106,8 +108,6 @@ void NotificationComponent::NotificationTimerHandler(void)
Log(LogWarning, "icinga", message); Log(LogWarning, "icinga", message);
} }
notification->SetNextNotification(Utility::GetTime() + notification->GetNotificationInterval());
} }
} }

View File

@ -54,6 +54,9 @@ type Host {
%attribute number "check_interval", %attribute number "check_interval",
%attribute number "retry_interval", %attribute number "retry_interval",
%attribute number "notification_interval",
%attribute string "notification_period",
%attribute array "servicegroups" { %attribute array "servicegroups" {
%attribute string "*" %attribute string "*"
}, },
@ -99,7 +102,10 @@ type Host {
%attribute string "check_period", %attribute string "check_period",
%attribute number "check_interval", %attribute number "check_interval",
%attribute number "retry_interval", %attribute number "retry_interval",
%attribute number "notification_interval", %attribute number "notification_interval",
%attribute string "notification_period",
%attribute dictionary "macros" { %attribute dictionary "macros" {
%attribute string "*" %attribute string "*"
}, },
@ -145,6 +151,10 @@ type Service {
%attribute string "check_period", %attribute string "check_period",
%attribute number "check_interval", %attribute number "check_interval",
%attribute number "retry_interval", %attribute number "retry_interval",
%attribute number "notification_interval",
%attribute string "notification_period",
%attribute array "hostdependencies" { %attribute array "hostdependencies" {
%attribute string "*" %attribute string "*"
}, },
@ -187,9 +197,7 @@ type Service {
%attribute string "*" %attribute string "*"
} }
} }
}, }
%attribute number "notification_interval"
} }
type ServiceGroup { type ServiceGroup {
@ -225,7 +233,8 @@ type Notification {
}, },
%attribute string "notification_command", %attribute string "notification_command",
%attribute number "notification_interval" %attribute number "notification_interval",
%attribute string "notification_period"
} }
type User { type User {

View File

@ -36,6 +36,7 @@ Notification::Notification(const Dictionary::Ptr& serializedUpdate)
{ {
RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand); RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
RegisterAttribute("notification_interval", Attribute_Config, &m_NotificationInterval); RegisterAttribute("notification_interval", Attribute_Config, &m_NotificationInterval);
RegisterAttribute("notification_period", Attribute_Config, &m_NotificationPeriod);
RegisterAttribute("last_notification", Attribute_Replicated, &m_LastNotification); RegisterAttribute("last_notification", Attribute_Replicated, &m_LastNotification);
RegisterAttribute("next_notification", Attribute_Replicated, &m_NextNotification); RegisterAttribute("next_notification", Attribute_Replicated, &m_NextNotification);
RegisterAttribute("macros", Attribute_Config, &m_Macros); RegisterAttribute("macros", Attribute_Config, &m_Macros);
@ -153,6 +154,14 @@ double Notification::GetNotificationInterval(void) const
return m_NotificationInterval; return m_NotificationInterval;
} }
/**
* @threadsafety Always.
*/
TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
{
return TimePeriod::GetByName(m_NotificationPeriod);
}
/** /**
* @threadsafety Always. * @threadsafety Always.
*/ */
@ -222,10 +231,19 @@ String Notification::NotificationTypeToString(NotificationType type)
/** /**
* @threadsafety Always. * @threadsafety Always.
*/ */
void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr) void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool ignore_timeperiod)
{ {
ASSERT(!OwnsLock()); ASSERT(!OwnsLock());
if (!ignore_timeperiod) {
TimePeriod::Ptr tp = GetNotificationPeriod();
if (tp && !tp->IsInside(Utility::GetTime())) {
Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
return;
}
}
{ {
ObjectLock olock(this); ObjectLock olock(this);
@ -246,28 +264,32 @@ void Notification::BeginExecuteNotification(NotificationType type, const Diction
BOOST_FOREACH(const User::Ptr& user, allUsers) { BOOST_FOREACH(const User::Ptr& user, allUsers) {
Log(LogDebug, "icinga", "Sending notification for user " + user->GetName()); Log(LogDebug, "icinga", "Sending notification for user " + user->GetName());
BeginExecuteNotificationHelper(macros, type, user); BeginExecuteNotificationHelper(macros, type, user, ignore_timeperiod);
}
if (allUsers.empty()) {
/* Send a notification even if there are no users specified. */
BeginExecuteNotificationHelper(macros, type, User::Ptr());
} }
} }
/** /**
* @threadsafety Always. * @threadsafety Always.
*/ */
void Notification::BeginExecuteNotificationHelper(const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user) void Notification::BeginExecuteNotificationHelper(const Dictionary::Ptr& notificationMacros,
NotificationType type, const User::Ptr& user, bool ignore_timeperiod)
{ {
ASSERT(!OwnsLock()); ASSERT(!OwnsLock());
if (!ignore_timeperiod) {
TimePeriod::Ptr tp = user->GetNotificationPeriod();
if (tp && !tp->IsInside(Utility::GetTime())) {
Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
GetName() + " and user '" + user->GetName() + "': user not in timeperiod");
return;
}
}
std::vector<Dictionary::Ptr> macroDicts; std::vector<Dictionary::Ptr> macroDicts;
if (user) { macroDicts.push_back(user->GetMacros());
macroDicts.push_back(user->GetMacros()); macroDicts.push_back(user->CalculateDynamicMacros());
macroDicts.push_back(user->CalculateDynamicMacros());
}
macroDicts.push_back(notificationMacros); macroDicts.push_back(notificationMacros);

View File

@ -23,6 +23,7 @@
#include "icinga/i2-icinga.h" #include "icinga/i2-icinga.h"
#include "icinga/user.h" #include "icinga/user.h"
#include "icinga/usergroup.h" #include "icinga/usergroup.h"
#include "icinga/timeperiod.h"
#include "base/array.h" #include "base/array.h"
namespace icinga namespace icinga
@ -65,6 +66,7 @@ public:
shared_ptr<Service> GetService(void) const; shared_ptr<Service> GetService(void) const;
Value GetNotificationCommand(void) const; Value GetNotificationCommand(void) const;
double GetNotificationInterval(void) const; double GetNotificationInterval(void) const;
TimePeriod::Ptr GetNotificationPeriod(void) const;
Dictionary::Ptr GetMacros(void) const; Dictionary::Ptr GetMacros(void) const;
std::set<User::Ptr> GetUsers(void) const; std::set<User::Ptr> GetUsers(void) const;
std::set<UserGroup::Ptr> GetGroups(void) const; std::set<UserGroup::Ptr> GetGroups(void) const;
@ -75,7 +77,7 @@ public:
double GetNextNotification(void) const; double GetNextNotification(void) const;
void SetNextNotification(double time); void SetNextNotification(double time);
void BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr); void BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool ignore_timeperiod);
static String NotificationTypeToString(NotificationType type); static String NotificationTypeToString(NotificationType type);
@ -85,6 +87,7 @@ protected:
private: private:
Attribute<Value> m_NotificationCommand; Attribute<Value> m_NotificationCommand;
Attribute<double> m_NotificationInterval; Attribute<double> m_NotificationInterval;
Attribute<String> m_NotificationPeriod;
Attribute<double> m_LastNotification; Attribute<double> m_LastNotification;
Attribute<double> m_NextNotification; Attribute<double> m_NextNotification;
Attribute<Dictionary::Ptr> m_Macros; Attribute<Dictionary::Ptr> m_Macros;
@ -98,7 +101,7 @@ private:
void NotificationCompletedHandler(const ScriptTask::Ptr& task); void NotificationCompletedHandler(const ScriptTask::Ptr& task);
void BeginExecuteNotificationHelper(const Dictionary::Ptr& notificationMacros, void BeginExecuteNotificationHelper(const Dictionary::Ptr& notificationMacros,
NotificationType type, const User::Ptr& user); NotificationType type, const User::Ptr& user, bool ignore_timeperiod);
}; };
} }

View File

@ -60,12 +60,15 @@ void Service::RequestNotifications(NotificationType type, const Dictionary::Ptr&
*/ */
void Service::SendNotifications(NotificationType type, const Dictionary::Ptr& cr) void Service::SendNotifications(NotificationType type, const Dictionary::Ptr& cr)
{ {
bool force = false;
if (!GetEnableNotifications()) { if (!GetEnableNotifications()) {
if (!GetForceNextNotification()) { if (!GetForceNextNotification()) {
Log(LogInformation, "icinga", "Notifications are disabled for service '" + GetName() + "'."); Log(LogInformation, "icinga", "Notifications are disabled for service '" + GetName() + "'.");
return; return;
} }
force = true;
SetForceNextNotification(false); SetForceNextNotification(false);
} }
@ -78,7 +81,7 @@ void Service::SendNotifications(NotificationType type, const Dictionary::Ptr& cr
BOOST_FOREACH(const Notification::Ptr& notification, notifications) { BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
try { try {
notification->BeginExecuteNotification(type, cr); notification->BeginExecuteNotification(type, cr, force);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
std::ostringstream msgbuf; std::ostringstream msgbuf;
msgbuf << "Exception occured during notification for service '" msgbuf << "Exception occured during notification for service '"
@ -254,6 +257,7 @@ void Service::UpdateSlaveNotifications(void)
keys.insert("users"); keys.insert("users");
keys.insert("groups"); keys.insert("groups");
keys.insert("notification_interval"); keys.insert("notification_interval");
keys.insert("notification_period");
ExpressionList::Ptr svc_exprl = boost::make_shared<ExpressionList>(); ExpressionList::Ptr svc_exprl = boost::make_shared<ExpressionList>();
item->GetLinkedExpressionList()->ExtractFiltered(keys, svc_exprl); item->GetLinkedExpressionList()->ExtractFiltered(keys, svc_exprl);

View File

@ -32,6 +32,7 @@ User::User(const Dictionary::Ptr& serializedUpdate)
RegisterAttribute("display_name", Attribute_Config, &m_DisplayName); RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
RegisterAttribute("macros", Attribute_Config, &m_Macros); RegisterAttribute("macros", Attribute_Config, &m_Macros);
RegisterAttribute("groups", Attribute_Config, &m_Groups); RegisterAttribute("groups", Attribute_Config, &m_Groups);
RegisterAttribute("notification_period", Attribute_Config, &m_NotificationPeriod);
} }
User::~User(void) User::~User(void)
@ -87,6 +88,11 @@ Dictionary::Ptr User::GetMacros(void) const
return m_Macros; return m_Macros;
} }
TimePeriod::Ptr User::GetNotificationPeriod(void) const
{
return TimePeriod::GetByName(m_NotificationPeriod);
}
/** /**
* @threadsafety Always. * @threadsafety Always.
*/ */

View File

@ -21,6 +21,7 @@
#define USER_H #define USER_H
#include "icinga/i2-icinga.h" #include "icinga/i2-icinga.h"
#include "icinga/timeperiod.h"
#include "base/dynamicobject.h" #include "base/dynamicobject.h"
#include "base/array.h" #include "base/array.h"
@ -45,6 +46,7 @@ public:
String GetDisplayName(void) const; String GetDisplayName(void) const;
Array::Ptr GetGroups(void) const; Array::Ptr GetGroups(void) const;
TimePeriod::Ptr GetNotificationPeriod(void) const;
Dictionary::Ptr GetMacros(void) const; Dictionary::Ptr GetMacros(void) const;
Dictionary::Ptr CalculateDynamicMacros(void) const; Dictionary::Ptr CalculateDynamicMacros(void) const;
@ -55,6 +57,7 @@ protected:
private: private:
Attribute<String> m_DisplayName; Attribute<String> m_DisplayName;
Attribute<Dictionary::Ptr> m_Macros; Attribute<Dictionary::Ptr> m_Macros;
Attribute<String> m_NotificationPeriod;
Attribute<Array::Ptr> m_Groups; Attribute<Array::Ptr> m_Groups;
}; };