mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-23 21:55:03 +02:00
Implement Utility::Random().
This commit is contained in:
parent
d2d6653236
commit
f449eb9e84
@ -20,8 +20,7 @@
|
|||||||
#ifndef DEBUG_H
|
#ifndef DEBUG_H
|
||||||
#define DEBUG_H
|
#define DEBUG_H
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include "i2-base.h"
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#ifdef NDEBUG
|
#ifdef NDEBUG
|
||||||
# define ASSERT(expr) ((void)0)
|
# define ASSERT(expr) ((void)0)
|
||||||
|
@ -586,3 +586,11 @@ int Utility::CompareVersion(const String& v1, const String& v2)
|
|||||||
|
|
||||||
return 0;
|
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 CompareVersion(const String& v1, const String& v2);
|
||||||
|
|
||||||
|
static int Random(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Utility(void);
|
Utility(void);
|
||||||
|
|
||||||
|
@ -40,30 +40,10 @@ Dictionary::Ptr NullCheckTask::ScriptFunc(const Service::Ptr&)
|
|||||||
String output = "Hello from ";
|
String output = "Hello from ";
|
||||||
output += name;
|
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>();
|
Dictionary::Ptr cr = boost::make_shared<Dictionary>();
|
||||||
cr->Set("output", output);
|
cr->Set("output", output);
|
||||||
cr->Set("state", ExitStatusToState(state));
|
cr->Set("state", static_cast<ServiceState>(Utility::Random() % 4));
|
||||||
|
|
||||||
return cr;
|
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:
|
public:
|
||||||
static Dictionary::Ptr ScriptFunc(const Service::Ptr& service);
|
static Dictionary::Ptr ScriptFunc(const Service::Ptr& service);
|
||||||
static ServiceState ExitStatusToState(int exitStatus);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
NullCheckTask(void);
|
NullCheckTask(void);
|
||||||
|
@ -48,7 +48,7 @@ void Service::Start(void)
|
|||||||
|
|
||||||
VERIFY(GetHost());
|
VERIFY(GetHost());
|
||||||
|
|
||||||
SetSchedulingOffset(rand());
|
SetSchedulingOffset(Utility::Random());
|
||||||
UpdateNextCheck();
|
UpdateNextCheck();
|
||||||
|
|
||||||
AddDowntimesToCache();
|
AddDowntimesToCache();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user