Implemented user groups.

This commit is contained in:
Gunnar Beutner 2013-02-27 21:49:03 +01:00
parent 6bf086dc91
commit e9d32eeada
18 changed files with 329 additions and 17 deletions

View File

@ -91,7 +91,7 @@ String CompatComponent::GetCommandPath(void) const
void CompatComponent::Start(void)
{
m_StatusTimer = boost::make_shared<Timer>();
m_StatusTimer->SetInterval(15);
m_StatusTimer->SetInterval(5);
m_StatusTimer->OnTimerExpired.connect(boost::bind(&CompatComponent::StatusTimerHandler, this));
m_StatusTimer->Start();
m_StatusTimer->Reschedule(0);

View File

@ -117,6 +117,10 @@ type Host {
%attribute dictionary "users" {
%attribute string "*"
},
%attribute dictionary "groups" {
%attribute string "*"
}
}
},
@ -209,6 +213,10 @@ type Service {
%attribute dictionary "users" {
%attribute string "*"
},
%attribute dictionary "groups" {
%attribute string "*"
}
}
},
@ -243,9 +251,17 @@ type Notification {
%attribute string "service",
%attribute dictionary "macros" {
%attribute string "*"
%attribute string "*"
},
%attribute dictionary "users" {
%attribute string "*"
},
%attribute dictionary "groups" {
%attribute string "*"
},
%attribute dictionary "notification_command" {
%attribute string "_*"
},
@ -263,6 +279,15 @@ type Script {
type User {
%attribute dictionary "macros" {
%attribute string "*"
},
%attribute dictionary "groups" {
%attribute string "*"
}
}
type UserGroup {
%attribute string "display_name",
%attribute string "action_url",
%attribute string "notes_url"
}

View File

@ -92,6 +92,7 @@
#include <list>
#include <algorithm>
#include <deque>
#include <iterator>
using std::vector;
using std::map;

View File

@ -42,7 +42,9 @@ libicinga_la_SOURCES = \
servicegroup.cpp \
servicegroup.h \
user.cpp \
user.h
user.h \
usergroup.cpp \
usergroup.h
libicinga_la_CPPFLAGS = \
-DI2_ICINGA_BUILD \

View File

@ -22,7 +22,7 @@
using namespace icinga;
boost::mutex Host::m_ServiceMutex;
map<String, map<String, weak_ptr<Service> > > Host::m_ServicesCache;
map<String, map<String, Service::WeakPtr> > Host::m_ServicesCache;
bool Host::m_ServicesCacheValid = true;
REGISTER_SCRIPTFUNCTION("ValidateServiceDictionary", &Host::ValidateServiceDictionary);
@ -345,7 +345,7 @@ void Host::RefreshServicesCache(void)
m_ServicesCacheValid = true;
}
map<String, map<String, weak_ptr<Service> > > newServicesCache;
map<String, map<String, Service::WeakPtr> > newServicesCache;
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
const Service::Ptr& service = static_pointer_cast<Service>(object);
@ -441,8 +441,8 @@ Service::Ptr Host::GetServiceByShortName(const Host::Ptr& self, const Value& nam
{
boost::mutex::scoped_lock lock(m_ServiceMutex);
map<String, weak_ptr<Service> >& services = m_ServicesCache[host_name];
map<String, weak_ptr<Service> >::iterator it = services.find(name);
map<String, Service::WeakPtr>& services = m_ServicesCache[host_name];
map<String, Service::WeakPtr>::iterator it = services.find(name);
if (it != services.end()) {
Service::Ptr service = it->second.lock();

View File

@ -126,7 +126,7 @@ void HostGroup::RefreshMembersCache(void)
m_MembersCacheValid = true;
}
map<String, vector<weak_ptr<Host> > > newMembersCache;
map<String, vector<Host::WeakPtr> > newMembersCache;
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Host")) {
const Host::Ptr& host = static_pointer_cast<Host>(object);

View File

@ -56,7 +56,7 @@ private:
Attribute<String> m_ActionUrl;
static boost::mutex m_Mutex;
static map<String, vector<weak_ptr<Host> > > m_MembersCache;
static map<String, vector<Host::WeakPtr> > m_MembersCache;
static bool m_MembersCacheValid;
static void RefreshMembersCache(void);

View File

@ -47,6 +47,7 @@ using boost::algorithm::is_any_of;
#include "icingaapplication.h"
#include "user.h"
#include "usergroup.h"
#include "notification.h"
#include "notificationrequestmessage.h"

View File

@ -45,6 +45,7 @@
<ClCompile Include="service-downtime.cpp" />
<ClCompile Include="servicegroup.cpp" />
<ClCompile Include="user.cpp" />
<ClCompile Include="usergroup.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="api.h" />
@ -64,6 +65,7 @@
<ClInclude Include="service.h" />
<ClInclude Include="servicegroup.h" />
<ClInclude Include="user.h" />
<ClInclude Include="usergroup.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{C1FC77E1-04A4-481B-A78B-2F7AF489C2F8}</ProjectGuid>

View File

@ -29,6 +29,7 @@ Notification::Notification(const Dictionary::Ptr& properties)
RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
RegisterAttribute("macros", Attribute_Config, &m_Macros);
RegisterAttribute("users", Attribute_Config, &m_Users);
RegisterAttribute("groups", Attribute_Config, &m_Groups);
RegisterAttribute("host_name", Attribute_Config, &m_HostName);
RegisterAttribute("service", Attribute_Config, &m_Service);
}
@ -80,7 +81,33 @@ set<User::Ptr> Notification::GetUsers(void) const
if (users) {
String name;
BOOST_FOREACH(tie(tuples::ignore, name), users) {
result.insert(User::GetByName(name));
User::Ptr user = User::GetByName(name);
if (!user)
continue;
result.insert(user);
}
}
return result;
}
set<UserGroup::Ptr> Notification::GetGroups(void) const
{
set<UserGroup::Ptr> result;
Dictionary::Ptr groups = m_Groups;
if (groups) {
String name;
BOOST_FOREACH(tie(tuples::ignore, name), groups) {
UserGroup::Ptr ug = UserGroup::GetByName(name);
if (!ug)
continue;
result.insert(ug);
}
}
@ -120,12 +147,14 @@ void Notification::BeginExecuteNotification(const Notification::Ptr& self, Notif
Service::Ptr service;
set<User::Ptr> users;
set<UserGroup::Ptr> groups;
{
ObjectLock olock(self);
macroDicts.push_back(self->GetMacros());
service = self->GetService();
users = self->GetUsers();
groups = self->GetGroups();
}
Host::Ptr host;
@ -157,14 +186,20 @@ void Notification::BeginExecuteNotification(const Notification::Ptr& self, Notif
Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
int count = 0;
set<User::Ptr> allUsers;
BOOST_FOREACH(const User::Ptr& user, users) {
BeginExecuteNotificationHelper(self, macros, type, user);
count++;
std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
BOOST_FOREACH(const UserGroup::Ptr& ug, groups) {
set<User::Ptr> members = UserGroup::GetMembers(ug);
std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
}
if (count == 0) {
BOOST_FOREACH(const User::Ptr& user, allUsers) {
BeginExecuteNotificationHelper(self, macros, type, user);
}
if (allUsers.size() == 0) {
/* Send a notification even if there are no users specified. */
BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
}

View File

@ -61,6 +61,7 @@ public:
Value GetNotificationCommand(void) const;
Dictionary::Ptr GetMacros(void) const;
set<User::Ptr> GetUsers(void) const;
set<UserGroup::Ptr> GetGroups(void) const;
static void BeginExecuteNotification(const Notification::Ptr& self, NotificationType type);
@ -73,6 +74,7 @@ private:
Attribute<Value> m_NotificationCommand;
Attribute<Dictionary::Ptr> m_Macros;
Attribute<Dictionary::Ptr> m_Users;
Attribute<Dictionary::Ptr> m_Groups;
Attribute<String> m_HostName;
Attribute<String> m_Service;

View File

@ -169,6 +169,10 @@ static void CopyNotificationAttributes(TDict notificationDesc, const ConfigItemB
if (!users.IsEmpty())
builder->AddExpression("users", OperatorPlus, users);
Value groups = notificationDesc->Get("groups");
if (!groups.IsEmpty())
builder->AddExpression("groups", OperatorPlus, groups);
/*Value notificationInterval = notificationDesc->Get("notification_interval");
if (!notificationInterval.IsEmpty())
builder->AddExpression("notification_interval", OperatorSet, notificationInterval);*/

View File

@ -126,7 +126,7 @@ void ServiceGroup::RefreshMembersCache(void)
m_MembersCacheValid = true;
}
map<String, vector<weak_ptr<Service> > > newMembersCache;
map<String, vector<Service::WeakPtr> > newMembersCache;
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
const Service::Ptr& service = static_pointer_cast<Service>(object);

View File

@ -56,7 +56,7 @@ private:
Attribute<String> m_ActionUrl;
static boost::mutex m_Mutex;
static map<String, vector<weak_ptr<Service> > > m_MembersCache;
static map<String, vector<Service::WeakPtr> > m_MembersCache;
static bool m_MembersCacheValid;
static void RefreshMembersCache(void);

View File

@ -27,6 +27,18 @@ User::User(const Dictionary::Ptr& properties)
: DynamicObject(properties)
{
RegisterAttribute("macros", Attribute_Config, &m_Macros);
RegisterAttribute("groups", Attribute_Config, &m_Groups);
}
User::~User(void)
{
UserGroup::InvalidateMembersCache();
}
void User::OnAttributeChanged(const String& name, const Value& oldValue)
{
if (name == "groups")
UserGroup::InvalidateMembersCache();
}
User::Ptr User::GetByName(const String& name)
@ -36,6 +48,11 @@ User::Ptr User::GetByName(const String& name)
return dynamic_pointer_cast<User>(configObject);
}
Dictionary::Ptr User::GetGroups(void) const
{
return m_Groups;
}
Dictionary::Ptr User::GetMacros(void) const
{
return m_Macros;

View File

@ -35,14 +35,21 @@ public:
typedef weak_ptr<User> WeakPtr;
User(const Dictionary::Ptr& properties);
~User(void);
static User::Ptr GetByName(const String& name);
Dictionary::Ptr GetGroups(void) const;
Dictionary::Ptr GetMacros(void) const;
static Dictionary::Ptr CalculateDynamicMacros(const User::Ptr& self);
protected:
void OnAttributeChanged(const String& name, const Value& oldValue);
private:
Attribute<Dictionary::Ptr> m_Macros;
Attribute<Dictionary::Ptr> m_Groups;
};
}

149
lib/icinga/usergroup.cpp Normal file
View File

@ -0,0 +1,149 @@
/******************************************************************************
* 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;
boost::mutex UserGroup::m_Mutex;
map<String, vector<User::WeakPtr> > UserGroup::m_MembersCache;
bool UserGroup::m_MembersCacheValid = true;
REGISTER_TYPE(UserGroup, NULL);
UserGroup::UserGroup(const Dictionary::Ptr& properties)
: DynamicObject(properties)
{
RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
RegisterAttribute("notes_url", Attribute_Config, &m_NotesUrl);
RegisterAttribute("action_url", Attribute_Config, &m_ActionUrl);
}
UserGroup::~UserGroup(void)
{
InvalidateMembersCache();
}
void UserGroup::OnRegistrationCompleted(void)
{
InvalidateMembersCache();
}
String UserGroup::GetDisplayName(void) const
{
if (!m_DisplayName.IsEmpty())
return m_DisplayName;
else
return GetName();
}
String UserGroup::GetNotesUrl(void) const
{
return m_NotesUrl;
}
String UserGroup::GetActionUrl(void) const
{
return m_ActionUrl;
}
/**
* @threadsafety Always.
*/
UserGroup::Ptr UserGroup::GetByName(const String& name)
{
DynamicObject::Ptr configObject = DynamicObject::GetObject("UserGroup", name);
if (!configObject)
BOOST_THROW_EXCEPTION(invalid_argument("UserGroup '" + name + "' does not exist."));
return dynamic_pointer_cast<UserGroup>(configObject);
}
set<User::Ptr> UserGroup::GetMembers(const UserGroup::Ptr& self)
{
String name;
{
ObjectLock olock(self);
name = self->GetName();
}
set<User::Ptr> users;
{
boost::mutex::scoped_lock lock(m_Mutex);
BOOST_FOREACH(const User::WeakPtr& wuser, m_MembersCache[name]) {
User::Ptr user = wuser.lock();
if (!user)
continue;
users.insert(user);
}
}
return users;
}
void UserGroup::InvalidateMembersCache(void)
{
{
boost::mutex::scoped_lock lock(m_Mutex);
if (m_MembersCacheValid)
Utility::QueueAsyncCallback(boost::bind(&UserGroup::RefreshMembersCache));
m_MembersCacheValid = false;
}
}
void UserGroup::RefreshMembersCache(void)
{
{
boost::mutex::scoped_lock lock(m_Mutex);
if (m_MembersCacheValid)
return;
m_MembersCacheValid = true;
}
map<String, vector<User::WeakPtr> > newMembersCache;
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("User")) {
const User::Ptr& user = static_pointer_cast<User>(object);
ObjectLock olock(user);
Dictionary::Ptr dict;
dict = user->GetGroups();
if (dict) {
ObjectLock mlock(dict);
Value UserGroup;
BOOST_FOREACH(tie(tuples::ignore, UserGroup), dict) {
newMembersCache[UserGroup].push_back(user);
}
}
}
boost::mutex::scoped_lock lock(m_Mutex);
m_MembersCache.swap(newMembersCache);
}

67
lib/icinga/usergroup.h Normal file
View File

@ -0,0 +1,67 @@
/******************************************************************************
* 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 USERGROUP_H
#define USERGROUP_H
namespace icinga
{
/**
* An Icinga user group.
*
* @ingroup icinga
*/
class I2_ICINGA_API UserGroup : public DynamicObject
{
public:
typedef shared_ptr<UserGroup> Ptr;
typedef weak_ptr<UserGroup> WeakPtr;
UserGroup(const Dictionary::Ptr& properties);
~UserGroup(void);
static UserGroup::Ptr GetByName(const String& name);
String GetDisplayName(void) const;
String GetNotesUrl(void) const;
String GetActionUrl(void) const;
static set<User::Ptr> GetMembers(const UserGroup::Ptr& self);
static void InvalidateMembersCache(void);
protected:
virtual void OnRegistrationCompleted(void);
private:
Attribute<String> m_DisplayName;
Attribute<String> m_NotesUrl;
Attribute<String> m_ActionUrl;
static boost::mutex m_Mutex;
static map<String, vector<User::WeakPtr> > m_MembersCache;
static bool m_MembersCacheValid;
static void RefreshMembersCache(void);
};
}
#endif /* HOSTGROUP_H */