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);
%attribute dictionary "users" {
%attribute string "*"
+ },
+
+ %attribute dictionary "groups" {
+ %attribute string "*"
}
}
},
%attribute dictionary "users" {
%attribute string "*"
+ },
+
+ %attribute dictionary "groups" {
+ %attribute string "*"
}
}
},
%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 "_*"
},
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"
+}
#include <list>
#include <algorithm>
#include <deque>
+#include <iterator>
using std::vector;
using std::map;
servicegroup.cpp \
servicegroup.h \
user.cpp \
- user.h
+ user.h \
+ usergroup.cpp \
+ usergroup.h
libicinga_la_CPPFLAGS = \
-DI2_ICINGA_BUILD \
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);
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);
{
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();
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);
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);
#include "icingaapplication.h"
#include "user.h"
+#include "usergroup.h"
#include "notification.h"
#include "notificationrequestmessage.h"
<ClCompile Include="service-downtime.cpp" />
<ClCompile Include="servicegroup.cpp" />
<ClCompile Include="user.cpp" />
+ <ClCompile Include="usergroup.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="api.h" />
<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>
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);
}
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);
}
}
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;
Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
- int count = 0;
+ set<User::Ptr> allUsers;
+
+ 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()));
+ }
- BOOST_FOREACH(const User::Ptr& user, users) {
+ BOOST_FOREACH(const User::Ptr& user, allUsers) {
BeginExecuteNotificationHelper(self, macros, type, user);
- count++;
}
- if (count == 0) {
+ if (allUsers.size() == 0) {
/* Send a notification even if there are no users specified. */
BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
}
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);
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;
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);*/
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);
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);
: 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)
return dynamic_pointer_cast<User>(configObject);
}
+Dictionary::Ptr User::GetGroups(void) const
+{
+ return m_Groups;
+}
+
Dictionary::Ptr User::GetMacros(void) const
{
return m_Macros;
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;
};
}
--- /dev/null
+/******************************************************************************
+ * 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);
+}
--- /dev/null
+/******************************************************************************
+ * 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 */