[icinga-checkins] icinga.org: icinga2/master: livestatus: add connections and extcmds to status table

git at icinga.org git at icinga.org
Fri Jul 19 15:42:27 CEST 2013


Module: icinga2
Branch: master
Commit: 669e3764bdf69d49a23ee816571839ac9e7aa446
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=669e3764bdf69d49a23ee816571839ac9e7aa446

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Fri Jul 19 15:42:00 2013 +0200

livestatus: add connections and extcmds to status table

refs #4372

---

 components/livestatus/component.cpp   |   31 +++++++++++++++++++++++++------
 components/livestatus/component.h     |    5 ++---
 components/livestatus/query.cpp       |   16 ++++++++++++++++
 components/livestatus/query.h         |    2 ++
 components/livestatus/statustable.cpp |   16 ++++++----------
 5 files changed, 51 insertions(+), 19 deletions(-)

diff --git a/components/livestatus/component.cpp b/components/livestatus/component.cpp
index 4ac0c47..dbaef39 100644
--- a/components/livestatus/component.cpp
+++ b/components/livestatus/component.cpp
@@ -18,6 +18,7 @@
  ******************************************************************************/
 
 #include "livestatus/component.h"
+#include "base/objectlock.h"
 #include "base/dynamictype.h"
 #include "base/logger_fwd.h"
 #include "base/tcpsocket.h"
@@ -31,6 +32,10 @@ using namespace livestatus;
 
 REGISTER_TYPE(LivestatusComponent);
 
+static int l_ClientsConnected = 0;
+static int l_Connections = 0;
+static boost::mutex l_ComponentMutex;
+
 LivestatusComponent::LivestatusComponent(const Dictionary::Ptr& serializedUpdate)
 	: DynamicObject(serializedUpdate)
 {
@@ -65,8 +70,6 @@ void LivestatusComponent::Start(void)
 		return;
 #endif
 	}
-
-	m_ClientsConnected = 0;
 }
 
 String LivestatusComponent::GetSocketType(void) const
@@ -105,9 +108,18 @@ String LivestatusComponent::GetPort(void) const
 		return service;
 }
 
-int LivestatusComponent::GetClientsConnected(void) const
+int LivestatusComponent::GetClientsConnected(void)
 {
-	return m_ClientsConnected;
+	boost::mutex::scoped_lock lock(l_ComponentMutex);
+
+	return l_ClientsConnected;
+}
+
+int LivestatusComponent::GetConnections(void)
+{
+	boost::mutex::scoped_lock lock(l_ComponentMutex);
+
+	return l_Connections;
 }
 
 void LivestatusComponent::ServerThreadProc(const Socket::Ptr& server)
@@ -126,7 +138,11 @@ void LivestatusComponent::ServerThreadProc(const Socket::Ptr& server)
 
 void LivestatusComponent::ClientThreadProc(const Socket::Ptr& client)
 {
-	m_ClientsConnected++;
+	{
+		boost::mutex::scoped_lock lock(l_ComponentMutex);
+		l_ClientsConnected++;
+		l_Connections++;
+	}
 
 	Stream::Ptr stream = boost::make_shared<NetworkStream>(client);
 
@@ -148,5 +164,8 @@ void LivestatusComponent::ClientThreadProc(const Socket::Ptr& client)
 			break;
 	}
 
-	m_ClientsConnected--;
+	{
+		boost::mutex::scoped_lock lock(l_ComponentMutex);
+		l_ClientsConnected--;
+	}
 }
diff --git a/components/livestatus/component.h b/components/livestatus/component.h
index 3903d75..a39a8f9 100644
--- a/components/livestatus/component.h
+++ b/components/livestatus/component.h
@@ -45,7 +45,8 @@ public:
 	String GetAddress(void) const;
 	String GetPort(void) const;
 
-	int GetClientsConnected(void) const;
+	static int GetClientsConnected(void);
+	static int GetConnections(void);
 
 private:
 	Attribute<String> m_SocketType;
@@ -53,8 +54,6 @@ private:
 	Attribute<String> m_Address;
 	Attribute<String> m_Port;
 
-	int m_ClientsConnected;
-
 	void ServerThreadProc(const Socket::Ptr& server);
 	void ClientThreadProc(const Socket::Ptr& client);
 };
diff --git a/components/livestatus/query.cpp b/components/livestatus/query.cpp
index ced8bca..6f8defb 100644
--- a/components/livestatus/query.cpp
+++ b/components/livestatus/query.cpp
@@ -44,6 +44,9 @@
 using namespace icinga;
 using namespace livestatus;
 
+static int l_ExternalCommands = 0;
+static boost::mutex l_QueryMutex;
+
 Query::Query(const std::vector<String>& lines)
 	: m_KeepAlive(false), m_OutputFormat("csv"), m_ColumnHeaders(true), m_Limit(-1)
 {
@@ -221,6 +224,13 @@ Query::Query(const std::vector<String>& lines)
 	m_Aggregators.swap(aggregators);
 }
 
+int Query::GetExternalCommands(void)
+{
+	boost::mutex::scoped_lock lock(l_QueryMutex);
+
+	return l_ExternalCommands;
+}
+
 Filter::Ptr Query::ParseFilter(const String& params)
 {
 	std::vector<String> tokens;
@@ -382,6 +392,12 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
 
 void Query::ExecuteCommandHelper(const Stream::Ptr& stream)
 {
+	{
+		boost::mutex::scoped_lock lock(l_QueryMutex);
+
+		l_ExternalCommands++;
+	}
+
 	Log(LogInformation, "livestatus", "Executing command: " + m_Command);
 	ExternalCommandProcessor::Execute(m_Command);
 	SendResponse(stream, LivestatusErrorOK, "");
diff --git a/components/livestatus/query.h b/components/livestatus/query.h
index 512c973..f76414d 100644
--- a/components/livestatus/query.h
+++ b/components/livestatus/query.h
@@ -51,6 +51,8 @@ public:
 
 	bool Execute(const Stream::Ptr& stream);
 
+	static int GetExternalCommands(void);
+
 private:
 	String m_Verb;
 
diff --git a/components/livestatus/statustable.cpp b/components/livestatus/statustable.cpp
index 3524eda..fc474aa 100644
--- a/components/livestatus/statustable.cpp
+++ b/components/livestatus/statustable.cpp
@@ -18,6 +18,7 @@
  ******************************************************************************/
 
 #include "livestatus/statustable.h"
+#include "livestatus/component.h"
 #include "icinga/icingaapplication.h"
 #include "icinga/cib.h"
 #include "base/dynamictype.h"
@@ -134,14 +135,12 @@ Value StatusTable::RequestsRateAccessor(const Value& row)
 
 Value StatusTable::ConnectionsAccessor(const Value& row)
 {
-	/* TODO */
-	return Empty;
+	return LivestatusComponent::GetConnections();
 }
 
 Value StatusTable::ConnectionsRateAccessor(const Value& row)
 {
-	/* TODO */
-	return Empty;
+	return (LivestatusComponent::GetConnections() / (Utility::GetTime() - IcingaApplication::GetInstance()->GetStartTime()));
 }
 
 Value StatusTable::ServiceChecksAccessor(const Value& row)
@@ -194,14 +193,12 @@ Value StatusTable::LogMessagesRateAccessor(const Value& row)
 
 Value StatusTable::ExternalCommandsAccessor(const Value& row)
 {
-	/* TODO */
-	return Empty;
+	return Query::GetExternalCommands();
 }
 
 Value StatusTable::ExternalCommandsRateAccessor(const Value& row)
 {
-	/* TODO */
-	return Empty;
+	return (Query::GetExternalCommands() / (Utility::GetTime() - IcingaApplication::GetInstance()->GetStartTime()));
 }
 
 Value StatusTable::LivechecksAccessor(const Value& row)
@@ -380,8 +377,7 @@ Value StatusTable::LivestatusVersionAccessor(const Value& row)
 
 Value StatusTable::LivestatusActiveConnectionsAccessor(const Value& row)
 {
-	/* TODO */
-	return Empty;
+	return LivestatusComponent::GetClientsConnected();
 }
 
 Value StatusTable::LivestatusQueuedConnectionsAccessor(const Value& row)





More information about the icinga-checkins mailing list