Rollback from r3608 to r3606
[public/netxms.git] / src / server / include / nms_events.h
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
3** Copyright (C) 2003 Victor Kirhenshtein
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**
19** $module: nms_events.h
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
33#define FIRST_USER_EVENT_ID 100000
34
35
36//
37// Event template
38//
39
40struct EVENT_TEMPLATE
41{
42 DWORD dwCode;
43 DWORD dwSeverity;
44 DWORD dwFlags;
45 TCHAR *pszMessageTemplate;
46 TCHAR *pszDescription;
47 TCHAR szName[MAX_EVENT_NAME];
48};
49
50
51//
52// Event
53//
54
55class Event
56{
57private:
58 QWORD m_qwId;
59 QWORD m_qwRootId; // Root event id
60 DWORD m_dwCode;
61 DWORD m_dwSeverity;
62 DWORD m_dwFlags;
63 DWORD m_dwSource;
64 TCHAR *m_pszMessageText;
65 TCHAR *m_pszMessageTemplate;
66 DWORD m_dwNumParameters;
67 TCHAR **m_ppszParameters;
68 time_t m_tTimeStamp;
69 TCHAR *m_pszUserTag;
70 TCHAR *m_pszCustomMessage;
71
72public:
73 Event();
74 Event(EVENT_TEMPLATE *pTemplate, DWORD dwSourceId, const TCHAR *pszUserTag, const char *szFormat, va_list args);
75 ~Event();
76
77 QWORD Id(void) { return m_qwId; }
78 DWORD Code(void) { return m_dwCode; }
79 DWORD Severity(void) { return m_dwSeverity; }
80 DWORD Flags(void) { return m_dwFlags; }
81 DWORD SourceId(void) { return m_dwSource; }
82 const char *Message(void) { return m_pszMessageText; }
83 const char *UserTag(void) { return m_pszUserTag; }
84 time_t TimeStamp(void) { return m_tTimeStamp; }
85
86 QWORD GetRootId(void) { return m_qwRootId; }
87 void SetRootId(QWORD qwId) { m_qwRootId = qwId; }
88
89 void PrepareMessage(CSCPMessage *pMsg);
90
91 void ExpandMessageText(void);
92 TCHAR *ExpandText(TCHAR *szTemplate, TCHAR *pszAlarmMsg = NULL);
93
94 DWORD GetParametersCount(void) { return m_dwNumParameters; }
95 char *GetParameter(DWORD dwIndex) { return (dwIndex < m_dwNumParameters) ? m_ppszParameters[dwIndex] : NULL; }
96 DWORD GetParameterAsULong(DWORD dwIndex) { return (dwIndex < m_dwNumParameters) ? _tcstoul(m_ppszParameters[dwIndex], NULL, 0) : 0; }
97 QWORD GetParameterAsUInt64(DWORD dwIndex) { return (dwIndex < m_dwNumParameters) ? _tcstoull(m_ppszParameters[dwIndex], NULL, 0) : 0; }
98
99 void SetCustomMessage(const TCHAR *pszMessage) { safe_free(m_pszCustomMessage); m_pszCustomMessage = (pszMessage != NULL) ? _tcsdup(pszMessage) : NULL; }
100};
101
102
103//
104// Event policy rule
105//
106
107class EPRule
108{
109private:
110 DWORD m_dwId;
111 DWORD m_dwFlags;
112 DWORD m_dwNumSources;
113 DWORD *m_pdwSourceList;
114 DWORD m_dwNumEvents;
115 DWORD *m_pdwEventList;
116 DWORD m_dwNumActions;
117 DWORD *m_pdwActionList;
118 TCHAR *m_pszComment;
119 TCHAR *m_pszScript;
120 NXSL_Program *m_pScript;
121
122 TCHAR m_szAlarmMessage[MAX_DB_STRING];
123 int m_iAlarmSeverity;
124 TCHAR m_szAlarmKey[MAX_DB_STRING];
125 DWORD m_dwAlarmTimeout;
126 DWORD m_dwAlarmTimeoutEvent;
127
128 DWORD m_dwSituationId;
129 TCHAR m_szSituationInstance[MAX_DB_STRING];
130 StringMap m_situationAttrList;
131
132 BOOL MatchSource(DWORD dwObjectId);
133 BOOL MatchEvent(DWORD dwEventCode);
134 BOOL MatchSeverity(DWORD dwSeverity);
135 BOOL MatchScript(Event *pEvent);
136
137 void GenerateAlarm(Event *pEvent);
138
139public:
140 EPRule(DWORD dwId);
141 EPRule(DB_RESULT hResult, int iRow);
142 EPRule(CSCPMessage *pMsg);
143 ~EPRule();
144
145 DWORD Id(void) { return m_dwId; }
146 void SetId(DWORD dwNewId) { m_dwId = dwNewId; }
147 BOOL LoadFromDB(void);
148 void SaveToDB(void);
149 BOOL ProcessEvent(Event *pEvent);
150 void CreateMessage(CSCPMessage *pMsg);
151
152 BOOL ActionInUse(DWORD dwActionId);
153};
154
155
156//
157// Event policy
158//
159
160class EventPolicy
161{
162private:
163 DWORD m_dwNumRules;
164 EPRule **m_ppRuleList;
165 RWLOCK m_rwlock;
166
167 void ReadLock(void) { RWLockReadLock(m_rwlock, INFINITE); }
168 void WriteLock(void) { RWLockWriteLock(m_rwlock, INFINITE); }
169 void Unlock(void) { RWLockUnlock(m_rwlock); }
170 void Clear(void);
171
172public:
173 EventPolicy();
174 ~EventPolicy();
175
176 DWORD NumRules(void) { return m_dwNumRules; }
177 BOOL LoadFromDB(void);
178 void SaveToDB(void);
179 void ProcessEvent(Event *pEvent);
180 void SendToClient(ClientSession *pSession, DWORD dwRqId);
181 void ReplacePolicy(DWORD dwNumRules, EPRule **ppRuleList);
182
183 BOOL ActionInUse(DWORD dwActionId);
184};
185
186
187//
188// Functions
189//
190
191BOOL InitEventSubsystem(void);
192void ShutdownEventSubsystem(void);
193BOOL PostEvent(DWORD dwEventCode, DWORD dwSourceId, const char *pszFormat, ...);
194BOOL PostEventWithTag(DWORD dwEventCode, DWORD dwSourceId, const TCHAR *pszUserTag, const char *pszFormat, ...);
195BOOL PostEventEx(Queue *pQueue, DWORD dwEventCode, DWORD dwSourceId,
196 const char *pszFormat, ...);
197void ResendEvents(Queue *pQueue);
198void ReloadEvents(void);
199void DeleteEventTemplateFromList(DWORD dwEventCode);
200void CorrelateEvent(Event *pEvent);
201void CreateNXMPEventRecord(String &str, DWORD dwCode);
202BOOL ResolveEventName(DWORD dwCode, TCHAR *pszBuffer);
203EVENT_TEMPLATE *FindEventTemplateByCode(DWORD dwCode);
204EVENT_TEMPLATE *FindEventTemplateByName(const TCHAR *pszName);
205
206
207//
208// Global variables
209//
210
211extern Queue *g_pEventQueue;
212extern EventPolicy *g_pEventPolicy;
213extern const char *g_szStatusText[];
214extern const char *g_szStatusTextSmall[];
215extern INT64 g_totalEventsProcessed;
216
217#endif /* _nms_events_h_ */