write audit log call variant which accepts json objects; old/new value logging for...
[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 #include <jansson.h>
28
29
30 //
31 // Constants
32 //
33
34 #define EVENTLOG_MAX_MESSAGE_SIZE 255
35 #define EVENTLOG_MAX_USERTAG_SIZE 63
36
37 /**
38 * Event template
39 */
40 class EventTemplate : public RefCountObject
41 {
42 private:
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
51 protected:
52 virtual ~EventTemplate();
53
54 public:
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; }
64
65 json_t *toJson() const;
66 };
67
68 /**
69 * Event
70 */
71 class NXCORE_EXPORTABLE Event
72 {
73 private:
74 UINT64 m_id;
75 UINT64 m_rootId; // Root event id
76 UINT32 m_code;
77 int m_severity;
78 UINT32 m_flags;
79 UINT32 m_sourceId;
80 UINT32 m_dciId;
81 TCHAR m_name[MAX_EVENT_NAME];
82 TCHAR *m_messageText;
83 TCHAR *m_messageTemplate;
84 time_t m_timeStamp;
85 TCHAR *m_userTag;
86 TCHAR *m_customMessage;
87 Array m_parameters;
88 StringList m_parameterNames;
89
90 public:
91 Event();
92 Event(const Event *src);
93 Event(const EventTemplate *eventTemplate, UINT32 sourceId, UINT32 dciId, const TCHAR *userTag, const char *format, const TCHAR **names, va_list args);
94 ~Event();
95
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; }
106
107 void setSeverity(int severity) { m_severity = severity; }
108
109 UINT64 getRootId() const { return m_rootId; }
110 void setRootId(UINT64 id) { m_rootId = id; }
111
112 void prepareMessage(NXCPMessage *msg) const;
113
114 void expandMessageText();
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);
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); }
119
120 int getParametersCount() const { return m_parameters.size(); }
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; }
124
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)); }
128
129 void addParameter(const TCHAR *name, const TCHAR *value);
130 void setNamedParameter(const TCHAR *name, const TCHAR *value);
131 void setParameter(int index, const TCHAR *name, const TCHAR *value);
132
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); }
135
136 String createJson();
137 };
138
139 /**
140 * Defines for type of persistent storage action
141 */
142 #define PSTORAGE_SET 1
143 #define PSTORAGE_DELETE 2
144
145 /**
146 * Event policy rule
147 */
148 class EPRule
149 {
150 private:
151 UINT32 m_id;
152 uuid m_guid;
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;
160 TCHAR *m_pszComment;
161 TCHAR *m_pszScript;
162 NXSL_VM *m_pScript;
163
164 TCHAR m_szAlarmMessage[MAX_EVENT_MSG_LENGTH];
165 int m_iAlarmSeverity;
166 TCHAR m_szAlarmKey[MAX_DB_STRING];
167 UINT32 m_dwAlarmTimeout;
168 UINT32 m_dwAlarmTimeoutEvent;
169 IntegerArray<UINT32> *m_alarmCategoryList;
170 StringMap m_pstorageSetActions;
171 StringList m_pstorageDeleteActions;
172
173 bool matchSource(UINT32 dwObjectId);
174 bool matchEvent(UINT32 eventCode);
175 bool matchSeverity(UINT32 dwSeverity);
176 bool matchScript(Event *pEvent);
177
178 void generateAlarm(Event *pEvent);
179
180 public:
181 EPRule(UINT32 id);
182 EPRule(DB_RESULT hResult, int row);
183 EPRule(NXCPMessage *msg);
184 EPRule(ConfigEntry *config);
185 ~EPRule();
186
187 UINT32 getId() const { return m_id; }
188 const uuid& getGuid() const { return m_guid; }
189 void setId(UINT32 dwNewId) { m_id = dwNewId; }
190 bool loadFromDB(DB_HANDLE hdb);
191 bool saveToDB(DB_HANDLE hdb);
192 bool processEvent(Event *pEvent);
193 void createMessage(NXCPMessage *pMsg);
194 void createNXMPRecord(String &str);
195
196 bool isActionInUse(UINT32 dwActionId);
197 bool isCategoryInUse(UINT32 categoryId) const { return m_alarmCategoryList->contains(categoryId); }
198 };
199
200 /**
201 * Event policy
202 */
203 class EventPolicy
204 {
205 private:
206 UINT32 m_dwNumRules;
207 EPRule **m_ppRuleList;
208 RWLOCK m_rwlock;
209
210 void readLock() { RWLockReadLock(m_rwlock, INFINITE); }
211 void writeLock() { RWLockWriteLock(m_rwlock, INFINITE); }
212 void unlock() { RWLockUnlock(m_rwlock); }
213 void clear();
214
215 public:
216 EventPolicy();
217 ~EventPolicy();
218
219 UINT32 getNumRules() { return m_dwNumRules; }
220 bool loadFromDB();
221 bool saveToDB();
222 void processEvent(Event *pEvent);
223 void sendToClient(ClientSession *pSession, UINT32 dwRqId);
224 void replacePolicy(UINT32 dwNumRules, EPRule **ppRuleList);
225 void exportRule(String& str, const uuid& guid);
226 void importRule(EPRule *rule);
227 void removeRuleCategory (UINT32 categoryId);
228
229 bool isActionInUse(UINT32 dwActionId);
230 bool isCategoryInUse(UINT32 dwCategoryId);
231 };
232
233 /**
234 * Functions
235 */
236 BOOL InitEventSubsystem();
237 void ShutdownEventSubsystem();
238 void ReloadEvents();
239 void DeleteEventTemplateFromList(UINT32 eventCode);
240 void CorrelateEvent(Event *pEvent);
241 void CreateNXMPEventRecord(String &str, UINT32 eventCode);
242
243 bool EventNameFromCode(UINT32 eventCode, TCHAR *buffer);
244 UINT32 NXCORE_EXPORTABLE EventCodeFromName(const TCHAR *name, UINT32 defaultValue = 0);
245 EventTemplate *FindEventTemplateByCode(UINT32 eventCode);
246 EventTemplate *FindEventTemplateByName(const TCHAR *pszName);
247
248 bool NXCORE_EXPORTABLE PostEvent(UINT32 eventCode, UINT32 sourceId, const char *format, ...);
249 bool NXCORE_EXPORTABLE PostDciEvent(UINT32 eventCode, UINT32 sourceId, UINT32 dciId, const char *format, ...);
250 UINT64 NXCORE_EXPORTABLE PostEvent2(UINT32 eventCode, UINT32 sourceId, const char *format, ...);
251 bool NXCORE_EXPORTABLE PostEventWithNames(UINT32 eventCode, UINT32 sourceId, const char *format, const TCHAR **names, ...);
252 bool NXCORE_EXPORTABLE PostEventWithNames(UINT32 eventCode, UINT32 sourceId, StringMap *parameters);
253 bool NXCORE_EXPORTABLE PostDciEventWithNames(UINT32 eventCode, UINT32 sourceId, UINT32 dciId, const char *format, const TCHAR **names, ...);
254 bool NXCORE_EXPORTABLE PostDciEventWithNames(UINT32 eventCode, UINT32 sourceId, UINT32 dciId, StringMap *parameters);
255 bool NXCORE_EXPORTABLE PostEventWithTagAndNames(UINT32 eventCode, UINT32 sourceId, const TCHAR *userTag, const char *format, const TCHAR **names, ...);
256 bool NXCORE_EXPORTABLE PostEventWithTag(UINT32 eventCode, UINT32 sourceId, const TCHAR *userTag, const char *format, ...);
257 bool NXCORE_EXPORTABLE PostEventEx(Queue *queue, UINT32 eventCode, UINT32 sourceId, const char *format, ...);
258 void NXCORE_EXPORTABLE ResendEvents(Queue *queue);
259
260 const TCHAR NXCORE_EXPORTABLE *GetStatusAsText(int status, bool allCaps);
261
262 /**
263 * Global variables
264 */
265 extern Queue *g_pEventQueue;
266 extern EventPolicy *g_pEventPolicy;
267 extern INT64 g_totalEventsProcessed;
268
269 #endif /* _nms_events_h_ */