mirror of https://github.com/Icinga/icinga2.git
Implement Utility::Random().
This commit is contained in:
parent
d2d6653236
commit
f449eb9e84
|
@ -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)
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -99,6 +99,8 @@ public:
|
|||
|
||||
static int CompareVersion(const String& v1, const String& v2);
|
||||
|
||||
static int Random(void);
|
||||
|
||||
private:
|
||||
Utility(void);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -48,7 +48,7 @@ void Service::Start(void)
|
|||
|
||||
VERIFY(GetHost());
|
||||
|
||||
SetSchedulingOffset(rand());
|
||||
SetSchedulingOffset(Utility::Random());
|
||||
UpdateNextCheck();
|
||||
|
||||
AddDowntimesToCache();
|
||||
|
|
Loading…
Reference in New Issue