]> granicus.if.org Git - icinga2/commitdiff
Livestatus: Add endpoints table.
authorMichael Friedrich <michael.friedrich@netways.de>
Thu, 20 Mar 2014 17:53:08 +0000 (18:53 +0100)
committerMichael Friedrich <michael.friedrich@netways.de>
Thu, 20 Mar 2014 17:53:08 +0000 (18:53 +0100)
Refs #5636
Refs #5811

components/livestatus/CMakeLists.txt
components/livestatus/endpointstable.cpp [new file with mode: 0644]
components/livestatus/endpointstable.h [new file with mode: 0644]
components/livestatus/table.cpp
test/livestatus/queries/endpoints/endpoints [new file with mode: 0644]

index babea867294875a073b581420ce846a64810f772..e48b331031ce57a1c949a827cc5e21cb3fb3b4a5 100644 (file)
@@ -19,9 +19,18 @@ mkclass_target(listener.ti listener.th)
 
 mkembedconfig_target(livestatus-type.conf livestatus-type.cpp)
 
-add_library(livestatus SHARED aggregator.cpp andfilter.cpp attributefilter.cpp avgaggregator.cpp column.cpp combinerfilter.cpp commandstable.cpp commentstable.cpp contactgroupstable.cpp contactstable.cpp countaggregator.cpp downtimestable.cpp filter.cpp historytable.cpp hostgroupstable.cpp hoststable.cpp invavgaggregator.cpp invsumaggregator.cpp listener.cpp listener.th logutility.cpp logtable.cpp maxaggregator.cpp minaggregator.cpp negatefilter.cpp orfilter.cpp query.cpp servicegroupstable.cpp servicestable.cpp statehisttable.cpp statustable.cpp stdaggregator.cpp sumaggregator.cpp table.cpp timeperiodstable.cpp livestatus-type.cpp)
+add_library(livestatus SHARED aggregator.cpp andfilter.cpp attributefilter.cpp
+  avgaggregator.cpp column.cpp combinerfilter.cpp commandstable.cpp
+  commentstable.cpp contactgroupstable.cpp contactstable.cpp countaggregator.cpp
+  downtimestable.cpp endpointstable.cpp filter.cpp historytable.cpp
+  hostgroupstable.cpp hoststable.cpp invavgaggregator.cpp invsumaggregator.cpp
+  listener.cpp listener.th logutility.cpp logtable.cpp maxaggregator.cpp
+  minaggregator.cpp negatefilter.cpp orfilter.cpp query.cpp
+  servicegroupstable.cpp servicestable.cpp statehisttable.cpp
+  statustable.cpp stdaggregator.cpp sumaggregator.cpp table.cpp
+  timeperiodstable.cpp livestatus-type.cpp)
 
-target_link_libraries(livestatus ${Boost_LIBRARIES} base config icinga)
+target_link_libraries(livestatus ${Boost_LIBRARIES} base config icinga remote)
 
 set_target_properties (
   livestatus PROPERTIES
diff --git a/components/livestatus/endpointstable.cpp b/components/livestatus/endpointstable.cpp
new file mode 100644 (file)
index 0000000..74d7990
--- /dev/null
@@ -0,0 +1,110 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2014 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 "livestatus/endpointstable.h"
+#include "icinga/host.h"
+#include "icinga/service.h"
+#include "icinga/icingaapplication.h"
+#include "remote/endpoint.h"
+#include "base/dynamictype.h"
+#include "base/objectlock.h"
+#include "base/convert.h"
+#include "base/utility.h"
+#include <boost/algorithm/string/classification.hpp>
+#include <boost/foreach.hpp>
+#include <boost/tuple/tuple.hpp>
+#include <boost/algorithm/string/replace.hpp>
+#include <boost/algorithm/string/split.hpp>
+
+using namespace icinga;
+
+EndpointsTable::EndpointsTable(void)
+{
+       AddColumns(this);
+}
+
+void EndpointsTable::AddColumns(Table *table, const String& prefix,
+    const Column::ObjectAccessor& objectAccessor)
+{
+       table->AddColumn(prefix + "name", Column(&EndpointsTable::NameAccessor, objectAccessor));
+       table->AddColumn(prefix + "identity", Column(&EndpointsTable::IdentityAccessor, objectAccessor));
+       table->AddColumn(prefix + "node", Column(&EndpointsTable::NodeAccessor, objectAccessor));
+       table->AddColumn(prefix + "is_connected", Column(&EndpointsTable::IsConnectedAccessor, objectAccessor));
+}
+
+String EndpointsTable::GetName(void) const
+{
+       return "endpoints";
+}
+
+void EndpointsTable::FetchRows(const AddRowFunction& addRowFn)
+{
+       BOOST_FOREACH(const Endpoint::Ptr& endpoint, DynamicType::GetObjects<Endpoint>()) {
+               addRowFn(endpoint);
+       }
+}
+
+Value EndpointsTable::NameAccessor(const Value& row)
+{
+       Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
+
+       if (!endpoint)
+               return Empty;
+
+       return endpoint->GetName();
+}
+
+Value EndpointsTable::IdentityAccessor(const Value& row)
+{
+       Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
+
+       if (!endpoint)
+               return Empty;
+
+       return endpoint->GetName();
+}
+
+Value EndpointsTable::NodeAccessor(const Value& row)
+{
+       Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
+
+       if (!endpoint)
+               return Empty;
+
+       return IcingaApplication::GetInstance()->GetNodeName();
+}
+
+Value EndpointsTable::IsConnectedAccessor(const Value& row)
+{
+       Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
+
+       if (!endpoint)
+               return Empty;
+
+       unsigned int is_connected = endpoint->IsConnected() ? 1 : 0;
+
+       /* if identity is equal to node, fake is_connected */
+       if (endpoint->GetName() == IcingaApplication::GetInstance()->GetNodeName())
+               is_connected = 1;
+
+       return is_connected;
+}
+
+
+
diff --git a/components/livestatus/endpointstable.h b/components/livestatus/endpointstable.h
new file mode 100644 (file)
index 0000000..8e2f50c
--- /dev/null
@@ -0,0 +1,56 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2014 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 ENDPOINTSTABLE_H
+#define ENDPOINTSTABLE_H
+
+#include "livestatus/table.h"
+
+using namespace icinga;
+
+namespace icinga
+{
+
+/**
+ * @ingroup livestatus
+ */
+class EndpointsTable : public Table
+{
+public:
+       DECLARE_PTR_TYPEDEFS(EndpointsTable);
+
+       EndpointsTable(void);
+
+       static void AddColumns(Table *table, const String& prefix = String(),
+           const Column::ObjectAccessor& objectAccessor = Column::ObjectAccessor());
+
+       virtual String GetName(void) const;
+
+protected:
+       virtual void FetchRows(const AddRowFunction& addRowFn);
+
+       static Value NameAccessor(const Value& row);
+        static Value IdentityAccessor(const Value& row);
+        static Value NodeAccessor(const Value& row);
+        static Value IsConnectedAccessor(const Value& row);
+};
+
+}
+
+#endif /* ENDPOINTSTABLE_H */
index 6873faf74b259199625fa0ac09ff49b88cac6a91..4d56b52371cff80ad3063b7184a1d1190682bfa1 100644 (file)
@@ -28,6 +28,7 @@
 #include "livestatus/commandstable.h"
 #include "livestatus/commentstable.h"
 #include "livestatus/downtimestable.h"
+#include "livestatus/endpointstable.h"
 #include "livestatus/timeperiodstable.h"
 #include "livestatus/logtable.h"
 #include "livestatus/statehisttable.h"
@@ -71,6 +72,8 @@ Table::Ptr Table::GetByName(const String& name, const String& compat_log_path, c
                return make_shared<LogTable>(compat_log_path, from, until);
        else if (name == "statehist")
                return make_shared<StateHistTable>(compat_log_path, from, until);
+       else if (name == "endpoints")
+               return make_shared<EndpointsTable>();
 
        return Table::Ptr();
 }
diff --git a/test/livestatus/queries/endpoints/endpoints b/test/livestatus/queries/endpoints/endpoints
new file mode 100644 (file)
index 0000000..3ff4a02
--- /dev/null
@@ -0,0 +1,3 @@
+GET endpoints
+ResponseHeader: fixed16
+