2013-03-10 03:09:01 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* 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-livestatus.h"
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2013-03-10 03:09:01 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
2013-03-10 09:23:13 +01:00
|
|
|
using namespace livestatus;
|
2013-03-10 03:09:01 +01:00
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
Table::Table(void)
|
2013-03-10 03:09:01 +01:00
|
|
|
{ }
|
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
Table::Ptr Table::GetByName(const String& name)
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
2013-03-10 09:23:13 +01:00
|
|
|
if (name == "status")
|
|
|
|
return boost::make_shared<StatusTable>();
|
2013-03-10 09:55:46 +01:00
|
|
|
else if (name == "contactgroups")
|
|
|
|
return boost::make_shared<ContactGroupsTable>();
|
|
|
|
else if (name == "contacts")
|
|
|
|
return boost::make_shared<ContactsTable>();
|
2013-03-10 18:49:14 +01:00
|
|
|
else if (name == "hosts")
|
|
|
|
return boost::make_shared<HostsTable>();
|
2013-03-10 22:20:13 +01:00
|
|
|
else if (name == "services")
|
|
|
|
return boost::make_shared<ServicesTable>();
|
|
|
|
else if (name == "comments")
|
|
|
|
return boost::make_shared<CommentsTable>();
|
|
|
|
else if (name == "downtimes")
|
|
|
|
return boost::make_shared<DowntimesTable>();
|
2013-03-10 03:09:01 +01:00
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
return Table::Ptr();
|
|
|
|
}
|
|
|
|
|
2013-03-10 18:49:14 +01:00
|
|
|
void Table::AddColumn(const String& name, const Column& column)
|
2013-03-10 09:23:13 +01:00
|
|
|
{
|
2013-03-10 18:49:14 +01:00
|
|
|
pair<String, Column> item = make_pair(name, column);
|
|
|
|
|
|
|
|
pair<map<String, Column>::iterator, bool> ret = m_Columns.insert(item);
|
|
|
|
|
|
|
|
if (!ret.second)
|
|
|
|
ret.first->second = column;
|
2013-03-10 09:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 18:49:14 +01:00
|
|
|
Column Table::GetColumn(const String& name) const
|
2013-03-10 09:23:13 +01:00
|
|
|
{
|
2013-03-10 18:49:14 +01:00
|
|
|
map<String, Column>::const_iterator it = m_Columns.find(name);
|
2013-03-10 09:23:13 +01:00
|
|
|
|
|
|
|
if (it == m_Columns.end())
|
2013-03-10 18:49:14 +01:00
|
|
|
BOOST_THROW_EXCEPTION(invalid_argument("Column '" + name + "' does not exist."));
|
2013-03-10 05:10:51 +01:00
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<String> Table::GetColumnNames(void) const
|
|
|
|
{
|
|
|
|
vector<String> names;
|
|
|
|
|
|
|
|
String name;
|
2013-03-15 18:21:29 +01:00
|
|
|
BOOST_FOREACH(boost::tie(name, boost::tuples::ignore), m_Columns) {
|
2013-03-10 09:23:13 +01:00
|
|
|
names.push_back(name);
|
2013-03-10 05:10:51 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
return names;
|
|
|
|
}
|
2013-03-10 03:09:01 +01:00
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
vector<Object::Ptr> Table::FilterRows(const Filter::Ptr& filter)
|
|
|
|
{
|
|
|
|
vector<Object::Ptr> rs;
|
2013-03-10 05:10:51 +01:00
|
|
|
|
2013-03-10 15:11:32 +01:00
|
|
|
FetchRows(boost::bind(&Table::FilteredAddRow, this, boost::ref(rs), filter, _1));
|
2013-03-10 09:23:13 +01:00
|
|
|
|
|
|
|
return rs;
|
|
|
|
}
|
2013-03-10 05:10:51 +01:00
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
void Table::FilteredAddRow(vector<Object::Ptr>& rs, const Filter::Ptr& filter, const Object::Ptr& object)
|
|
|
|
{
|
2013-03-10 15:11:32 +01:00
|
|
|
if (!filter || filter->Apply(GetSelf(), object))
|
2013-03-10 09:23:13 +01:00
|
|
|
rs.push_back(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Table::ZeroAccessor(const Object::Ptr&)
|
|
|
|
{
|
|
|
|
return 0;
|
2013-03-10 03:09:01 +01:00
|
|
|
}
|
2013-03-10 09:55:46 +01:00
|
|
|
|
|
|
|
Value Table::OneAccessor(const Object::Ptr&)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Table::EmptyStringAccessor(const Object::Ptr&)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Table::EmptyArrayAccessor(const Object::Ptr&)
|
|
|
|
{
|
|
|
|
return boost::make_shared<Array>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Table::EmptyDictionaryAccessor(const Object::Ptr&)
|
|
|
|
{
|
|
|
|
return boost::make_shared<Dictionary>();
|
|
|
|
}
|