mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-20 12:14:44 +02:00
Implemented the User class.
This commit is contained in:
parent
2ef255b9f6
commit
77affd3ad6
@ -113,6 +113,10 @@ type Host {
|
|||||||
|
|
||||||
%attribute dictionary "macros" {
|
%attribute dictionary "macros" {
|
||||||
%attribute string "*"
|
%attribute string "*"
|
||||||
|
},
|
||||||
|
|
||||||
|
%attribute dictionary "users" {
|
||||||
|
%attribute string "*"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -194,11 +198,16 @@ type Service {
|
|||||||
%attribute dictionary "notifications" {
|
%attribute dictionary "notifications" {
|
||||||
%attribute string "*",
|
%attribute string "*",
|
||||||
%attribute dictionary "*" {
|
%attribute dictionary "*" {
|
||||||
%require "notification",
|
%attribute dictionary "templates" {
|
||||||
%attribute string "notification",
|
%attribute string "*"
|
||||||
|
},
|
||||||
|
|
||||||
%attribute dictionary "macros" {
|
%attribute dictionary "macros" {
|
||||||
%attribute string "*"
|
%attribute string "*"
|
||||||
|
},
|
||||||
|
|
||||||
|
%attribute dictionary "users" {
|
||||||
|
%attribute string "*"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -247,3 +256,10 @@ type Script {
|
|||||||
%require "code",
|
%require "code",
|
||||||
%attribute string "code"
|
%attribute string "code"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type User {
|
||||||
|
%attribute dictionary "macros" {
|
||||||
|
%attribute string "*"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,9 @@ libicinga_la_SOURCES = \
|
|||||||
service-notification.cpp \
|
service-notification.cpp \
|
||||||
service.h \
|
service.h \
|
||||||
servicegroup.cpp \
|
servicegroup.cpp \
|
||||||
servicegroup.h
|
servicegroup.h \
|
||||||
|
user.cpp \
|
||||||
|
user.h
|
||||||
|
|
||||||
libicinga_la_CPPFLAGS = \
|
libicinga_la_CPPFLAGS = \
|
||||||
-DI2_ICINGA_BUILD \
|
-DI2_ICINGA_BUILD \
|
||||||
|
@ -46,6 +46,8 @@ using boost::algorithm::is_any_of;
|
|||||||
#include "endpointmanager.h"
|
#include "endpointmanager.h"
|
||||||
#include "icingaapplication.h"
|
#include "icingaapplication.h"
|
||||||
|
|
||||||
|
#include "user.h"
|
||||||
|
|
||||||
#include "notification.h"
|
#include "notification.h"
|
||||||
#include "notificationrequestmessage.h"
|
#include "notificationrequestmessage.h"
|
||||||
|
|
||||||
|
@ -44,6 +44,7 @@
|
|||||||
<ClCompile Include="service-comment.cpp" />
|
<ClCompile Include="service-comment.cpp" />
|
||||||
<ClCompile Include="service-downtime.cpp" />
|
<ClCompile Include="service-downtime.cpp" />
|
||||||
<ClCompile Include="servicegroup.cpp" />
|
<ClCompile Include="servicegroup.cpp" />
|
||||||
|
<ClCompile Include="user.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="api.h" />
|
<ClInclude Include="api.h" />
|
||||||
@ -62,6 +63,7 @@
|
|||||||
<ClInclude Include="pluginnotificationtask.h" />
|
<ClInclude Include="pluginnotificationtask.h" />
|
||||||
<ClInclude Include="service.h" />
|
<ClInclude Include="service.h" />
|
||||||
<ClInclude Include="servicegroup.h" />
|
<ClInclude Include="servicegroup.h" />
|
||||||
|
<ClInclude Include="user.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<PropertyGroup Label="Globals">
|
<PropertyGroup Label="Globals">
|
||||||
<ProjectGuid>{C1FC77E1-04A4-481B-A78B-2F7AF489C2F8}</ProjectGuid>
|
<ProjectGuid>{C1FC77E1-04A4-481B-A78B-2F7AF489C2F8}</ProjectGuid>
|
||||||
|
@ -70,6 +70,22 @@ Dictionary::Ptr Notification::GetMacros(void) const
|
|||||||
return Get("macros");
|
return Get("macros");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set<User::Ptr> Notification::GetUsers(void) const
|
||||||
|
{
|
||||||
|
set<User::Ptr> result;
|
||||||
|
|
||||||
|
Dictionary::Ptr users = Get("users");
|
||||||
|
|
||||||
|
if (users) {
|
||||||
|
String name;
|
||||||
|
BOOST_FOREACH(tie(tuples::ignore, name), users) {
|
||||||
|
result.insert(User::GetByName(name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
String Notification::NotificationTypeToString(NotificationType type)
|
String Notification::NotificationTypeToString(NotificationType type)
|
||||||
{
|
{
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@ -100,11 +116,13 @@ void Notification::BeginExecuteNotification(const Notification::Ptr& self, Notif
|
|||||||
macroDicts.push_back(notificationMacros);
|
macroDicts.push_back(notificationMacros);
|
||||||
|
|
||||||
Service::Ptr service;
|
Service::Ptr service;
|
||||||
|
set<User::Ptr> users;
|
||||||
|
|
||||||
{
|
{
|
||||||
ObjectLock olock(self);
|
ObjectLock olock(self);
|
||||||
macroDicts.push_back(self->GetMacros());
|
macroDicts.push_back(self->GetMacros());
|
||||||
service = self->GetService();
|
service = self->GetService();
|
||||||
|
users = self->GetUsers();
|
||||||
}
|
}
|
||||||
|
|
||||||
Host::Ptr host;
|
Host::Ptr host;
|
||||||
@ -136,15 +154,45 @@ void Notification::BeginExecuteNotification(const Notification::Ptr& self, Notif
|
|||||||
|
|
||||||
Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
|
Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
|
||||||
|
|
||||||
vector<Value> arguments;
|
int count = 0;
|
||||||
arguments.push_back(self);
|
|
||||||
arguments.push_back(macros);
|
BOOST_FOREACH(const User::Ptr& user, users) {
|
||||||
arguments.push_back(type);
|
BeginExecuteNotificationHelper(self, macros, type, user);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count == 0) {
|
||||||
|
/* Send a notification even if there are no users specified. */
|
||||||
|
BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Notification::BeginExecuteNotificationHelper(const Notification::Ptr& self, const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
|
||||||
|
{
|
||||||
|
vector<Dictionary::Ptr> macroDicts;
|
||||||
|
|
||||||
|
if (user) {
|
||||||
|
{
|
||||||
|
ObjectLock olock(self);
|
||||||
|
macroDicts.push_back(user->GetMacros());
|
||||||
|
}
|
||||||
|
|
||||||
|
macroDicts.push_back(User::CalculateDynamicMacros(user));
|
||||||
|
}
|
||||||
|
|
||||||
|
macroDicts.push_back(notificationMacros);
|
||||||
|
|
||||||
|
Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
|
||||||
|
|
||||||
ScriptTask::Ptr task;
|
ScriptTask::Ptr task;
|
||||||
|
|
||||||
{
|
{
|
||||||
ObjectLock olock(self);
|
ObjectLock olock(self);
|
||||||
|
|
||||||
|
vector<Value> arguments;
|
||||||
|
arguments.push_back(self);
|
||||||
|
arguments.push_back(macros);
|
||||||
|
arguments.push_back(type);
|
||||||
task = self->MakeMethodTask("notify", arguments);
|
task = self->MakeMethodTask("notify", arguments);
|
||||||
|
|
||||||
if (!task) {
|
if (!task) {
|
||||||
|
@ -60,6 +60,7 @@ public:
|
|||||||
shared_ptr<Service> GetService(void) const;
|
shared_ptr<Service> GetService(void) const;
|
||||||
Value GetNotificationCommand(void) const;
|
Value GetNotificationCommand(void) const;
|
||||||
Dictionary::Ptr GetMacros(void) const;
|
Dictionary::Ptr GetMacros(void) const;
|
||||||
|
set<User::Ptr> GetUsers(void) const;
|
||||||
|
|
||||||
static void BeginExecuteNotification(const Notification::Ptr& self, NotificationType type);
|
static void BeginExecuteNotification(const Notification::Ptr& self, NotificationType type);
|
||||||
|
|
||||||
@ -72,6 +73,9 @@ private:
|
|||||||
set<ScriptTask::Ptr> m_Tasks;
|
set<ScriptTask::Ptr> m_Tasks;
|
||||||
|
|
||||||
void NotificationCompletedHandler(const ScriptTask::Ptr& task);
|
void NotificationCompletedHandler(const ScriptTask::Ptr& task);
|
||||||
|
|
||||||
|
static void BeginExecuteNotificationHelper(const Notification::Ptr& self,
|
||||||
|
const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,16 @@ void Service::SendNotifications(NotificationType type)
|
|||||||
Logger::Write(LogInformation, "icinga", "Service '" + GetName() + "' does not have any notifications.");
|
Logger::Write(LogInformation, "icinga", "Service '" + GetName() + "' does not have any notifications.");
|
||||||
|
|
||||||
BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
|
BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
|
||||||
Notification::BeginExecuteNotification(notification, type);
|
try {
|
||||||
|
Notification::BeginExecuteNotification(notification, type);
|
||||||
|
} catch (const exception& ex) {
|
||||||
|
stringstream msgbuf;
|
||||||
|
msgbuf << "Exception occured during notification for service '"
|
||||||
|
<< GetName() << "': " << diagnostic_information(ex);
|
||||||
|
String message = msgbuf.str();
|
||||||
|
|
||||||
|
Logger::Write(LogWarning, "icinga", message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SetLastNotification(Utility::GetTime());
|
SetLastNotification(Utility::GetTime());
|
||||||
@ -105,6 +114,10 @@ static void CopyNotificationAttributes(TDict notificationDesc, const ConfigItemB
|
|||||||
if (!macros.IsEmpty())
|
if (!macros.IsEmpty())
|
||||||
builder->AddExpression("macros", OperatorPlus, macros);
|
builder->AddExpression("macros", OperatorPlus, macros);
|
||||||
|
|
||||||
|
Value users = notificationDesc->Get("users");
|
||||||
|
if (!users.IsEmpty())
|
||||||
|
builder->AddExpression("users", OperatorPlus, users);
|
||||||
|
|
||||||
/*Value notificationInterval = notificationDesc->Get("notification_interval");
|
/*Value notificationInterval = notificationDesc->Get("notification_interval");
|
||||||
if (!notificationInterval.IsEmpty())
|
if (!notificationInterval.IsEmpty())
|
||||||
builder->AddExpression("notification_interval", OperatorSet, notificationInterval);*/
|
builder->AddExpression("notification_interval", OperatorSet, notificationInterval);*/
|
||||||
|
63
lib/icinga/user.cpp
Normal file
63
lib/icinga/user.cpp
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
* Icinga 2 *
|
||||||
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
||||||
|
* *
|
||||||
|
* 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. *
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include "i2-icinga.h"
|
||||||
|
|
||||||
|
using namespace icinga;
|
||||||
|
|
||||||
|
REGISTER_TYPE(User, NULL);
|
||||||
|
|
||||||
|
User::User(const Dictionary::Ptr& properties)
|
||||||
|
: DynamicObject(properties)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
bool User::Exists(const String& name)
|
||||||
|
{
|
||||||
|
return (DynamicObject::GetObject("User", name));
|
||||||
|
}
|
||||||
|
|
||||||
|
User::Ptr User::GetByName(const String& name)
|
||||||
|
{
|
||||||
|
DynamicObject::Ptr configObject = DynamicObject::GetObject("User", name);
|
||||||
|
|
||||||
|
if (!configObject)
|
||||||
|
BOOST_THROW_EXCEPTION(invalid_argument("User '" + name + "' does not exist."));
|
||||||
|
|
||||||
|
return dynamic_pointer_cast<User>(configObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
Dictionary::Ptr User::GetMacros(void) const
|
||||||
|
{
|
||||||
|
return Get("macros");
|
||||||
|
}
|
||||||
|
|
||||||
|
Dictionary::Ptr User::CalculateDynamicMacros(const User::Ptr& self)
|
||||||
|
{
|
||||||
|
Dictionary::Ptr macros = boost::make_shared<Dictionary>();
|
||||||
|
|
||||||
|
{
|
||||||
|
ObjectLock olock(self);
|
||||||
|
macros->Set("CONTACTNAME", self->GetName());
|
||||||
|
macros->Set("CONTACTALIAS", self->GetName());
|
||||||
|
}
|
||||||
|
|
||||||
|
macros->Seal();
|
||||||
|
|
||||||
|
return macros;
|
||||||
|
}
|
48
lib/icinga/user.h
Normal file
48
lib/icinga/user.h
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
* Icinga 2 *
|
||||||
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
||||||
|
* *
|
||||||
|
* 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. *
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#ifndef USER_H
|
||||||
|
#define USER_H
|
||||||
|
|
||||||
|
namespace icinga
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A User.
|
||||||
|
*
|
||||||
|
* @ingroup icinga
|
||||||
|
*/
|
||||||
|
class I2_ICINGA_API User : public DynamicObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef shared_ptr<User> Ptr;
|
||||||
|
typedef weak_ptr<User> WeakPtr;
|
||||||
|
|
||||||
|
User(const Dictionary::Ptr& properties);
|
||||||
|
|
||||||
|
static bool Exists(const String& name);
|
||||||
|
static User::Ptr GetByName(const String& name);
|
||||||
|
|
||||||
|
Dictionary::Ptr GetMacros(void) const;
|
||||||
|
static Dictionary::Ptr CalculateDynamicMacros(const User::Ptr& self);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* USER_H */
|
Loading…
x
Reference in New Issue
Block a user