icinga2/lib/icinga/notification.cpp

544 lines
16 KiB
C++
Raw Normal View History

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"
#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"
#include "base/scriptglobal.hpp"
#include "base/function.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);
REGISTER_SCRIPTFUNCTION(ValidateNotificationFilters, &Notification::ValidateFilters);
REGISTER_SCRIPTFUNCTION(ValidateNotificationUsers, &Notification::ValidateUsers);
INITIALIZE_ONCE(&Notification::StaticInitialize);
2013-02-09 11:42:22 +01: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-11-06 19:35:47 +01:00
Notification::Ptr notification = dynamic_pointer_cast<Notification>(context);
if (!notification)
return "";
2014-11-06 19:35:47 +01:00
String name = notification->GetHostName();
2014-11-06 19:35:47 +01:00
if (!notification->GetServiceName().IsEmpty())
name += "!" + notification->GetServiceName();
name += "!" + shortName;
return name;
}
void Notification::StaticInitialize(void)
{
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);
}
void Notification::OnConfigLoaded(void)
{
SetTypeFilter(FilterArrayToInt(GetTypes(), ~0));
SetStateFilter(FilterArrayToInt(GetStates(), ~0));
}
void Notification::OnAllConfigLoaded(void)
{
Checkable::Ptr obj = GetCheckable();
if (!obj)
BOOST_THROW_EXCEPTION(ScriptError("Notification object refers to a host/service which doesn't exist.", GetDebugInfo()));
obj->AddNotification(this);
}
void Notification::Start(void)
{
DynamicObject::Start();
2013-02-09 11:42:22 +01:00
Checkable::Ptr obj = GetCheckable();
if (obj)
obj->AddNotification(this);
2013-02-09 11:42:22 +01:00
}
void Notification::Stop(void)
2013-02-09 11:42:22 +01:00
{
DynamicObject::Stop();
2013-02-09 11:42:22 +01:00
Checkable::Ptr obj = GetCheckable();
if (obj)
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
{
Host::Ptr host = Host::GetByName(GetHostName());
2013-02-09 11:42:22 +01:00
if (GetServiceName().IsEmpty())
2014-04-03 15:36:13 +02:00
return host;
2013-02-09 11:42:22 +01:00
else
return host->GetServiceByShortName(GetServiceName());
2013-02-09 11:42:22 +01:00
}
NotificationCommand::Ptr Notification::GetCommand(void) const
2013-02-09 11:42:22 +01:00
{
return NotificationCommand::GetByName(GetCommandRaw());
}
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;
}
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;
}
TimePeriod::Ptr Notification::GetPeriod(void) const
{
return TimePeriod::GetByName(GetPeriodRaw());
}
double Notification::GetNextNotification(void) const
{
2013-10-26 09:41:45 +02:00
return GetNextNotificationRaw();
}
/**
* Sets the timestamp when the next periodical notification should be sent.
* This does not affect notifications that are sent for state changes.
*/
void Notification::SetNextNotification(double time, const MessageOrigin& origin)
{
2013-10-26 09:41:45 +02:00
SetNextNotificationRaw(time);
2013-08-28 14:59:41 +02:00
OnNextNotificationChanged(this, time, origin);
}
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
}
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:
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";
}
}
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
{
ASSERT(!OwnsLock());
2013-02-24 01:10:34 +01:00
Log(LogInformation, "Notification")
<< "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) {
TimePeriod::Ptr tp = GetPeriod();
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";
return;
}
2013-05-13 13:44:57 +02:00
double now = Utility::GetTime();
Dictionary::Ptr times = GetTimes();
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";
/* we need to adjust the next notification time
* to now + begin delaying the first notification
*/
double nextProposedNotification = now + times->Get("begin") + 1.0;
if (GetNextNotification() > nextProposedNotification)
SetNextNotification(nextProposedNotification);
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";
return;
}
2013-05-13 13:44:57 +02:00
}
unsigned long ftype = 1 << type;
2014-10-19 17:52:17 +02:00
Log(LogDebug, "Notification")
<< "FType=" << ftype << ", TypeFilter=" << GetTypeFilter();
if (!(ftype & GetTypeFilter())) {
2014-10-19 17:52:17 +02:00
Log(LogNotice, "Notification")
<< "Not sending notifications for notification object '" << GetName() << "': type filter does not match '"
<< NotificationTypeToString(type) << "'";
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);
2014-04-03 15:36:13 +02:00
unsigned long fstate;
2014-04-03 15:36:13 +02:00
if (service)
fstate = ServiceStateToFilter(service->GetState());
else
fstate = HostStateToFilter(host->GetState());
if (!(fstate & GetStateFilter())) {
Log(LogNotice, "Notification")
<< "Not sending notifications for notification object '" << GetName() << "': state filter does not match";
return;
}
}
}
{
ObjectLock olock(this);
double now = Utility::GetTime();
SetLastNotification(now);
if (type == NotificationProblem)
SetLastProblemNotification(now);
}
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()));
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
Service::OnNotificationSendStart(this, checkable, allUsers, type, cr, author, text);
std::set<User::Ptr> allNotifiedUsers;
Array::Ptr notifiedUsers = GetNotifiedUsers();
2013-02-27 21:49:03 +01:00
BOOST_FOREACH(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)) {
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 (!notifiedUsers->Contains(userName)) {
Log(LogNotice, "Notification")
<< "We did not notify user '" << userName << "' before. Not sending recovery notification.";
continue;
}
}
2014-10-19 17:52:17 +02:00
Log(LogInformation, "Notification")
<< "Sending notification '" << GetName() << "' for user '" << userName << "'";
2014-10-19 17:52:17 +02:00
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 (!notifiedUsers->Contains(userName))
notifiedUsers->Add(userName);
2013-02-24 08:26:10 +01:00
}
/* if this was a recovery notification, reset all notified users */
if (type == NotificationRecovery)
notifiedUsers->Clear();
/* used in db_ido for notification history */
Service::OnNotificationSentToAllUsers(this, checkable, allNotifiedUsers, type, cr, author, text);
2013-02-24 08:26:10 +01:00
}
bool Notification::CheckNotificationUserFilters(NotificationType type, const User::Ptr& user, bool force)
2013-02-24 08:26:10 +01:00
{
ASSERT(!OwnsLock());
2013-03-02 09:07:47 +01:00
if (!force) {
TimePeriod::Ptr tp = user->GetPeriod();
2013-02-24 08:26:10 +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";
return false;
}
unsigned long ftype = 1 << type;
if (!(ftype & user->GetTypeFilter())) {
2014-10-19 17:52:17 +02:00
Log(LogNotice, "Notification")
<< "Not sending notifications for notification object '"
<< GetName() << " and user '" << user->GetName() << "': type filter does not match";
return false;
}
/* 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
unsigned long fstate;
2014-04-03 15:36:13 +02:00
if (service)
fstate = ServiceStateToFilter(service->GetState());
else
fstate = HostStateToFilter(host->GetState());
if (!(fstate & user->GetStateFilter())) {
Log(LogNotice, "Notification")
<< "Not sending notifications for notification object '"
<< GetName() << " and user '" << user->GetName() << "': state filter does not match";
return false;
}
}
2013-02-24 08:26:10 +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 {
NotificationCommand::Ptr command = GetCommand();
if (!command) {
2014-10-19 17:52:17 +02:00
Log(LogDebug, "Notification")
<< "No notification_command found for notification '" << GetName() << "'. Skipping execution.";
return;
}
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
}
/* required by compatlogger */
Service::OnNotificationSentToUser(this, GetCheckable(), user, type, cr, author, text, command->GetName());
2014-10-19 17:52:17 +02:00
Log(LogInformation, "Notification")
<< "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")
<< "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
}
}
int icinga::ServiceStateToFilter(ServiceState state)
{
switch (state) {
case ServiceOK:
return StateFilterOK;
case ServiceWarning:
return StateFilterWarning;
case ServiceCritical:
return StateFilterCritical;
case ServiceUnknown:
return StateFilterUnknown;
default:
VERIFY(!"Invalid state type.");
}
}
int icinga::HostStateToFilter(HostState state)
{
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)
return defaultValue;
resultTypeFilter = 0;
ObjectLock olock(typeFilters);
BOOST_FOREACH(const Value& typeFilter, typeFilters) {
resultTypeFilter = resultTypeFilter | typeFilter;
}
return resultTypeFilter;
}
void Notification::ValidateUsers(const String& location, const Notification::Ptr& object)
{
Array::Ptr users = object->GetUsersRaw();
Array::Ptr groups = object->GetUserGroupsRaw();
if ((!users || users->GetLength() == 0) && (!groups || groups->GetLength() == 0)) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
location + ": No users/user_groups specified.", object->GetDebugInfo()));
}
}
void Notification::ValidateFilters(const String& location, const Notification::Ptr& object)
{
int sfilter = FilterArrayToInt(object->GetStates(), 0);
if (object->GetServiceName().IsEmpty() && (sfilter & ~(StateFilterUp | StateFilterDown)) != 0) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
2014-12-18 15:43:01 +01:00
location + ": State filter is invalid.", object->GetDebugInfo()));
}
if (!object->GetServiceName().IsEmpty() && (sfilter & ~(StateFilterOK | StateFilterWarning | StateFilterCritical | StateFilterUnknown)) != 0) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
2014-12-18 15:43:01 +01:00
location + ": State filter is invalid.", object->GetDebugInfo()));
}
int tfilter = FilterArrayToInt(object->GetTypes(), 0);
if ((tfilter & ~(1 << NotificationDowntimeStart | 1 << NotificationDowntimeEnd | 1 << NotificationDowntimeRemoved |
1 << NotificationCustom | 1 << NotificationAcknowledgement | 1 << NotificationProblem | 1 << NotificationRecovery |
1 << NotificationFlappingStart | 1 << NotificationFlappingEnd)) != 0) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
2014-12-18 15:43:01 +01:00
location + ": Type filter is invalid.", object->GetDebugInfo()));
}
}
Endpoint::Ptr Notification::GetCommandEndpoint(void) const
{
return Endpoint::GetByName(GetCommandEndpointRaw());
}