Implement Utility::Random().

This commit is contained in:
Gunnar Beutner 2013-10-03 22:10:46 +02:00
parent d2d6653236
commit f449eb9e84
6 changed files with 13 additions and 25 deletions

View File

@ -20,8 +20,7 @@
#ifndef DEBUG_H
#define DEBUG_H
#include <stdlib.h>
#include <stdio.h>
#include "i2-base.h"
#ifdef NDEBUG
# define ASSERT(expr) ((void)0)

View File

@ -586,3 +586,11 @@ int Utility::CompareVersion(const String& v1, const String& v2)
return 0;
}
int Utility::Random(void)
{
static boost::mutex mtx;
boost::mutex::scoped_lock lock(mtx);
return rand();
}

View File

@ -99,6 +99,8 @@ public:
static int CompareVersion(const String& v1, const String& v2);
static int Random(void);
private:
Utility(void);

View File

@ -40,30 +40,10 @@ Dictionary::Ptr NullCheckTask::ScriptFunc(const Service::Ptr&)
String output = "Hello from ";
output += name;
#ifndef _WIN32
unsigned int seed = Utility::GetPid() * time(NULL);
int state = rand_r(&seed) % 4;
#else
int state = 0;
#endif
Dictionary::Ptr cr = boost::make_shared<Dictionary>();
cr->Set("output", output);
cr->Set("state", ExitStatusToState(state));
cr->Set("state", static_cast<ServiceState>(Utility::Random() % 4));
return cr;
}
ServiceState NullCheckTask::ExitStatusToState(int exitStatus)
{
switch (exitStatus) {
case 0:
return StateOK;
case 1:
return StateWarning;
case 2:
return StateCritical;
default:
return StateUnknown;
}
}

View File

@ -36,7 +36,6 @@ class I2_ICINGA_API NullCheckTask
{
public:
static Dictionary::Ptr ScriptFunc(const Service::Ptr& service);
static ServiceState ExitStatusToState(int exitStatus);
private:
NullCheckTask(void);

View File

@ -48,7 +48,7 @@ void Service::Start(void)
VERIFY(GetHost());
SetSchedulingOffset(rand());
SetSchedulingOffset(Utility::Random());
UpdateNextCheck();
AddDowntimesToCache();