Skip to content

Commit

Permalink
Merge pull request #8107 from WuerthPhoenix/feature/v1-actions-execut…
Browse files Browse the repository at this point in the history
…e-command-8034-2

WIP implement feature/actions/execute-command
  • Loading branch information
Al2Klimov authored Sep 7, 2020
2 parents 53fa09d + 80dc908 commit f10477b
Show file tree
Hide file tree
Showing 36 changed files with 1,025 additions and 211 deletions.
76 changes: 76 additions & 0 deletions lib/base/atomic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#define ATOMIC_H

#include <atomic>
#include <type_traits>
#include <utility>

namespace icinga
{
Expand Down Expand Up @@ -38,6 +40,80 @@ class Atomic : public std::atomic<T> {
}
};

/**
* Wraps T into a std::atomic<T>-like interface.
*
* @ingroup base
*/
template<class T>
class NotAtomic
{
public:
inline T load() const
{
return m_Value;
}

inline void store(T desired)
{
m_Value = std::move(desired);
}

T m_Value;
};

/**
* Tells whether to use std::atomic<T> or NotAtomic<T>.
*
* @ingroup base
*/
template<class T>
struct Atomicable
{
// Doesn't work with too old compilers.
//static constexpr bool value = std::is_trivially_copyable<T>::value && sizeof(T) <= sizeof(void*);
static constexpr bool value = (std::is_fundamental<T>::value || std::is_pointer<T>::value) && sizeof(T) <= sizeof(void*);
};

/**
* Uses either std::atomic<T> or NotAtomic<T> depending on atomicable.
*
* @ingroup base
*/
template<bool atomicable>
struct AtomicTemplate;

template<>
struct AtomicTemplate<false>
{
template<class T>
struct tmplt
{
typedef NotAtomic<T> type;
};
};

template<>
struct AtomicTemplate<true>
{
template<class T>
struct tmplt
{
typedef std::atomic<T> type;
};
};

/**
* Uses either std::atomic<T> or NotAtomic<T> depending on T.
*
* @ingroup base
*/
template<class T>
struct EventuallyAtomic
{
typedef typename AtomicTemplate<Atomicable<T>::value>::template tmplt<T>::type type;
};

}

#endif /* ATOMIC_H */
4 changes: 2 additions & 2 deletions lib/base/configobject.ti
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ abstract class ConfigObject : ConfigObjectBase < ConfigType
[config, no_user_modify] String __name (Name);
[config, no_user_modify] String "name" (ShortName) {
get {{{
if (m_ShortName.IsEmpty())
if (m_ShortName.m_Value.IsEmpty())
return GetName();
else
return m_ShortName;
return m_ShortName.m_Value;
}}}
};
[config, no_user_modify] name(Zone) zone (ZoneName);
Expand Down
180 changes: 144 additions & 36 deletions lib/db_ido/idochecktask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ REGISTER_FUNCTION_NONCONST(Internal, IdoCheck, &IdoCheckTask::ScriptFunc, "check
void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{
CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
ServiceState state;
CheckCommand::Ptr commandObj = CheckCommand::ExecuteOverride ? CheckCommand::ExecuteOverride : checkable->GetCheckCommand();
Value raw_command = commandObj->GetCommandLine();

Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(checkable);

MacroProcessor::ResolverList resolvers;
if (MacroResolver::OverrideMacros)
resolvers.emplace_back("override", MacroResolver::OverrideMacros);

if (service)
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
Expand Down Expand Up @@ -61,25 +65,70 @@ void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult
return;

if (idoType.IsEmpty()) {
cr->SetOutput("Attribute 'ido_type' must be set.");
cr->SetState(ServiceUnknown);
checkable->ProcessCheckResult(cr);
String output = "Attribute 'ido_type' must be set.";
state = ServiceUnknown;

if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;

Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr);
} else {
cr->SetState(state);
cr->SetOutput(output);
checkable->ProcessCheckResult(cr);
}
return;
}

if (idoName.IsEmpty()) {
cr->SetOutput("Attribute 'ido_name' must be set.");
cr->SetState(ServiceUnknown);
checkable->ProcessCheckResult(cr);
String output = "Attribute 'ido_name' must be set.";
state = ServiceUnknown;

if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;

Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr);
} else {
cr->SetState(state);
cr->SetOutput(output);
checkable->ProcessCheckResult(cr);
}
return;
}

Type::Ptr type = Type::GetByName(idoType);

if (!type || !DbConnection::TypeInstance->IsAssignableFrom(type)) {
cr->SetOutput("DB IDO type '" + idoType + "' is invalid.");
cr->SetState(ServiceUnknown);
checkable->ProcessCheckResult(cr);
String output = "DB IDO type '" + idoType + "' is invalid.";
state = ServiceUnknown;

if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;

Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr);
} else {
cr->SetState(state);
cr->SetOutput(output);
checkable->ProcessCheckResult(cr);
}
return;
}

Expand All @@ -89,34 +138,78 @@ void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult
DbConnection::Ptr conn = static_pointer_cast<DbConnection>(dtype->GetObject(idoName));

if (!conn) {
cr->SetOutput("DB IDO connection '" + idoName + "' does not exist.");
cr->SetState(ServiceUnknown);
checkable->ProcessCheckResult(cr);
String output = "DB IDO connection '" + idoName + "' does not exist.";
state = ServiceUnknown;

if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;

Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr);
} else {
cr->SetState(state);
cr->SetOutput(output);
checkable->ProcessCheckResult(cr);
}
return;
}

double qps = conn->GetQueryCount(60) / 60.0;

if (conn->IsPaused()) {
cr->SetOutput("DB IDO connection is temporarily disabled on this cluster instance.");
cr->SetState(ServiceOK);
checkable->ProcessCheckResult(cr);
String output = "DB IDO connection is temporarily disabled on this cluster instance.";
state = ServiceOK;

if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;

Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr);
} else {
cr->SetState(state);
cr->SetOutput(output);
checkable->ProcessCheckResult(cr);
}
return;
}

double pendingQueries = conn->GetPendingQueryCount();

if (!conn->GetConnected()) {
String output;
if (conn->GetShouldConnect()) {
cr->SetOutput("Could not connect to the database server.");
cr->SetState(ServiceCritical);
output ="Could not connect to the database server.";
state = ServiceCritical;
} else {
cr->SetOutput("Not currently enabled: Another cluster instance is responsible for the IDO database.");
cr->SetState(ServiceOK);
output = "Not currently enabled: Another cluster instance is responsible for the IDO database.";
state = ServiceOK;
}

checkable->ProcessCheckResult(cr);
if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;

Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr);
} else {
cr->SetState(state);
cr->SetOutput(output);
checkable->ProcessCheckResult(cr);
}
return;
}

Expand All @@ -130,13 +223,13 @@ void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult
<< " Queries per second: " << std::fixed << std::setprecision(3) << qps
<< " Pending queries: " << std::fixed << std::setprecision(3) << pendingQueries << ".";

cr->SetState(ServiceWarning);
state = ServiceWarning;
} else {
msgbuf << "Connected to the database server (Schema version: '" << schema_version << "')."
<< " Queries per second: " << std::fixed << std::setprecision(3) << qps
<< " Pending queries: " << std::fixed << std::setprecision(3) << pendingQueries << ".";

cr->SetState(ServiceOK);
state = ServiceOK;
}

if (conn->GetEnableHa()) {
Expand All @@ -149,34 +242,49 @@ void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult
if (missingQueriesCritical.IsEmpty() && qps < queriesCritical) {
msgbuf << " " << qps << " queries/s lower than critical threshold (" << queriesCritical << " queries/s).";

cr->SetState(ServiceCritical);
state= ServiceCritical;
} else if (missingQueriesWarning.IsEmpty() && qps < queriesWarning) {
msgbuf << " " << qps << " queries/s lower than warning threshold (" << queriesWarning << " queries/s).";

cr->SetState(ServiceWarning);
state = ServiceWarning;
}

if (missingPendingQueriesCritical.IsEmpty() && pendingQueries > pendingQueriesCritical) {
msgbuf << " " << pendingQueries << " pending queries greater than critical threshold ("
<< pendingQueriesCritical << " queries).";

cr->SetState(ServiceCritical);
state = ServiceCritical;
} else if (missingPendingQueriesWarning.IsEmpty() && pendingQueries > pendingQueriesWarning) {
msgbuf << " " << pendingQueries << " pending queries greater than warning threshold ("
<< pendingQueriesWarning << " queries).";

cr->SetState(ServiceWarning);
state = ServiceWarning;
}

cr->SetOutput(msgbuf.str());
String output = msgbuf.str();

if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;

Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr);
} else {
cr->SetState(state);
cr->SetOutput(output);

cr->SetPerformanceData(new Array({
{ new PerfdataValue("queries", qps, false, "", queriesWarning, queriesCritical) },
{ new PerfdataValue("queries_1min", conn->GetQueryCount(60)) },
{ new PerfdataValue("queries_5mins", conn->GetQueryCount(5 * 60)) },
{ new PerfdataValue("queries_15mins", conn->GetQueryCount(15 * 60)) },
{ new PerfdataValue("pending_queries", pendingQueries, false, "", pendingQueriesWarning, pendingQueriesCritical) }
}));
cr->SetPerformanceData(new Array({
{ new PerfdataValue("queries", qps, false, "", queriesWarning, queriesCritical) },
{ new PerfdataValue("queries_1min", conn->GetQueryCount(60)) },
{ new PerfdataValue("queries_5mins", conn->GetQueryCount(5 * 60)) },
{ new PerfdataValue("queries_15mins", conn->GetQueryCount(15 * 60)) },
{ new PerfdataValue("pending_queries", pendingQueries, false, "", pendingQueriesWarning, pendingQueriesCritical) }
}));

checkable->ProcessCheckResult(cr);
checkable->ProcessCheckResult(cr);
}
}
Loading

0 comments on commit f10477b

Please sign in to comment.