icinga2/lib/icinga/notification.cpp

359 lines
12 KiB
C++
Raw Normal View History

2013-02-09 11:42:22 +01:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 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. *
******************************************************************************/
2013-03-17 20:19:29 +01:00
#include "icinga/notification.h"
#include "icinga/notificationcommand.h"
2013-03-17 20:19:29 +01:00
#include "icinga/macroprocessor.h"
#include "icinga/service.h"
2013-03-16 21:18:53 +01:00
#include "base/dynamictype.h"
#include "base/objectlock.h"
#include "base/logger_fwd.h"
2013-03-25 18:36:15 +01:00
#include "base/utility.h"
#include "base/convert.h"
#include "base/exception.h"
#include "base/initialize.h"
#include "base/scriptvariable.h"
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);
INITIALIZE_ONCE(&Notification::StaticInitialize);
2013-02-09 11:42:22 +01:00
2013-08-28 14:59:41 +02:00
boost::signals2::signal<void (const Notification::Ptr&, double, const String&)> Notification::OnNextNotificationChanged;
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);
}
void Notification::Start(void)
{
DynamicObject::Start();
2013-02-09 11:42:22 +01:00
GetService()->AddNotification(GetSelf());
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
GetService()->RemoveNotification(GetSelf());
2013-02-09 11:42:22 +01:00
}
Service::Ptr Notification::GetService(void) const
{
2013-10-26 09:41:45 +02:00
Host::Ptr host = Host::GetByName(GetHostRaw());
2013-02-09 11:42:22 +01:00
2013-10-26 09:41:45 +02:00
if (GetServiceRaw().IsEmpty())
return host->GetCheckService();
2013-02-09 11:42:22 +01:00
else
2013-10-26 09:41:45 +02:00
return host->GetServiceByShortName(GetServiceRaw());
2013-02-09 11:42:22 +01:00
}
NotificationCommand::Ptr Notification::GetNotificationCommand(void) const
2013-02-09 11:42:22 +01:00
{
2013-10-26 09:41:45 +02:00
return NotificationCommand::GetByName(GetNotificationCommandRaw());
}
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::GetNotificationPeriod(void) const
{
2013-10-26 09:41:45 +02:00
return TimePeriod::GetByName(GetNotificationPeriodRaw());
}
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.
*/
2013-08-28 14:59:41 +02:00
void Notification::SetNextNotification(double time, const String& authority)
{
2013-10-26 09:41:45 +02:00
SetNextNotificationRaw(time);
2013-08-28 14:59:41 +02:00
2013-11-10 16:53:57 +01:00
OnNextNotificationChanged(GetSelf(), time, authority);
}
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
2013-05-13 13:44:57 +02:00
if (!force) {
TimePeriod::Ptr tp = GetNotificationPeriod();
if (tp && !tp->IsInside(Utility::GetTime())) {
Log(LogInformation, "icinga", "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();
Service::Ptr service = GetService();
if (type == NotificationProblem) {
if (times && times->Contains("begin") && now < service->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")) {
Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': after escalation range");
return;
}
2013-05-13 13:44:57 +02:00
}
unsigned long ftype = 1 << type;
Log(LogDebug, "icinga", "FType=" + Convert::ToString(ftype) + ", TypeFilter=" + Convert::ToString(GetNotificationTypeFilter()));
if (!(ftype & GetNotificationTypeFilter())) {
Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': type filter does not match");
return;
}
unsigned long fstate = 1 << GetService()->GetState();
if (!(fstate & GetNotificationStateFilter())) {
Log(LogInformation, "icinga", "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(GetSelf(), GetService(), allUsers, type, cr, author, text);
std::set<User::Ptr> allNotifiedUsers;
2013-02-27 21:49:03 +01:00
BOOST_FOREACH(const User::Ptr& user, allUsers) {
if (!CheckNotificationUserFilters(type, user, force))
continue;
Log(LogDebug, "icinga", "Sending notification for user '" + user->GetName() + "'");
Utility::QueueAsyncCallback(boost::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force, author, text));
/* collect all notified users */
allNotifiedUsers.insert(user);
2013-02-24 08:26:10 +01:00
}
/* used in db_ido for notification history */
Service::OnNotificationSentToAllUsers(GetSelf(), GetService(), 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->GetNotificationPeriod();
2013-02-24 08:26:10 +01:00
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 false;
}
unsigned long ftype = 1 << type;
if (!(ftype & user->GetNotificationTypeFilter())) {
Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
GetName() + " and user '" + user->GetName() + "': type filter does not match");
return false;
}
unsigned long fstate = 1 << GetService()->GetState();
if (!(fstate & user->GetNotificationStateFilter())) {
Log(LogInformation, "icinga", "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 = GetNotificationCommand();
if (!command) {
Log(LogDebug, "icinga", "No notification_command found for notification '" + GetName() + "'. Skipping execution.");
return;
}
command->Execute(GetSelf(), 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(GetSelf(), GetService(), user, type, cr, author, text, command->GetName());
2013-03-16 21:18:53 +01:00
Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
} catch (const std::exception& ex) {
std::ostringstream msgbuf;
2013-02-09 11:42:22 +01:00
msgbuf << "Exception occured during notification for service '"
<< GetService()->GetName() << "': " << DiagnosticInformation(ex);
Log(LogWarning, "icinga", msgbuf.str());
2013-02-09 11:42:22 +01:00
}
}
bool Notification::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
{
if (macro.SubStr(0, 13) == "_NOTIFICATION") {
Dictionary::Ptr custom = GetCustom();
*result = custom ? custom->Get(macro.SubStr(13)) : "";
return true;
}
Dictionary::Ptr macros = GetMacros();
if (macros && macros->Contains(macro)) {
*result = macros->Get(macro);
return true;
}
return false;
}