1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2 
3 #ifndef NOTIFICATION_H
4 #define NOTIFICATION_H
5 
6 #include "icinga/i2-icinga.hpp"
7 #include "icinga/notification-ti.hpp"
8 #include "icinga/checkable-ti.hpp"
9 #include "icinga/user.hpp"
10 #include "icinga/usergroup.hpp"
11 #include "icinga/timeperiod.hpp"
12 #include "icinga/checkresult.hpp"
13 #include "remote/endpoint.hpp"
14 #include "remote/messageorigin.hpp"
15 #include "base/array.hpp"
16 
17 namespace icinga
18 {
19 
20 /**
21  * @ingroup icinga
22  */
23 enum NotificationFilter
24 {
25 	StateFilterOK = 1,
26 	StateFilterWarning = 2,
27 	StateFilterCritical = 4,
28 	StateFilterUnknown = 8,
29 
30 	StateFilterUp = 16,
31 	StateFilterDown = 32
32 };
33 
34 /**
35  * The notification type.
36  *
37  * @ingroup icinga
38  */
39 enum NotificationType
40 {
41 	NotificationDowntimeStart = 1,
42 	NotificationDowntimeEnd = 2,
43 	NotificationDowntimeRemoved = 4,
44 	NotificationCustom = 8,
45 	NotificationAcknowledgement = 16,
46 	NotificationProblem = 32,
47 	NotificationRecovery = 64,
48 	NotificationFlappingStart = 128,
49 	NotificationFlappingEnd = 256
50 };
51 
52 class NotificationCommand;
53 class ApplyRule;
54 struct ScriptFrame;
55 class Host;
56 class Service;
57 
58 /**
59  * An Icinga notification specification.
60  *
61  * @ingroup icinga
62  */
63 class Notification final : public ObjectImpl<Notification>
64 {
65 public:
66 	DECLARE_OBJECT(Notification);
67 	DECLARE_OBJECTNAME(Notification);
68 
69 	static void StaticInitialize();
70 
71 	intrusive_ptr<Checkable> GetCheckable() const;
72 	intrusive_ptr<NotificationCommand> GetCommand() const;
73 	TimePeriod::Ptr GetPeriod() const;
74 	std::set<User::Ptr> GetUsers() const;
75 	std::set<UserGroup::Ptr> GetUserGroups() const;
76 
77 	void UpdateNotificationNumber();
78 	void ResetNotificationNumber();
79 
80 	void BeginExecuteNotification(NotificationType type, const CheckResult::Ptr& cr, bool force,
81 		bool reminder = false, const String& author = "", const String& text = "");
82 
83 	Endpoint::Ptr GetCommandEndpoint() const;
84 
85 	// Logging, etc.
86 	static String NotificationTypeToString(NotificationType type);
87 	// Compat, used for notifications, etc.
88 	static String NotificationTypeToStringCompat(NotificationType type);
89 	static String NotificationFilterToString(int filter, const std::map<String, int>& filterMap);
90 
91 	static String NotificationServiceStateToString(ServiceState state);
92 	static String NotificationHostStateToString(HostState state);
93 
94 	static boost::signals2::signal<void (const Notification::Ptr&, const MessageOrigin::Ptr&)> OnNextNotificationChanged;
95 
96 	void Validate(int types, const ValidationUtils& utils) override;
97 
98 	void ValidateStates(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils) override;
99 	void ValidateTypes(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils) override;
100 	void ValidateTimes(const Lazy<Dictionary::Ptr>& lvalue, const ValidationUtils& utils) override;
101 
102 	static void EvaluateApplyRules(const intrusive_ptr<Host>& host);
103 	static void EvaluateApplyRules(const intrusive_ptr<Service>& service);
104 
105 	static const std::map<String, int>& GetStateFilterMap();
106 	static const std::map<String, int>& GetTypeFilterMap();
107 
108 protected:
109 	void OnConfigLoaded() override;
110 	void OnAllConfigLoaded() override;
111 	void Start(bool runtimeCreated) override;
112 	void Stop(bool runtimeRemoved) override;
113 
114 private:
115 	ObjectImpl<Checkable>::Ptr m_Checkable;
116 
117 	bool CheckNotificationUserFilters(NotificationType type, const User::Ptr& user, bool force, bool reminder);
118 
119 	void ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author = "", const String& text = "");
120 
121 	static bool EvaluateApplyRuleInstance(const intrusive_ptr<Checkable>& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule);
122 	static bool EvaluateApplyRule(const intrusive_ptr<Checkable>& checkable, const ApplyRule& rule);
123 
124 	static std::map<String, int> m_StateFilterMap;
125 	static std::map<String, int> m_TypeFilterMap;
126 };
127 
128 int ServiceStateToFilter(ServiceState state);
129 int HostStateToFilter(HostState state);
130 
131 }
132 
133 #endif /* NOTIFICATION_H */
134