From f449eb9e84517cb014fa5a38dae2ffad9619df42 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 3 Oct 2013 22:10:46 +0200 Subject: [PATCH] Implement Utility::Random(). --- lib/base/debug.h | 3 +-- lib/base/utility.cpp | 8 ++++++++ lib/base/utility.h | 2 ++ lib/icinga/nullchecktask.cpp | 22 +--------------------- lib/icinga/nullchecktask.h | 1 - lib/icinga/service.cpp | 2 +- 6 files changed, 13 insertions(+), 25 deletions(-) diff --git a/lib/base/debug.h b/lib/base/debug.h index bc785f43a..98050f3f4 100644 --- a/lib/base/debug.h +++ b/lib/base/debug.h @@ -20,8 +20,7 @@ #ifndef DEBUG_H #define DEBUG_H -#include -#include +#include "i2-base.h" #ifdef NDEBUG # define ASSERT(expr) ((void)0) diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 602521b07..698f378d6 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -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(); +} diff --git a/lib/base/utility.h b/lib/base/utility.h index 5a5bdb9d8..8c9d587af 100644 --- a/lib/base/utility.h +++ b/lib/base/utility.h @@ -99,6 +99,8 @@ public: static int CompareVersion(const String& v1, const String& v2); + static int Random(void); + private: Utility(void); diff --git a/lib/icinga/nullchecktask.cpp b/lib/icinga/nullchecktask.cpp index 9fc0bb1f1..371600309 100644 --- a/lib/icinga/nullchecktask.cpp +++ b/lib/icinga/nullchecktask.cpp @@ -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(); cr->Set("output", output); - cr->Set("state", ExitStatusToState(state)); + cr->Set("state", static_cast(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; - } -} diff --git a/lib/icinga/nullchecktask.h b/lib/icinga/nullchecktask.h index edd8a1ade..fa9fcf873 100644 --- a/lib/icinga/nullchecktask.h +++ b/lib/icinga/nullchecktask.h @@ -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); diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp index 465befd99..52bba4cfb 100644 --- a/lib/icinga/service.cpp +++ b/lib/icinga/service.cpp @@ -48,7 +48,7 @@ void Service::Start(void) VERIFY(GetHost()); - SetSchedulingOffset(rand()); + SetSchedulingOffset(Utility::Random()); UpdateNextCheck(); AddDowntimesToCache(); -- 2.40.0