MIB file renamed to follow naming convention; makefile updated
[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
480e036b
VK
114/**
115 * Event policy rule
116 */
5039dede
AK
117class EPRule
118{
119private:
120 DWORD m_dwId;
121 DWORD m_dwFlags;
122 DWORD m_dwNumSources;
123 DWORD *m_pdwSourceList;
124 DWORD m_dwNumEvents;
125 DWORD *m_pdwEventList;
126 DWORD m_dwNumActions;
127 DWORD *m_pdwActionList;
128 TCHAR *m_pszComment;
129 TCHAR *m_pszScript;
130 NXSL_Program *m_pScript;
131
480e036b 132 TCHAR m_szAlarmMessage[MAX_EVENT_MSG_LENGTH];
5039dede
AK
133 int m_iAlarmSeverity;
134 TCHAR m_szAlarmKey[MAX_DB_STRING];
135 DWORD m_dwAlarmTimeout;
136 DWORD m_dwAlarmTimeoutEvent;
137
138 DWORD m_dwSituationId;
139 TCHAR m_szSituationInstance[MAX_DB_STRING];
140 StringMap m_situationAttrList;
141
0688e29b
VK
142 bool matchSource(DWORD dwObjectId);
143 bool matchEvent(DWORD dwEventCode);
144 bool matchSeverity(DWORD dwSeverity);
145 bool matchScript(Event *pEvent);
5039dede 146
0688e29b 147 void generateAlarm(Event *pEvent);
5039dede
AK
148
149public:
150 EPRule(DWORD dwId);
151 EPRule(DB_RESULT hResult, int iRow);
152 EPRule(CSCPMessage *pMsg);
153 ~EPRule();
154
0688e29b
VK
155 DWORD getId() { return m_dwId; }
156 void setId(DWORD dwNewId) { m_dwId = dwNewId; }
157 bool loadFromDB();
158 void saveToDB(DB_HANDLE hdb);
159 bool processEvent(Event *pEvent);
160 void createMessage(CSCPMessage *pMsg);
5039dede 161
0688e29b 162 bool isActionInUse(DWORD dwActionId);
5039dede
AK
163};
164
480e036b
VK
165/**
166 * Event policy
167 */
5039dede
AK
168class EventPolicy
169{
170private:
171 DWORD m_dwNumRules;
172 EPRule **m_ppRuleList;
173 RWLOCK m_rwlock;
174
0688e29b
VK
175 void readLock() { RWLockReadLock(m_rwlock, INFINITE); }
176 void writeLock() { RWLockWriteLock(m_rwlock, INFINITE); }
177 void unlock() { RWLockUnlock(m_rwlock); }
178 void clear();
5039dede
AK
179
180public:
181 EventPolicy();
182 ~EventPolicy();
183
0688e29b
VK
184 DWORD getNumRules() { return m_dwNumRules; }
185 bool loadFromDB();
186 void saveToDB();
187 void processEvent(Event *pEvent);
188 void sendToClient(ClientSession *pSession, DWORD dwRqId);
189 void replacePolicy(DWORD dwNumRules, EPRule **ppRuleList);
5039dede 190
0688e29b 191 bool isActionInUse(DWORD dwActionId);
5039dede
AK
192};
193
0688e29b
VK
194/**
195 * Functions
196 */
197BOOL InitEventSubsystem();
198void ShutdownEventSubsystem();
5039dede 199BOOL PostEvent(DWORD dwEventCode, DWORD dwSourceId, const char *pszFormat, ...);
ed806a1a 200BOOL PostEventWithNames(DWORD dwEventCode, DWORD dwSourceId, const char *pszFormat, const TCHAR **names, ...);
5039dede 201BOOL PostEventWithTag(DWORD dwEventCode, DWORD dwSourceId, const TCHAR *pszUserTag, const char *pszFormat, ...);
0688e29b 202BOOL PostEventEx(Queue *pQueue, DWORD dwEventCode, DWORD dwSourceId, const char *pszFormat, ...);
5039dede 203void ResendEvents(Queue *pQueue);
0688e29b 204void ReloadEvents();
5039dede
AK
205void DeleteEventTemplateFromList(DWORD dwEventCode);
206void CorrelateEvent(Event *pEvent);
207void CreateNXMPEventRecord(String &str, DWORD dwCode);
a65c1819
VK
208BOOL EventNameFromCode(DWORD dwCode, TCHAR *pszBuffer);
209DWORD EventCodeFromName(const TCHAR *name, DWORD defaultValue = 0);
5039dede
AK
210EVENT_TEMPLATE *FindEventTemplateByCode(DWORD dwCode);
211EVENT_TEMPLATE *FindEventTemplateByName(const TCHAR *pszName);
212
0688e29b
VK
213/**
214 * Global variables
215 */
5039dede
AK
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_ */