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