]> granicus.if.org Git - icinga2/commitdiff
Fix incorrect formatting for some macro values
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 16 Jun 2016 13:14:35 +0000 (15:14 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 16 Jun 2016 13:14:35 +0000 (15:14 +0200)
fixes #11483

lib/base/convert.cpp
lib/base/convert.hpp
lib/base/number-script.cpp
lib/base/value-operators.cpp
lib/icinga/host.cpp
lib/icinga/icingaapplication.cpp
lib/icinga/service.cpp

index 3495fdd056f14b7c48d20640da43bf83ccee7fdf..45f6a965b5c0e5976661097f2653f64d3dc529a9 100644 (file)
@@ -33,6 +33,13 @@ String Convert::ToString(const Value& val)
        return val;
 }
 
+String Convert::ToString(double val)
+{
+       std::ostringstream msgbuf;
+       msgbuf << std::fixed << val;
+       return msgbuf.str();
+}
+
 double Convert::ToDateTimeValue(double val)
 {
        return val;
index fd2daa3a60cbea1fbcecc38ab62a6999a8d12df5..103909f39d0f5085a34a6d68ffae382163a2c061 100644 (file)
@@ -82,6 +82,7 @@ public:
 
        static String ToString(const String& val);
        static String ToString(const Value& val);
+       static String ToString(double val);
 
        static double ToDateTimeValue(double val);
        static double ToDateTimeValue(const Value& val);
index 9765a368ec2aa0528a5b2bd79ca3ec77d0706caf..30d2e43b75f5590389cbca4417ac4ca2951b5f5e 100644 (file)
@@ -28,8 +28,7 @@ using namespace icinga;
 static String NumberToString(void)
 {
        ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
-       double self = vframe->Self;
-       return Convert::ToString(self);
+       return vframe->Self;
 }
 
 Object::Ptr Number::GetPrototype(void)
index ad2691a5951b6fb06a9d9e4cabab9a69cfc71f97..a658180a6b106d9bfabbcb6780401e955f2f29e7 100644 (file)
@@ -56,18 +56,19 @@ Value::operator double(void) const
 Value::operator String(void) const
 {
        Object *object;
-       double integral, fractional;
+       double dvalue, integral, fractional;
 
        switch (GetType()) {
                case ValueEmpty:
                        return String();
                case ValueNumber:
-                       fractional = std::modf(boost::get<double>(m_Value), &integral);
+                       dvalue = boost::get<double>(m_Value);
+                       fractional = std::modf(dvalue, &integral);
 
                        if (fractional != 0)
-                               return boost::lexical_cast<std::string>(m_Value);
+                               return Convert::ToString(dvalue);
                        else
-                               return boost::lexical_cast<std::string>((long)integral);
+                               return Convert::ToString(static_cast<long>(dvalue));
                case ValueBoolean:
                        if (boost::get<bool>(m_Value))
                                return "true";
index af33d528b8237babe03d5dc2fa55af5db7ad5a52..dd166aa3b7a239dea144862f8fa24ab507cc84e4 100644 (file)
@@ -237,7 +237,7 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, Value *res
                *result = StateToString(GetState());
                return true;
        } else if (macro == "state_id") {
-               *result = Convert::ToString(GetState());
+               *result = GetState();
                return true;
        } else if (macro == "state_type") {
                *result = StateTypeToString(GetStateType());
@@ -246,19 +246,19 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, Value *res
                *result = StateToString(GetLastState());
                return true;
        } else if (macro == "last_state_id") {
-               *result = Convert::ToString(GetLastState());
+               *result = GetLastState();
                return true;
        } else if (macro == "last_state_type") {
                *result = StateTypeToString(GetLastStateType());
                return true;
        } else if (macro == "last_state_change") {
-               *result = Convert::ToString((long)GetLastStateChange());
+               *result = GetLastStateChange();
                return true;
        } else if (macro == "downtime_depth") {
-               *result = Convert::ToString((long)GetDowntimeDepth());
+               *result = GetDowntimeDepth();
                return true;
        } else if (macro == "duration_sec") {
-               *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
+               *result = Utility::GetTime() - GetLastStateChange();
                return true;
        } else if (macro == "num_services" || macro == "num_services_ok" || macro == "num_services_warning"
                    || macro == "num_services_unknown" || macro == "num_services_critical") {
@@ -281,7 +281,7 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, Value *res
                                count++;
                        }
 
-                       *result = Convert::ToString(count);
+                       *result = count;
                        return true;
        }
 
@@ -289,10 +289,10 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, Value *res
 
        if (cr) {
                if (macro == "latency") {
-                       *result = Convert::ToString(cr->CalculateLatency());
+                       *result = cr->CalculateLatency();
                        return true;
                } else if (macro == "execution_time") {
-                       *result = Convert::ToString(cr->CalculateExecutionTime());
+                       *result = cr->CalculateExecutionTime();
                        return true;
                } else if (macro == "output") {
                        *result = cr->GetOutput();
@@ -301,7 +301,7 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, Value *res
                        *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
                        return true;
                } else if (macro == "last_check") {
-                       *result = Convert::ToString((long)cr->GetScheduleStart());
+                       *result = cr->GetScheduleStart();
                        return true;
                } else if (macro == "check_source") {
                        *result = cr->GetCheckSource();
index 26f41d0b20d852f89bc4051de86abeac1e04b3e2..5d7afa21cc0668d56eafe98983e1d41ce23248cd 100644 (file)
@@ -210,7 +210,7 @@ bool IcingaApplication::ResolveMacro(const String& macro, const CheckResult::Ptr
        double now = Utility::GetTime();
 
        if (macro == "timet") {
-               *result = Convert::ToString((long)now);
+               *result = now;
                return true;
        } else if (macro == "long_date_time") {
                *result = Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", now);
@@ -240,31 +240,31 @@ bool IcingaApplication::ResolveMacro(const String& macro, const CheckResult::Ptr
                ServiceStatistics ss = CIB::CalculateServiceStats();
 
                if (macro == "num_services_ok") {
-                       *result = Convert::ToString(ss.services_ok);
+                       *result = ss.services_ok;
                        return true;
                } else if (macro == "num_services_warning") {
-                       *result = Convert::ToString(ss.services_warning);
+                       *result = ss.services_warning;
                        return true;
                } else if (macro == "num_services_critical") {
-                       *result = Convert::ToString(ss.services_critical);
+                       *result = ss.services_critical;
                        return true;
                } else if (macro == "num_services_unknown") {
-                       *result = Convert::ToString(ss.services_unknown);
+                       *result = ss.services_unknown;
                        return true;
                } else if (macro == "num_services_pending") {
-                       *result = Convert::ToString(ss.services_pending);
+                       *result = ss.services_pending;
                        return true;
                } else if (macro == "num_services_unreachable") {
-                       *result = Convert::ToString(ss.services_unreachable);
+                       *result = ss.services_unreachable;
                        return true;
                } else if (macro == "num_services_flapping") {
-                       *result = Convert::ToString(ss.services_flapping);
+                       *result = ss.services_flapping;
                        return true;
                } else if (macro == "num_services_in_downtime") {
-                       *result = Convert::ToString(ss.services_in_downtime);
+                       *result = ss.services_in_downtime;
                        return true;
                } else if (macro == "num_services_acknowledged") {
-                       *result = Convert::ToString(ss.services_acknowledged);
+                       *result = ss.services_acknowledged;
                        return true;
                }
        }
@@ -272,25 +272,25 @@ bool IcingaApplication::ResolveMacro(const String& macro, const CheckResult::Ptr
                HostStatistics hs = CIB::CalculateHostStats();
 
                if (macro == "num_hosts_up") {
-                       *result = Convert::ToString(hs.hosts_up);
+                       *result = hs.hosts_up;
                        return true;
                } else if (macro == "num_hosts_down") {
-                       *result = Convert::ToString(hs.hosts_down);
+                       *result = hs.hosts_down;
                        return true;
                } else if (macro == "num_hosts_pending") {
-                       *result = Convert::ToString(hs.hosts_pending);
+                       *result = hs.hosts_pending;
                        return true;
                } else if (macro == "num_hosts_unreachable") {
-                       *result = Convert::ToString(hs.hosts_unreachable);
+                       *result = hs.hosts_unreachable;
                        return true;
                } else if (macro == "num_hosts_flapping") {
-                       *result = Convert::ToString(hs.hosts_flapping);
+                       *result = hs.hosts_flapping;
                        return true;
                } else if (macro == "num_hosts_in_downtime") {
-                       *result = Convert::ToString(hs.hosts_in_downtime);
+                       *result = hs.hosts_in_downtime;
                        return true;
                } else if (macro == "num_hosts_acknowledged") {
-                       *result = Convert::ToString(hs.hosts_acknowledged);
+                       *result = hs.hosts_acknowledged;
                        return true;
                }
        }
index 78b389ce2f084a3e0471c7b3b5b7bffcecad929e..fd233f0b2f80081bbc91b89962ddb9756b7069f3 100644 (file)
@@ -192,7 +192,7 @@ bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, Valu
                *result = StateToString(GetState());
                return true;
        } else if (macro == "state_id") {
-               *result = Convert::ToString(GetState());
+               *result = GetState();
                return true;
        } else if (macro == "state_type") {
                *result = StateTypeToString(GetStateType());
@@ -201,28 +201,28 @@ bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, Valu
                *result = StateToString(GetLastState());
                return true;
        } else if (macro == "last_state_id") {
-               *result = Convert::ToString(GetLastState());
+               *result = GetLastState();
                return true;
        } else if (macro == "last_state_type") {
                *result = StateTypeToString(GetLastStateType());
                return true;
        } else if (macro == "last_state_change") {
-               *result = Convert::ToString((long)GetLastStateChange());
+               *result = GetLastStateChange();
                return true;
        } else if (macro == "downtime_depth") {
-               *result = Convert::ToString((long)GetDowntimeDepth());
+               *result = GetDowntimeDepth();
                return true;
        } else if (macro == "duration_sec") {
-               *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
+               *result = Utility::GetTime() - GetLastStateChange();
                return true;
        }
 
        if (cr) {
                if (macro == "latency") {
-                       *result = Convert::ToString(cr->CalculateLatency());
+                       *result = cr->CalculateLatency();
                        return true;
                } else if (macro == "execution_time") {
-                       *result = Convert::ToString(cr->CalculateExecutionTime());
+                       *result = cr->CalculateExecutionTime();
                        return true;
                } else if (macro == "output") {
                        *result = cr->GetOutput();
@@ -231,7 +231,7 @@ bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, Valu
                        *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
                        return true;
                } else if (macro == "last_check") {
-                       *result = Convert::ToString((long)cr->GetExecutionEnd());
+                       *result = cr->GetExecutionEnd();
                        return true;
                } else if (macro == "check_source") {
                        *result = cr->GetCheckSource();