]> granicus.if.org Git - icinga2/blob - components/livestatus/commandstable.cpp
Livestatus: Add log table.
[icinga2] / components / livestatus / commandstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2013 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/commandstable.h"
21 #include "icinga/icingaapplication.h"
22 #include "icinga/checkcommand.h"
23 #include "icinga/eventcommand.h"
24 #include "icinga/notificationcommand.h"
25 #include "base/dynamictype.h"
26 #include "base/objectlock.h"
27 #include "base/convert.h"
28 #include <boost/foreach.hpp>
29 #include <boost/algorithm/string/replace.hpp>
30
31 using namespace icinga;
32 using namespace livestatus;
33
34 CommandsTable::CommandsTable(void)
35 {
36         AddColumns(this);
37 }
38
39 void CommandsTable::AddColumns(Table *table, const String& prefix,
40     const Column::ObjectAccessor& objectAccessor)
41 {
42         table->AddColumn(prefix + "name", Column(&CommandsTable::NameAccessor, objectAccessor));
43         table->AddColumn(prefix + "line", Column(&CommandsTable::LineAccessor, objectAccessor));
44 }
45
46 String CommandsTable::GetName(void) const
47 {
48         return "command";
49 }
50
51 void CommandsTable::FetchRows(const AddRowFunction& addRowFn)
52 {
53         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<CheckCommand>()) {
54                 addRowFn(object);
55         }
56         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<EventCommand>()) {
57                 addRowFn(object);
58         }
59         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects<NotificationCommand>()) {
60                 addRowFn(object);
61         }
62 }
63
64 Value CommandsTable::NameAccessor(const Value& row)
65 {
66         String buf;
67         Command::Ptr command = static_cast<Command::Ptr>(row);
68         
69         if (!command)
70                 return Empty;
71
72         if (command->GetType() == DynamicType::GetByName("CheckCommand"))
73                 buf += "check_";
74         if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
75                 buf += "notification_";
76         if (command->GetType() == DynamicType::GetByName("EventCommand"))
77                 buf += "event_";
78
79         buf += command->GetName();
80
81         return buf;
82 }
83
84 Value CommandsTable::LineAccessor(const Value& row)
85 {
86         String buf;
87         Command::Ptr command = static_cast<Command::Ptr>(row);
88         
89         if (!command)
90                 return Empty;
91
92         Value commandLine = command->GetCommandLine();
93
94         if (commandLine.IsObjectType<Array>()) {
95                 Array::Ptr args = commandLine;
96
97                 ObjectLock olock(args);
98                 String arg;
99                 BOOST_FOREACH(arg, args) {
100                         // This is obviously incorrect for non-trivial cases.
101                         String argitem = " \"" + arg + "\"";
102                         boost::algorithm::replace_all(argitem, "\n", "\\n");
103                         buf += argitem;
104                 }
105         } else if (!commandLine.IsEmpty()) {
106                 String args = Convert::ToString(commandLine);
107                 boost::algorithm::replace_all(args, "\n", "\\n");
108                 buf += args;
109         } else {
110                 buf += "<internal>";
111         }
112
113         return buf;
114 }