]> granicus.if.org Git - icinga2/commitdiff
DB IDO: Add endpoints/endpointstatus tables.
authorMichael Friedrich <michael.friedrich@netways.de>
Fri, 21 Feb 2014 13:12:34 +0000 (14:12 +0100)
committerMichael Friedrich <michael.friedrich@netways.de>
Thu, 20 Mar 2014 17:03:47 +0000 (18:03 +0100)
Refs #5636

components/cluster/endpoint.cpp
components/cluster/endpoint.h
components/db_ido_mysql/schema/mysql.sql
lib/db_ido/CMakeLists.txt
lib/db_ido/dbobject.h
lib/db_ido/endpointdbobject.cpp [new file with mode: 0644]
lib/db_ido/endpointdbobject.h [new file with mode: 0644]

index adea27395cd191bdac72050ee496763cfe75113b..ba8bd378c8ef320600d5da4750dd9766c68f76b2 100644 (file)
@@ -32,6 +32,7 @@ using namespace icinga;
 REGISTER_TYPE(Endpoint);
 
 boost::signals2::signal<void (const Endpoint::Ptr&)> Endpoint::OnConnected;
+boost::signals2::signal<void (const Endpoint::Ptr&)> Endpoint::OnDisconnected;
 boost::signals2::signal<void (const Endpoint::Ptr&, const Dictionary::Ptr&)> Endpoint::OnMessageReceived;
 
 /**
@@ -61,6 +62,10 @@ void Endpoint::SetClient(const Stream::Ptr& client)
                thread.detach();
 
                OnConnected(GetSelf());
+               Log(LogWarning, "cluster", "Endpoint connected: " + GetName());
+       } else {
+               OnDisconnected(GetSelf());
+               Log(LogWarning, "cluster", "Endpoint disconnected: " + GetName());
        }
 }
 
@@ -79,6 +84,9 @@ void Endpoint::SendMessage(const Dictionary::Ptr& message)
                Log(LogWarning, "cluster", msgbuf.str());
 
                m_Client.reset();
+
+               OnDisconnected(GetSelf());
+               Log(LogWarning, "cluster", "Endpoint disconnected: " + GetName());
        }
 }
 
@@ -96,6 +104,9 @@ void Endpoint::MessageThreadProc(const Stream::Ptr& stream)
 
                        m_Client.reset();
 
+                       OnDisconnected(GetSelf());
+                       Log(LogWarning, "cluster", "Endpoint disconnected: " + GetName());
+
                        return;
                }
 
index 62925012efa29365e6e8d9bded95646e74e5f43f..97f369a5e5d97226dd93e74355be8f2a6070faad 100644 (file)
@@ -42,6 +42,7 @@ public:
        DECLARE_TYPENAME(Endpoint);
 
        static boost::signals2::signal<void (const Endpoint::Ptr&)> OnConnected;
+        static boost::signals2::signal<void (const Endpoint::Ptr&)> OnDisconnected;
        static boost::signals2::signal<void (const Endpoint::Ptr&, const Dictionary::Ptr&)> OnMessageReceived;
 
        Stream::Ptr GetClient(void) const;
index 45a23969f9f380ea9b12233e3279c5b15c57b507..6e1deec6e31deb4375fc97fdafe5a6d5480f80d3 100644 (file)
@@ -1392,7 +1392,6 @@ ALTER TABLE icinga_servicechecks ADD COLUMN endpoint_object_id bigint default NU
 ALTER TABLE icinga_statehistory ADD COLUMN endpoint_object_id bigint default NULL;
 ALTER TABLE icinga_systemcommands ADD COLUMN endpoint_object_id bigint default NULL;
 
-
 -- -----------------------------------------
 -- add index (delete)
 -- -----------------------------------------
index 2ad7fb90bf1c6484be1c95cfe02cad3340aac350..9d1c8a3533d0cf8e5b339ff831af8a9a753360f9 100644 (file)
@@ -22,13 +22,13 @@ mkembedconfig_target(db_ido-type.conf db_ido-type.cpp)
 add_library(db_ido SHARED
   commanddbobject.cpp dbconnection.cpp dbconnection.th dbconnection.th
   db_ido-type.cpp dbobject.cpp dbquery.cpp dbreference.cpp dbtype.cpp
-  dbvalue.cpp hostdbobject.cpp hostgroupdbobject.cpp servicedbobject.cpp
-  servicegroupdbobject.cpp timeperioddbobject.cpp userdbobject.cpp
-  usergroupdbobject.cpp
+  dbvalue.cpp endpointdbobject.cpp hostdbobject.cpp hostgroupdbobject.cpp
+  servicedbobject.cpp servicegroupdbobject.cpp timeperioddbobject.cpp
+  userdbobject.cpp usergroupdbobject.cpp
 )
 
 include_directories(${Boost_INCLUDE_DIRS})
-target_link_libraries(db_ido ${Boost_LIBRARIES} base config icinga)
+target_link_libraries(db_ido ${Boost_LIBRARIES} base config icinga cluster)
 
 set_target_properties (
   db_ido PROPERTIES
index 8031eeab6b90eb465100c7e431d6737745fa9fd4..d6bc9abbb32bc932f292bcd5bd3ae1a7dfa2a314 100644 (file)
@@ -49,6 +49,7 @@ enum DbObjectType
        DbObjectTypeContact = 10,
        DbObjectTypeContactGroup = 11,
        DbObjectTypeCommand = 12,
+       DbObjectTypeEndpoint = 13,
 };
 
 /**
diff --git a/lib/db_ido/endpointdbobject.cpp b/lib/db_ido/endpointdbobject.cpp
new file mode 100644 (file)
index 0000000..33ae555
--- /dev/null
@@ -0,0 +1,89 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-present 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 "db_ido/endpointdbobject.h"
+#include "db_ido/dbtype.h"
+#include "db_ido/dbvalue.h"
+#include "icinga/icingaapplication.h"
+#include "base/objectlock.h"
+#include "base/initialize.h"
+#include "base/dynamictype.h"
+#include "base/utility.h"
+#include "base/logger_fwd.h"
+#include <boost/foreach.hpp>
+
+using namespace icinga;
+
+
+REGISTER_DBTYPE(Endpoint, "endpoint", DbObjectTypeEndpoint, "endpoint_object_id", EndpointDbObject);
+
+INITIALIZE_ONCE(&EndpointDbObject::StaticInitialize);
+
+void EndpointDbObject::StaticInitialize(void)
+{
+       Endpoint::OnConnected.connect(boost::bind(&EndpointDbObject::UpdateConnectedStatus, _1));
+       Endpoint::OnDisconnected.connect(boost::bind(&EndpointDbObject::UpdateConnectedStatus, _1));
+}
+
+EndpointDbObject::EndpointDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
+       : DbObject(type, name1, name2)
+{ }
+
+Dictionary::Ptr EndpointDbObject::GetConfigFields(void) const
+{
+       Dictionary::Ptr fields = make_shared<Dictionary>();
+       Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
+
+       fields->Set("identity", endpoint->GetName());
+       fields->Set("node", IcingaApplication::GetInstance()->GetNodeName());
+
+       return fields;
+}
+
+Dictionary::Ptr EndpointDbObject::GetStatusFields(void) const
+{
+       Dictionary::Ptr fields = make_shared<Dictionary>();
+       Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
+
+       fields->Set("identity", endpoint->GetName());
+       fields->Set("node", IcingaApplication::GetInstance()->GetNodeName());
+       fields->Set("is_connected", endpoint->IsConnected() ? 1 : 0);
+
+       return fields;
+}
+
+void EndpointDbObject::UpdateConnectedStatus(const Endpoint::Ptr& endpoint)
+{
+       Log(LogDebug, "db_ido", "update is_connected for endpoint '" + endpoint->GetName() + "'");
+
+       DbQuery query1;
+       query1.Table = "endpointstatus";
+       query1.Type = DbQueryUpdate;
+
+       Dictionary::Ptr fields1 = make_shared<Dictionary>();
+       fields1->Set("is_connected", endpoint->IsConnected() ? 1 : 0);
+       fields1->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
+       query1.Fields = fields1;
+
+       query1.WhereCriteria = make_shared<Dictionary>();
+       query1.WhereCriteria->Set("endpoint_object_id", endpoint);
+       query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
+
+       OnQuery(query1);
+}
diff --git a/lib/db_ido/endpointdbobject.h b/lib/db_ido/endpointdbobject.h
new file mode 100644 (file)
index 0000000..a15a820
--- /dev/null
@@ -0,0 +1,53 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-present 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 ENDPOINTDBOBJECT_H
+#define ENDPOINTDBOBJECT_H
+
+#include "db_ido/dbobject.h"
+#include "base/dynamicobject.h"
+#include "cluster/endpoint.h"
+
+namespace icinga
+{
+
+/**
+ * A Command database object.
+ *
+ * @ingroup ido
+ */
+class EndpointDbObject : public DbObject
+{
+public:
+       DECLARE_PTR_TYPEDEFS(EndpointDbObject);
+
+       EndpointDbObject(const shared_ptr<DbType>& type, const String& name1, const String& name2);
+
+        static void StaticInitialize(void);
+
+       virtual Dictionary::Ptr GetConfigFields(void) const;
+       virtual Dictionary::Ptr GetStatusFields(void) const;
+
+private:
+        static void UpdateConnectedStatus(const Endpoint::Ptr& endpoint);
+};
+
+}
+
+#endif /* ENDPOINTDBOBJECT_H */