added uuid class; code rerfactoring
[public/netxms.git] / src / server / include / nms_events.h
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
967893bb 3** Copyright (C) 2003-2013 Victor Kirhenshtein
5039dede
AK
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**
b4fe4886 19** File: nms_events.h
5039dede
AK
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
9f6712bc
VK
33#define EVENTLOG_MAX_MESSAGE_SIZE 255
34#define EVENTLOG_MAX_USERTAG_SIZE 63
5039dede 35
46ee6286
VK
36/**
37 * Event template
38 */
5039dede
AK
39struct EVENT_TEMPLATE
40{
967893bb
VK
41 UINT32 dwCode;
42 UINT32 dwSeverity;
43 UINT32 dwFlags;
5039dede
AK
44 TCHAR *pszMessageTemplate;
45 TCHAR *pszDescription;
46 TCHAR szName[MAX_EVENT_NAME];
47};
48
46ee6286
VK
49/**
50 * Event
51 */
b78b8d2b 52class NXCORE_EXPORTABLE Event
5039dede
AK
53{
54private:
967893bb
VK
55 UINT64 m_qwId;
56 UINT64 m_qwRootId; // Root event id
57 UINT32 m_dwCode;
ad13c0e2 58 int m_severity;
967893bb
VK
59 UINT32 m_dwFlags;
60 UINT32 m_dwSource;
c42b4551 61 TCHAR m_name[MAX_EVENT_NAME];
5039dede
AK
62 TCHAR *m_pszMessageText;
63 TCHAR *m_pszMessageTemplate;
5039dede
AK
64 time_t m_tTimeStamp;
65 TCHAR *m_pszUserTag;
66 TCHAR *m_pszCustomMessage;
ed806a1a
VK
67 Array m_parameters;
68 StringList m_parameterNames;
5039dede
AK
69
70public:
71 Event();
816852b2 72 Event(Event *src);
b8ad978c 73 Event(EVENT_TEMPLATE *pTemplate, UINT32 sourceId, const TCHAR *userTag, const char *format, const TCHAR **names, va_list args);
5039dede
AK
74 ~Event();
75
967893bb
VK
76 UINT64 getId() { return m_qwId; }
77 UINT32 getCode() { return m_dwCode; }
ad13c0e2 78 UINT32 getSeverity() { return m_severity; }
967893bb
VK
79 UINT32 getFlags() { return m_dwFlags; }
80 UINT32 getSourceId() { return m_dwSource; }
c42b4551 81 const TCHAR *getName() { return m_name; }
210642a1
VK
82 const TCHAR *getMessage() { return m_pszMessageText; }
83 const TCHAR *getUserTag() { return m_pszUserTag; }
84 time_t getTimeStamp() { return m_tTimeStamp; }
ad13c0e2
VK
85
86 void setSeverity(int severity) { m_severity = severity; }
5039dede 87
967893bb
VK
88 UINT64 getRootId() { return m_qwRootId; }
89 void setRootId(UINT64 qwId) { m_qwRootId = qwId; }
5039dede 90
b368969c 91 void prepareMessage(NXCPMessage *pMsg);
5039dede 92
210642a1 93 void expandMessageText();
ca0e18d2
VK
94 TCHAR *expandText(const TCHAR *textTemplate, const TCHAR *alarmMsg = NULL, const TCHAR *alarmKey = NULL);
95 static TCHAR *expandText(Event *event, UINT32 sourceObject, const TCHAR *textTemplate, const TCHAR *alarmMsg, const TCHAR *alarmKey);
ad13c0e2
VK
96 void setMessage(const TCHAR *text) { safe_free(m_pszMessageText); m_pszMessageText = _tcsdup(CHECK_NULL_EX(text)); }
97 void setUserTag(const TCHAR *text) { safe_free(m_pszUserTag); m_pszUserTag = _tcsdup(CHECK_NULL_EX(text)); }
5039dede 98
967893bb 99 UINT32 getParametersCount() { return m_parameters.size(); }
ed806a1a 100 const TCHAR *getParameter(int index) { return (TCHAR *)m_parameters.get(index); }
967893bb
VK
101 UINT32 getParameterAsULong(int index) { const TCHAR *v = (TCHAR *)m_parameters.get(index); return (v != NULL) ? _tcstoul(v, NULL, 0) : 0; }
102 UINT64 getParameterAsUInt64(int index) { const TCHAR *v = (TCHAR *)m_parameters.get(index); return (v != NULL) ? _tcstoull(v, NULL, 0) : 0; }
ed806a1a 103
a6312bd6
VK
104 const TCHAR *getNamedParameter(const TCHAR *name) { return getParameter(m_parameterNames.indexOfIgnoreCase(name)); }
105 UINT32 getNamedParameterAsULong(const TCHAR *name) { return getParameterAsULong(m_parameterNames.indexOfIgnoreCase(name)); }
106 UINT64 getNamedParameterAsUInt64(const TCHAR *name) { return getParameterAsUInt64(m_parameterNames.indexOfIgnoreCase(name)); }
ed806a1a
VK
107
108 void addParameter(const TCHAR *name, const TCHAR *value);
24dc5346 109 void setNamedParameter(const TCHAR *name, const TCHAR *value);
d5cf7cda 110 void setParameter(int index, const TCHAR *name, const TCHAR *value);
ed806a1a 111
210642a1
VK
112 const TCHAR *getCustomMessage() { return CHECK_NULL_EX(m_pszCustomMessage); }
113 void setCustomMessage(const TCHAR *message) { safe_free(m_pszCustomMessage); m_pszCustomMessage = (message != NULL) ? _tcsdup(message) : NULL; }
5039dede
AK
114};
115
480e036b
VK
116/**
117 * Event policy rule
118 */
5039dede
AK
119class EPRule
120{
121private:
c42b4551 122 UINT32 m_id;
de4af576 123 uuid m_guid;
967893bb
VK
124 UINT32 m_dwFlags;
125 UINT32 m_dwNumSources;
126 UINT32 *m_pdwSourceList;
127 UINT32 m_dwNumEvents;
128 UINT32 *m_pdwEventList;
129 UINT32 m_dwNumActions;
130 UINT32 *m_pdwActionList;
5039dede
AK
131 TCHAR *m_pszComment;
132 TCHAR *m_pszScript;
6b29839d 133 NXSL_VM *m_pScript;
5039dede 134
480e036b 135 TCHAR m_szAlarmMessage[MAX_EVENT_MSG_LENGTH];
5039dede
AK
136 int m_iAlarmSeverity;
137 TCHAR m_szAlarmKey[MAX_DB_STRING];
967893bb
VK
138 UINT32 m_dwAlarmTimeout;
139 UINT32 m_dwAlarmTimeoutEvent;
5039dede 140
967893bb 141 UINT32 m_dwSituationId;
5039dede
AK
142 TCHAR m_szSituationInstance[MAX_DB_STRING];
143 StringMap m_situationAttrList;
144
967893bb 145 bool matchSource(UINT32 dwObjectId);
b8ad978c 146 bool matchEvent(UINT32 eventCode);
967893bb 147 bool matchSeverity(UINT32 dwSeverity);
0688e29b 148 bool matchScript(Event *pEvent);
5039dede 149
0688e29b 150 void generateAlarm(Event *pEvent);
5039dede
AK
151
152public:
c42b4551
VK
153 EPRule(UINT32 id);
154 EPRule(DB_RESULT hResult, int row);
b368969c 155 EPRule(NXCPMessage *msg);
2292ff1d 156 EPRule(ConfigEntry *config);
5039dede
AK
157 ~EPRule();
158
de4af576
VK
159 UINT32 getId() const { return m_id; }
160 const uuid& getGuid() const { return m_guid; }
c42b4551 161 void setId(UINT32 dwNewId) { m_id = dwNewId; }
0688e29b
VK
162 bool loadFromDB();
163 void saveToDB(DB_HANDLE hdb);
164 bool processEvent(Event *pEvent);
b368969c 165 void createMessage(NXCPMessage *pMsg);
badf9a95 166 void createNXMPRecord(String &str);
5039dede 167
967893bb 168 bool isActionInUse(UINT32 dwActionId);
5039dede
AK
169};
170
480e036b
VK
171/**
172 * Event policy
173 */
5039dede
AK
174class EventPolicy
175{
176private:
967893bb 177 UINT32 m_dwNumRules;
5039dede
AK
178 EPRule **m_ppRuleList;
179 RWLOCK m_rwlock;
180
0688e29b
VK
181 void readLock() { RWLockReadLock(m_rwlock, INFINITE); }
182 void writeLock() { RWLockWriteLock(m_rwlock, INFINITE); }
183 void unlock() { RWLockUnlock(m_rwlock); }
184 void clear();
5039dede
AK
185
186public:
187 EventPolicy();
188 ~EventPolicy();
189
967893bb 190 UINT32 getNumRules() { return m_dwNumRules; }
0688e29b
VK
191 bool loadFromDB();
192 void saveToDB();
193 void processEvent(Event *pEvent);
967893bb
VK
194 void sendToClient(ClientSession *pSession, UINT32 dwRqId);
195 void replacePolicy(UINT32 dwNumRules, EPRule **ppRuleList);
de4af576 196 void exportRule(String& str, const uuid& guid);
c4a6167c 197 void importRule(EPRule *rule);
5039dede 198
967893bb 199 bool isActionInUse(UINT32 dwActionId);
5039dede
AK
200};
201
0688e29b
VK
202/**
203 * Functions
204 */
205BOOL InitEventSubsystem();
206void ShutdownEventSubsystem();
0688e29b 207void ReloadEvents();
b8ad978c 208void DeleteEventTemplateFromList(UINT32 eventCode);
5039dede 209void CorrelateEvent(Event *pEvent);
b8ad978c 210void CreateNXMPEventRecord(String &str, UINT32 eventCode);
3ae780c4 211
b8ad978c 212BOOL EventNameFromCode(UINT32 eventCode, TCHAR *pszBuffer);
6fec127d 213UINT32 NXCORE_EXPORTABLE EventCodeFromName(const TCHAR *name, UINT32 defaultValue = 0);
b8ad978c 214EVENT_TEMPLATE *FindEventTemplateByCode(UINT32 eventCode);
5039dede
AK
215EVENT_TEMPLATE *FindEventTemplateByName(const TCHAR *pszName);
216
3ae780c4
VK
217BOOL NXCORE_EXPORTABLE PostEvent(UINT32 eventCode, UINT32 sourceId, const char *format, ...);
218BOOL NXCORE_EXPORTABLE PostEventWithNames(UINT32 eventCode, UINT32 sourceId, const char *format, const TCHAR **names, ...);
7806c906 219BOOL NXCORE_EXPORTABLE PostEventWithNames(UINT32 eventCode, UINT32 sourceId, StringMap *parameters);
d999988d 220BOOL NXCORE_EXPORTABLE PostEventWithTagAndNames(UINT32 eventCode, UINT32 sourceId, const TCHAR *userTag, const char *format, const TCHAR **names, ...);
3ae780c4
VK
221BOOL NXCORE_EXPORTABLE PostEventWithTag(UINT32 eventCode, UINT32 sourceId, const TCHAR *userTag, const char *format, ...);
222BOOL NXCORE_EXPORTABLE PostEventEx(Queue *queue, UINT32 eventCode, UINT32 sourceId, const char *format, ...);
223void NXCORE_EXPORTABLE ResendEvents(Queue *queue);
224
6fec127d
VK
225const TCHAR NXCORE_EXPORTABLE *GetStatusAsText(int status, bool allCaps);
226
0688e29b
VK
227/**
228 * Global variables
229 */
5039dede
AK
230extern Queue *g_pEventQueue;
231extern EventPolicy *g_pEventPolicy;
5039dede
AK
232extern INT64 g_totalEventsProcessed;
233
234#endif /* _nms_events_h_ */