icinga2/lib/icinga/checkable.hpp

285 lines
9.9 KiB
C++
Raw Permalink Normal View History

Replace all existing copyright headers with `SPDX` headers I've used the following command to replace the original copyright header lines in a C-style comment block: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{/\*[^*]*\(\s*c\s*\)\s*(\d{4})\s*Icinga\s+GmbH[^*]*\*/}{// SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n// SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` For files that use shell-style comments (#) like CMakeLists.txt, I've used this command: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{#.*\(\s*c\s*\)\s(\d{4})\sIcinga\s+GmbH.*}{# SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n# SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` And for SQL files: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{--.*\(c\)\s(\d{4})\sIcinga\sGmbH.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{-- Copyright \(c\)\s(\d{4})\sIcinga\s+Development\sTeam.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ```
2026-01-27 09:06:40 -05:00
// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
2014-04-03 09:36:13 -04:00
#ifndef CHECKABLE_H
#define CHECKABLE_H
#include "base/atomic.hpp"
#include "base/timer.hpp"
#include "base/process.hpp"
2014-05-25 10:23:35 -04:00
#include "icinga/i2-icinga.hpp"
2018-01-18 07:50:38 -05:00
#include "icinga/checkable-ti.hpp"
2014-05-25 10:23:35 -04:00
#include "icinga/timeperiod.hpp"
#include "icinga/notification.hpp"
#include "icinga/comment.hpp"
#include "icinga/downtime.hpp"
#include "base/wait-group.hpp"
#include "remote/endpoint.hpp"
2014-05-25 10:23:35 -04:00
#include "remote/messageorigin.hpp"
2021-02-02 04:16:04 -05:00
#include <condition_variable>
#include <cstdint>
#include <functional>
#include <limits>
#include <variant>
2014-04-03 09:36:13 -04:00
namespace icinga
{
/**
* @ingroup icinga
*/
enum DependencyType
{
DependencyState,
DependencyCheckExecution,
DependencyNotification
};
/**
* Checkable Types
*
* @ingroup icinga
*/
enum CheckableType
{
CheckableHost,
CheckableService
};
/**
* @ingroup icinga
*/
enum FlappingStateFilter
{
FlappingStateFilterOk = 1,
FlappingStateFilterWarning = 2,
FlappingStateFilterCritical = 4,
FlappingStateFilterUnknown = 8,
};
2014-04-03 09:36:13 -04:00
class CheckCommand;
class EventCommand;
class Dependency;
class DependencyGroup;
2014-04-03 09:36:13 -04:00
/**
* An Icinga service.
*
* @ingroup icinga
*/
2017-12-31 01:22:16 -05:00
class Checkable : public ObjectImpl<Checkable>
2014-04-03 09:36:13 -04:00
{
public:
2014-11-02 18:44:04 -05:00
DECLARE_OBJECT(Checkable);
DECLARE_OBJECTNAME(Checkable);
2014-04-03 09:36:13 -04:00
static void StaticInitialize();
static thread_local std::function<void(const Value& commandLine, const ProcessResult&)> ExecuteCommandProcessFinishedHandler;
Checkable();
2014-04-03 09:36:13 -04:00
std::set<Checkable::Ptr> GetParents() const;
std::set<Checkable::Ptr> GetChildren() const;
std::set<Checkable::Ptr> GetAllChildren() const;
size_t GetAllChildrenCount() const;
2014-04-03 09:36:13 -04:00
void AddGroup(const String& name);
bool IsReachable(DependencyType dt = DependencyState) const;
bool AffectsChildren() const;
2014-04-03 09:36:13 -04:00
AcknowledgementType GetAcknowledgement();
2014-04-03 09:36:13 -04:00
void AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify = true, bool persistent = false, double changeTime = Utility::GetTime(), double expiry = 0, const MessageOrigin::Ptr& origin = nullptr);
void ClearAcknowledgement(const String& removedBy, double changeTime = Utility::GetTime(), const MessageOrigin::Ptr& origin = nullptr);
2014-04-03 09:36:13 -04:00
int GetSeverity() const override;
2019-04-09 05:34:59 -04:00
bool GetProblem() const override;
bool GetHandled() const override;
2019-09-24 11:54:20 -04:00
Timestamp GetNextUpdate() const override;
2014-04-03 09:36:13 -04:00
/* Checks */
intrusive_ptr<CheckCommand> GetCheckCommand() const;
TimePeriod::Ptr GetCheckPeriod() const;
2014-04-03 09:36:13 -04:00
long GetSchedulingOffset();
2014-04-03 09:36:13 -04:00
void SetSchedulingOffset(long offset);
2017-11-30 02:36:35 -05:00
void UpdateNextCheck(const MessageOrigin::Ptr& origin = nullptr);
2014-04-03 09:36:13 -04:00
static String StateTypeToString(StateType type);
bool HasBeenChecked() const;
2019-04-09 05:26:34 -04:00
virtual bool IsStateOK(ServiceState state) const = 0;
double GetLastCheck() const final;
2014-04-03 09:36:13 -04:00
virtual void SaveLastState(ServiceState state, double timestamp) = 0;
2014-04-03 09:36:13 -04:00
static void UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type);
2014-04-03 09:36:13 -04:00
void ExecuteRemoteCheck(const WaitGroup::Ptr& producer, const Dictionary::Ptr& resolvedMacros = nullptr);
void ExecuteCheck(const WaitGroup::Ptr& producer);
enum class ProcessingResult
{
Ok,
CheckableInactive,
NewerCheckResultPresent,
};
ProcessingResult ProcessCheckResult(const CheckResult::Ptr& cr, const WaitGroup::Ptr& producer, const MessageOrigin::Ptr& origin = nullptr);
2014-04-03 09:36:13 -04:00
Endpoint::Ptr GetCommandEndpoint() const;
static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, const MessageOrigin::Ptr&)> OnNewCheckResult;
static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, StateType, const MessageOrigin::Ptr&)> OnStateChange;
static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, std::set<Checkable::Ptr>, const MessageOrigin::Ptr&)> OnReachabilityChanged;
2014-04-03 09:36:13 -04:00
static boost::signals2::signal<void (const Checkable::Ptr&, NotificationType, const CheckResult::Ptr&,
const String&, const String&, const MessageOrigin::Ptr&)> OnNotificationsRequested;
2014-04-03 09:36:13 -04:00
static boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const User::Ptr&,
const NotificationType&, const CheckResult::Ptr&, const String&, const String&, const String&,
const MessageOrigin::Ptr&)> OnNotificationSentToUser;
2014-04-03 09:36:13 -04:00
static boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
const NotificationType&, const CheckResult::Ptr&, const String&,
const String&, const MessageOrigin::Ptr&)> OnNotificationSentToAllUsers;
2014-04-03 09:36:13 -04:00
static boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType,
bool, bool, double, double, const MessageOrigin::Ptr&)> OnAcknowledgementSet;
static boost::signals2::signal<void (const Checkable::Ptr&, const String&, double, const MessageOrigin::Ptr&)> OnAcknowledgementCleared;
static boost::signals2::signal<void (const Checkable::Ptr&, double)> OnFlappingChange;
static boost::signals2::signal<void (const Checkable::Ptr&)> OnNextCheckUpdated;
2014-04-03 09:36:13 -04:00
static boost::signals2::signal<void (const Checkable::Ptr&)> OnEventCommandExecuted;
static Atomic<uint_fast64_t> CurrentConcurrentChecks;
2014-04-03 09:36:13 -04:00
/* Downtimes */
int GetDowntimeDepth() const final;
2014-04-03 09:36:13 -04:00
void TriggerDowntimes(double triggerTime);
bool IsInDowntime() const;
bool IsAcknowledged() const;
2014-04-03 09:36:13 -04:00
std::set<Downtime::Ptr> GetDowntimes() const;
void RegisterDowntime(const Downtime::Ptr& downtime);
void UnregisterDowntime(const Downtime::Ptr& downtime);
2014-04-03 09:36:13 -04:00
/* Comments */
void RemoveAllComments();
void RemoveAckComments(const String& removedBy = String(), double createdBefore = std::numeric_limits<double>::max());
2014-04-03 09:36:13 -04:00
std::set<Comment::Ptr> GetComments() const;
2021-07-29 06:10:42 -04:00
Comment::Ptr GetLastComment() const;
void RegisterComment(const Comment::Ptr& comment);
void UnregisterComment(const Comment::Ptr& comment);
2014-04-03 09:36:13 -04:00
/* Notifications */
void SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author = "", const String& text = "");
std::set<Notification::Ptr> GetNotifications() const;
void RegisterNotification(const Notification::Ptr& notification);
void UnregisterNotification(const Notification::Ptr& notification);
2014-04-03 09:36:13 -04:00
void ResetNotificationNumbers();
2014-04-03 09:36:13 -04:00
/* Event Handler */
2017-11-30 02:36:35 -05:00
void ExecuteEventHandler(const Dictionary::Ptr& resolvedMacros = nullptr,
bool useResolvedMacros = false);
2014-04-03 09:36:13 -04:00
intrusive_ptr<EventCommand> GetEventCommand() const;
2014-04-03 09:36:13 -04:00
/* Flapping Detection */
bool IsFlapping() const;
2014-04-03 09:36:13 -04:00
/* Dependencies */
void PushDependencyGroupsToRegistry();
std::vector<intrusive_ptr<DependencyGroup>> GetDependencyGroups() const;
void AddDependency(const intrusive_ptr<Dependency>& dependency);
void RemoveDependency(const intrusive_ptr<Dependency>& dependency, bool runtimeRemoved = false);
std::vector<intrusive_ptr<Dependency> > GetDependencies(bool includePending = false) const;
bool HasAnyDependencies() const;
2014-04-03 09:36:13 -04:00
void AddReverseDependency(const intrusive_ptr<Dependency>& dep);
void RemoveReverseDependency(const intrusive_ptr<Dependency>& dep);
std::vector<intrusive_ptr<Dependency> > GetReverseDependencies() const;
2014-04-03 09:36:13 -04:00
void ValidateCheckInterval(const Lazy<double>& lvalue, const ValidationUtils& value) final;
2019-01-28 11:33:05 -05:00
void ValidateRetryInterval(const Lazy<double>& lvalue, const ValidationUtils& value) final;
void ValidateMaxCheckAttempts(const Lazy<int>& lvalue, const ValidationUtils& value) final;
bool NotificationReasonApplies(NotificationType type);
bool NotificationReasonSuppressed(NotificationType type);
bool IsLikelyToBeCheckedSoon();
void FireSuppressedNotifications();
static void IncreasePendingChecks();
static void DecreasePendingChecks();
static int GetPendingChecks();
static void AquirePendingCheckSlot(int maxPendingChecks);
2014-04-03 09:36:13 -04:00
protected:
void Start(bool runtimeCreated) override;
void OnConfigLoaded() override;
void OnAllConfigLoaded() override;
2014-04-03 09:36:13 -04:00
private:
2021-02-02 04:16:04 -05:00
mutable std::mutex m_CheckableMutex;
bool m_CheckRunning{false};
2014-04-03 09:36:13 -04:00
long m_SchedulingOffset;
2021-02-02 04:16:04 -05:00
static std::mutex m_StatsMutex;
static int m_PendingChecks;
2021-02-02 04:16:04 -05:00
static std::condition_variable m_PendingChecksCV;
2014-04-03 09:36:13 -04:00
/* Downtimes */
std::set<Downtime::Ptr> m_Downtimes;
2021-02-02 04:16:04 -05:00
mutable std::mutex m_DowntimeMutex;
2014-04-03 09:36:13 -04:00
static void NotifyFixedDowntimeStart(const Downtime::Ptr& downtime);
static void NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime);
static void NotifyDowntimeInternal(const Downtime::Ptr& downtime);
static void NotifyDowntimeEnd(const Downtime::Ptr& downtime);
static void FireSuppressedNotificationsTimer(const Timer * const&);
static void CleanDeadlinedExecutions(const Timer * const&);
2014-04-03 09:36:13 -04:00
/* Comments */
std::set<Comment::Ptr> m_Comments;
2021-02-02 04:16:04 -05:00
mutable std::mutex m_CommentMutex;
2014-04-03 09:36:13 -04:00
/* Notifications */
std::set<Notification::Ptr> m_Notifications;
2021-02-02 04:16:04 -05:00
mutable std::mutex m_NotificationMutex;
2014-04-03 09:36:13 -04:00
/* Dependencies */
2021-02-02 04:16:04 -05:00
mutable std::mutex m_DependencyMutex;
std::map<std::variant<Checkable*, String>, intrusive_ptr<DependencyGroup>> m_DependencyGroups;
std::set<intrusive_ptr<Dependency> > m_ReverseDependencies;
/**
* Registering a checkable to its parent DependencyGroups is delayed during config loading until all dependencies
* were registered on the checkable. m_PendingDependencies is used to temporarily store the dependencies until then.
* It is a pointer type for two reasons:
* 1. The field is no longer needed after the DependencyGroups were registered, having it as a pointer reduces the
* overhead from sizeof(std::map<>) to sizeof(std::map<>*).
* 2. It allows the field to also be used as a flag: the delayed group registration is only done until it is reset
* to nullptr.
*/
std::unique_ptr<std::map<std::variant<Checkable*, String>, std::set<intrusive_ptr<Dependency>>>>
m_PendingDependencies {std::make_unique<decltype(m_PendingDependencies)::element_type>()};
void GetAllChildrenInternal(std::set<Checkable::Ptr>& seenChildren, int level = 0) const;
/* Flapping */
static const std::map<String, int> m_FlappingStateFilterMap;
void UpdateFlappingStatus(ServiceState newState);
static int ServiceStateToFlappingFilter(ServiceState state);
2014-04-03 09:36:13 -04:00
};
}
#endif /* CHECKABLE_H */
#include "icinga/dependency.hpp"