2013-07-09 18:09:03 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2013-09-25 07:43:57 +02:00
|
|
|
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
|
2013-07-09 18:09:03 +02:00
|
|
|
* *
|
|
|
|
* 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 "livestatus/commandstable.h"
|
|
|
|
#include "icinga/icingaapplication.h"
|
|
|
|
#include "icinga/checkcommand.h"
|
|
|
|
#include "icinga/eventcommand.h"
|
|
|
|
#include "icinga/notificationcommand.h"
|
|
|
|
#include "base/dynamictype.h"
|
2013-07-11 11:47:32 +02:00
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/convert.h"
|
2013-07-09 18:09:03 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2013-07-11 11:47:32 +02:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2013-07-09 18:09:03 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
using namespace livestatus;
|
|
|
|
|
|
|
|
CommandsTable::CommandsTable(void)
|
|
|
|
{
|
|
|
|
AddColumns(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandsTable::AddColumns(Table *table, const String& prefix,
|
|
|
|
const Column::ObjectAccessor& objectAccessor)
|
|
|
|
{
|
|
|
|
table->AddColumn(prefix + "name", Column(&CommandsTable::NameAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "line", Column(&CommandsTable::LineAccessor, objectAccessor));
|
|
|
|
}
|
|
|
|
|
|
|
|
String CommandsTable::GetName(void) const
|
|
|
|
{
|
|
|
|
return "command";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandsTable::FetchRows(const AddRowFunction& addRowFn)
|
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<CheckCommand>()) {
|
2013-07-09 18:09:03 +02:00
|
|
|
addRowFn(object);
|
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<EventCommand>()) {
|
2013-07-09 18:09:03 +02:00
|
|
|
addRowFn(object);
|
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<NotificationCommand>()) {
|
2013-07-09 18:09:03 +02:00
|
|
|
addRowFn(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value CommandsTable::NameAccessor(const Value& row)
|
2013-07-09 18:09:03 +02:00
|
|
|
{
|
2013-07-11 11:47:32 +02:00
|
|
|
String buf;
|
|
|
|
Command::Ptr command = static_cast<Command::Ptr>(row);
|
2013-10-29 13:44:43 +01:00
|
|
|
|
|
|
|
if (!command)
|
|
|
|
return Empty;
|
2013-07-11 11:47:32 +02:00
|
|
|
|
|
|
|
if (command->GetType() == DynamicType::GetByName("CheckCommand"))
|
|
|
|
buf += "check_";
|
|
|
|
if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
|
|
|
|
buf += "notification_";
|
|
|
|
if (command->GetType() == DynamicType::GetByName("EventCommand"))
|
|
|
|
buf += "event_";
|
|
|
|
|
|
|
|
buf += command->GetName();
|
|
|
|
|
|
|
|
return buf;
|
2013-07-09 18:09:03 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value CommandsTable::LineAccessor(const Value& row)
|
2013-07-09 18:09:03 +02:00
|
|
|
{
|
2013-07-11 11:47:32 +02:00
|
|
|
String buf;
|
|
|
|
Command::Ptr command = static_cast<Command::Ptr>(row);
|
2013-10-29 13:44:43 +01:00
|
|
|
|
|
|
|
if (!command)
|
|
|
|
return Empty;
|
2013-07-11 11:47:32 +02:00
|
|
|
|
|
|
|
Value commandLine = command->GetCommandLine();
|
|
|
|
|
|
|
|
if (commandLine.IsObjectType<Array>()) {
|
|
|
|
Array::Ptr args = commandLine;
|
|
|
|
|
|
|
|
ObjectLock olock(args);
|
|
|
|
String arg;
|
|
|
|
BOOST_FOREACH(arg, args) {
|
|
|
|
// This is obviously incorrect for non-trivial cases.
|
|
|
|
String argitem = " \"" + arg + "\"";
|
|
|
|
boost::algorithm::replace_all(argitem, "\n", "\\n");
|
|
|
|
buf += argitem;
|
|
|
|
}
|
|
|
|
} else if (!commandLine.IsEmpty()) {
|
|
|
|
String args = Convert::ToString(commandLine);
|
|
|
|
boost::algorithm::replace_all(args, "\n", "\\n");
|
|
|
|
buf += args;
|
|
|
|
} else {
|
|
|
|
buf += "<internal>";
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
2013-07-09 18:09:03 +02:00
|
|
|
}
|