2013-02-09 11:42:22 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2013-02-09 11:42:22 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/notification.hpp"
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "icinga/notification.tcpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/notificationcommand.hpp"
|
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/initialize.hpp"
|
2014-12-14 11:33:45 +01:00
|
|
|
#include "base/scriptglobal.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2013-02-09 11:42:22 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
REGISTER_TYPE(Notification);
|
2013-12-17 14:24:29 +01:00
|
|
|
INITIALIZE_ONCE(&Notification::StaticInitialize);
|
2013-02-09 11:42:22 +01:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
boost::signals2::signal<void (const Notification::Ptr&, double, const MessageOrigin&)> Notification::OnNextNotificationChanged;
|
2013-08-28 14:59:41 +02:00
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
String NotificationNameComposer::MakeName(const String& shortName, const Object::Ptr& context) const
|
2014-04-05 22:17:20 +02:00
|
|
|
{
|
2014-11-06 19:35:47 +01:00
|
|
|
Notification::Ptr notification = dynamic_pointer_cast<Notification>(context);
|
|
|
|
|
|
|
|
if (!notification)
|
2014-04-05 22:17:20 +02:00
|
|
|
return "";
|
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
String name = notification->GetHostName();
|
2014-04-05 22:17:20 +02:00
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
if (!notification->GetServiceName().IsEmpty())
|
|
|
|
name += "!" + notification->GetServiceName();
|
2014-04-05 22:17:20 +02:00
|
|
|
|
|
|
|
name += "!" + shortName;
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2013-12-17 14:24:29 +01:00
|
|
|
void Notification::StaticInitialize(void)
|
|
|
|
{
|
2014-12-14 11:33:45 +01:00
|
|
|
ScriptGlobal::Set("OK", StateFilterOK);
|
|
|
|
ScriptGlobal::Set("Warning", StateFilterWarning);
|
|
|
|
ScriptGlobal::Set("Critical", StateFilterCritical);
|
|
|
|
ScriptGlobal::Set("Unknown", StateFilterUnknown);
|
|
|
|
ScriptGlobal::Set("Up", StateFilterUp);
|
|
|
|
ScriptGlobal::Set("Down", StateFilterDown);
|
|
|
|
|
|
|
|
ScriptGlobal::Set("DowntimeStart", 1 << NotificationDowntimeStart);
|
|
|
|
ScriptGlobal::Set("DowntimeEnd", 1 << NotificationDowntimeEnd);
|
|
|
|
ScriptGlobal::Set("DowntimeRemoved", 1 << NotificationDowntimeRemoved);
|
|
|
|
ScriptGlobal::Set("Custom", 1 << NotificationCustom);
|
|
|
|
ScriptGlobal::Set("Acknowledgement", 1 << NotificationAcknowledgement);
|
|
|
|
ScriptGlobal::Set("Problem", 1 << NotificationProblem);
|
|
|
|
ScriptGlobal::Set("Recovery", 1 << NotificationRecovery);
|
|
|
|
ScriptGlobal::Set("FlappingStart", 1 << NotificationFlappingStart);
|
|
|
|
ScriptGlobal::Set("FlappingEnd", 1 << NotificationFlappingEnd);
|
2014-04-07 13:59:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::OnConfigLoaded(void)
|
|
|
|
{
|
2014-04-09 10:25:23 +02:00
|
|
|
SetTypeFilter(FilterArrayToInt(GetTypes(), ~0));
|
|
|
|
SetStateFilter(FilterArrayToInt(GetStates(), ~0));
|
2014-11-22 16:42:57 +01:00
|
|
|
}
|
2014-04-07 13:59:41 +02:00
|
|
|
|
2014-11-22 16:42:57 +01:00
|
|
|
void Notification::OnAllConfigLoaded(void)
|
|
|
|
{
|
2014-05-02 19:43:49 +02:00
|
|
|
Checkable::Ptr obj = GetCheckable();
|
|
|
|
|
2014-12-19 10:56:34 +01:00
|
|
|
if (!obj)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Notification object refers to a host/service which doesn't exist.", GetDebugInfo()));
|
|
|
|
|
|
|
|
obj->AddNotification(this);
|
2013-12-17 14:24:29 +01:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void Notification::Start(void)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Start();
|
2013-02-09 11:42:22 +01:00
|
|
|
|
2014-05-02 19:43:49 +02:00
|
|
|
Checkable::Ptr obj = GetCheckable();
|
|
|
|
|
|
|
|
if (obj)
|
2014-11-08 21:17:16 +01:00
|
|
|
obj->AddNotification(this);
|
2013-02-09 11:42:22 +01:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void Notification::Stop(void)
|
2013-02-09 11:42:22 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Stop();
|
2013-02-09 11:42:22 +01:00
|
|
|
|
2014-05-02 19:43:49 +02:00
|
|
|
Checkable::Ptr obj = GetCheckable();
|
|
|
|
|
|
|
|
if (obj)
|
2014-11-08 21:17:16 +01:00
|
|
|
obj->RemoveNotification(this);
|
2013-02-09 11:42:22 +01:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
Checkable::Ptr Notification::GetCheckable(void) const
|
2013-02-09 11:42:22 +01:00
|
|
|
{
|
2014-04-05 09:15:40 +02:00
|
|
|
Host::Ptr host = Host::GetByName(GetHostName());
|
2013-02-09 11:42:22 +01:00
|
|
|
|
2014-04-05 09:15:40 +02:00
|
|
|
if (GetServiceName().IsEmpty())
|
2014-04-03 15:36:13 +02:00
|
|
|
return host;
|
2013-02-09 11:42:22 +01:00
|
|
|
else
|
2014-04-05 09:15:40 +02:00
|
|
|
return host->GetServiceByShortName(GetServiceName());
|
2013-02-09 11:42:22 +01:00
|
|
|
}
|
|
|
|
|
2014-04-09 10:25:23 +02:00
|
|
|
NotificationCommand::Ptr Notification::GetCommand(void) const
|
2013-02-09 11:42:22 +01:00
|
|
|
{
|
2014-04-09 10:25:23 +02:00
|
|
|
return NotificationCommand::GetByName(GetCommandRaw());
|
2013-03-22 14:40:55 +01:00
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<User::Ptr> Notification::GetUsers(void) const
|
2013-02-24 08:26:10 +01:00
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<User::Ptr> result;
|
2013-02-24 08:26:10 +01:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
Array::Ptr users = GetUsersRaw();
|
2013-02-24 08:26:10 +01:00
|
|
|
|
|
|
|
if (users) {
|
2013-03-01 12:07:52 +01:00
|
|
|
ObjectLock olock(users);
|
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
BOOST_FOREACH(const String& name, users) {
|
2013-02-27 21:49:03 +01:00
|
|
|
User::Ptr user = User::GetByName(name);
|
|
|
|
|
|
|
|
if (!user)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result.insert(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-09-25 09:23:12 +02:00
|
|
|
std::set<UserGroup::Ptr> Notification::GetUserGroups(void) const
|
2013-02-27 21:49:03 +01:00
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<UserGroup::Ptr> result;
|
2013-02-27 21:49:03 +01:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
Array::Ptr groups = GetUserGroupsRaw();
|
2013-02-27 21:49:03 +01:00
|
|
|
|
|
|
|
if (groups) {
|
2013-03-01 12:07:52 +01:00
|
|
|
ObjectLock olock(groups);
|
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
BOOST_FOREACH(const String& name, groups) {
|
2013-02-27 21:49:03 +01:00
|
|
|
UserGroup::Ptr ug = UserGroup::GetByName(name);
|
|
|
|
|
|
|
|
if (!ug)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result.insert(ug);
|
2013-02-24 08:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-04-09 10:25:23 +02:00
|
|
|
TimePeriod::Ptr Notification::GetPeriod(void) const
|
2013-03-21 13:42:46 +01:00
|
|
|
{
|
2014-04-09 10:25:23 +02:00
|
|
|
return TimePeriod::GetByName(GetPeriodRaw());
|
2013-03-20 10:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
double Notification::GetNextNotification(void) const
|
|
|
|
{
|
2013-10-26 09:41:45 +02:00
|
|
|
return GetNextNotificationRaw();
|
2013-03-20 10:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the timestamp when the next periodical notification should be sent.
|
|
|
|
* This does not affect notifications that are sent for state changes.
|
|
|
|
*/
|
2014-05-03 20:02:22 +02:00
|
|
|
void Notification::SetNextNotification(double time, const MessageOrigin& origin)
|
2013-03-20 10:08:27 +01:00
|
|
|
{
|
2013-10-26 09:41:45 +02:00
|
|
|
SetNextNotificationRaw(time);
|
2013-08-28 14:59:41 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
OnNextNotificationChanged(this, time, origin);
|
2013-03-20 10:08:27 +01:00
|
|
|
}
|
|
|
|
|
2013-07-18 17:04:09 +02:00
|
|
|
void Notification::UpdateNotificationNumber(void)
|
|
|
|
{
|
2013-10-26 09:41:45 +02:00
|
|
|
SetNotificationNumber(GetNotificationNumber() + 1);
|
2013-07-18 17:04:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::ResetNotificationNumber(void)
|
|
|
|
{
|
2013-10-26 09:41:45 +02:00
|
|
|
SetNotificationNumber(0);
|
2013-07-18 17:04:09 +02:00
|
|
|
}
|
|
|
|
|
2015-03-12 17:09:26 +01:00
|
|
|
/* the upper case string used in all interfaces */
|
2013-02-24 01:10:34 +01:00
|
|
|
String Notification::NotificationTypeToString(NotificationType type)
|
2013-02-09 11:42:22 +01:00
|
|
|
{
|
2013-02-24 01:10:34 +01:00
|
|
|
switch (type) {
|
|
|
|
case NotificationDowntimeStart:
|
|
|
|
return "DOWNTIMESTART";
|
|
|
|
case NotificationDowntimeEnd:
|
|
|
|
return "DOWNTIMEEND";
|
|
|
|
case NotificationDowntimeRemoved:
|
|
|
|
return "DOWNTIMECANCELLED";
|
|
|
|
case NotificationCustom:
|
2013-02-24 12:47:24 +01:00
|
|
|
return "CUSTOM";
|
|
|
|
case NotificationAcknowledgement:
|
|
|
|
return "ACKNOWLEDGEMENT";
|
2013-02-24 01:10:34 +01:00
|
|
|
case NotificationProblem:
|
|
|
|
return "PROBLEM";
|
|
|
|
case NotificationRecovery:
|
|
|
|
return "RECOVERY";
|
2013-06-21 10:20:29 +02:00
|
|
|
case NotificationFlappingStart:
|
|
|
|
return "FLAPPINGSTART";
|
|
|
|
case NotificationFlappingEnd:
|
|
|
|
return "FLAPPINGEND";
|
2013-02-24 01:10:34 +01:00
|
|
|
default:
|
|
|
|
return "UNKNOWN_NOTIFICATION";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-09 14:22:38 +01:00
|
|
|
void Notification::BeginExecuteNotification(NotificationType type, const CheckResult::Ptr& cr, bool force, const String& author, const String& text)
|
2013-02-24 01:10:34 +01:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-02-24 01:10:34 +01:00
|
|
|
|
2015-04-12 15:11:38 +02:00
|
|
|
Log(LogNotice, "Notification")
|
2015-01-08 16:18:11 +01:00
|
|
|
<< "Attempting to send notifications for notification object '" << GetName() << "'.";
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
Checkable::Ptr checkable = GetCheckable();
|
|
|
|
|
2013-05-13 13:44:57 +02:00
|
|
|
if (!force) {
|
2014-04-09 10:25:23 +02:00
|
|
|
TimePeriod::Ptr tp = GetPeriod();
|
2013-03-21 13:42:46 +01:00
|
|
|
|
|
|
|
if (tp && !tp->IsInside(Utility::GetTime())) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Notification")
|
|
|
|
<< "Not sending notifications for notification object '" << GetName() << "': not in timeperiod";
|
2013-03-21 13:42:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-05-13 13:44:57 +02:00
|
|
|
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
Dictionary::Ptr times = GetTimes();
|
|
|
|
|
2013-07-19 11:02:43 +02:00
|
|
|
if (type == NotificationProblem) {
|
2014-04-03 15:36:13 +02:00
|
|
|
if (times && times->Contains("begin") && now < checkable->GetLastHardStateChange() + times->Get("begin")) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Notification")
|
|
|
|
<< "Not sending notifications for notification object '" << GetName() << "': before escalation range";
|
2014-11-04 22:03:39 +01:00
|
|
|
|
|
|
|
/* we need to adjust the next notification time
|
2014-11-30 23:32:13 +01:00
|
|
|
* to now + begin delaying the first notification
|
|
|
|
*/
|
2014-11-04 22:03:39 +01:00
|
|
|
double nextProposedNotification = now + times->Get("begin") + 1.0;
|
|
|
|
if (GetNextNotification() > nextProposedNotification)
|
|
|
|
SetNextNotification(nextProposedNotification);
|
|
|
|
|
2013-07-19 11:02:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
if (times && times->Contains("end") && now > checkable->GetLastHardStateChange() + times->Get("end")) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Notification")
|
|
|
|
<< "Not sending notifications for notification object '" << GetName() << "': after escalation range";
|
2013-07-19 11:02:43 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-05-13 13:44:57 +02:00
|
|
|
}
|
2013-06-26 09:08:50 +02:00
|
|
|
|
|
|
|
unsigned long ftype = 1 << type;
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "Notification")
|
2015-03-12 17:09:26 +01:00
|
|
|
<< "Type '" << NotificationTypeToStringInternal(type)
|
|
|
|
<< "', TypeFilter " << NotificationFilterToString(GetTypeFilter())
|
|
|
|
<< " (FType=" << ftype << ", TypeFilter=" << GetTypeFilter() << ")";
|
2013-06-26 09:08:50 +02:00
|
|
|
|
2014-04-09 10:25:23 +02:00
|
|
|
if (!(ftype & GetTypeFilter())) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Notification")
|
2015-03-12 17:09:26 +01:00
|
|
|
<< "Not sending notifications for notification object '" << GetName() << "': type '"
|
|
|
|
<< NotificationTypeToStringInternal(type) << "' does not match type filter '"
|
|
|
|
<< NotificationFilterToString(GetTypeFilter()) << ".";
|
2013-06-26 09:08:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-04 22:03:39 +01:00
|
|
|
/* 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);
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2014-11-04 22:03:39 +01:00
|
|
|
unsigned long fstate;
|
2015-03-12 17:09:26 +01:00
|
|
|
String stateStr;
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2015-03-12 17:09:26 +01:00
|
|
|
if (service) {
|
2014-11-04 22:03:39 +01:00
|
|
|
fstate = ServiceStateToFilter(service->GetState());
|
2015-03-12 17:09:26 +01:00
|
|
|
stateStr = NotificationServiceStateToString(service->GetState());
|
|
|
|
} else {
|
2014-11-04 22:03:39 +01:00
|
|
|
fstate = HostStateToFilter(host->GetState());
|
2015-03-12 17:09:26 +01:00
|
|
|
stateStr = NotificationHostStateToString(host->GetState());
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(LogDebug, "Notification")
|
|
|
|
<< "State '" << stateStr << "', StateFilter " << NotificationFilterToString(GetStateFilter())
|
|
|
|
<< " (FState=" << fstate << ", StateFilter=" << GetStateFilter() << ")";
|
2013-06-26 09:08:50 +02:00
|
|
|
|
2014-11-04 22:03:39 +01:00
|
|
|
if (!(fstate & GetStateFilter())) {
|
|
|
|
Log(LogNotice, "Notification")
|
2015-03-12 17:09:26 +01:00
|
|
|
<< "Not sending notifications for notification object '" << GetName() << "': state '" << stateStr
|
|
|
|
<< "' does not match state filter " << NotificationFilterToString(GetStateFilter()) << ".";
|
2014-11-04 22:03:39 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-06-26 09:08:50 +02:00
|
|
|
}
|
2013-03-21 13:42:46 +01:00
|
|
|
}
|
|
|
|
|
2013-03-20 10:08:27 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-11-06 08:36:22 +01:00
|
|
|
double now = Utility::GetTime();
|
|
|
|
SetLastNotification(now);
|
|
|
|
|
|
|
|
if (type == NotificationProblem)
|
|
|
|
SetLastProblemNotification(now);
|
2013-03-20 10:08:27 +01:00
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<User::Ptr> allUsers;
|
2013-02-27 21:49:03 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<User::Ptr> users = GetUsers();
|
2013-02-27 21:49:03 +01:00
|
|
|
std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
|
|
|
|
|
2013-09-25 09:23:12 +02:00
|
|
|
BOOST_FOREACH(const UserGroup::Ptr& ug, GetUserGroups()) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<User::Ptr> members = ug->GetMembers();
|
2013-02-27 21:49:03 +01:00
|
|
|
std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
|
|
|
|
}
|
2013-02-24 08:26:10 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Service::OnNotificationSendStart(this, checkable, allUsers, type, cr, author, text);
|
2014-01-27 17:22:48 +01:00
|
|
|
|
2014-01-28 17:53:40 +01:00
|
|
|
std::set<User::Ptr> allNotifiedUsers;
|
2014-11-12 20:32:34 +01:00
|
|
|
Array::Ptr notifiedUsers = GetNotifiedUsers();
|
2014-11-04 22:03:39 +01:00
|
|
|
|
2013-02-27 21:49:03 +01:00
|
|
|
BOOST_FOREACH(const User::Ptr& user, allUsers) {
|
2014-11-04 22:03:39 +01:00
|
|
|
String userName = user->GetName();
|
|
|
|
|
|
|
|
if (!user->GetEnableNotifications()) {
|
|
|
|
Log(LogNotice, "Notification")
|
|
|
|
<< "Disabled notifications for user '" << userName << "'. Not sending notification.";
|
2014-01-28 17:53:40 +01:00
|
|
|
continue;
|
2014-11-04 22:03:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!CheckNotificationUserFilters(type, user, force)) {
|
|
|
|
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) {
|
2014-11-12 20:32:34 +01:00
|
|
|
if (!notifiedUsers->Contains(userName)) {
|
2014-11-04 22:03:39 +01:00
|
|
|
Log(LogNotice, "Notification")
|
|
|
|
<< "We did not notify user '" << userName << "' before. Not sending recovery notification.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 17:53:40 +01:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "Notification")
|
2015-01-08 16:18:11 +01:00
|
|
|
<< "Sending notification '" << GetName() << "' for user '" << userName << "'";
|
2014-10-19 17:52:17 +02:00
|
|
|
|
2013-07-01 11:17:58 +02:00
|
|
|
Utility::QueueAsyncCallback(boost::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force, author, text));
|
2014-01-28 17:53:40 +01:00
|
|
|
|
|
|
|
/* collect all notified users */
|
|
|
|
allNotifiedUsers.insert(user);
|
2014-11-04 22:03:39 +01:00
|
|
|
|
|
|
|
/* store all notified users for later recovery checks */
|
2014-11-12 20:32:34 +01:00
|
|
|
if (!notifiedUsers->Contains(userName))
|
|
|
|
notifiedUsers->Add(userName);
|
2013-02-24 08:26:10 +01:00
|
|
|
}
|
2013-09-24 18:49:25 +02:00
|
|
|
|
2014-11-04 22:03:39 +01:00
|
|
|
/* if this was a recovery notification, reset all notified users */
|
|
|
|
if (type == NotificationRecovery)
|
2014-11-12 20:32:34 +01:00
|
|
|
notifiedUsers->Clear();
|
2014-11-04 22:03:39 +01:00
|
|
|
|
2014-01-28 17:53:40 +01:00
|
|
|
/* used in db_ido for notification history */
|
2014-11-08 21:17:16 +01:00
|
|
|
Service::OnNotificationSentToAllUsers(this, checkable, allNotifiedUsers, type, cr, author, text);
|
2013-02-24 08:26:10 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:53:40 +01:00
|
|
|
bool Notification::CheckNotificationUserFilters(NotificationType type, const User::Ptr& user, bool force)
|
2013-02-24 08:26:10 +01:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-06-26 09:08:50 +02:00
|
|
|
if (!force) {
|
2014-04-09 10:25:23 +02:00
|
|
|
TimePeriod::Ptr tp = user->GetPeriod();
|
2013-02-24 08:26:10 +01:00
|
|
|
|
2013-03-21 13:42:46 +01:00
|
|
|
if (tp && !tp->IsInside(Utility::GetTime())) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Notification")
|
|
|
|
<< "Not sending notifications for notification object '"
|
|
|
|
<< GetName() << " and user '" << user->GetName() << "': user not in timeperiod";
|
2014-01-28 17:53:40 +01:00
|
|
|
return false;
|
2013-03-21 13:42:46 +01:00
|
|
|
}
|
2013-06-26 09:08:50 +02:00
|
|
|
|
|
|
|
unsigned long ftype = 1 << type;
|
|
|
|
|
2015-03-12 17:09:26 +01:00
|
|
|
Log(LogDebug, "Notification")
|
|
|
|
<< "User notification, Type '" << NotificationTypeToStringInternal(type)
|
|
|
|
<< "', TypeFilter " << NotificationFilterToString(user->GetTypeFilter())
|
|
|
|
<< " (FType=" << ftype << ", TypeFilter=" << GetTypeFilter() << ")";
|
|
|
|
|
|
|
|
|
2014-04-09 10:25:23 +02:00
|
|
|
if (!(ftype & user->GetTypeFilter())) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Notification")
|
|
|
|
<< "Not sending notifications for notification object '"
|
2015-03-12 17:09:26 +01:00
|
|
|
<< GetName() << " and user '" << user->GetName() << "': type '"
|
|
|
|
<< NotificationTypeToStringInternal(type) << "' does not match type filter "
|
|
|
|
<< NotificationFilterToString(user->GetTypeFilter()) << ".";
|
2014-01-28 17:53:40 +01:00
|
|
|
return false;
|
2013-06-26 09:08:50 +02:00
|
|
|
}
|
|
|
|
|
2014-11-04 22:03:39 +01:00
|
|
|
/* check state filters it this is not a recovery notification */
|
|
|
|
if (type != NotificationRecovery) {
|
|
|
|
Checkable::Ptr checkable = GetCheckable();
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2014-11-04 22:03:39 +01:00
|
|
|
unsigned long fstate;
|
2015-03-12 17:09:26 +01:00
|
|
|
String stateStr;
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2015-03-12 17:09:26 +01:00
|
|
|
if (service) {
|
|
|
|
fstate = ServiceStateToFilter(service->GetState());
|
|
|
|
stateStr = NotificationServiceStateToString(service->GetState());
|
|
|
|
} else {
|
|
|
|
fstate = HostStateToFilter(host->GetState());
|
|
|
|
stateStr = NotificationHostStateToString(host->GetState());
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(LogDebug, "Notification")
|
|
|
|
<< "User notification, State '" << stateStr << "', StateFilter "
|
|
|
|
<< NotificationFilterToString(user->GetStateFilter())
|
|
|
|
<< " (FState=" << fstate << ", StateFilter=" << user->GetStateFilter() << ")";
|
2013-06-26 09:08:50 +02:00
|
|
|
|
2014-11-04 22:03:39 +01:00
|
|
|
if (!(fstate & user->GetStateFilter())) {
|
|
|
|
Log(LogNotice, "Notification")
|
|
|
|
<< "Not sending notifications for notification object '"
|
2015-03-12 17:09:26 +01:00
|
|
|
<< GetName() << " and user '" << user->GetName() << "': state '" << stateStr
|
|
|
|
<< "' does not match state filter " << NotificationFilterToString(user->GetStateFilter()) << ".";
|
2014-11-04 22:03:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-26 09:08:50 +02:00
|
|
|
}
|
2013-02-24 08:26:10 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:53:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author, const String& text)
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
2013-02-09 11:42:22 +01:00
|
|
|
try {
|
2014-04-09 10:25:23 +02:00
|
|
|
NotificationCommand::Ptr command = GetCommand();
|
2013-09-24 18:49:25 +02:00
|
|
|
|
2013-09-24 18:58:51 +02:00
|
|
|
if (!command) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "Notification")
|
|
|
|
<< "No notification_command found for notification '" << GetName() << "'. Skipping execution.";
|
2013-09-24 18:58:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-09-24 18:49:25 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
command->Execute(this, user, cr, type, author, text);
|
2013-02-09 11:42:22 +01:00
|
|
|
|
2013-07-18 17:04:09 +02:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
UpdateNotificationNumber();
|
2013-08-08 08:30:19 +02:00
|
|
|
SetLastNotification(Utility::GetTime());
|
2013-07-18 17:04:09 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:53:40 +01:00
|
|
|
/* required by compatlogger */
|
2014-11-08 21:17:16 +01:00
|
|
|
Service::OnNotificationSentToUser(this, GetCheckable(), user, type, cr, author, text, command->GetName());
|
2013-06-28 21:31:38 +02:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "Notification")
|
2015-01-08 16:18:11 +01:00
|
|
|
<< "Completed sending notification '" << GetName() << "' for checkable '" << GetCheckable()->GetName() << "'";
|
2013-03-16 21:18:53 +01:00
|
|
|
} catch (const std::exception& ex) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogWarning, "Notification")
|
2015-01-08 16:18:11 +01:00
|
|
|
<< "Exception occured during notification for checkable '"
|
2014-10-19 17:52:17 +02:00
|
|
|
<< GetCheckable()->GetName() << "': " << DiagnosticInformation(ex);
|
2013-02-09 11:42:22 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-11 14:01:52 +01:00
|
|
|
|
2014-04-07 13:59:41 +02:00
|
|
|
int icinga::ServiceStateToFilter(ServiceState state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
2014-04-08 09:11:54 +02:00
|
|
|
case ServiceOK:
|
2014-04-07 13:59:41 +02:00
|
|
|
return StateFilterOK;
|
2014-04-08 09:11:54 +02:00
|
|
|
case ServiceWarning:
|
2014-04-07 13:59:41 +02:00
|
|
|
return StateFilterWarning;
|
2014-04-08 09:11:54 +02:00
|
|
|
case ServiceCritical:
|
2014-04-07 13:59:41 +02:00
|
|
|
return StateFilterCritical;
|
2014-04-08 09:11:54 +02:00
|
|
|
case ServiceUnknown:
|
2014-04-07 13:59:41 +02:00
|
|
|
return StateFilterUnknown;
|
|
|
|
default:
|
|
|
|
VERIFY(!"Invalid state type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int icinga::HostStateToFilter(HostState state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case HostUp:
|
|
|
|
return StateFilterUp;
|
2014-04-08 08:54:49 +02:00
|
|
|
case HostDown:
|
2014-04-07 13:59:41 +02:00
|
|
|
return StateFilterDown;
|
|
|
|
default:
|
|
|
|
VERIFY(!"Invalid state type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int icinga::FilterArrayToInt(const Array::Ptr& typeFilters, int defaultValue)
|
|
|
|
{
|
|
|
|
Value resultTypeFilter;
|
|
|
|
|
2014-04-08 23:33:25 +02:00
|
|
|
if (!typeFilters)
|
2014-04-07 13:59:41 +02:00
|
|
|
return defaultValue;
|
|
|
|
|
|
|
|
resultTypeFilter = 0;
|
|
|
|
|
|
|
|
ObjectLock olock(typeFilters);
|
|
|
|
BOOST_FOREACH(const Value& typeFilter, typeFilters) {
|
|
|
|
resultTypeFilter = resultTypeFilter | typeFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultTypeFilter;
|
|
|
|
}
|
|
|
|
|
2015-03-12 17:09:26 +01:00
|
|
|
std::vector<String> icinga::FilterIntToArray(int iFilter)
|
|
|
|
{
|
|
|
|
std::vector<String> filter;
|
|
|
|
|
|
|
|
if (iFilter & StateFilterOK)
|
|
|
|
filter.push_back("OK");
|
|
|
|
if (iFilter & StateFilterWarning)
|
|
|
|
filter.push_back("Warning");
|
|
|
|
if (iFilter & StateFilterUnknown)
|
|
|
|
filter.push_back("Unknown");
|
|
|
|
if (iFilter & StateFilterUp)
|
|
|
|
filter.push_back("Up");
|
|
|
|
if (iFilter & StateFilterDown)
|
|
|
|
filter.push_back("Down");
|
|
|
|
if (iFilter & NotificationDowntimeStart)
|
|
|
|
filter.push_back("DowntimeStart");
|
|
|
|
if (iFilter & NotificationDowntimeEnd)
|
|
|
|
filter.push_back("DowntimeEnd");
|
|
|
|
if (iFilter & NotificationDowntimeRemoved)
|
|
|
|
filter.push_back("DowntimeRemoved");
|
|
|
|
if (iFilter & NotificationCustom)
|
|
|
|
filter.push_back("Custom");
|
|
|
|
if (iFilter & NotificationAcknowledgement)
|
|
|
|
filter.push_back("Acknowledgement");
|
|
|
|
if (iFilter & NotificationProblem)
|
|
|
|
filter.push_back("Problem");
|
|
|
|
if (iFilter & NotificationRecovery)
|
|
|
|
filter.push_back("Recovery");
|
|
|
|
if (iFilter & NotificationFlappingStart)
|
|
|
|
filter.push_back("FlappingStart");
|
|
|
|
if (iFilter & NotificationFlappingEnd)
|
|
|
|
filter.push_back("FlappingEnd");
|
|
|
|
|
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
String Notification::NotificationFilterToString(int filter)
|
|
|
|
{
|
2015-03-19 14:08:06 +01:00
|
|
|
return Utility::NaturalJoin(FilterIntToArray(filter));
|
2015-03-12 17:09:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* internal for logging */
|
|
|
|
String Notification::NotificationTypeToStringInternal(NotificationType type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case NotificationDowntimeStart:
|
|
|
|
return "DowntimeStart";
|
|
|
|
case NotificationDowntimeEnd:
|
|
|
|
return "DowntimeEnd";
|
|
|
|
case NotificationDowntimeRemoved:
|
|
|
|
return "DowntimeRemoved";
|
|
|
|
case NotificationCustom:
|
|
|
|
return "Custom";
|
|
|
|
case NotificationAcknowledgement:
|
|
|
|
return "Acknowledgement";
|
|
|
|
case NotificationProblem:
|
|
|
|
return "Problem";
|
|
|
|
case NotificationRecovery:
|
|
|
|
return "Recovery";
|
|
|
|
case NotificationFlappingStart:
|
|
|
|
return "FlappingStart";
|
|
|
|
case NotificationFlappingEnd:
|
|
|
|
return "FlappingEnd";
|
|
|
|
default:
|
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String Notification::NotificationServiceStateToString(ServiceState state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case ServiceOK:
|
|
|
|
return "OK";
|
|
|
|
case ServiceWarning:
|
|
|
|
return "Warning";
|
|
|
|
case ServiceCritical:
|
|
|
|
return "Critical";
|
|
|
|
case ServiceUnknown:
|
|
|
|
return "Unknown";
|
|
|
|
default:
|
|
|
|
VERIFY(!"Invalid state type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String Notification::NotificationHostStateToString(HostState state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case HostUp:
|
|
|
|
return "Up";
|
|
|
|
case HostDown:
|
|
|
|
return "Down";
|
|
|
|
default:
|
|
|
|
VERIFY(!"Invalid state type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
void Notification::Validate(int types, const ValidationUtils& utils)
|
2015-02-07 23:04:01 +01:00
|
|
|
{
|
2014-11-30 23:32:13 +01:00
|
|
|
ObjectImpl<Notification>::Validate(types, utils);
|
2015-02-07 23:04:01 +01:00
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
if (!(types & FAConfig))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Array::Ptr users = GetUsersRaw();
|
|
|
|
Array::Ptr groups = GetUserGroupsRaw();
|
|
|
|
|
|
|
|
if ((!users || users->GetLength() == 0) && (!groups || groups->GetLength() == 0))
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, std::vector<String>(), "Validation failed: No users/user_groups specified."));
|
2015-02-07 23:04:01 +01:00
|
|
|
}
|
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
void Notification::ValidateStates(const Array::Ptr& value, const ValidationUtils& utils)
|
2014-04-07 13:59:41 +02:00
|
|
|
{
|
2014-11-30 23:32:13 +01:00
|
|
|
ObjectImpl<Notification>::ValidateStates(value, utils);
|
2014-04-07 13:59:41 +02:00
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
int sfilter = FilterArrayToInt(value, 0);
|
2014-04-07 13:59:41 +02:00
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
if (GetServiceName().IsEmpty() && (sfilter & ~(StateFilterUp | StateFilterDown)) != 0)
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("states"), "State filter is invalid."));
|
2014-04-07 13:59:41 +02:00
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
if (!GetServiceName().IsEmpty() && (sfilter & ~(StateFilterOK | StateFilterWarning | StateFilterCritical | StateFilterUnknown)) != 0)
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("types"), "State filter is invalid."));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::ValidateTypes(const Array::Ptr& value, const ValidationUtils& utils)
|
|
|
|
{
|
|
|
|
ObjectImpl<Notification>::ValidateTypes(value, utils);
|
|
|
|
|
|
|
|
int tfilter = FilterArrayToInt(value, 0);
|
2014-04-07 13:59:41 +02:00
|
|
|
|
|
|
|
if ((tfilter & ~(1 << NotificationDowntimeStart | 1 << NotificationDowntimeEnd | 1 << NotificationDowntimeRemoved |
|
|
|
|
1 << NotificationCustom | 1 << NotificationAcknowledgement | 1 << NotificationProblem | 1 << NotificationRecovery |
|
2014-11-30 23:32:13 +01:00
|
|
|
1 << NotificationFlappingStart | 1 << NotificationFlappingEnd)) != 0)
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("types"), "Type filter is invalid."));
|
2014-04-07 13:59:41 +02:00
|
|
|
}
|
2014-11-13 11:23:57 +01:00
|
|
|
|
|
|
|
Endpoint::Ptr Notification::GetCommandEndpoint(void) const
|
|
|
|
{
|
|
|
|
return Endpoint::GetByName(GetCommandEndpointRaw());
|
|
|
|
}
|