/****************************************************************************** * Icinga 2 * * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software Foundation * * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ #ifndef SERVICE_H #define SERVICE_H namespace icinga { /** * The state of a service. * * @ingroup icinga */ enum ServiceState { StateOK, StateWarning, StateCritical, StateUnknown, StateUncheckable, }; /** * The state type of a service. * * @ingroup icinga */ enum ServiceStateType { StateTypeSoft, StateTypeHard }; /** * The acknowledgement type of a service. * * @ingroup icinga */ enum AcknowledgementType { AcknowledgementNone = 0, AcknowledgementNormal = 1, AcknowledgementSticky = 2 }; /** * The type of a service comment. * * @ingroup icinga */ enum CommentType { CommentUser = 1, CommentDowntime = 2, CommentFlapping = 3, CommentAcknowledgement = 4 }; class CheckResultMessage; /** * An Icinga service. * * @ingroup icinga */ class I2_ICINGA_API Service : public DynamicObject { public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; Service(const Dictionary::Ptr& properties); ~Service(void); static Service::Ptr GetByName(const String& name); static Service::Ptr GetByNamePair(const String& hostName, const String& serviceName); static const int DefaultMaxCheckAttempts; static const int DefaultCheckInterval; static const int CheckIntervalDivisor; String GetDisplayName(void) const; Host::Ptr GetHost(void) const; Dictionary::Ptr GetMacros(void) const; Dictionary::Ptr GetHostDependencies(void) const; Dictionary::Ptr GetServiceDependencies(void) const; Dictionary::Ptr GetGroups(void) const; String GetHostName(void) const; String GetShortName(void) const; static Dictionary::Ptr CalculateDynamicMacros(const Service::Ptr& self); static set GetParentHosts(const Service::Ptr& self); static set GetParentServices(const Service::Ptr& self); static bool IsReachable(const Service::Ptr& self); AcknowledgementType GetAcknowledgement(void); void SetAcknowledgement(AcknowledgementType acknowledgement); /* Checks */ Dictionary::Ptr GetCheckers(void) const; Value GetCheckCommand(void) const; long GetMaxCheckAttempts(void) const; double GetCheckInterval(void) const; double GetRetryInterval(void) const; long GetSchedulingOffset(void); void SetSchedulingOffset(long offset); void SetNextCheck(double nextCheck); double GetNextCheck(void); void UpdateNextCheck(void); void SetCurrentChecker(const String& checker); String GetCurrentChecker(void) const; bool IsAllowedChecker(const String& checker) const; void SetCurrentCheckAttempt(long attempt); long GetCurrentCheckAttempt(void) const; void SetState(ServiceState state); ServiceState GetState(void) const; void SetStateType(ServiceStateType type); ServiceStateType GetStateType(void) const; void SetLastCheckResult(const Dictionary::Ptr& result); Dictionary::Ptr GetLastCheckResult(void) const; void SetLastStateChange(double ts); double GetLastStateChange(void) const; void SetLastHardStateChange(double ts); double GetLastHardStateChange(void) const; bool GetEnableActiveChecks(void) const; void SetEnableActiveChecks(bool enabled); bool GetEnablePassiveChecks(void) const; void SetEnablePassiveChecks(bool enabled); bool GetForceNextCheck(void) const; void SetForceNextCheck(bool forced); double GetAcknowledgementExpiry(void) const; void SetAcknowledgementExpiry(double timestamp); void ApplyCheckResult(const Dictionary::Ptr& cr); static void UpdateStatistics(const Dictionary::Ptr& cr); void AcknowledgeProblem(AcknowledgementType type, double expiry = 0); void ClearAcknowledgement(void); static void BeginExecuteCheck(const Service::Ptr& self, const function& callback); void ProcessCheckResult(const Dictionary::Ptr& cr); static double CalculateExecutionTime(const Dictionary::Ptr& cr); static double CalculateLatency(const Dictionary::Ptr& cr); static ServiceState StateFromString(const String& state); static String StateToString(ServiceState state); static ServiceStateType StateTypeFromString(const String& state); static String StateTypeToString(ServiceStateType state); static signals2::signal OnCheckerChanged; static signals2::signal OnNextCheckChanged; /* Downtimes */ static int GetNextDowntimeID(void); Dictionary::Ptr GetDowntimes(void) const; String AddDowntime(const String& author, const String& comment, double startTime, double endTime, bool fixed, const String& triggeredBy, double duration); static void RemoveDowntime(const String& id); void TriggerDowntimes(void); static void TriggerDowntime(const String& id); static String GetDowntimeIDFromLegacyID(int id); static Service::Ptr GetOwnerByDowntimeID(const String& id); static Dictionary::Ptr GetDowntimeByID(const String& id); static bool IsDowntimeActive(const Dictionary::Ptr& downtime); static bool IsDowntimeExpired(const Dictionary::Ptr& downtime); static void InvalidateDowntimesCache(void); bool IsInDowntime(void) const; bool IsAcknowledged(void); /* Comments */ static int GetNextCommentID(void); Dictionary::Ptr GetComments(void) const; String AddComment(CommentType entryType, const String& author, const String& text, double expireTime); void RemoveAllComments(void); static void RemoveComment(const String& id); static String GetCommentIDFromLegacyID(int id); static Service::Ptr GetOwnerByCommentID(const String& id); static Dictionary::Ptr GetCommentByID(const String& id); static bool IsCommentExpired(const Dictionary::Ptr& comment); static void InvalidateCommentsCache(void); /* Notifications */ bool GetEnableNotifications(void) const; void SetEnableNotifications(bool enabled); double GetNotificationInterval(void) const; void RequestNotifications(NotificationType type); static void SendNotifications(const Service::Ptr& self, NotificationType type); static set GetNotifications(const Service::Ptr& self); static void InvalidateNotificationsCache(void); static void UpdateSlaveNotifications(const Service::Ptr& self); double GetLastNotification(void) const; void SetLastNotification(double time); protected: virtual void OnRegistrationCompleted(void); virtual void OnAttributeChanged(const String& name, const Value& oldValue); private: Dictionary::Ptr m_SlaveNotifications; Attribute m_DisplayName; Attribute m_Macros; Attribute m_HostDependencies; Attribute m_ServiceDependencies; Attribute m_ServiceGroups; Attribute m_ShortName; Attribute m_Acknowledgement; Attribute m_AcknowledgementExpiry; Attribute m_HostName; /* Checks */ Attribute m_CheckCommand; Attribute m_MaxCheckAttempts; Attribute m_CheckInterval; Attribute m_RetryInterval; Attribute m_NextCheck; Attribute m_Checkers; Attribute m_CurrentChecker; Attribute m_CheckAttempt; Attribute m_State; Attribute m_StateType; Attribute m_LastResult; Attribute m_LastStateChange; Attribute m_LastHardStateChange; Attribute m_EnableActiveChecks; Attribute m_EnablePassiveChecks; Attribute m_ForceNextCheck; ScriptTask::Ptr m_CurrentTask; long m_SchedulingOffset; void CheckCompletedHandler(const Dictionary::Ptr& checkInfo, const ScriptTask::Ptr& task, const function& callback); /* Downtimes */ Attribute m_Downtimes; static int m_NextDowntimeID; static boost::mutex m_DowntimeMutex; static map m_LegacyDowntimesCache; static map m_DowntimesCache; static bool m_DowntimesCacheValid; static Timer::Ptr m_DowntimesExpireTimer; static void DowntimesExpireTimerHandler(void); void RemoveExpiredDowntimes(void); static void RefreshDowntimesCache(void); /* Comments */ Attribute m_Comments; static int m_NextCommentID; static boost::mutex m_CommentMutex; static map m_LegacyCommentsCache; static map m_CommentsCache; static bool m_CommentsCacheValid; static Timer::Ptr m_CommentsExpireTimer; static void CommentsExpireTimerHandler(void); void AddCommentsToCache(void); void RemoveExpiredComments(void); static void RefreshCommentsCache(void); /* Notifications */ Attribute m_EnableNotifications; Attribute m_LastNotification; Attribute m_NotificationInterval; static boost::mutex m_NotificationMutex; static map > m_NotificationsCache; static bool m_NotificationsCacheValid; static void RefreshNotificationsCache(void); }; } #endif /* SERVICE_H */