]> granicus.if.org Git - icinga2/commitdiff
livestatus: add connections and extcmds to status table
authorMichael Friedrich <michael.friedrich@netways.de>
Fri, 19 Jul 2013 13:42:00 +0000 (15:42 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Fri, 19 Jul 2013 13:42:00 +0000 (15:42 +0200)
refs #4372

components/livestatus/component.cpp
components/livestatus/component.h
components/livestatus/query.cpp
components/livestatus/query.h
components/livestatus/statustable.cpp

index 4ac0c4723aa84a2a0a7864bf754c1b4f51f91e4c..dbaef39e37f7c75b13d931f412d82efe40c62dd8 100644 (file)
@@ -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--;
+       }
 }
index 3903d7575d0c4432e3ad74c2695cc58fa6706df7..a39a8f967fb8c612a0f6695111422bf1fedcb957 100644 (file)
@@ -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);
 };
index ced8bca83e1cd60d1785f707c08809cf979c6503..6f8defbd7602c7ea042b6ef99dcfaf34df239e86 100644 (file)
@@ -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, "");
index 512c973b1763f4235bce0fd0158de84da33e1de7..f76414dbc2e797acf80dbb40f90c33f6a384e5b0 100644 (file)
@@ -51,6 +51,8 @@ public:
 
        bool Execute(const Stream::Ptr& stream);
 
+       static int GetExternalCommands(void);
+
 private:
        String m_Verb;
 
index 3524eda4cfedf77952d6ffb980da2b13035e13cc..fc474aa26476fbd0b98160d2090eef533c5e25c9 100644 (file)
@@ -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)