fixed issue 329
[public/netxms.git] / src / server / include / nms_events.h
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
b4fe4886 3** Copyright (C) 2003-2009 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;
68 DWORD m_dwNumParameters;
69 TCHAR **m_ppszParameters;
70 time_t m_tTimeStamp;
71 TCHAR *m_pszUserTag;
72 TCHAR *m_pszCustomMessage;
73
74public:
75 Event();
76 Event(EVENT_TEMPLATE *pTemplate, DWORD dwSourceId, const TCHAR *pszUserTag, const char *szFormat, va_list args);
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);
5039dede 96
210642a1 97 DWORD getParametersCount() { return m_dwNumParameters; }
35f836fe 98 const TCHAR *getParameter(DWORD index) { return (index < m_dwNumParameters) ? m_ppszParameters[index] : NULL; }
210642a1
VK
99 DWORD getParameterAsULong(DWORD index) { return (index < m_dwNumParameters) ? _tcstoul(m_ppszParameters[index], NULL, 0) : 0; }
100 QWORD getParameterAsUInt64(DWORD index) { return (index < m_dwNumParameters) ? _tcstoull(m_ppszParameters[index], NULL, 0) : 0; }
5039dede 101
210642a1
VK
102 const TCHAR *getCustomMessage() { return CHECK_NULL_EX(m_pszCustomMessage); }
103 void setCustomMessage(const TCHAR *message) { safe_free(m_pszCustomMessage); m_pszCustomMessage = (message != NULL) ? _tcsdup(message) : NULL; }
5039dede
AK
104};
105
106
107//
108// Event policy rule
109//
110
111class EPRule
112{
113private:
114 DWORD m_dwId;
115 DWORD m_dwFlags;
116 DWORD m_dwNumSources;
117 DWORD *m_pdwSourceList;
118 DWORD m_dwNumEvents;
119 DWORD *m_pdwEventList;
120 DWORD m_dwNumActions;
121 DWORD *m_pdwActionList;
122 TCHAR *m_pszComment;
123 TCHAR *m_pszScript;
124 NXSL_Program *m_pScript;
125
126 TCHAR m_szAlarmMessage[MAX_DB_STRING];
127 int m_iAlarmSeverity;
128 TCHAR m_szAlarmKey[MAX_DB_STRING];
129 DWORD m_dwAlarmTimeout;
130 DWORD m_dwAlarmTimeoutEvent;
131
132 DWORD m_dwSituationId;
133 TCHAR m_szSituationInstance[MAX_DB_STRING];
134 StringMap m_situationAttrList;
135
136 BOOL MatchSource(DWORD dwObjectId);
137 BOOL MatchEvent(DWORD dwEventCode);
138 BOOL MatchSeverity(DWORD dwSeverity);
139 BOOL MatchScript(Event *pEvent);
140
141 void GenerateAlarm(Event *pEvent);
142
143public:
144 EPRule(DWORD dwId);
145 EPRule(DB_RESULT hResult, int iRow);
146 EPRule(CSCPMessage *pMsg);
147 ~EPRule();
148
149 DWORD Id(void) { return m_dwId; }
150 void SetId(DWORD dwNewId) { m_dwId = dwNewId; }
151 BOOL LoadFromDB(void);
152 void SaveToDB(void);
153 BOOL ProcessEvent(Event *pEvent);
154 void CreateMessage(CSCPMessage *pMsg);
155
156 BOOL ActionInUse(DWORD dwActionId);
157};
158
159
160//
161// Event policy
162//
163
164class EventPolicy
165{
166private:
167 DWORD m_dwNumRules;
168 EPRule **m_ppRuleList;
169 RWLOCK m_rwlock;
170
171 void ReadLock(void) { RWLockReadLock(m_rwlock, INFINITE); }
172 void WriteLock(void) { RWLockWriteLock(m_rwlock, INFINITE); }
173 void Unlock(void) { RWLockUnlock(m_rwlock); }
174 void Clear(void);
175
176public:
177 EventPolicy();
178 ~EventPolicy();
179
180 DWORD NumRules(void) { return m_dwNumRules; }
181 BOOL LoadFromDB(void);
182 void SaveToDB(void);
183 void ProcessEvent(Event *pEvent);
184 void SendToClient(ClientSession *pSession, DWORD dwRqId);
185 void ReplacePolicy(DWORD dwNumRules, EPRule **ppRuleList);
186
187 BOOL ActionInUse(DWORD dwActionId);
188};
189
190
191//
192// Functions
193//
194
195BOOL InitEventSubsystem(void);
196void ShutdownEventSubsystem(void);
197BOOL PostEvent(DWORD dwEventCode, DWORD dwSourceId, const char *pszFormat, ...);
198BOOL PostEventWithTag(DWORD dwEventCode, DWORD dwSourceId, const TCHAR *pszUserTag, const char *pszFormat, ...);
199BOOL PostEventEx(Queue *pQueue, DWORD dwEventCode, DWORD dwSourceId,
200 const char *pszFormat, ...);
201void ResendEvents(Queue *pQueue);
202void ReloadEvents(void);
203void DeleteEventTemplateFromList(DWORD dwEventCode);
204void CorrelateEvent(Event *pEvent);
205void CreateNXMPEventRecord(String &str, DWORD dwCode);
a65c1819
VK
206BOOL EventNameFromCode(DWORD dwCode, TCHAR *pszBuffer);
207DWORD EventCodeFromName(const TCHAR *name, DWORD defaultValue = 0);
5039dede
AK
208EVENT_TEMPLATE *FindEventTemplateByCode(DWORD dwCode);
209EVENT_TEMPLATE *FindEventTemplateByName(const TCHAR *pszName);
210
211
212//
213// Global variables
214//
215
216extern Queue *g_pEventQueue;
217extern EventPolicy *g_pEventPolicy;
35f836fe
VK
218extern const TCHAR *g_szStatusText[];
219extern const TCHAR *g_szStatusTextSmall[];
5039dede
AK
220extern INT64 g_totalEventsProcessed;
221
222#endif /* _nms_events_h_ */