fixed errors in nxdevcfg
[public/netxms.git] / src / server / include / nms_events.h
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
feab3324 3** Copyright (C) 2003-2012 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
AK
35
36
37//
38// Event template
39//
40
41struct EVENT_TEMPLATE
42{
43 DWORD dwCode;
44 DWORD dwSeverity;
45 DWORD dwFlags;
46 TCHAR *pszMessageTemplate;
47 TCHAR *pszDescription;
48 TCHAR szName[MAX_EVENT_NAME];
49};
50
51
52//
53// Event
54//
55
56class Event
57{
58private:
59 QWORD m_qwId;
60 QWORD m_qwRootId; // Root event id
61 DWORD m_dwCode;
62 DWORD m_dwSeverity;
63 DWORD m_dwFlags;
64 DWORD m_dwSource;
b4fe4886 65 TCHAR m_szName[MAX_EVENT_NAME];
5039dede
AK
66 TCHAR *m_pszMessageText;
67 TCHAR *m_pszMessageTemplate;
5039dede
AK
68 time_t m_tTimeStamp;
69 TCHAR *m_pszUserTag;
70 TCHAR *m_pszCustomMessage;
ed806a1a
VK
71 Array m_parameters;
72 StringList m_parameterNames;
5039dede
AK
73
74public:
75 Event();
ed806a1a 76 Event(EVENT_TEMPLATE *pTemplate, DWORD dwSourceId, const TCHAR *pszUserTag, const char *szFormat, const TCHAR **names, va_list args);
5039dede
AK
77 ~Event();
78
210642a1
VK
79 QWORD getId() { return m_qwId; }
80 DWORD getCode() { return m_dwCode; }
81 DWORD getSeverity() { return m_dwSeverity; }
82 DWORD getFlags() { return m_dwFlags; }
83 DWORD getSourceId() { return m_dwSource; }
84 const TCHAR *getName() { return m_szName; }
85 const TCHAR *getMessage() { return m_pszMessageText; }
86 const TCHAR *getUserTag() { return m_pszUserTag; }
87 time_t getTimeStamp() { return m_tTimeStamp; }
5039dede 88
210642a1
VK
89 QWORD getRootId() { return m_qwRootId; }
90 void setRootId(QWORD qwId) { m_qwRootId = qwId; }
5039dede 91
210642a1 92 void prepareMessage(CSCPMessage *pMsg);
5039dede 93
210642a1 94 void expandMessageText();
fb986055 95 TCHAR *expandText(const TCHAR *szTemplate, const TCHAR *pszAlarmMsg = NULL);
6c984ccc 96 static TCHAR *expandText(Event *event, DWORD sourceObject, const TCHAR *szTemplate, const TCHAR *pszAlarmMsg);
5039dede 97
ed806a1a
VK
98 DWORD getParametersCount() { return m_parameters.size(); }
99 const TCHAR *getParameter(int index) { return (TCHAR *)m_parameters.get(index); }
100 DWORD getParameterAsULong(int index) { const TCHAR *v = (TCHAR *)m_parameters.get(index); return (v != NULL) ? _tcstoul(v, NULL, 0) : 0; }
101 QWORD getParameterAsUInt64(int index) { const TCHAR *v = (TCHAR *)m_parameters.get(index); return (v != NULL) ? _tcstoull(v, NULL, 0) : 0; }
102
103 const TCHAR *getNamedParameter(const TCHAR *name) { return getParameter(m_parameterNames.getIndexIgnoreCase(name)); }
104 DWORD getNamedParameterAsULong(const TCHAR *name) { return getParameterAsULong(m_parameterNames.getIndexIgnoreCase(name)); }
105 QWORD getNamedParameterAsUInt64(const TCHAR *name) { return getParameterAsUInt64(m_parameterNames.getIndexIgnoreCase(name)); }
106
107 void addParameter(const TCHAR *name, const TCHAR *value);
24dc5346 108 void setNamedParameter(const TCHAR *name, const TCHAR *value);
ed806a1a 109
210642a1
VK
110 const TCHAR *getCustomMessage() { return CHECK_NULL_EX(m_pszCustomMessage); }
111 void setCustomMessage(const TCHAR *message) { safe_free(m_pszCustomMessage); m_pszCustomMessage = (message != NULL) ? _tcsdup(message) : NULL; }
5039dede
AK
112};
113
114
115//
116// Event policy rule
117//
118
119class EPRule
120{
121private:
122 DWORD m_dwId;
123 DWORD m_dwFlags;
124 DWORD m_dwNumSources;
125 DWORD *m_pdwSourceList;
126 DWORD m_dwNumEvents;
127 DWORD *m_pdwEventList;
128 DWORD m_dwNumActions;
129 DWORD *m_pdwActionList;
130 TCHAR *m_pszComment;
131 TCHAR *m_pszScript;
132 NXSL_Program *m_pScript;
133
134 TCHAR m_szAlarmMessage[MAX_DB_STRING];
135 int m_iAlarmSeverity;
136 TCHAR m_szAlarmKey[MAX_DB_STRING];
137 DWORD m_dwAlarmTimeout;
138 DWORD m_dwAlarmTimeoutEvent;
139
140 DWORD m_dwSituationId;
141 TCHAR m_szSituationInstance[MAX_DB_STRING];
142 StringMap m_situationAttrList;
143
144 BOOL MatchSource(DWORD dwObjectId);
145 BOOL MatchEvent(DWORD dwEventCode);
146 BOOL MatchSeverity(DWORD dwSeverity);
147 BOOL MatchScript(Event *pEvent);
148
149 void GenerateAlarm(Event *pEvent);
150
151public:
152 EPRule(DWORD dwId);
153 EPRule(DB_RESULT hResult, int iRow);
154 EPRule(CSCPMessage *pMsg);
155 ~EPRule();
156
157 DWORD Id(void) { return m_dwId; }
158 void SetId(DWORD dwNewId) { m_dwId = dwNewId; }
159 BOOL LoadFromDB(void);
160 void SaveToDB(void);
161 BOOL ProcessEvent(Event *pEvent);
162 void CreateMessage(CSCPMessage *pMsg);
163
164 BOOL ActionInUse(DWORD dwActionId);
165};
166
167
168//
169// Event policy
170//
171
172class EventPolicy
173{
174private:
175 DWORD m_dwNumRules;
176 EPRule **m_ppRuleList;
177 RWLOCK m_rwlock;
178
179 void ReadLock(void) { RWLockReadLock(m_rwlock, INFINITE); }
180 void WriteLock(void) { RWLockWriteLock(m_rwlock, INFINITE); }
181 void Unlock(void) { RWLockUnlock(m_rwlock); }
182 void Clear(void);
183
184public:
185 EventPolicy();
186 ~EventPolicy();
187
188 DWORD NumRules(void) { return m_dwNumRules; }
189 BOOL LoadFromDB(void);
190 void SaveToDB(void);
191 void ProcessEvent(Event *pEvent);
192 void SendToClient(ClientSession *pSession, DWORD dwRqId);
193 void ReplacePolicy(DWORD dwNumRules, EPRule **ppRuleList);
194
195 BOOL ActionInUse(DWORD dwActionId);
196};
197
198
199//
200// Functions
201//
202
203BOOL InitEventSubsystem(void);
204void ShutdownEventSubsystem(void);
205BOOL PostEvent(DWORD dwEventCode, DWORD dwSourceId, const char *pszFormat, ...);
ed806a1a 206BOOL PostEventWithNames(DWORD dwEventCode, DWORD dwSourceId, const char *pszFormat, const TCHAR **names, ...);
5039dede
AK
207BOOL PostEventWithTag(DWORD dwEventCode, DWORD dwSourceId, const TCHAR *pszUserTag, const char *pszFormat, ...);
208BOOL PostEventEx(Queue *pQueue, DWORD dwEventCode, DWORD dwSourceId,
209 const char *pszFormat, ...);
210void ResendEvents(Queue *pQueue);
211void ReloadEvents(void);
212void DeleteEventTemplateFromList(DWORD dwEventCode);
213void CorrelateEvent(Event *pEvent);
214void CreateNXMPEventRecord(String &str, DWORD dwCode);
a65c1819
VK
215BOOL EventNameFromCode(DWORD dwCode, TCHAR *pszBuffer);
216DWORD EventCodeFromName(const TCHAR *name, DWORD defaultValue = 0);
5039dede
AK
217EVENT_TEMPLATE *FindEventTemplateByCode(DWORD dwCode);
218EVENT_TEMPLATE *FindEventTemplateByName(const TCHAR *pszName);
219
220
221//
222// Global variables
223//
224
225extern Queue *g_pEventQueue;
226extern EventPolicy *g_pEventPolicy;
35f836fe
VK
227extern const TCHAR *g_szStatusText[];
228extern const TCHAR *g_szStatusTextSmall[];
5039dede
AK
229extern INT64 g_totalEventsProcessed;
230
231#endif /* _nms_events_h_ */