From: Gunnar Beutner Date: Thu, 28 Jun 2012 13:43:49 +0000 (+0200) Subject: Implement task statistics. X-Git-Tag: v0.0.1~350 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2272e410c2c178781dcfdc21024eff08473707b1;p=icinga2 Implement task statistics. --- diff --git a/base/Makefile.am b/base/Makefile.am index 5ceedd730..b7bd62322 100644 --- a/base/Makefile.am +++ b/base/Makefile.am @@ -26,6 +26,8 @@ libbase_la_SOURCES = \ objectset.h \ objectmap.cpp \ objectmap.h \ + ringbuffer.cpp \ + ringbuffer.h \ socket.cpp \ socket.h \ tcpclient.cpp \ diff --git a/base/i2-base.h b/base/i2-base.h index f795e507e..fbd44ca17 100644 --- a/base/i2-base.h +++ b/base/i2-base.h @@ -154,6 +154,7 @@ using boost::system_time; #include "event.h" #include "variant.h" #include "dictionary.h" +#include "ringbuffer.h" #include "timer.h" #include "fifo.h" #include "socket.h" diff --git a/base/ringbuffer.cpp b/base/ringbuffer.cpp new file mode 100644 index 000000000..88a094884 --- /dev/null +++ b/base/ringbuffer.cpp @@ -0,0 +1,49 @@ +#include "i2-base.h" + +using namespace icinga; + +Ringbuffer::Ringbuffer(int slots) + : m_Slots(slots, 0), m_Offset(0) +{ } + +int Ringbuffer::GetLength(void) const +{ + return m_Slots.size(); +} + +void Ringbuffer::InsertValue(long tv, int num) +{ + int offsetTarget = tv % m_Slots.size(); + + /* walk towards the target offset, resetting slots to 0 */ + while (m_Offset != offsetTarget) { + m_Offset++; + + if (m_Offset >= m_Slots.size()) + m_Offset = 0; + + m_Slots[m_Offset] = 0; + } + + m_Slots[m_Offset] += num; +} + +int Ringbuffer::GetValues(long span) const +{ + if (span > m_Slots.size()) + span = m_Slots.size(); + + int off = m_Offset; + int sum = 0; + while (span > 0) { + sum += m_Slots[off]; + + if (off == 0) + off = m_Slots.size(); + + off--; + span--; + } + + return sum; +} diff --git a/base/ringbuffer.h b/base/ringbuffer.h new file mode 100644 index 000000000..a74f784fb --- /dev/null +++ b/base/ringbuffer.h @@ -0,0 +1,23 @@ +#ifndef RINGBUFFER_H +#define RINGBUFFER_H + +namespace icinga +{ + +class Ringbuffer +{ +public: + Ringbuffer(int slots); + + int GetLength(void) const; + void InsertValue(long tv, int num); + int GetValues(long span) const; + +private: + vector m_Slots; + int m_Offset; +}; + +} + +#endif /* RINGBUFFER_H */ diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index 191d89768..cb1328a79 100644 --- a/components/compat/compatcomponent.cpp +++ b/components/compat/compatcomponent.cpp @@ -117,7 +117,7 @@ void CompatComponent::DumpServiceStatus(ofstream& fp, Service service) << "\t" << "current_state=" << service.GetState() << endl << "\t" << "state_type=" << service.GetStateType() << endl << "\t" << "plugin_output=" << plugin_output << endl - << "\t" << "last_check=" << schedule_start << endl + << "\t" << "last_check=" << schedule_end << endl << "\t" << "next_check=" << service.GetNextCheck() << endl << "\t" << "current_attempt=" << service.GetCurrentCheckAttempt() << endl << "\t" << "max_attempts=" << service.GetMaxCheckAttempts() << endl @@ -174,6 +174,7 @@ void CompatComponent::StatusTimerHandler(void) << "\t" << "check_host_freshness=0" << endl << "\t" << "enable_flap_detection=1" << endl << "\t" << "enable_failure_prediction=0" << endl + << "\t" << "active_scheduled_service_check_stats=" << CheckTask::GetTaskStatistics(60) << "," << CheckTask::GetTaskStatistics(5 * 60) << "," << CheckTask::GetTaskStatistics(15 * 60) << endl << endl; ofstream objectfp; diff --git a/icinga/checktask.cpp b/icinga/checktask.cpp index e94f7ed00..be78472ac 100644 --- a/icinga/checktask.cpp +++ b/icinga/checktask.cpp @@ -5,6 +5,7 @@ using namespace icinga; map CheckTask::m_Types; vector CheckTask::m_FinishedTasks; mutex CheckTask::m_FinishedTasksMutex; +Ringbuffer CheckTask::m_TaskStatistics(15 * 60); CheckTask::CheckTask(const Service& service) : m_Service(service) @@ -67,4 +68,11 @@ void CheckTask::FinishTask(const CheckTask::Ptr& task) { mutex::scoped_lock lock(m_FinishedTasksMutex); m_FinishedTasks.push_back(task); + m_TaskStatistics.InsertValue(task->GetResult().GetScheduleEnd(), 1); +} + +int CheckTask::GetTaskStatistics(time_t timespan) +{ + mutex::scoped_lock lock(m_FinishedTasksMutex); + return m_TaskStatistics.GetValues(timespan); } diff --git a/icinga/checktask.h b/icinga/checktask.h index 92f4f0460..e3a6799a8 100644 --- a/icinga/checktask.h +++ b/icinga/checktask.h @@ -25,9 +25,12 @@ public: static void Enqueue(const CheckTask::Ptr& task); static void FlushQueue(void); + static int GetTaskHistogramSlots(void); static void FinishTask(const CheckTask::Ptr& task); static vector GetFinishedTasks(void); + static int GetTaskStatistics(time_t timespan); + protected: CheckTask(const Service& service); @@ -39,6 +42,7 @@ private: static vector m_FinishedTasks; static mutex m_FinishedTasksMutex; + static Ringbuffer m_TaskStatistics; }; struct CheckTaskType