]> granicus.if.org Git - icinga2/blob - lib/icinga/notification.hpp
Merge pull request #6577 from Icinga/fix/setup-api-including-users-file
[icinga2] / lib / icinga / notification.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef NOTIFICATION_H
21 #define NOTIFICATION_H
22
23 #include "icinga/i2-icinga.hpp"
24 #include "icinga/notification-ti.hpp"
25 #include "icinga/checkable-ti.hpp"
26 #include "icinga/user.hpp"
27 #include "icinga/usergroup.hpp"
28 #include "icinga/timeperiod.hpp"
29 #include "icinga/checkresult.hpp"
30 #include "remote/endpoint.hpp"
31 #include "remote/messageorigin.hpp"
32 #include "base/array.hpp"
33
34 namespace icinga
35 {
36
37 /**
38  * @ingroup icinga
39  */
40 enum NotificationFilter
41 {
42         StateFilterOK = 1,
43         StateFilterWarning = 2,
44         StateFilterCritical = 4,
45         StateFilterUnknown = 8,
46
47         StateFilterUp = 16,
48         StateFilterDown = 32
49 };
50
51 /**
52  * The notification type.
53  *
54  * @ingroup icinga
55  */
56 enum NotificationType
57 {
58         NotificationDowntimeStart = 1,
59         NotificationDowntimeEnd = 2,
60         NotificationDowntimeRemoved = 4,
61         NotificationCustom = 8,
62         NotificationAcknowledgement = 16,
63         NotificationProblem = 32,
64         NotificationRecovery = 64,
65         NotificationFlappingStart = 128,
66         NotificationFlappingEnd = 256
67 };
68
69 class NotificationCommand;
70 class ApplyRule;
71 struct ScriptFrame;
72 class Host;
73 class Service;
74
75 /**
76  * An Icinga notification specification.
77  *
78  * @ingroup icinga
79  */
80 class Notification final : public ObjectImpl<Notification>
81 {
82 public:
83         DECLARE_OBJECT(Notification);
84         DECLARE_OBJECTNAME(Notification);
85
86         static void StaticInitialize();
87
88         intrusive_ptr<Checkable> GetCheckable() const;
89         intrusive_ptr<NotificationCommand> GetCommand() const;
90         TimePeriod::Ptr GetPeriod() const;
91         std::set<User::Ptr> GetUsers() const;
92         std::set<UserGroup::Ptr> GetUserGroups() const;
93
94         void UpdateNotificationNumber();
95         void ResetNotificationNumber();
96
97         void BeginExecuteNotification(NotificationType type, const CheckResult::Ptr& cr, bool force,
98                 bool reminder = false, const String& author = "", const String& text = "");
99
100         Endpoint::Ptr GetCommandEndpoint() const;
101
102         static String NotificationTypeToString(NotificationType type);
103         static String NotificationFilterToString(int filter, const std::map<String, int>& filterMap);
104
105         static boost::signals2::signal<void (const Notification::Ptr&, const MessageOrigin::Ptr&)> OnNextNotificationChanged;
106
107         void Validate(int types, const ValidationUtils& utils) override;
108
109         void ValidateStates(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils) override;
110         void ValidateTypes(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils) override;
111
112         static void EvaluateApplyRules(const intrusive_ptr<Host>& host);
113         static void EvaluateApplyRules(const intrusive_ptr<Service>& service);
114
115         static const std::map<String, int>& GetStateFilterMap();
116         static const std::map<String, int>& GetTypeFilterMap();
117
118 protected:
119         void OnConfigLoaded() override;
120         void OnAllConfigLoaded() override;
121         void Start(bool runtimeCreated) override;
122         void Stop(bool runtimeRemoved) override;
123
124 private:
125         ObjectImpl<Checkable>::Ptr m_Checkable;
126
127         bool CheckNotificationUserFilters(NotificationType type, const User::Ptr& user, bool force, bool reminder);
128
129         void ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author = "", const String& text = "");
130
131         static bool EvaluateApplyRuleInstance(const intrusive_ptr<Checkable>& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule);
132         static bool EvaluateApplyRule(const intrusive_ptr<Checkable>& checkable, const ApplyRule& rule);
133
134         static String NotificationTypeToStringInternal(NotificationType type);
135         static String NotificationServiceStateToString(ServiceState state);
136         static String NotificationHostStateToString(HostState state);
137
138         static std::map<String, int> m_StateFilterMap;
139         static std::map<String, int> m_TypeFilterMap;
140 };
141
142 int ServiceStateToFilter(ServiceState state);
143 int HostStateToFilter(HostState state);
144
145 }
146
147 #endif /* NOTIFICATION_H */