2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-10-23 05:23:48 -04:00
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2013-10-24 01:38:29 -04:00
|
|
|
# include <stdlib.h>
|
|
|
|
|
#endif /* _WIN32 */
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "methods/randomchecktask.hpp"
|
2015-12-10 09:27:49 -05:00
|
|
|
#include "icinga/icingaapplication.hpp"
|
2019-07-10 04:15:15 -04:00
|
|
|
#include "icinga/checkcommand.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/utility.hpp"
|
2017-05-15 09:51:39 -04:00
|
|
|
#include "base/perfdatavalue.hpp"
|
2015-01-21 02:47:45 -05:00
|
|
|
#include "base/function.hpp"
|
2014-10-19 08:21:12 -04:00
|
|
|
#include "base/logger.hpp"
|
2013-10-23 05:23:48 -04:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2018-08-07 07:55:41 -04:00
|
|
|
REGISTER_FUNCTION_NONCONST(Internal, RandomCheck, &RandomCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
|
2013-10-23 05:23:48 -04:00
|
|
|
|
2018-01-30 05:26:07 -05:00
|
|
|
void RandomCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
|
2017-12-19 09:50:05 -05:00
|
|
|
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
|
2013-10-23 05:23:48 -04:00
|
|
|
{
|
2018-02-21 07:42:58 -05:00
|
|
|
REQUIRE_NOT_NULL(checkable);
|
|
|
|
|
REQUIRE_NOT_NULL(cr);
|
2018-01-30 05:26:07 -05:00
|
|
|
|
2014-12-01 07:19:07 -05:00
|
|
|
if (resolvedMacros && !useResolvedMacros)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-02-06 09:56:07 -05:00
|
|
|
double now = Utility::GetTime();
|
2020-01-31 10:49:49 -05:00
|
|
|
double uptime = Application::GetUptime();
|
2018-02-06 09:56:07 -05:00
|
|
|
|
|
|
|
|
String output = "Hello from " + IcingaApplication::GetInstance()->GetNodeName()
|
|
|
|
|
+ ". Icinga 2 has been running for " + Utility::FormatDuration(uptime)
|
|
|
|
|
+ ". Version: " + Application::GetAppVersion();
|
2013-11-07 08:38:37 -05:00
|
|
|
|
2020-08-03 02:07:32 -04:00
|
|
|
CheckCommand::Ptr command = CheckCommand::ExecuteOverride ? CheckCommand::ExecuteOverride : checkable->GetCheckCommand();
|
2020-07-20 10:29:26 -04:00
|
|
|
String commandName = command->GetName();
|
|
|
|
|
ServiceState state = static_cast<ServiceState>(Utility::Random() % 4);
|
2018-02-06 09:56:07 -05:00
|
|
|
|
2020-07-20 10:29:26 -04:00
|
|
|
if (Checkable::ExecuteCommandProcessFinishedHandler) {
|
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
|
ProcessResult pr;
|
|
|
|
|
pr.PID = -1;
|
|
|
|
|
pr.Output = output;
|
|
|
|
|
pr.ExecutionStart = now;
|
|
|
|
|
pr.ExecutionEnd = now;
|
|
|
|
|
pr.ExitStatus = state;
|
2018-02-06 09:56:07 -05:00
|
|
|
|
2020-07-20 10:29:26 -04:00
|
|
|
Checkable::ExecuteCommandProcessFinishedHandler(commandName, pr);
|
|
|
|
|
} else {
|
|
|
|
|
cr->SetOutput(output);
|
2018-02-06 09:56:07 -05:00
|
|
|
|
2020-07-20 10:29:26 -04:00
|
|
|
double random = Utility::Random() % 1000;
|
|
|
|
|
cr->SetPerformanceData(new Array({
|
|
|
|
|
new PerfdataValue("time", now),
|
|
|
|
|
new PerfdataValue("value", random),
|
|
|
|
|
new PerfdataValue("value_1m", random * 0.9),
|
|
|
|
|
new PerfdataValue("value_5m", random * 0.8),
|
|
|
|
|
new PerfdataValue("uptime", uptime),
|
|
|
|
|
}));
|
2013-10-23 05:23:48 -04:00
|
|
|
|
2020-07-20 10:29:26 -04:00
|
|
|
cr->SetState(state);
|
|
|
|
|
cr->SetCommand(commandName);
|
2019-07-10 04:15:15 -04:00
|
|
|
|
2020-07-20 10:29:26 -04:00
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
|
}
|
2013-10-23 05:23:48 -04:00
|
|
|
}
|