ec84a0a2e9a62d777452a71743a21c93743d2926
[public/netxms.git] / src / server / include / nms_events.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2013 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (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
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: nms_events.h
20 **
21 **/
22
23 #ifndef _nms_events_h_
24 #define _nms_events_h_
25
26 #include <nxevent.h>
27
28
29 //
30 // Constants
31 //
32
33 #define EVENTLOG_MAX_MESSAGE_SIZE 255
34 #define EVENTLOG_MAX_USERTAG_SIZE 63
35
36 /**
37 * Event template
38 */
39 class EventTemplate : public RefCountObject
40 {
41 private:
42 UINT32 m_code;
43 int m_severity;
44 uuid m_guid;
45 TCHAR m_name[MAX_EVENT_NAME];
46 UINT32 m_flags;
47 TCHAR *m_messageTemplate;
48 TCHAR *m_description;
49
50 protected:
51 virtual ~EventTemplate();
52
53 public:
54 EventTemplate(DB_RESULT hResult, int row);
55
56 UINT32 getCode() const { return m_code; }
57 int getSeverity() const { return m_severity; }
58 const uuid& getGuid() const { return m_guid; }
59 const TCHAR *getName() const { return m_name; }
60 UINT32 getFlags() const { return m_flags; }
61 const TCHAR *getMessageTemplate() const { return m_messageTemplate; }
62 const TCHAR *getDescription() const { return m_description; }
63 };
64
65 /**
66 * Event
67 */
68 class NXCORE_EXPORTABLE Event
69 {
70 private:
71 UINT64 m_id;
72 UINT64 m_rootId; // Root event id
73 UINT32 m_code;
74 int m_severity;
75 UINT32 m_flags;
76 UINT32 m_sourceId;
77 UINT32 m_dciId;
78 TCHAR m_name[MAX_EVENT_NAME];
79 TCHAR *m_messageText;
80 TCHAR *m_messageTemplate;
81 time_t m_timeStamp;
82 TCHAR *m_userTag;
83 TCHAR *m_customMessage;
84 Array m_parameters;
85 StringList m_parameterNames;
86
87 public:
88 Event();
89 Event(const Event *src);
90 Event(const EventTemplate *eventTemplate, UINT32 sourceId, UINT32 dciId, const TCHAR *userTag, const char *format, const TCHAR **names, va_list args);
91 ~Event();
92
93 UINT64 getId() const { return m_id; }
94 UINT32 getCode() const { return m_code; }
95 UINT32 getSeverity() const { return m_severity; }
96 UINT32 getFlags() const { return m_flags; }
97 UINT32 getSourceId() const { return m_sourceId; }
98 UINT32 getDciId() const { return m_dciId; }
99 const TCHAR *getName() const { return m_name; }
100 const TCHAR *getMessage() const { return m_messageText; }
101 const TCHAR *getUserTag() const { return m_userTag; }
102 time_t getTimeStamp() const { return m_timeStamp; }
103
104 void setSeverity(int severity) { m_severity = severity; }
105
106 UINT64 getRootId() const { return m_rootId; }
107 void setRootId(UINT64 id) { m_rootId = id; }
108
109 void prepareMessage(NXCPMessage *msg) const;
110
111 void expandMessageText();
112 TCHAR *expandText(const TCHAR *textTemplate, const TCHAR *alarmMsg = NULL, const TCHAR *alarmKey = NULL);
113 static TCHAR *expandText(Event *event, UINT32 sourceObject, const TCHAR *textTemplate, const TCHAR *alarmMsg, const TCHAR *alarmKey);
114 void setMessage(const TCHAR *text) { free(m_messageText); m_messageText = _tcsdup_ex(text); }
115 void setUserTag(const TCHAR *text) { free(m_userTag); m_userTag = _tcsdup_ex(text); }
116
117 int getParametersCount() const { return m_parameters.size(); }
118 const TCHAR *getParameter(int index) const { return (TCHAR *)m_parameters.get(index); }
119 UINT32 getParameterAsULong(int index) const { const TCHAR *v = (TCHAR *)m_parameters.get(index); return (v != NULL) ? _tcstoul(v, NULL, 0) : 0; }
120 UINT64 getParameterAsUInt64(int index) const { const TCHAR *v = (TCHAR *)m_parameters.get(index); return (v != NULL) ? _tcstoull(v, NULL, 0) : 0; }
121
122 const TCHAR *getNamedParameter(const TCHAR *name) const { return getParameter(m_parameterNames.indexOfIgnoreCase(name)); }
123 UINT32 getNamedParameterAsULong(const TCHAR *name) const { return getParameterAsULong(m_parameterNames.indexOfIgnoreCase(name)); }
124 UINT64 getNamedParameterAsUInt64(const TCHAR *name) const { return getParameterAsUInt64(m_parameterNames.indexOfIgnoreCase(name)); }
125
126 void addParameter(const TCHAR *name, const TCHAR *value);
127 void setNamedParameter(const TCHAR *name, const TCHAR *value);
128 void setParameter(int index, const TCHAR *name, const TCHAR *value);
129
130 const TCHAR *getCustomMessage() const { return CHECK_NULL_EX(m_customMessage); }
131 void setCustomMessage(const TCHAR *message) { free(m_customMessage); m_customMessage = _tcsdup_ex(message); }
132
133 String createJson();
134 };
135
136 /**
137 * Defines for type of persistent storage action
138 */
139 #define PSTORAGE_SET 1
140 #define PSTORAGE_DELETE 2
141
142 /**
143 * Event policy rule
144 */
145 class EPRule
146 {
147 private:
148 UINT32 m_id;
149 uuid m_guid;
150 UINT32 m_dwFlags;
151 UINT32 m_dwNumSources;
152 UINT32 *m_pdwSourceList;
153 UINT32 m_dwNumEvents;
154 UINT32 *m_pdwEventList;
155 UINT32 m_dwNumActions;
156 UINT32 *m_pdwActionList;
157 TCHAR *m_pszComment;
158 TCHAR *m_pszScript;
159 NXSL_VM *m_pScript;
160
161 TCHAR m_szAlarmMessage[MAX_EVENT_MSG_LENGTH];
162 int m_iAlarmSeverity;
163 TCHAR m_szAlarmKey[MAX_DB_STRING];
164 UINT32 m_dwAlarmTimeout;
165 UINT32 m_dwAlarmTimeoutEvent;
166 IntegerArray<UINT32> *m_alarmCategoryList;
167 StringMap m_pstorageSetActions;
168 StringList m_pstorageDeleteActions;
169
170 bool matchSource(UINT32 dwObjectId);
171 bool matchEvent(UINT32 eventCode);
172 bool matchSeverity(UINT32 dwSeverity);
173 bool matchScript(Event *pEvent);
174
175 void generateAlarm(Event *pEvent);
176
177 public:
178 EPRule(UINT32 id);
179 EPRule(DB_RESULT hResult, int row);
180 EPRule(NXCPMessage *msg);
181 EPRule(ConfigEntry *config);
182 ~EPRule();
183
184 UINT32 getId() const { return m_id; }
185 const uuid& getGuid() const { return m_guid; }
186 void setId(UINT32 dwNewId) { m_id = dwNewId; }
187 bool loadFromDB(DB_HANDLE hdb);
188 bool saveToDB(DB_HANDLE hdb);
189 bool processEvent(Event *pEvent);
190 void createMessage(NXCPMessage *pMsg);
191 void createNXMPRecord(String &str);
192
193 bool isActionInUse(UINT32 dwActionId);
194 bool isCategoryInUse(UINT32 categoryId) const { return m_alarmCategoryList->contains(categoryId); }
195 };
196
197 /**
198 * Event policy
199 */
200 class EventPolicy
201 {
202 private:
203 UINT32 m_dwNumRules;
204 EPRule **m_ppRuleList;
205 RWLOCK m_rwlock;
206
207 void readLock() { RWLockReadLock(m_rwlock, INFINITE); }
208 void writeLock() { RWLockWriteLock(m_rwlock, INFINITE); }
209 void unlock() { RWLockUnlock(m_rwlock); }
210 void clear();
211
212 public:
213 EventPolicy();
214 ~EventPolicy();
215
216 UINT32 getNumRules() { return m_dwNumRules; }
217 bool loadFromDB();
218 bool saveToDB();
219 void processEvent(Event *pEvent);
220 void sendToClient(ClientSession *pSession, UINT32 dwRqId);
221 void replacePolicy(UINT32 dwNumRules, EPRule **ppRuleList);
222 void exportRule(String& str, const uuid& guid);
223 void importRule(EPRule *rule);
224 void removeRuleCategory (UINT32 categoryId);
225
226 bool isActionInUse(UINT32 dwActionId);
227 bool isCategoryInUse(UINT32 dwCategoryId);
228 };
229
230 /**
231 * Functions
232 */
233 BOOL InitEventSubsystem();
234 void ShutdownEventSubsystem();
235 void ReloadEvents();
236 void DeleteEventTemplateFromList(UINT32 eventCode);
237 void CorrelateEvent(Event *pEvent);
238 void CreateNXMPEventRecord(String &str, UINT32 eventCode);
239
240 bool EventNameFromCode(UINT32 eventCode, TCHAR *buffer);
241 UINT32 NXCORE_EXPORTABLE EventCodeFromName(const TCHAR *name, UINT32 defaultValue = 0);
242 EventTemplate *FindEventTemplateByCode(UINT32 eventCode);
243 EventTemplate *FindEventTemplateByName(const TCHAR *pszName);
244
245 bool NXCORE_EXPORTABLE PostEvent(UINT32 eventCode, UINT32 sourceId, const char *format, ...);
246 bool NXCORE_EXPORTABLE PostDciEvent(UINT32 eventCode, UINT32 sourceId, UINT32 dciId, const char *format, ...);
247 UINT64 NXCORE_EXPORTABLE PostEvent2(UINT32 eventCode, UINT32 sourceId, const char *format, ...);
248 bool NXCORE_EXPORTABLE PostEventWithNames(UINT32 eventCode, UINT32 sourceId, const char *format, const TCHAR **names, ...);
249 bool NXCORE_EXPORTABLE PostEventWithNames(UINT32 eventCode, UINT32 sourceId, StringMap *parameters);
250 bool NXCORE_EXPORTABLE PostDciEventWithNames(UINT32 eventCode, UINT32 sourceId, UINT32 dciId, const char *format, const TCHAR **names, ...);
251 bool NXCORE_EXPORTABLE PostDciEventWithNames(UINT32 eventCode, UINT32 sourceId, UINT32 dciId, StringMap *parameters);
252 bool NXCORE_EXPORTABLE PostEventWithTagAndNames(UINT32 eventCode, UINT32 sourceId, const TCHAR *userTag, const char *format, const TCHAR **names, ...);
253 bool NXCORE_EXPORTABLE PostEventWithTag(UINT32 eventCode, UINT32 sourceId, const TCHAR *userTag, const char *format, ...);
254 bool NXCORE_EXPORTABLE PostEventEx(Queue *queue, UINT32 eventCode, UINT32 sourceId, const char *format, ...);
255 void NXCORE_EXPORTABLE ResendEvents(Queue *queue);
256
257 const TCHAR NXCORE_EXPORTABLE *GetStatusAsText(int status, bool allCaps);
258
259 /**
260 * Global variables
261 */
262 extern Queue *g_pEventQueue;
263 extern EventPolicy *g_pEventPolicy;
264 extern INT64 g_totalEventsProcessed;
265
266 #endif /* _nms_events_h_ */