write audit log call variant which accepts json objects; old/new value logging for...
[public/netxms.git] / src / server / include / nms_events.h
CommitLineData
6a6b96c0 1/*
5039dede 2** NetXMS - Network Management System
967893bb 3** Copyright (C) 2003-2013 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>
0d402aa8 27#include <jansson.h>
5039dede
AK
28
29
30//
31// Constants
32//
33
9f6712bc
VK
34#define EVENTLOG_MAX_MESSAGE_SIZE 255
35#define EVENTLOG_MAX_USERTAG_SIZE 63
5039dede 36
46ee6286
VK
37/**
38 * Event template
39 */
50963ced 40class EventTemplate : public RefCountObject
5039dede 41{
50963ced
VK
42private:
43 UINT32 m_code;
44 int m_severity;
45 uuid m_guid;
46 TCHAR m_name[MAX_EVENT_NAME];
47 UINT32 m_flags;
48 TCHAR *m_messageTemplate;
49 TCHAR *m_description;
50
51protected:
52 virtual ~EventTemplate();
53
54public:
55 EventTemplate(DB_RESULT hResult, int row);
56
57 UINT32 getCode() const { return m_code; }
58 int getSeverity() const { return m_severity; }
59 const uuid& getGuid() const { return m_guid; }
60 const TCHAR *getName() const { return m_name; }
61 UINT32 getFlags() const { return m_flags; }
62 const TCHAR *getMessageTemplate() const { return m_messageTemplate; }
63 const TCHAR *getDescription() const { return m_description; }
0d402aa8
VK
64
65 json_t *toJson() const;
5039dede
AK
66};
67
46ee6286
VK
68/**
69 * Event
70 */
b78b8d2b 71class NXCORE_EXPORTABLE Event
5039dede
AK
72{
73private:
65ce7452
VK
74 UINT64 m_id;
75 UINT64 m_rootId; // Root event id
76 UINT32 m_code;
ad13c0e2 77 int m_severity;
65ce7452
VK
78 UINT32 m_flags;
79 UINT32 m_sourceId;
80 UINT32 m_dciId;
c42b4551 81 TCHAR m_name[MAX_EVENT_NAME];
65ce7452
VK
82 TCHAR *m_messageText;
83 TCHAR *m_messageTemplate;
84 time_t m_timeStamp;
85 TCHAR *m_userTag;
86 TCHAR *m_customMessage;
ed806a1a
VK
87 Array m_parameters;
88 StringList m_parameterNames;
5039dede
AK
89
90public:
91 Event();
65ce7452 92 Event(const Event *src);
50963ced 93 Event(const EventTemplate *eventTemplate, UINT32 sourceId, UINT32 dciId, const TCHAR *userTag, const char *format, const TCHAR **names, va_list args);
5039dede
AK
94 ~Event();
95
65ce7452
VK
96 UINT64 getId() const { return m_id; }
97 UINT32 getCode() const { return m_code; }
98 UINT32 getSeverity() const { return m_severity; }
99 UINT32 getFlags() const { return m_flags; }
100 UINT32 getSourceId() const { return m_sourceId; }
101 UINT32 getDciId() const { return m_dciId; }
102 const TCHAR *getName() const { return m_name; }
103 const TCHAR *getMessage() const { return m_messageText; }
104 const TCHAR *getUserTag() const { return m_userTag; }
105 time_t getTimeStamp() const { return m_timeStamp; }
ad13c0e2
VK
106
107 void setSeverity(int severity) { m_severity = severity; }
6a6b96c0 108
65ce7452
VK
109 UINT64 getRootId() const { return m_rootId; }
110 void setRootId(UINT64 id) { m_rootId = id; }
5039dede 111
65ce7452 112 void prepareMessage(NXCPMessage *msg) const;
5039dede 113
210642a1 114 void expandMessageText();
ca0e18d2
VK
115 TCHAR *expandText(const TCHAR *textTemplate, const TCHAR *alarmMsg = NULL, const TCHAR *alarmKey = NULL);
116 static TCHAR *expandText(Event *event, UINT32 sourceObject, const TCHAR *textTemplate, const TCHAR *alarmMsg, const TCHAR *alarmKey);
65ce7452
VK
117 void setMessage(const TCHAR *text) { free(m_messageText); m_messageText = _tcsdup_ex(text); }
118 void setUserTag(const TCHAR *text) { free(m_userTag); m_userTag = _tcsdup_ex(text); }
5039dede 119
4058f716 120 int getParametersCount() const { return m_parameters.size(); }
65ce7452
VK
121 const TCHAR *getParameter(int index) const { return (TCHAR *)m_parameters.get(index); }
122 UINT32 getParameterAsULong(int index) const { const TCHAR *v = (TCHAR *)m_parameters.get(index); return (v != NULL) ? _tcstoul(v, NULL, 0) : 0; }
123 UINT64 getParameterAsUInt64(int index) const { const TCHAR *v = (TCHAR *)m_parameters.get(index); return (v != NULL) ? _tcstoull(v, NULL, 0) : 0; }
ed806a1a 124
65ce7452
VK
125 const TCHAR *getNamedParameter(const TCHAR *name) const { return getParameter(m_parameterNames.indexOfIgnoreCase(name)); }
126 UINT32 getNamedParameterAsULong(const TCHAR *name) const { return getParameterAsULong(m_parameterNames.indexOfIgnoreCase(name)); }
127 UINT64 getNamedParameterAsUInt64(const TCHAR *name) const { return getParameterAsUInt64(m_parameterNames.indexOfIgnoreCase(name)); }
ed806a1a
VK
128
129 void addParameter(const TCHAR *name, const TCHAR *value);
24dc5346 130 void setNamedParameter(const TCHAR *name, const TCHAR *value);
d5cf7cda 131 void setParameter(int index, const TCHAR *name, const TCHAR *value);
ed806a1a 132
65ce7452
VK
133 const TCHAR *getCustomMessage() const { return CHECK_NULL_EX(m_customMessage); }
134 void setCustomMessage(const TCHAR *message) { free(m_customMessage); m_customMessage = _tcsdup_ex(message); }
0356b995
VK
135
136 String createJson();
5039dede
AK
137};
138
00420032 139/**
140 * Defines for type of persistent storage action
141 */
142 #define PSTORAGE_SET 1
143 #define PSTORAGE_DELETE 2
144
480e036b
VK
145/**
146 * Event policy rule
147 */
5039dede
AK
148class EPRule
149{
150private:
c42b4551 151 UINT32 m_id;
de4af576 152 uuid m_guid;
967893bb
VK
153 UINT32 m_dwFlags;
154 UINT32 m_dwNumSources;
155 UINT32 *m_pdwSourceList;
156 UINT32 m_dwNumEvents;
157 UINT32 *m_pdwEventList;
158 UINT32 m_dwNumActions;
159 UINT32 *m_pdwActionList;
5039dede
AK
160 TCHAR *m_pszComment;
161 TCHAR *m_pszScript;
6b29839d 162 NXSL_VM *m_pScript;
5039dede 163
480e036b 164 TCHAR m_szAlarmMessage[MAX_EVENT_MSG_LENGTH];
5039dede
AK
165 int m_iAlarmSeverity;
166 TCHAR m_szAlarmKey[MAX_DB_STRING];
967893bb
VK
167 UINT32 m_dwAlarmTimeout;
168 UINT32 m_dwAlarmTimeoutEvent;
6a6b96c0 169 IntegerArray<UINT32> *m_alarmCategoryList;
00420032 170 StringMap m_pstorageSetActions;
171 StringList m_pstorageDeleteActions;
5039dede 172
967893bb 173 bool matchSource(UINT32 dwObjectId);
b8ad978c 174 bool matchEvent(UINT32 eventCode);
967893bb 175 bool matchSeverity(UINT32 dwSeverity);
0688e29b 176 bool matchScript(Event *pEvent);
5039dede 177
0688e29b 178 void generateAlarm(Event *pEvent);
5039dede
AK
179
180public:
c42b4551
VK
181 EPRule(UINT32 id);
182 EPRule(DB_RESULT hResult, int row);
b368969c 183 EPRule(NXCPMessage *msg);
2292ff1d 184 EPRule(ConfigEntry *config);
5039dede
AK
185 ~EPRule();
186
de4af576
VK
187 UINT32 getId() const { return m_id; }
188 const uuid& getGuid() const { return m_guid; }
c42b4551 189 void setId(UINT32 dwNewId) { m_id = dwNewId; }
9bd1bace 190 bool loadFromDB(DB_HANDLE hdb);
00420032 191 bool saveToDB(DB_HANDLE hdb);
0688e29b 192 bool processEvent(Event *pEvent);
b368969c 193 void createMessage(NXCPMessage *pMsg);
badf9a95 194 void createNXMPRecord(String &str);
5039dede 195
967893bb 196 bool isActionInUse(UINT32 dwActionId);
739d4e24 197 bool isCategoryInUse(UINT32 categoryId) const { return m_alarmCategoryList->contains(categoryId); }
5039dede
AK
198};
199
480e036b
VK
200/**
201 * Event policy
202 */
5039dede
AK
203class EventPolicy
204{
205private:
967893bb 206 UINT32 m_dwNumRules;
5039dede
AK
207 EPRule **m_ppRuleList;
208 RWLOCK m_rwlock;
209
0688e29b
VK
210 void readLock() { RWLockReadLock(m_rwlock, INFINITE); }
211 void writeLock() { RWLockWriteLock(m_rwlock, INFINITE); }
212 void unlock() { RWLockUnlock(m_rwlock); }
213 void clear();
5039dede
AK
214
215public:
216 EventPolicy();
217 ~EventPolicy();
218
967893bb 219 UINT32 getNumRules() { return m_dwNumRules; }
0688e29b 220 bool loadFromDB();
00420032 221 bool saveToDB();
0688e29b 222 void processEvent(Event *pEvent);
967893bb
VK
223 void sendToClient(ClientSession *pSession, UINT32 dwRqId);
224 void replacePolicy(UINT32 dwNumRules, EPRule **ppRuleList);
de4af576 225 void exportRule(String& str, const uuid& guid);
c4a6167c 226 void importRule(EPRule *rule);
6a6b96c0 227 void removeRuleCategory (UINT32 categoryId);
5039dede 228
967893bb 229 bool isActionInUse(UINT32 dwActionId);
6a6b96c0 230 bool isCategoryInUse(UINT32 dwCategoryId);
5039dede
AK
231};
232
0688e29b
VK
233/**
234 * Functions
235 */
236BOOL InitEventSubsystem();
237void ShutdownEventSubsystem();
0688e29b 238void ReloadEvents();
b8ad978c 239void DeleteEventTemplateFromList(UINT32 eventCode);
5039dede 240void CorrelateEvent(Event *pEvent);
b8ad978c 241void CreateNXMPEventRecord(String &str, UINT32 eventCode);
3ae780c4 242
50963ced 243bool EventNameFromCode(UINT32 eventCode, TCHAR *buffer);
6fec127d 244UINT32 NXCORE_EXPORTABLE EventCodeFromName(const TCHAR *name, UINT32 defaultValue = 0);
50963ced
VK
245EventTemplate *FindEventTemplateByCode(UINT32 eventCode);
246EventTemplate *FindEventTemplateByName(const TCHAR *pszName);
5039dede 247
65ce7452
VK
248bool NXCORE_EXPORTABLE PostEvent(UINT32 eventCode, UINT32 sourceId, const char *format, ...);
249bool NXCORE_EXPORTABLE PostDciEvent(UINT32 eventCode, UINT32 sourceId, UINT32 dciId, const char *format, ...);
7e18667a 250UINT64 NXCORE_EXPORTABLE PostEvent2(UINT32 eventCode, UINT32 sourceId, const char *format, ...);
65ce7452
VK
251bool NXCORE_EXPORTABLE PostEventWithNames(UINT32 eventCode, UINT32 sourceId, const char *format, const TCHAR **names, ...);
252bool NXCORE_EXPORTABLE PostEventWithNames(UINT32 eventCode, UINT32 sourceId, StringMap *parameters);
253bool NXCORE_EXPORTABLE PostDciEventWithNames(UINT32 eventCode, UINT32 sourceId, UINT32 dciId, const char *format, const TCHAR **names, ...);
254bool NXCORE_EXPORTABLE PostDciEventWithNames(UINT32 eventCode, UINT32 sourceId, UINT32 dciId, StringMap *parameters);
255bool NXCORE_EXPORTABLE PostEventWithTagAndNames(UINT32 eventCode, UINT32 sourceId, const TCHAR *userTag, const char *format, const TCHAR **names, ...);
256bool NXCORE_EXPORTABLE PostEventWithTag(UINT32 eventCode, UINT32 sourceId, const TCHAR *userTag, const char *format, ...);
257bool NXCORE_EXPORTABLE PostEventEx(Queue *queue, UINT32 eventCode, UINT32 sourceId, const char *format, ...);
3ae780c4
VK
258void NXCORE_EXPORTABLE ResendEvents(Queue *queue);
259
6fec127d
VK
260const TCHAR NXCORE_EXPORTABLE *GetStatusAsText(int status, bool allCaps);
261
0688e29b
VK
262/**
263 * Global variables
264 */
5039dede
AK
265extern Queue *g_pEventQueue;
266extern EventPolicy *g_pEventPolicy;
5039dede
AK
267extern INT64 g_totalEventsProcessed;
268
269#endif /* _nms_events_h_ */