]> granicus.if.org Git - icinga2/blob - lib/livestatus/contactgroupstable.cpp
Livestatus: Add GroupBy tables: hostsbygroup, servicesbygroup, servicesbyhostgroup
[icinga2] / lib / livestatus / contactgroupstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "livestatus/contactgroupstable.hpp"
21 #include "icinga/usergroup.hpp"
22 #include "base/dynamictype.hpp"
23 #include <boost/foreach.hpp>
24
25 using namespace icinga;
26
27 ContactGroupsTable::ContactGroupsTable(void)
28 {
29         AddColumns(this);
30 }
31
32 void ContactGroupsTable::AddColumns(Table *table, const String& prefix,
33     const Column::ObjectAccessor& objectAccessor)
34 {
35         table->AddColumn(prefix + "name", Column(&ContactGroupsTable::NameAccessor, objectAccessor));
36         table->AddColumn(prefix + "alias", Column(&ContactGroupsTable::AliasAccessor, objectAccessor));
37         table->AddColumn(prefix + "members", Column(&ContactGroupsTable::MembersAccessor, objectAccessor));
38 }
39
40 String ContactGroupsTable::GetName(void) const
41 {
42         return "contactgroups";
43 }
44
45 String ContactGroupsTable::GetPrefix(void) const
46 {
47         return "contactgroup";
48 }
49
50 void ContactGroupsTable::FetchRows(const AddRowFunction& addRowFn)
51 {
52         BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjectsByType<UserGroup>()) {
53                 addRowFn(ug, LivestatusGroupByNone, Empty);
54         }
55 }
56
57 Value ContactGroupsTable::NameAccessor(const Value& row)
58 {
59         UserGroup::Ptr user_group = static_cast<UserGroup::Ptr>(row);
60
61         if (!user_group)
62                 return Empty;
63
64         return user_group->GetName();
65 }
66
67 Value ContactGroupsTable::AliasAccessor(const Value& row)
68 {
69         UserGroup::Ptr user_group = static_cast<UserGroup::Ptr>(row);
70
71         if (!user_group)
72                 return Empty;
73
74         return user_group->GetName();
75 }
76
77 Value ContactGroupsTable::MembersAccessor(const Value& row)
78 {
79         UserGroup::Ptr user_group = static_cast<UserGroup::Ptr>(row);
80
81         if (!user_group)
82                 return Empty;
83
84         Array::Ptr members = new Array();
85
86         BOOST_FOREACH(const User::Ptr& user, user_group->GetMembers()) {
87                 members->Add(user->GetName());
88         }
89
90         return members;
91 }