fixed some Windows makefile build issues; added code signing to Windows makefiles
[public/netxms.git] / include / nxlpapi.h
CommitLineData
659bf814 1/*
8b1de1cc
AK
2** NetXMS - Network Management System
3** Log Parsing Library
4** Copyright (C) 2003-2012 Victor Kirhenshtein
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU Lesser General Public License as published by
8** the Free Software Foundation; either version 3 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU Lesser General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
20** File: nxlpapi.h
21**
22**/
23
24#ifndef _nxlpapi_h_
25#define _nxlpapi_h_
26
27#ifdef _WIN32
28#ifdef LIBNXLP_EXPORTS
29#define LIBNXLP_EXPORTABLE __declspec(dllexport)
30#else
31#define LIBNXLP_EXPORTABLE __declspec(dllimport)
32#endif
33#else /* _WIN32 */
34#define LIBNXLP_EXPORTABLE
35#endif
36
37#include <netxms-regex.h>
38#include <nms_util.h>
39
268105f1
VK
40/**
41 * Parser status
42 */
43enum LogParserStatus
44{
45 LPS_INIT = 0,
46 LPS_RUNNING = 1,
47 LPS_NO_FILE = 2,
48 LPS_OPEN_ERROR = 3,
49 LPS_SUSPENDED = 4,
50 LPS_EVT_SUBSCRIBE_ERROR = 5,
51 LPS_EVT_READ_ERROR = 6,
52 LPS_EVT_OPEN_ERROR = 7
53};
8b1de1cc 54
268105f1
VK
55/**
56 * Context actions
57 */
58enum LogParserContextAction
59{
60 CONTEXT_SET_MANUAL = 0,
61 CONTEXT_SET_AUTOMATIC = 1,
62 CONTEXT_CLEAR = 2
63};
8b1de1cc 64
268105f1
VK
65/**
66 * File encoding
67 */
68enum LogParserFileEncoding
69{
70 LP_FCP_AUTO = -1,
71 LP_FCP_ACP = 0,
72 LP_FCP_UTF8 = 1,
73 LP_FCP_UCS2 = 2,
74 LP_FCP_UCS2_LE = 3,
75 LP_FCP_UCS2_BE = 4,
76 LP_FCP_UCS4 = 5,
77 LP_FCP_UCS4_LE = 6,
78 LP_FCP_UCS4_BE = 7
79};
8b1de1cc 80
da623f8b
VK
81/**
82 * Log parser callback
83 * Parameters:
659bf814 84 * NetXMS event code, NetXMS event name, original text, source,
da623f8b
VK
85 * original event ID (facility), original severity,
86 * number of capture groups, list of capture groups,
87 * object id, user arg
88 */
d930bfc9 89typedef void (* LogParserCallback)(UINT32, const TCHAR *, const TCHAR *, const TCHAR *, UINT32, UINT32, int, TCHAR **, UINT32, int, void *);
8b1de1cc
AK
90
91class LIBNXLP_EXPORTABLE LogParser;
92
da623f8b 93/**
e2845745
VK
94 * Per object rule statistics
95 */
96struct ObjectRuleStats
97{
98 int checkCount;
99 int matchCount;
100};
101
102/**
da623f8b
VK
103 * Log parser rule
104 */
8b1de1cc
AK
105class LIBNXLP_EXPORTABLE LogParserRule
106{
da623f8b
VK
107 friend class LogParser;
108
8b1de1cc
AK
109private:
110 LogParser *m_parser;
839931d3 111 TCHAR *m_name;
8b1de1cc 112 regex_t m_preg;
da623f8b 113 UINT32 m_eventCode;
8b1de1cc
AK
114 TCHAR *m_eventName;
115 bool m_isValid;
116 int m_numParams;
117 regmatch_t *m_pmatch;
118 TCHAR *m_regexp;
119 TCHAR *m_source;
da623f8b
VK
120 UINT32 m_level;
121 UINT32 m_idStart;
122 UINT32 m_idEnd;
8b1de1cc
AK
123 TCHAR *m_context;
124 int m_contextAction;
125 TCHAR *m_contextToChange;
126 bool m_isInverted;
127 bool m_breakOnMatch;
128 TCHAR *m_description;
659bf814 129 int m_repeatInterval;
130 int m_repeatCount;
131 IntegerArray<time_t> *m_matchArray;
132 bool m_resetRepeat;
839931d3
VK
133 int m_checkCount;
134 int m_matchCount;
e2845745 135 HashMap<UINT32, ObjectRuleStats> *m_objectCounters;
8b1de1cc 136
da623f8b 137 bool matchInternal(bool extMode, const TCHAR *source, UINT32 eventId, UINT32 level,
659bf814 138 const TCHAR *line, LogParserCallback cb, UINT32 objectId, void *userArg);
d930bfc9 139 bool matchRepeatCount();
da623f8b 140 void expandMacros(const TCHAR *regexp, String &out);
e2845745
VK
141 void incCheckCount(UINT32 objectId);
142 void incMatchCount(UINT32 objectId);
8b1de1cc
AK
143
144public:
839931d3 145 LogParserRule(LogParser *parser, const TCHAR *name,
da623f8b 146 const TCHAR *regexp, UINT32 eventCode = 0, const TCHAR *eventName = NULL,
659bf814 147 int numParams = 0, int repeatInterval = 0, int repeatCount = 0,
148 bool resetRepeat = true, const TCHAR *source = NULL, UINT32 level = 0xFFFFFFFF,
da623f8b 149 UINT32 idStart = 0, UINT32 idEnd = 0xFFFFFFFF);
8b1de1cc
AK
150 LogParserRule(LogParserRule *src, LogParser *parser);
151 ~LogParserRule();
152
839931d3 153 const TCHAR *getName() const { return m_name; }
aa07a370 154 bool isValid() const { return m_isValid; }
839931d3 155
da623f8b
VK
156 bool match(const TCHAR *line, LogParserCallback cb, UINT32 objectId, void *userArg);
157 bool matchEx(const TCHAR *source, UINT32 eventId, UINT32 level,
659bf814 158 const TCHAR *line, LogParserCallback cb, UINT32 objectId, void *userArg);
159
8b1de1cc
AK
160 void setContext(const TCHAR *context) { safe_free(m_context); m_context = (context != NULL) ? _tcsdup(context) : NULL; }
161 void setContextToChange(const TCHAR *context) { safe_free(m_contextToChange); m_contextToChange = (context != NULL) ? _tcsdup(context) : NULL; }
162 void setContextAction(int action) { m_contextAction = action; }
163
164 void setInverted(bool flag) { m_isInverted = flag; }
839931d3 165 bool isInverted() const { return m_isInverted; }
8b1de1cc
AK
166
167 void setBreakFlag(bool flag) { m_breakOnMatch = flag; }
839931d3 168 bool getBreakFlag() const { return m_breakOnMatch; }
8b1de1cc 169
aa07a370
VK
170 const TCHAR *getContext() const { return m_context; }
171 const TCHAR *getContextToChange() const { return m_contextToChange; }
172 int getContextAction() const { return m_contextAction; }
8b1de1cc
AK
173
174 void setDescription(const TCHAR *descr) { safe_free(m_description); m_description = (descr != NULL) ? _tcsdup(descr) : NULL; }
aa07a370 175 const TCHAR *getDescription() const { return CHECK_NULL_EX(m_description); }
8b1de1cc
AK
176
177 void setSource(const TCHAR *source) { safe_free(m_source); m_source = (source != NULL) ? _tcsdup(source) : NULL; }
aa07a370 178 const TCHAR *getSource() const { return CHECK_NULL_EX(m_source); }
8b1de1cc 179
da623f8b 180 void setLevel(UINT32 level) { m_level = level; }
aa07a370 181 UINT32 getLevel() const { return m_level; }
8b1de1cc 182
da623f8b 183 void setIdRange(UINT32 start, UINT32 end) { m_idStart = start; m_idEnd = end; }
aa07a370 184 QWORD getIdRange() const { return ((QWORD)m_idStart << 32) | (QWORD)m_idEnd; }
8b1de1cc 185
659bf814 186 void setRepeatInterval(int repeatInterval) { m_repeatInterval = repeatInterval; }
aa07a370 187 int getRepeatInterval() const { return m_repeatInterval; }
659bf814 188
189 void setRepeatCount(int repeatCount) { m_repeatCount = repeatCount; }
aa07a370 190 int getRepeatCount() const { return m_repeatCount; }
659bf814 191
192 void setRepeatReset(bool resetRepeat) { m_resetRepeat = resetRepeat; }
aa07a370 193 bool isRepeatReset() const { return m_resetRepeat; }
659bf814 194
aa07a370
VK
195 const TCHAR *getRegexpSource() const { return CHECK_NULL(m_regexp); }
196
e2845745
VK
197 int getCheckCount(UINT32 objectId = 0) const;
198 int getMatchCount(UINT32 objectId = 0) const;
29b701c3
VK
199
200 void restoreCounters(const LogParserRule *rule);
8b1de1cc
AK
201};
202
da623f8b
VK
203/**
204 * Log parser class
205 */
8b1de1cc
AK
206class LIBNXLP_EXPORTABLE LogParser
207{
da623f8b 208 friend bool LogParserRule::matchInternal(bool, const TCHAR *, UINT32, UINT32, const TCHAR *, LogParserCallback, UINT32, void *);
8b1de1cc
AK
209
210private:
1df332d8 211 ObjectArray<LogParserRule> *m_rules;
8b1de1cc
AK
212 StringMap m_contexts;
213 StringMap m_macros;
214 LogParserCallback m_cb;
215 void *m_userArg;
216 TCHAR *m_fileName;
217 int m_fileEncoding;
bc3e2810 218 bool m_preallocatedFile;
073283c5 219 StringList m_exclusionSchedules;
8b1de1cc
AK
220 TCHAR *m_name;
221 CODE_TO_TEXT *m_eventNameList;
da623f8b 222 bool (*m_eventResolver)(const TCHAR *, UINT32 *);
8b1de1cc
AK
223 THREAD m_thread; // Associated thread
224 int m_recordsProcessed;
225 int m_recordsMatched;
226 bool m_processAllRules;
4d8c4cf3 227 bool m_suspended;
8b1de1cc 228 int m_traceLevel;
bc3e2810 229 void (*m_traceCallback)(int, const TCHAR *, va_list);
268105f1 230 LogParserStatus m_status;
18b96a67
VK
231#ifdef _WIN32
232 TCHAR *m_marker;
233#endif
659bf814 234
8b1de1cc
AK
235 const TCHAR *checkContext(LogParserRule *rule);
236 void trace(int level, const TCHAR *format, ...);
da623f8b 237 bool matchLogRecord(bool hasAttributes, const TCHAR *source, UINT32 eventId, UINT32 level, const TCHAR *line, UINT32 objectId);
8b1de1cc 238
073283c5
VK
239 bool isExclusionPeriod();
240
bc63d017
VK
241 const LogParserRule *findRuleByName(const TCHAR *name) const;
242
243 int getCharSize() const;
244
268105f1 245 void setStatus(LogParserStatus status) { m_status = status; }
bc63d017 246
07a1156a
VK
247#ifdef _WIN32
248 void parseEvent(EVENTLOGRECORD *rec);
249
250 bool monitorEventLogV6(CONDITION stopCondition);
251 bool monitorEventLogV4(CONDITION stopCondition);
18b96a67
VK
252
253 time_t readLastProcessedRecordTimestamp();
07a1156a
VK
254#endif
255
8b1de1cc
AK
256public:
257 LogParser();
dedeffb3 258 LogParser(const LogParser *src);
8b1de1cc 259 ~LogParser();
659bf814 260
261 static ObjectArray<LogParser> *createFromXml(const char *xml, int xmlLen = -1,
da623f8b 262 TCHAR *errorText = NULL, int errBufSize = 0, bool (*eventResolver)(const TCHAR *, UINT32 *) = NULL);
8b1de1cc 263
bc3e2810
VK
264 const TCHAR *getName() const { return m_name; }
265 const TCHAR *getFileName() const { return m_fileName; }
266 int getFileEncoding() const { return m_fileEncoding; }
268105f1
VK
267 LogParserStatus getStatus() const { return m_status; }
268 const TCHAR *getStatusText() const;
bc3e2810 269 bool isFilePreallocated() const { return m_preallocatedFile; }
8b1de1cc
AK
270
271 void setName(const TCHAR *name);
bc3e2810 272 void setFileName(const TCHAR *name);
8b1de1cc
AK
273
274 void setThread(THREAD th) { m_thread = th; }
275 THREAD getThread() { return m_thread; }
276
277 void setProcessAllFlag(bool flag) { m_processAllRules = flag; }
278 bool getProcessAllFlag() { return m_processAllRules; }
279
659bf814 280 bool addRule(const TCHAR *regexp, UINT32 eventCode = 0, const TCHAR *eventName = NULL, int numParams = 0, int repeatInterval = 0, int repeatCount = 0, bool resetRepeat = true);
8b1de1cc
AK
281 bool addRule(LogParserRule *rule);
282 void setCallback(LogParserCallback cb) { m_cb = cb; }
283 void setUserArg(void *arg) { m_userArg = arg; }
284 void setEventNameList(CODE_TO_TEXT *ctt) { m_eventNameList = ctt; }
da623f8b
VK
285 void setEventNameResolver(bool (*cb)(const TCHAR *, UINT32 *)) { m_eventResolver = cb; }
286 UINT32 resolveEventName(const TCHAR *name, UINT32 defVal = 0);
8b1de1cc 287
073283c5
VK
288 void addExclusionSchedule(const TCHAR *schedule) { m_exclusionSchedules.add(schedule); }
289
8b1de1cc
AK
290 void addMacro(const TCHAR *name, const TCHAR *value);
291 const TCHAR *getMacro(const TCHAR *name);
292
da623f8b
VK
293 bool matchLine(const TCHAR *line, UINT32 objectId = 0);
294 bool matchEvent(const TCHAR *source, UINT32 eventId, UINT32 level, const TCHAR *line, UINT32 objectId = 0);
8b1de1cc 295
bc3e2810
VK
296 int getProcessedRecordsCount() const { return m_recordsProcessed; }
297 int getMatchedRecordsCount() const { return m_recordsMatched; }
8b1de1cc 298
bc3e2810 299 int getTraceLevel() const { return m_traceLevel; }
8b1de1cc 300 void setTraceLevel(int level) { m_traceLevel = level; }
bc3e2810 301 void setTraceCallback(void (*cb)(int, const TCHAR *, va_list)) { m_traceCallback = cb; }
8b1de1cc 302
07a1156a
VK
303 bool monitorFile(CONDITION stopCondition, bool readFromCurrPos = true);
304#ifdef _WIN32
18b96a67
VK
305 bool monitorEventLog(CONDITION stopCondition, const TCHAR *markerPrefix);
306 void saveLastProcessedRecordTimestamp(time_t timestamp);
07a1156a 307#endif
839931d3 308
e2845745
VK
309 int getRuleCheckCount(const TCHAR *ruleName, UINT32 objectId = 0) const { const LogParserRule *r = findRuleByName(ruleName); return (r != NULL) ? r->getCheckCount(objectId) : -1; }
310 int getRuleMatchCount(const TCHAR *ruleName, UINT32 objectId = 0) const { const LogParserRule *r = findRuleByName(ruleName); return (r != NULL) ? r->getMatchCount(objectId) : -1; }
29b701c3
VK
311
312 void restoreCounters(const LogParser *parser);
4d8c4cf3
VK
313
314 void suspend();
315 void resume();
8b1de1cc
AK
316};
317
07a1156a
VK
318/**
319 * Init log parser library
320 */
321void LIBNXLP_EXPORTABLE InitLogParserLibrary();
322
323/**
324 * Cleanup event log parsig library
325 */
326void LIBNXLP_EXPORTABLE CleanupLogParserLibrary();
327
328/**
329 * Set trace callback for log parser library
330 */
331void LIBNXLP_EXPORTABLE SetLogParserTraceCallback(void (* traceCallback)(int, const TCHAR *, va_list));
332
8b1de1cc 333#endif