initial Timer class
[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;
58 UINT32 m_dwSeverity;
59 UINT32 m_dwFlags;
60 UINT32 m_dwSource;
b4fe4886 61 TCHAR m_szName[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; }
78 UINT32 getSeverity() { return m_dwSeverity; }
79 UINT32 getFlags() { return m_dwFlags; }
80 UINT32 getSourceId() { return m_dwSource; }
210642a1
VK
81 const TCHAR *getName() { return m_szName; }
82 const TCHAR *getMessage() { return m_pszMessageText; }
83 const TCHAR *getUserTag() { return m_pszUserTag; }
84 time_t getTimeStamp() { return m_tTimeStamp; }
5039dede 85
967893bb
VK
86 UINT64 getRootId() { return m_qwRootId; }
87 void setRootId(UINT64 qwId) { m_qwRootId = qwId; }
5039dede 88
210642a1 89 void prepareMessage(CSCPMessage *pMsg);
5039dede 90
210642a1 91 void expandMessageText();
ca0e18d2
VK
92 TCHAR *expandText(const TCHAR *textTemplate, const TCHAR *alarmMsg = NULL, const TCHAR *alarmKey = NULL);
93 static TCHAR *expandText(Event *event, UINT32 sourceObject, const TCHAR *textTemplate, const TCHAR *alarmMsg, const TCHAR *alarmKey);
5039dede 94
967893bb 95 UINT32 getParametersCount() { return m_parameters.size(); }
ed806a1a 96 const TCHAR *getParameter(int index) { return (TCHAR *)m_parameters.get(index); }
967893bb
VK
97 UINT32 getParameterAsULong(int index) { const TCHAR *v = (TCHAR *)m_parameters.get(index); return (v != NULL) ? _tcstoul(v, NULL, 0) : 0; }
98 UINT64 getParameterAsUInt64(int index) { const TCHAR *v = (TCHAR *)m_parameters.get(index); return (v != NULL) ? _tcstoull(v, NULL, 0) : 0; }
ed806a1a 99
a6312bd6
VK
100 const TCHAR *getNamedParameter(const TCHAR *name) { return getParameter(m_parameterNames.indexOfIgnoreCase(name)); }
101 UINT32 getNamedParameterAsULong(const TCHAR *name) { return getParameterAsULong(m_parameterNames.indexOfIgnoreCase(name)); }
102 UINT64 getNamedParameterAsUInt64(const TCHAR *name) { return getParameterAsUInt64(m_parameterNames.indexOfIgnoreCase(name)); }
ed806a1a
VK
103
104 void addParameter(const TCHAR *name, const TCHAR *value);
24dc5346 105 void setNamedParameter(const TCHAR *name, const TCHAR *value);
d5cf7cda 106 void setParameter(int index, const TCHAR *name, const TCHAR *value);
ed806a1a 107
210642a1
VK
108 const TCHAR *getCustomMessage() { return CHECK_NULL_EX(m_pszCustomMessage); }
109 void setCustomMessage(const TCHAR *message) { safe_free(m_pszCustomMessage); m_pszCustomMessage = (message != NULL) ? _tcsdup(message) : NULL; }
5039dede
AK
110};
111
480e036b
VK
112/**
113 * Event policy rule
114 */
5039dede
AK
115class EPRule
116{
117private:
967893bb 118 UINT32 m_dwId;
badf9a95 119 uuid_t m_guid;
967893bb
VK
120 UINT32 m_dwFlags;
121 UINT32 m_dwNumSources;
122 UINT32 *m_pdwSourceList;
123 UINT32 m_dwNumEvents;
124 UINT32 *m_pdwEventList;
125 UINT32 m_dwNumActions;
126 UINT32 *m_pdwActionList;
5039dede
AK
127 TCHAR *m_pszComment;
128 TCHAR *m_pszScript;
6b29839d 129 NXSL_VM *m_pScript;
5039dede 130
480e036b 131 TCHAR m_szAlarmMessage[MAX_EVENT_MSG_LENGTH];
5039dede
AK
132 int m_iAlarmSeverity;
133 TCHAR m_szAlarmKey[MAX_DB_STRING];
967893bb
VK
134 UINT32 m_dwAlarmTimeout;
135 UINT32 m_dwAlarmTimeoutEvent;
5039dede 136
967893bb 137 UINT32 m_dwSituationId;
5039dede
AK
138 TCHAR m_szSituationInstance[MAX_DB_STRING];
139 StringMap m_situationAttrList;
140
967893bb 141 bool matchSource(UINT32 dwObjectId);
b8ad978c 142 bool matchEvent(UINT32 eventCode);
967893bb 143 bool matchSeverity(UINT32 dwSeverity);
0688e29b 144 bool matchScript(Event *pEvent);
5039dede 145
0688e29b 146 void generateAlarm(Event *pEvent);
5039dede
AK
147
148public:
967893bb 149 EPRule(UINT32 dwId);
5039dede
AK
150 EPRule(DB_RESULT hResult, int iRow);
151 EPRule(CSCPMessage *pMsg);
2292ff1d 152 EPRule(ConfigEntry *config);
5039dede
AK
153 ~EPRule();
154
967893bb 155 UINT32 getId() { return m_dwId; }
2f431c19 156 BYTE *getGuid() { return m_guid; }
967893bb 157 void setId(UINT32 dwNewId) { m_dwId = dwNewId; }
0688e29b
VK
158 bool loadFromDB();
159 void saveToDB(DB_HANDLE hdb);
160 bool processEvent(Event *pEvent);
161 void createMessage(CSCPMessage *pMsg);
badf9a95 162 void createNXMPRecord(String &str);
5039dede 163
967893bb 164 bool isActionInUse(UINT32 dwActionId);
5039dede
AK
165};
166
480e036b
VK
167/**
168 * Event policy
169 */
5039dede
AK
170class EventPolicy
171{
172private:
967893bb 173 UINT32 m_dwNumRules;
5039dede
AK
174 EPRule **m_ppRuleList;
175 RWLOCK m_rwlock;
176
0688e29b
VK
177 void readLock() { RWLockReadLock(m_rwlock, INFINITE); }
178 void writeLock() { RWLockWriteLock(m_rwlock, INFINITE); }
179 void unlock() { RWLockUnlock(m_rwlock); }
180 void clear();
5039dede
AK
181
182public:
183 EventPolicy();
184 ~EventPolicy();
185
967893bb 186 UINT32 getNumRules() { return m_dwNumRules; }
0688e29b
VK
187 bool loadFromDB();
188 void saveToDB();
189 void processEvent(Event *pEvent);
967893bb
VK
190 void sendToClient(ClientSession *pSession, UINT32 dwRqId);
191 void replacePolicy(UINT32 dwNumRules, EPRule **ppRuleList);
2f431c19 192 void exportRule(String &str, uuid_t guid);
c4a6167c 193 void importRule(EPRule *rule);
5039dede 194
967893bb 195 bool isActionInUse(UINT32 dwActionId);
5039dede
AK
196};
197
0688e29b
VK
198/**
199 * Functions
200 */
201BOOL InitEventSubsystem();
202void ShutdownEventSubsystem();
0688e29b 203void ReloadEvents();
b8ad978c 204void DeleteEventTemplateFromList(UINT32 eventCode);
5039dede 205void CorrelateEvent(Event *pEvent);
b8ad978c 206void CreateNXMPEventRecord(String &str, UINT32 eventCode);
3ae780c4 207
b8ad978c 208BOOL EventNameFromCode(UINT32 eventCode, TCHAR *pszBuffer);
967893bb 209UINT32 EventCodeFromName(const TCHAR *name, UINT32 defaultValue = 0);
b8ad978c 210EVENT_TEMPLATE *FindEventTemplateByCode(UINT32 eventCode);
5039dede
AK
211EVENT_TEMPLATE *FindEventTemplateByName(const TCHAR *pszName);
212
3ae780c4
VK
213BOOL NXCORE_EXPORTABLE PostEvent(UINT32 eventCode, UINT32 sourceId, const char *format, ...);
214BOOL NXCORE_EXPORTABLE PostEventWithNames(UINT32 eventCode, UINT32 sourceId, const char *format, const TCHAR **names, ...);
7806c906 215BOOL NXCORE_EXPORTABLE PostEventWithNames(UINT32 eventCode, UINT32 sourceId, StringMap *parameters);
3ae780c4
VK
216BOOL NXCORE_EXPORTABLE PostEventWithTag(UINT32 eventCode, UINT32 sourceId, const TCHAR *userTag, const char *format, ...);
217BOOL NXCORE_EXPORTABLE PostEventEx(Queue *queue, UINT32 eventCode, UINT32 sourceId, const char *format, ...);
218void NXCORE_EXPORTABLE ResendEvents(Queue *queue);
219
0688e29b
VK
220/**
221 * Global variables
222 */
5039dede
AK
223extern Queue *g_pEventQueue;
224extern EventPolicy *g_pEventPolicy;
35f836fe
VK
225extern const TCHAR *g_szStatusText[];
226extern const TCHAR *g_szStatusTextSmall[];
5039dede
AK
227extern INT64 g_totalEventsProcessed;
228
229#endif /* _nms_events_h_ */