3b8468f2b70c8a854c278484fff518613594b3d0
[public/netxms.git] / src / server / include / nms_events.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2013 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 ** File: 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 EVENTLOG_MAX_MESSAGE_SIZE 255
34 #define EVENTLOG_MAX_USERTAG_SIZE 63
35
36 /**
37 * Event template
38 */
39 struct EVENT_TEMPLATE
40 {
41 UINT32 dwCode;
42 UINT32 dwSeverity;
43 UINT32 dwFlags;
44 TCHAR *pszMessageTemplate;
45 TCHAR *pszDescription;
46 TCHAR szName[MAX_EVENT_NAME];
47 };
48
49 /**
50 * Event
51 */
52 class NXCORE_EXPORTABLE Event
53 {
54 private:
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;
61 TCHAR m_szName[MAX_EVENT_NAME];
62 TCHAR *m_pszMessageText;
63 TCHAR *m_pszMessageTemplate;
64 time_t m_tTimeStamp;
65 TCHAR *m_pszUserTag;
66 TCHAR *m_pszCustomMessage;
67 Array m_parameters;
68 StringList m_parameterNames;
69
70 public:
71 Event();
72 Event(Event *src);
73 Event(EVENT_TEMPLATE *pTemplate, UINT32 sourceId, const TCHAR *userTag, const char *format, const TCHAR **names, va_list args);
74 ~Event();
75
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; }
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; }
85
86 UINT64 getRootId() { return m_qwRootId; }
87 void setRootId(UINT64 qwId) { m_qwRootId = qwId; }
88
89 void prepareMessage(CSCPMessage *pMsg);
90
91 void expandMessageText();
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);
94
95 UINT32 getParametersCount() { return m_parameters.size(); }
96 const TCHAR *getParameter(int index) { return (TCHAR *)m_parameters.get(index); }
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; }
99
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)); }
103
104 void addParameter(const TCHAR *name, const TCHAR *value);
105 void setNamedParameter(const TCHAR *name, const TCHAR *value);
106 void setParameter(int index, const TCHAR *name, const TCHAR *value);
107
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; }
110 };
111
112 /**
113 * Event policy rule
114 */
115 class EPRule
116 {
117 private:
118 UINT32 m_dwId;
119 uuid_t m_guid;
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;
127 TCHAR *m_pszComment;
128 TCHAR *m_pszScript;
129 NXSL_VM *m_pScript;
130
131 TCHAR m_szAlarmMessage[MAX_EVENT_MSG_LENGTH];
132 int m_iAlarmSeverity;
133 TCHAR m_szAlarmKey[MAX_DB_STRING];
134 UINT32 m_dwAlarmTimeout;
135 UINT32 m_dwAlarmTimeoutEvent;
136
137 UINT32 m_dwSituationId;
138 TCHAR m_szSituationInstance[MAX_DB_STRING];
139 StringMap m_situationAttrList;
140
141 bool matchSource(UINT32 dwObjectId);
142 bool matchEvent(UINT32 eventCode);
143 bool matchSeverity(UINT32 dwSeverity);
144 bool matchScript(Event *pEvent);
145
146 void generateAlarm(Event *pEvent);
147
148 public:
149 EPRule(UINT32 dwId);
150 EPRule(DB_RESULT hResult, int iRow);
151 EPRule(CSCPMessage *pMsg);
152 EPRule(ConfigEntry *config);
153 ~EPRule();
154
155 UINT32 getId() { return m_dwId; }
156 BYTE *getGuid() { return m_guid; }
157 void setId(UINT32 dwNewId) { m_dwId = dwNewId; }
158 bool loadFromDB();
159 void saveToDB(DB_HANDLE hdb);
160 bool processEvent(Event *pEvent);
161 void createMessage(CSCPMessage *pMsg);
162 void createNXMPRecord(String &str);
163
164 bool isActionInUse(UINT32 dwActionId);
165 };
166
167 /**
168 * Event policy
169 */
170 class EventPolicy
171 {
172 private:
173 UINT32 m_dwNumRules;
174 EPRule **m_ppRuleList;
175 RWLOCK m_rwlock;
176
177 void readLock() { RWLockReadLock(m_rwlock, INFINITE); }
178 void writeLock() { RWLockWriteLock(m_rwlock, INFINITE); }
179 void unlock() { RWLockUnlock(m_rwlock); }
180 void clear();
181
182 public:
183 EventPolicy();
184 ~EventPolicy();
185
186 UINT32 getNumRules() { return m_dwNumRules; }
187 bool loadFromDB();
188 void saveToDB();
189 void processEvent(Event *pEvent);
190 void sendToClient(ClientSession *pSession, UINT32 dwRqId);
191 void replacePolicy(UINT32 dwNumRules, EPRule **ppRuleList);
192 void exportRule(String &str, uuid_t guid);
193 void importRule(EPRule *rule);
194
195 bool isActionInUse(UINT32 dwActionId);
196 };
197
198 /**
199 * Functions
200 */
201 BOOL InitEventSubsystem();
202 void ShutdownEventSubsystem();
203 void ReloadEvents();
204 void DeleteEventTemplateFromList(UINT32 eventCode);
205 void CorrelateEvent(Event *pEvent);
206 void CreateNXMPEventRecord(String &str, UINT32 eventCode);
207
208 BOOL EventNameFromCode(UINT32 eventCode, TCHAR *pszBuffer);
209 UINT32 EventCodeFromName(const TCHAR *name, UINT32 defaultValue = 0);
210 EVENT_TEMPLATE *FindEventTemplateByCode(UINT32 eventCode);
211 EVENT_TEMPLATE *FindEventTemplateByName(const TCHAR *pszName);
212
213 BOOL NXCORE_EXPORTABLE PostEvent(UINT32 eventCode, UINT32 sourceId, const char *format, ...);
214 BOOL NXCORE_EXPORTABLE PostEventWithNames(UINT32 eventCode, UINT32 sourceId, const char *format, const TCHAR **names, ...);
215 BOOL NXCORE_EXPORTABLE PostEventWithTag(UINT32 eventCode, UINT32 sourceId, const TCHAR *userTag, const char *format, ...);
216 BOOL NXCORE_EXPORTABLE PostEventEx(Queue *queue, UINT32 eventCode, UINT32 sourceId, const char *format, ...);
217 void NXCORE_EXPORTABLE ResendEvents(Queue *queue);
218
219 /**
220 * Global variables
221 */
222 extern Queue *g_pEventQueue;
223 extern EventPolicy *g_pEventPolicy;
224 extern const TCHAR *g_szStatusText[];
225 extern const TCHAR *g_szStatusTextSmall[];
226 extern INT64 g_totalEventsProcessed;
227
228 #endif /* _nms_events_h_ */