fixed compilation warnings for flex generated files on Windows
[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,
f4181253
VK
52 LPS_EVT_OPEN_ERROR = 7,
53 LPS_VSS_FAILURE = 8
268105f1 54};
8b1de1cc 55
268105f1
VK
56/**
57 * Context actions
58 */
59enum LogParserContextAction
60{
61 CONTEXT_SET_MANUAL = 0,
62 CONTEXT_SET_AUTOMATIC = 1,
63 CONTEXT_CLEAR = 2
64};
8b1de1cc 65
268105f1
VK
66/**
67 * File encoding
68 */
69enum LogParserFileEncoding
70{
71 LP_FCP_AUTO = -1,
72 LP_FCP_ACP = 0,
73 LP_FCP_UTF8 = 1,
74 LP_FCP_UCS2 = 2,
75 LP_FCP_UCS2_LE = 3,
76 LP_FCP_UCS2_BE = 4,
77 LP_FCP_UCS4 = 5,
78 LP_FCP_UCS4_LE = 6,
79 LP_FCP_UCS4_BE = 7
80};
8b1de1cc 81
da623f8b
VK
82/**
83 * Log parser callback
84 * Parameters:
659bf814 85 * NetXMS event code, NetXMS event name, original text, source,
da623f8b
VK
86 * original event ID (facility), original severity,
87 * number of capture groups, list of capture groups,
88 * object id, user arg
89 */
d930bfc9 90typedef void (* LogParserCallback)(UINT32, const TCHAR *, const TCHAR *, const TCHAR *, UINT32, UINT32, int, TCHAR **, UINT32, int, void *);
8b1de1cc
AK
91
92class LIBNXLP_EXPORTABLE LogParser;
93
c13733bd
VK
94#ifdef _WIN32
95
96/**
97 * Snapshot handle
98 */
99struct FileSnapshot
100{
101 void *handle;
102 TCHAR *name;
103};
104
105#endif
106
da623f8b 107/**
e2845745
VK
108 * Per object rule statistics
109 */
110struct ObjectRuleStats
111{
112 int checkCount;
113 int matchCount;
114};
115
116/**
da623f8b
VK
117 * Log parser rule
118 */
8b1de1cc
AK
119class LIBNXLP_EXPORTABLE LogParserRule
120{
da623f8b
VK
121 friend class LogParser;
122
8b1de1cc
AK
123private:
124 LogParser *m_parser;
839931d3 125 TCHAR *m_name;
8b1de1cc 126 regex_t m_preg;
da623f8b 127 UINT32 m_eventCode;
8b1de1cc
AK
128 TCHAR *m_eventName;
129 bool m_isValid;
130 int m_numParams;
131 regmatch_t *m_pmatch;
132 TCHAR *m_regexp;
133 TCHAR *m_source;
da623f8b
VK
134 UINT32 m_level;
135 UINT32 m_idStart;
136 UINT32 m_idEnd;
8b1de1cc
AK
137 TCHAR *m_context;
138 int m_contextAction;
139 TCHAR *m_contextToChange;
140 bool m_isInverted;
141 bool m_breakOnMatch;
142 TCHAR *m_description;
659bf814 143 int m_repeatInterval;
144 int m_repeatCount;
145 IntegerArray<time_t> *m_matchArray;
146 bool m_resetRepeat;
839931d3
VK
147 int m_checkCount;
148 int m_matchCount;
e2845745 149 HashMap<UINT32, ObjectRuleStats> *m_objectCounters;
8b1de1cc 150
da623f8b 151 bool matchInternal(bool extMode, const TCHAR *source, UINT32 eventId, UINT32 level,
659bf814 152 const TCHAR *line, LogParserCallback cb, UINT32 objectId, void *userArg);
d930bfc9 153 bool matchRepeatCount();
da623f8b 154 void expandMacros(const TCHAR *regexp, String &out);
e2845745
VK
155 void incCheckCount(UINT32 objectId);
156 void incMatchCount(UINT32 objectId);
8b1de1cc
AK
157
158public:
839931d3 159 LogParserRule(LogParser *parser, const TCHAR *name,
da623f8b 160 const TCHAR *regexp, UINT32 eventCode = 0, const TCHAR *eventName = NULL,
659bf814 161 int numParams = 0, int repeatInterval = 0, int repeatCount = 0,
162 bool resetRepeat = true, const TCHAR *source = NULL, UINT32 level = 0xFFFFFFFF,
da623f8b 163 UINT32 idStart = 0, UINT32 idEnd = 0xFFFFFFFF);
8b1de1cc
AK
164 LogParserRule(LogParserRule *src, LogParser *parser);
165 ~LogParserRule();
166
839931d3 167 const TCHAR *getName() const { return m_name; }
aa07a370 168 bool isValid() const { return m_isValid; }
839931d3 169
da623f8b
VK
170 bool match(const TCHAR *line, LogParserCallback cb, UINT32 objectId, void *userArg);
171 bool matchEx(const TCHAR *source, UINT32 eventId, UINT32 level,
659bf814 172 const TCHAR *line, LogParserCallback cb, UINT32 objectId, void *userArg);
173
8b1de1cc
AK
174 void setContext(const TCHAR *context) { safe_free(m_context); m_context = (context != NULL) ? _tcsdup(context) : NULL; }
175 void setContextToChange(const TCHAR *context) { safe_free(m_contextToChange); m_contextToChange = (context != NULL) ? _tcsdup(context) : NULL; }
176 void setContextAction(int action) { m_contextAction = action; }
177
178 void setInverted(bool flag) { m_isInverted = flag; }
839931d3 179 bool isInverted() const { return m_isInverted; }
8b1de1cc
AK
180
181 void setBreakFlag(bool flag) { m_breakOnMatch = flag; }
839931d3 182 bool getBreakFlag() const { return m_breakOnMatch; }
8b1de1cc 183
aa07a370
VK
184 const TCHAR *getContext() const { return m_context; }
185 const TCHAR *getContextToChange() const { return m_contextToChange; }
186 int getContextAction() const { return m_contextAction; }
8b1de1cc
AK
187
188 void setDescription(const TCHAR *descr) { safe_free(m_description); m_description = (descr != NULL) ? _tcsdup(descr) : NULL; }
aa07a370 189 const TCHAR *getDescription() const { return CHECK_NULL_EX(m_description); }
8b1de1cc
AK
190
191 void setSource(const TCHAR *source) { safe_free(m_source); m_source = (source != NULL) ? _tcsdup(source) : NULL; }
aa07a370 192 const TCHAR *getSource() const { return CHECK_NULL_EX(m_source); }
8b1de1cc 193
da623f8b 194 void setLevel(UINT32 level) { m_level = level; }
aa07a370 195 UINT32 getLevel() const { return m_level; }
8b1de1cc 196
da623f8b 197 void setIdRange(UINT32 start, UINT32 end) { m_idStart = start; m_idEnd = end; }
aa07a370 198 QWORD getIdRange() const { return ((QWORD)m_idStart << 32) | (QWORD)m_idEnd; }
8b1de1cc 199
659bf814 200 void setRepeatInterval(int repeatInterval) { m_repeatInterval = repeatInterval; }
aa07a370 201 int getRepeatInterval() const { return m_repeatInterval; }
659bf814 202
203 void setRepeatCount(int repeatCount) { m_repeatCount = repeatCount; }
aa07a370 204 int getRepeatCount() const { return m_repeatCount; }
659bf814 205
206 void setRepeatReset(bool resetRepeat) { m_resetRepeat = resetRepeat; }
aa07a370 207 bool isRepeatReset() const { return m_resetRepeat; }
659bf814 208
aa07a370
VK
209 const TCHAR *getRegexpSource() const { return CHECK_NULL(m_regexp); }
210
e2845745
VK
211 int getCheckCount(UINT32 objectId = 0) const;
212 int getMatchCount(UINT32 objectId = 0) const;
29b701c3
VK
213
214 void restoreCounters(const LogParserRule *rule);
8b1de1cc
AK
215};
216
da623f8b
VK
217/**
218 * Log parser class
219 */
8b1de1cc
AK
220class LIBNXLP_EXPORTABLE LogParser
221{
da623f8b 222 friend bool LogParserRule::matchInternal(bool, const TCHAR *, UINT32, UINT32, const TCHAR *, LogParserCallback, UINT32, void *);
8b1de1cc
AK
223
224private:
1df332d8 225 ObjectArray<LogParserRule> *m_rules;
8b1de1cc
AK
226 StringMap m_contexts;
227 StringMap m_macros;
228 LogParserCallback m_cb;
229 void *m_userArg;
230 TCHAR *m_fileName;
231 int m_fileEncoding;
bc3e2810 232 bool m_preallocatedFile;
073283c5 233 StringList m_exclusionSchedules;
8b1de1cc
AK
234 TCHAR *m_name;
235 CODE_TO_TEXT *m_eventNameList;
da623f8b 236 bool (*m_eventResolver)(const TCHAR *, UINT32 *);
8b1de1cc
AK
237 THREAD m_thread; // Associated thread
238 int m_recordsProcessed;
239 int m_recordsMatched;
240 bool m_processAllRules;
4d8c4cf3 241 bool m_suspended;
8b1de1cc 242 int m_traceLevel;
bc3e2810 243 void (*m_traceCallback)(int, const TCHAR *, va_list);
268105f1 244 LogParserStatus m_status;
18b96a67
VK
245#ifdef _WIN32
246 TCHAR *m_marker;
f4181253 247 bool m_useSnapshot;
18b96a67 248#endif
659bf814 249
8b1de1cc
AK
250 const TCHAR *checkContext(LogParserRule *rule);
251 void trace(int level, const TCHAR *format, ...);
da623f8b 252 bool matchLogRecord(bool hasAttributes, const TCHAR *source, UINT32 eventId, UINT32 level, const TCHAR *line, UINT32 objectId);
8b1de1cc 253
073283c5
VK
254 bool isExclusionPeriod();
255
bc63d017
VK
256 const LogParserRule *findRuleByName(const TCHAR *name) const;
257
258 int getCharSize() const;
259
268105f1 260 void setStatus(LogParserStatus status) { m_status = status; }
bc63d017 261
07a1156a
VK
262#ifdef _WIN32
263 void parseEvent(EVENTLOGRECORD *rec);
264
f4181253
VK
265 bool monitorFileWithSnapshot(CONDITION stopCondition, bool readFromCurrPos);
266 bool monitorEventLogV6(CONDITION stopCondition);
07a1156a 267 bool monitorEventLogV4(CONDITION stopCondition);
18b96a67
VK
268
269 time_t readLastProcessedRecordTimestamp();
07a1156a
VK
270#endif
271
8b1de1cc
AK
272public:
273 LogParser();
dedeffb3 274 LogParser(const LogParser *src);
8b1de1cc 275 ~LogParser();
659bf814 276
277 static ObjectArray<LogParser> *createFromXml(const char *xml, int xmlLen = -1,
da623f8b 278 TCHAR *errorText = NULL, int errBufSize = 0, bool (*eventResolver)(const TCHAR *, UINT32 *) = NULL);
8b1de1cc 279
bc3e2810
VK
280 const TCHAR *getName() const { return m_name; }
281 const TCHAR *getFileName() const { return m_fileName; }
282 int getFileEncoding() const { return m_fileEncoding; }
268105f1
VK
283 LogParserStatus getStatus() const { return m_status; }
284 const TCHAR *getStatusText() const;
bc3e2810 285 bool isFilePreallocated() const { return m_preallocatedFile; }
8b1de1cc
AK
286
287 void setName(const TCHAR *name);
bc3e2810 288 void setFileName(const TCHAR *name);
8b1de1cc
AK
289
290 void setThread(THREAD th) { m_thread = th; }
291 THREAD getThread() { return m_thread; }
292
293 void setProcessAllFlag(bool flag) { m_processAllRules = flag; }
294 bool getProcessAllFlag() { return m_processAllRules; }
295
f4181253
VK
296#ifdef _WIN32
297 void setSnapshotMode(bool enable) { m_useSnapshot = enable; }
298 bool isSnapshotMode() const { return m_useSnapshot; }
299#endif
300
659bf814 301 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
302 bool addRule(LogParserRule *rule);
303 void setCallback(LogParserCallback cb) { m_cb = cb; }
304 void setUserArg(void *arg) { m_userArg = arg; }
305 void setEventNameList(CODE_TO_TEXT *ctt) { m_eventNameList = ctt; }
da623f8b
VK
306 void setEventNameResolver(bool (*cb)(const TCHAR *, UINT32 *)) { m_eventResolver = cb; }
307 UINT32 resolveEventName(const TCHAR *name, UINT32 defVal = 0);
8b1de1cc 308
073283c5
VK
309 void addExclusionSchedule(const TCHAR *schedule) { m_exclusionSchedules.add(schedule); }
310
8b1de1cc
AK
311 void addMacro(const TCHAR *name, const TCHAR *value);
312 const TCHAR *getMacro(const TCHAR *name);
313
da623f8b
VK
314 bool matchLine(const TCHAR *line, UINT32 objectId = 0);
315 bool matchEvent(const TCHAR *source, UINT32 eventId, UINT32 level, const TCHAR *line, UINT32 objectId = 0);
8b1de1cc 316
bc3e2810
VK
317 int getProcessedRecordsCount() const { return m_recordsProcessed; }
318 int getMatchedRecordsCount() const { return m_recordsMatched; }
8b1de1cc 319
bc3e2810 320 int getTraceLevel() const { return m_traceLevel; }
8b1de1cc 321 void setTraceLevel(int level) { m_traceLevel = level; }
bc3e2810 322 void setTraceCallback(void (*cb)(int, const TCHAR *, va_list)) { m_traceCallback = cb; }
8b1de1cc 323
07a1156a
VK
324 bool monitorFile(CONDITION stopCondition, bool readFromCurrPos = true);
325#ifdef _WIN32
f4181253 326 bool monitorEventLog(CONDITION stopCondition, const TCHAR *markerPrefix);
18b96a67 327 void saveLastProcessedRecordTimestamp(time_t timestamp);
07a1156a 328#endif
839931d3 329
e2845745
VK
330 int getRuleCheckCount(const TCHAR *ruleName, UINT32 objectId = 0) const { const LogParserRule *r = findRuleByName(ruleName); return (r != NULL) ? r->getCheckCount(objectId) : -1; }
331 int getRuleMatchCount(const TCHAR *ruleName, UINT32 objectId = 0) const { const LogParserRule *r = findRuleByName(ruleName); return (r != NULL) ? r->getMatchCount(objectId) : -1; }
29b701c3
VK
332
333 void restoreCounters(const LogParser *parser);
4d8c4cf3
VK
334
335 void suspend();
336 void resume();
8b1de1cc
AK
337};
338
07a1156a
VK
339/**
340 * Init log parser library
341 */
342void LIBNXLP_EXPORTABLE InitLogParserLibrary();
343
344/**
345 * Cleanup event log parsig library
346 */
347void LIBNXLP_EXPORTABLE CleanupLogParserLibrary();
348
349/**
350 * Set trace callback for log parser library
351 */
352void LIBNXLP_EXPORTABLE SetLogParserTraceCallback(void (* traceCallback)(int, const TCHAR *, va_list));
353
c13733bd
VK
354#ifdef _WIN32
355
356/**
357 * Create file snapshot
358 */
359FileSnapshot LIBNXLP_EXPORTABLE *CreateFileSnapshot(const TCHAR *path);
360
361/**
362* Destroy file snapshot
363*/
364void LIBNXLP_EXPORTABLE DestroyFileSnapshot(FileSnapshot *snapshot);
365
366#endif
367
8b1de1cc 368#endif