]> granicus.if.org Git - icinga2/commitdiff
Livestatus: Use HistoryTable for log/statehist tables.
authorMichael Friedrich <michael.friedrich@netways.de>
Wed, 18 Dec 2013 15:06:39 +0000 (16:06 +0100)
committerMichael Friedrich <michael.friedrich@netways.de>
Wed, 18 Dec 2013 15:06:39 +0000 (16:06 +0100)
Refs #5351

components/livestatus/CMakeLists.txt
components/livestatus/historytable.cpp [new file with mode: 0644]
components/livestatus/historytable.h [new file with mode: 0644]
components/livestatus/logtable.cpp
components/livestatus/logtable.h
components/livestatus/logutility.cpp
components/livestatus/logutility.h
components/livestatus/statehisttable.cpp
components/livestatus/statehisttable.h
components/livestatus/table.cpp
components/livestatus/table.h

index 0c3cc0b0d3cc2f8d89e141287ce96d026bcdf47b..3d34d369a0da231e46714620c098f74fb2696d3d 100644 (file)
@@ -19,7 +19,7 @@ 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 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 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)
 
diff --git a/components/livestatus/historytable.cpp b/components/livestatus/historytable.cpp
new file mode 100644 (file)
index 0000000..fc90076
--- /dev/null
@@ -0,0 +1,27 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2013 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/historytable.h"
+
+using namespace icinga;
+
+void HistoryTable::UpdateLogEntries(const Dictionary::Ptr&, int, int)
+{
+       /* does nothing by default */
+}
diff --git a/components/livestatus/historytable.h b/components/livestatus/historytable.h
new file mode 100644 (file)
index 0000000..11e7b28
--- /dev/null
@@ -0,0 +1,41 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2013 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 HISTORYTABLE_H
+#define HISTORYTABLE_H
+
+#include "livestatus/table.h"
+#include "base/dictionary.h"
+
+namespace icinga
+{
+
+
+/**
+ * @ingroup livestatus
+ */
+class HistoryTable : public Table
+{
+public:
+        virtual void UpdateLogEntries(const Dictionary::Ptr& bag, int line_count, int lineno);
+};
+
+}
+
+#endif /* HISTORYTABLE_H */
index 4a745b93006104cde41cbde84bea45fd13392218..86a6f53d01f5203e1a587b7754c5b6e99d1984e1 100644 (file)
@@ -47,7 +47,7 @@
 
 using namespace icinga;
 
-LogTable::LogTable(const String& compat_log_path, const unsigned long& from, const unsigned long& until)
+LogTable::LogTable(const String& compat_log_path, time_t from, time_t until)
 {
        Log(LogInformation, "livestatus", "Pre-selecting log file from " + Convert::ToString(from) + " until " + Convert::ToString(until));
 
@@ -64,15 +64,12 @@ LogTable::LogTable(const String& compat_log_path, const unsigned long& from, con
        AddColumns(this);
 }
 
-void LogTable::UpdateLogCache(const Dictionary::Ptr& bag, int line_count, int lineno)
+void LogTable::UpdateLogEntries(const Dictionary::Ptr& log_entry_attrs, int line_count, int lineno)
 {
        /* additional attributes only for log table */
-       bag->Set("lineno", lineno);
+       log_entry_attrs->Set("lineno", lineno);
 
-       {
-               boost::mutex::scoped_lock lock(m_Mutex);
-               m_RowsCache[line_count] = bag;
-       }
+       m_RowsCache[line_count] = log_entry_attrs;
 }
 
 void LogTable::AddColumns(Table *table, const String& prefix,
index 7aec5b89150fb7cd210d31be2f77733498469ec8..082d7dc4f78a7351153250193e88382b6a1c5572 100644 (file)
@@ -20,7 +20,7 @@
 #ifndef LOGTABLE_H
 #define LOGTABLE_H
 
-#include "livestatus/table.h"
+#include "livestatus/historytable.h"
 #include <boost/thread/mutex.hpp>
 
 using namespace icinga;
@@ -31,19 +31,19 @@ namespace icinga
 /**
  * @ingroup livestatus
  */
-class LogTable : public Table
+class LogTable : public HistoryTable
 {
 public:
        DECLARE_PTR_TYPEDEFS(LogTable);
 
-       LogTable(const String& compat_log_path, const unsigned long& from, const unsigned long& until);
+       LogTable(const String& compat_log_path, time_t from, time_t until);
 
        static void AddColumns(Table *table, const String& prefix = String(),
            const Column::ObjectAccessor& objectAccessor = Column::ObjectAccessor());
 
        virtual String GetName(void) const;
 
-        void UpdateLogCache(const Dictionary::Ptr& bag, int line_count, int lineno);
+        void UpdateLogEntries(const Dictionary::Ptr& log_entry_attrs, int line_count, int lineno);
 
 protected:
        virtual void FetchRows(const AddRowFunction& addRowFn);
@@ -70,11 +70,10 @@ protected:
        static Value CommandNameAccessor(const Value& row);
 
 private:
-        std::map<unsigned int, String> m_LogFileIndex;
-        std::map<unsigned int, Dictionary::Ptr> m_RowsCache;
-        unsigned int m_TimeFrom;
-        unsigned int m_TimeUntil;
-        boost::mutex m_Mutex;
+        std::map<time_t, String> m_LogFileIndex;
+        std::map<time_t, Dictionary::Ptr> m_RowsCache;
+        time_t m_TimeFrom;
+        time_t m_TimeUntil;
 };
 
 }
index 3115ae2c04f43e08b7534ae4937256e3e5c71d22..1ac1bb8061ae7a9bd6aa9b546c99c069bfd2e037 100644 (file)
 
 using namespace icinga;
 
-void LogUtility::CreateLogIndex(const String& path, std::map<unsigned int, String>& index)
+void LogUtility::CreateLogIndex(const String& path, std::map<time_t, String>& index)
 {
        Utility::Glob(path + "/icinga.log", boost::bind(&LogUtility::CreateLogIndexFileHandler, _1, boost::ref(index)), GlobFile);
        Utility::Glob(path + "/archives/*.log", boost::bind(&LogUtility::CreateLogIndexFileHandler, _1, boost::ref(index)), GlobFile);
 }
 
-void LogUtility::CreateLogIndexFileHandler(const String& path, std::map<unsigned int, String>& index)
+void LogUtility::CreateLogIndexFileHandler(const String& path, std::map<time_t, String>& index)
 {
        std::ifstream stream;
        stream.open(path.CStr(), std::ifstream::in);
@@ -64,7 +64,7 @@ void LogUtility::CreateLogIndexFileHandler(const String& path, std::map<unsigned
 
        /* extract timestamp */
        buffer[11] = 0;
-       unsigned int ts_start = atoi(buffer+1);
+       time_t ts_start = atoi(buffer+1);
 
        stream.close();
 
@@ -73,11 +73,10 @@ void LogUtility::CreateLogIndexFileHandler(const String& path, std::map<unsigned
        index[ts_start] = path;
 }
 
-void LogUtility::CreateLogCache(std::map<unsigned int, String> index, Table *table,
-    const unsigned int& from, const unsigned int& until)
+void LogUtility::CreateLogCache(std::map<time_t, String> index, HistoryTable *table,
+    time_t from, time_t until)
 {
-       if (!table)
-               return;
+       ASSERT(table);
 
        /* m_LogFileIndex map tells which log files are involved ordered by their start timestamp */
        unsigned int ts;
@@ -101,15 +100,15 @@ void LogUtility::CreateLogCache(std::map<unsigned int, String> index, Table *tab
                        if (line.empty())
                                continue; /* Ignore empty lines */
 
-                       Dictionary::Ptr bag = LogUtility::GetAttributes(line);
+                       Dictionary::Ptr log_entry_attrs = LogUtility::GetAttributes(line);
 
                        /* no attributes available - invalid log line */
-                       if (!bag) {
+                       if (!log_entry_attrs) {
                                Log(LogDebug, "livestatus", "Skipping invalid log line: '" + line + "'.");
                                continue;
                        }
 
-                       table->UpdateLogCache(bag, line_count, lineno);
+                       table->UpdateLogEntries(log_entry_attrs, line_count, lineno);
 
                        line_count++;
                        lineno++;
@@ -155,7 +154,6 @@ Dictionary::Ptr LogUtility::GetAttributes(const String& text)
        bag->Set("attempt", 0);
        bag->Set("message", text); /* used as 'message' in log table, and 'log_output' in statehist table */
 
-       /* Host States */
        if (type.Contains("INITIAL HOST STATE") ||
            type.Contains("CURRENT HOST STATE") ||
            type.Contains("HOST ALERT")) {
@@ -182,8 +180,7 @@ Dictionary::Ptr LogUtility::GetAttributes(const String& text)
                }
 
                return bag;
-       }
-       else if (type.Contains("HOST DOWNTIME ALERT") ||
+       } else if (type.Contains("HOST DOWNTIME ALERT") ||
                 type.Contains("HOST FLAPPING ALERT")) {
                if (tokens.size() < 3)
                        return bag;
@@ -201,9 +198,7 @@ Dictionary::Ptr LogUtility::GetAttributes(const String& text)
                }
 
                return bag;
-       }
-       /* Service States */
-       else if (type.Contains("INITIAL SERVICE STATE") ||
+       } else if (type.Contains("INITIAL SERVICE STATE") ||
                 type.Contains("CURRENT SERVICE STATE") ||
                 type.Contains("SERVICE ALERT")) {
                if (tokens.size() < 6)
@@ -230,8 +225,7 @@ Dictionary::Ptr LogUtility::GetAttributes(const String& text)
                }
 
                return bag;
-       }
-       else if (type.Contains("SERVICE DOWNTIME ALERT") ||
+       } else if (type.Contains("SERVICE DOWNTIME ALERT") ||
                 type.Contains("SERVICE FLAPPING ALERT")) {
                if (tokens.size() < 4)
                        return bag;
@@ -250,9 +244,7 @@ Dictionary::Ptr LogUtility::GetAttributes(const String& text)
                }
 
                return bag;
-       }
-       /* Timeperiods */
-       else if (type.Contains("TIMEPERIOD TRANSITION")) {
+       } else if (type.Contains("TIMEPERIOD TRANSITION")) {
                if (tokens.size() < 4)
                        return bag;
 
@@ -263,9 +255,7 @@ Dictionary::Ptr LogUtility::GetAttributes(const String& text)
                bag->Set("service_description", tokens[1]);
                bag->Set("state_type", tokens[2]);
                bag->Set("comment", tokens[3]);
-       }
-       /* Notifications */
-       else if (type.Contains("HOST NOTIFICATION")) {
+       } else if (type.Contains("HOST NOTIFICATION")) {
                if (tokens.size() < 6)
                        return bag;
 
@@ -280,8 +270,7 @@ Dictionary::Ptr LogUtility::GetAttributes(const String& text)
                bag->Set("log_type", LogEntryTypeHostNotification);
 
                return bag;
-       }
-       else if (type.Contains("SERVICE NOTIFICATION")) {
+       } else if (type.Contains("SERVICE NOTIFICATION")) {
                if (tokens.size() < 7)
                        return bag;
 
@@ -297,9 +286,7 @@ Dictionary::Ptr LogUtility::GetAttributes(const String& text)
                bag->Set("log_type", LogEntryTypeServiceNotification);
 
                return bag;
-       }
-        /* Passive Checks */
-       else if (type.Contains("PASSIVE HOST CHECK")) {
+       } else if (type.Contains("PASSIVE HOST CHECK")) {
                if (tokens.size() < 3)
                        return bag;
 
@@ -310,8 +297,7 @@ Dictionary::Ptr LogUtility::GetAttributes(const String& text)
                 bag->Set("log_class", LogEntryClassPassive);
 
                 return bag;
-       }
-       else if (type.Contains("PASSIVE SERVICE CHECK")) {
+       } else if (type.Contains("PASSIVE SERVICE CHECK")) {
                if (tokens.size() < 4)
                        return bag;
 
@@ -323,28 +309,22 @@ Dictionary::Ptr LogUtility::GetAttributes(const String& text)
                 bag->Set("log_class", LogEntryClassPassive);
 
                 return bag;
-       }
-       /* External Command */
-       else if (type.Contains("EXTERNAL COMMAND")) {
+       } else if (type.Contains("EXTERNAL COMMAND")) {
                bag->Set("log_class", LogEntryClassCommand);
                /* string processing not implemented in 1.x */
 
                 return bag;
-       }
-       /* normal text entries */
-       else if (type.Contains("LOG VERSION")) {
+       } else if (type.Contains("LOG VERSION")) {
                bag->Set("log_class", LogEntryClassProgram);
                bag->Set("log_type", LogEntryTypeVersion);
 
                 return bag;
-       }
-       else if (type.Contains("logging initial states")) {
+       } else if (type.Contains("logging initial states")) {
                bag->Set("log_class", LogEntryClassProgram);
                bag->Set("log_type", LogEntryTypeInitialStates);
 
                 return bag;
-       }
-       else if (type.Contains("starting... (PID=")) {
+       } else if (type.Contains("starting... (PID=")) {
                bag->Set("log_class", LogEntryClassProgram);
                bag->Set("log_type", LogEntryTypeProgramStarting);
 
index b5d44d2668457fc2f968f8d601b67002ecd708fe..09e68300cf216c79ee721bb03fd2ad7fb7131f43 100644 (file)
@@ -20,8 +20,7 @@
 #ifndef LOGUTILITY_H
 #define LOGUTILITY_H
 
-#include "livestatus/table.h"
-#include <boost/thread/mutex.hpp>
+#include "livestatus/historytable.h"
 
 using namespace icinga;
 
@@ -65,9 +64,9 @@ class LogUtility
 {
 
 public:
-       static void CreateLogIndex(const String& path, std::map<unsigned int, String>& index);
-       static void CreateLogIndexFileHandler(const String& path, std::map<unsigned int, String>& index);
-       static void CreateLogCache(std::map<unsigned int, String> index, Table *table, const unsigned int& from, const unsigned int& until);
+       static void CreateLogIndex(const String& path, std::map<time_t, String>& index);
+       static void CreateLogIndexFileHandler(const String& path, std::map<time_t, String>& index);
+       static void CreateLogCache(std::map<time_t, String> index, HistoryTable *table, time_t from, time_t until);
        static Dictionary::Ptr GetAttributes(const String& text);
 
 private:
index d233803c99a0f38d895482f9e79682783e7a34eb..d5d816ce9174b8437247dc29f67df7e67827eec8 100644 (file)
@@ -48,7 +48,7 @@
 
 using namespace icinga;
 
-StateHistTable::StateHistTable(const String& compat_log_path, const unsigned long& from, const unsigned long& until)
+StateHistTable::StateHistTable(const String& compat_log_path, time_t from, time_t until)
 {
        Log(LogInformation, "livestatus", "Pre-selecting log file from " + Convert::ToString(from) + " until " + Convert::ToString(until));
 
@@ -65,15 +65,15 @@ StateHistTable::StateHistTable(const String& compat_log_path, const unsigned lon
        AddColumns(this);
 }
 
-void StateHistTable::UpdateLogCache(const Dictionary::Ptr& bag, int line_count, int lineno)
+void StateHistTable::UpdateLogEntries(const Dictionary::Ptr& log_entry_attrs, int line_count, int lineno)
 {
-       unsigned int time = bag->Get("time");
-       String host_name = bag->Get("host_name");
-       String service_description = bag->Get("service_description");
-       unsigned long state = bag->Get("state");
-       int log_type = bag->Get("log_type");
-       String state_type = bag->Get("state_type"); //SOFT, HARD, STARTED, STOPPED, ...
-       String log_line = bag->Get("message"); /* use message from log table */
+       unsigned int time = log_entry_attrs->Get("time");
+       String host_name = log_entry_attrs->Get("host_name");
+       String service_description = log_entry_attrs->Get("service_description");
+       unsigned long state = log_entry_attrs->Get("state");
+       int log_type = log_entry_attrs->Get("log_type");
+       String state_type = log_entry_attrs->Get("state_type"); //SOFT, HARD, STARTED, STOPPED, ...
+       String log_line = log_entry_attrs->Get("message"); /* use message from log table */
 
        Service::Ptr state_hist_service;
 
@@ -123,11 +123,8 @@ void StateHistTable::UpdateLogCache(const Dictionary::Ptr& bag, int line_count,
 
                Log(LogDebug, "livestatus", "statehist: Adding new service '" + state_hist_service->GetName() + "' to services cache.");
        } else {
-               {
-                       boost::mutex::scoped_lock lock(m_Mutex);
-                       state_hist_service_states = m_ServicesCache[state_hist_service];
-                       state_hist_bag = state_hist_service_states->Get(state_hist_service_states->GetLength()-1); /* fetch latest state from history */
-               }
+               state_hist_service_states = m_ServicesCache[state_hist_service];
+               state_hist_bag = state_hist_service_states->Get(state_hist_service_states->GetLength()-1); /* fetch latest state from history */
 
                /* state duration */
 
@@ -212,10 +209,7 @@ void StateHistTable::UpdateLogCache(const Dictionary::Ptr& bag, int line_count,
 
        }
 
-       {
-               boost::mutex::scoped_lock lock(m_Mutex);
-               m_ServicesCache[state_hist_service] = state_hist_service_states;
-       }
+       m_ServicesCache[state_hist_service] = state_hist_service_states;
 }
 
 void StateHistTable::AddColumns(Table *table, const String& prefix,
index 91bff7ae635bc307b1ba488c283a43cd4e7e8192..6fcc742570567ef0a152ea3aac342c340159f118 100644 (file)
@@ -21,7 +21,7 @@
 #define STATEHISTTABLE_H
 
 #include "icinga/service.h"
-#include "livestatus/table.h"
+#include "livestatus/historytable.h"
 #include <boost/thread/mutex.hpp>
 
 using namespace icinga;
@@ -32,19 +32,19 @@ namespace icinga
 /**
  * @ingroup livestatus
  */
-class StateHistTable : public Table
+class StateHistTable : public HistoryTable
 {
 public:
        DECLARE_PTR_TYPEDEFS(StateHistTable);
 
-       StateHistTable(const String& compat_log_path, const unsigned long& from, const unsigned long& until);
+       StateHistTable(const String& compat_log_path, time_t from, time_t until);
 
        static void AddColumns(Table *table, const String& prefix = String(),
            const Column::ObjectAccessor& objectAccessor = Column::ObjectAccessor());
 
        virtual String GetName(void) const;
 
-       void UpdateLogCache(const Dictionary::Ptr& bag, int line_count, int lineno);
+       void UpdateLogEntries(const Dictionary::Ptr& log_entry_attrs, int line_count, int lineno);
 
 protected:
        virtual void FetchRows(const AddRowFunction& addRowFn);
@@ -80,11 +80,10 @@ protected:
        static Value DurationPartUnmonitoredAccessor(const Value& row);
 
 private:
-        std::map<unsigned int, String> m_LogFileIndex;
+        std::map<time_t, String> m_LogFileIndex;
         std::map<Service::Ptr, Array::Ptr> m_ServicesCache;
-        unsigned int m_TimeFrom;
-        unsigned int m_TimeUntil;
-        boost::mutex m_Mutex;
+        time_t m_TimeFrom;
+        time_t m_TimeUntil;
 };
 
 }
index 4933174380dc385b940d750fef8c2b2cf630d962..bd476d3fc1b67e32c89b029a1401b4fb6a8a7bdc 100644 (file)
@@ -146,8 +146,3 @@ Value Table::EmptyDictionaryAccessor(const Value&)
 {
        return make_shared<Dictionary>();
 }
-
-void Table::UpdateLogCache(const Dictionary::Ptr&, int, int)
-{
-       /* does nothing by default */
-}
index dc76e6d270c62624361fd4abca9274ae87ecd595..dd5a5abdb101c562ccc6d7079703c9efe2221623 100644 (file)
@@ -50,8 +50,6 @@ public:
        Column GetColumn(const String& name) const;
        std::vector<String> GetColumnNames(void) const;
 
-        virtual void UpdateLogCache(const Dictionary::Ptr& bag, int line_count, int lineno);
-
 protected:
        Table(void);