2013-02-09 11:42:22 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2013-09-25 07:43:57 +02:00
|
|
|
* Copyright (C) 2012-2013 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"
|
2013-06-13 11:33:00 +02:00
|
|
|
#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"
|
2013-06-26 09:08:50 +02:00
|
|
|
#include "base/convert.h"
|
2013-11-20 21:55:14 +01:00
|
|
|
#include "base/exception.h"
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/tuple/tuple.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-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;
|
|
|
|
|
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
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
GetService()->AddNotification(GetSelf());
|
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
|
|
|
|
2013-08-20 11:06:04 +02: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())
|
2013-09-25 09:12:15 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-13 11:33:00 +02: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-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;
|
|
|
|
}
|
|
|
|
|
2013-03-21 13:42:46 +01:00
|
|
|
TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
|
|
|
|
{
|
2013-10-26 09:41:45 +02:00
|
|
|
return TimePeriod::GetByName(GetNotificationPeriodRaw());
|
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.
|
|
|
|
*/
|
2013-08-28 14:59:41 +02:00
|
|
|
void Notification::SetNextNotification(double time, const String& authority)
|
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
|
|
|
|
2013-11-10 16:53:57 +01:00
|
|
|
OnNextNotificationChanged(GetSelf(), time, authority);
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-05-13 13:44:57 +02:00
|
|
|
if (!force) {
|
2013-03-21 13:42:46 +01:00
|
|
|
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();
|
|
|
|
|
2013-07-19 11:02:43 +02:00
|
|
|
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
|
|
|
}
|
2013-06-26 09:08:50 +02:00
|
|
|
|
|
|
|
unsigned long ftype = 1 << type;
|
|
|
|
|
2013-06-26 09:50:04 +02:00
|
|
|
Log(LogDebug, "icinga", "FType=" + Convert::ToString(ftype) + ", TypeFilter=" + Convert::ToString(GetNotificationTypeFilter()));
|
2013-06-26 09:08:50 +02:00
|
|
|
|
2013-06-26 09:50:04 +02:00
|
|
|
if (!(ftype & GetNotificationTypeFilter())) {
|
2013-06-26 09:08:50 +02:00
|
|
|
Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': type filter does not match");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long fstate = 1 << GetService()->GetState();
|
|
|
|
|
2013-06-26 09:50:04 +02:00
|
|
|
if (!(fstate & GetNotificationStateFilter())) {
|
2013-06-26 09:08:50 +02:00
|
|
|
Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': state filter does not match");
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
2013-09-24 18:49:25 +02:00
|
|
|
unsigned long notified_users = 0;
|
2013-02-27 21:49:03 +01:00
|
|
|
BOOST_FOREACH(const User::Ptr& user, allUsers) {
|
2013-06-13 11:33:00 +02:00
|
|
|
Log(LogDebug, "icinga", "Sending notification for user '" + user->GetName() + "'");
|
2013-07-01 11:17:58 +02:00
|
|
|
Utility::QueueAsyncCallback(boost::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force, author, text));
|
2013-09-24 18:49:25 +02:00
|
|
|
notified_users++;
|
2013-02-24 08:26:10 +01:00
|
|
|
}
|
2013-09-24 18:49:25 +02:00
|
|
|
|
2013-09-25 11:21:20 +02:00
|
|
|
Service::OnNotificationSentToAllUsers(GetService(), allUsers, type, cr, author, text);
|
2013-02-24 08:26:10 +01:00
|
|
|
}
|
|
|
|
|
2013-11-09 14:22:38 +01:00
|
|
|
void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author, const String& text)
|
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) {
|
2013-03-21 13:42:46 +01:00
|
|
|
TimePeriod::Ptr tp = user->GetNotificationPeriod();
|
2013-02-24 08:26:10 +01:00
|
|
|
|
2013-03-21 13:42:46 +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;
|
|
|
|
}
|
2013-06-26 09:08:50 +02:00
|
|
|
|
|
|
|
unsigned long ftype = 1 << type;
|
|
|
|
|
2013-06-26 09:50:04 +02:00
|
|
|
if (!(ftype & user->GetNotificationTypeFilter())) {
|
2013-06-26 09:08:50 +02:00
|
|
|
Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
|
|
|
|
GetName() + " and user '" + user->GetName() + "': type filter does not match");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long fstate = 1 << GetService()->GetState();
|
|
|
|
|
2013-06-26 09:50:04 +02:00
|
|
|
if (!(fstate & user->GetNotificationStateFilter())) {
|
2013-06-26 09:08:50 +02:00
|
|
|
Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
|
|
|
|
GetName() + " and user '" + user->GetName() + "': state filter does not match");
|
|
|
|
return;
|
|
|
|
}
|
2013-02-24 08:26:10 +01:00
|
|
|
}
|
|
|
|
|
2013-02-09 11:42:22 +01:00
|
|
|
try {
|
2013-09-24 18:58:51 +02:00
|
|
|
NotificationCommand::Ptr command = GetNotificationCommand();
|
2013-09-24 18:49:25 +02:00
|
|
|
|
2013-09-24 18:58:51 +02:00
|
|
|
if (!command) {
|
|
|
|
Log(LogDebug, "icinga", "No notification_command found for notification '" + GetName() + "'. Skipping execution.");
|
|
|
|
return;
|
|
|
|
}
|
2013-09-24 18:49:25 +02:00
|
|
|
|
2013-10-19 00:19:16 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-29 13:44:43 +01:00
|
|
|
Service::OnNotificationSentToUser(GetService(), user, type, cr, author, text, command->GetName());
|
2013-06-28 21:31:38 +02:00
|
|
|
|
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 '"
|
2013-11-20 21:55:14 +01:00
|
|
|
<< GetService()->GetName() << "': " << DiagnosticInformation(ex);
|
2013-09-03 15:23:47 +02:00
|
|
|
Log(LogWarning, "icinga", msgbuf.str());
|
2013-02-09 11:42:22 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-11 14:01:52 +01:00
|
|
|
|
2013-11-09 14:22:38 +01:00
|
|
|
bool Notification::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
|
2013-03-22 14:40:55 +01:00
|
|
|
{
|
|
|
|
Dictionary::Ptr macros = GetMacros();
|
|
|
|
|
|
|
|
if (macros && macros->Contains(macro)) {
|
|
|
|
*result = macros->Get(macro);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|