]> granicus.if.org Git - icinga2/commitdiff
Implement Utility::Random().
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 3 Oct 2013 20:10:46 +0000 (22:10 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 3 Oct 2013 20:10:46 +0000 (22:10 +0200)
lib/base/debug.h
lib/base/utility.cpp
lib/base/utility.h
lib/icinga/nullchecktask.cpp
lib/icinga/nullchecktask.h
lib/icinga/service.cpp

index bc785f43aaa9baa3698d93898f06fedc7d3b6720..98050f3f4c67f0ded5be76324fee49108da77355 100644 (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)
index 602521b07deb63c2fa5f24dea36fda467f2c9200..698f378d69279d31810cd4dae3322f224c04805f 100644 (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();
+}
index 5a5bdb9d8b7f247a6e304588c88c76eecfd905c6..8c9d587af9c218912f6aae10c2849936f9c73277 100644 (file)
@@ -99,6 +99,8 @@ public:
 
        static int CompareVersion(const String& v1, const String& v2);
 
+       static int Random(void);
+
 private:
        Utility(void);
 
index 9fc0bb1f1d791e51d42d11fe9a39528f15ed199c..371600309d511a6d92d4845395d1539ebe7dcde6 100644 (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;
-       }
-}
index edd8a1ade5539722bfaffc656a76575a505f9da1..fa9fcf873f16648a679742bd545b05725ff727d1 100644 (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);
index 465befd990180be9eb8be0bd7dbae4c29f22db9c..52bba4cfb9a3252dee0fe78fdb531a2ff654b5a7 100644 (file)
@@ -48,7 +48,7 @@ void Service::Start(void)
 
        VERIFY(GetHost());
 
-       SetSchedulingOffset(rand());
+       SetSchedulingOffset(Utility::Random());
        UpdateNextCheck();
 
        AddDowntimesToCache();