Implement task statistics.

This commit is contained in:
Gunnar Beutner 2012-06-28 15:43:49 +02:00
parent ab08736338
commit 2272e410c2
7 changed files with 89 additions and 1 deletions

View File

@ -26,6 +26,8 @@ libbase_la_SOURCES = \
objectset.h \
objectmap.cpp \
objectmap.h \
ringbuffer.cpp \
ringbuffer.h \
socket.cpp \
socket.h \
tcpclient.cpp \

View File

@ -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"

49
base/ringbuffer.cpp Normal file
View File

@ -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;
}

23
base/ringbuffer.h Normal file
View File

@ -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<int> m_Slots;
int m_Offset;
};
}
#endif /* RINGBUFFER_H */

View File

@ -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;

View File

@ -5,6 +5,7 @@ using namespace icinga;
map<string, CheckTaskType> CheckTask::m_Types;
vector<CheckTask::Ptr> 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);
}

View File

@ -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<CheckTask::Ptr> GetFinishedTasks(void);
static int GetTaskStatistics(time_t timespan);
protected:
CheckTask(const Service& service);
@ -39,6 +42,7 @@ private:
static vector<CheckTask::Ptr> m_FinishedTasks;
static mutex m_FinishedTasksMutex;
static Ringbuffer m_TaskStatistics;
};
struct CheckTaskType