e6f2bea65c271efc384c833f9539c33f06b77da3
[public/netxms.git] / include / nxlpapi.h
1 /*
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 * Parser status
42 */
43 enum 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 };
54
55 /**
56 * Context actions
57 */
58 enum LogParserContextAction
59 {
60 CONTEXT_SET_MANUAL = 0,
61 CONTEXT_SET_AUTOMATIC = 1,
62 CONTEXT_CLEAR = 2
63 };
64
65 /**
66 * File encoding
67 */
68 enum 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 };
80
81 /**
82 * Log parser callback
83 * Parameters:
84 * NetXMS event code, NetXMS event name, original text, source,
85 * original event ID (facility), original severity,
86 * number of capture groups, list of capture groups,
87 * object id, user arg
88 */
89 typedef void (* LogParserCallback)(UINT32, const TCHAR *, const TCHAR *, const TCHAR *, UINT32, UINT32, int, TCHAR **, UINT32, int, void *);
90
91 class LIBNXLP_EXPORTABLE LogParser;
92
93 /**
94 * Per object rule statistics
95 */
96 struct ObjectRuleStats
97 {
98 int checkCount;
99 int matchCount;
100 };
101
102 /**
103 * Log parser rule
104 */
105 class LIBNXLP_EXPORTABLE LogParserRule
106 {
107 friend class LogParser;
108
109 private:
110 LogParser *m_parser;
111 TCHAR *m_name;
112 regex_t m_preg;
113 UINT32 m_eventCode;
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;
120 UINT32 m_level;
121 UINT32 m_idStart;
122 UINT32 m_idEnd;
123 TCHAR *m_context;
124 int m_contextAction;
125 TCHAR *m_contextToChange;
126 bool m_isInverted;
127 bool m_breakOnMatch;
128 TCHAR *m_description;
129 int m_repeatInterval;
130 int m_repeatCount;
131 IntegerArray<time_t> *m_matchArray;
132 bool m_resetRepeat;
133 int m_checkCount;
134 int m_matchCount;
135 HashMap<UINT32, ObjectRuleStats> *m_objectCounters;
136
137 bool matchInternal(bool extMode, const TCHAR *source, UINT32 eventId, UINT32 level,
138 const TCHAR *line, LogParserCallback cb, UINT32 objectId, void *userArg);
139 bool matchRepeatCount();
140 void expandMacros(const TCHAR *regexp, String &out);
141 void incCheckCount(UINT32 objectId);
142 void incMatchCount(UINT32 objectId);
143
144 public:
145 LogParserRule(LogParser *parser, const TCHAR *name,
146 const TCHAR *regexp, UINT32 eventCode = 0, const TCHAR *eventName = NULL,
147 int numParams = 0, int repeatInterval = 0, int repeatCount = 0,
148 bool resetRepeat = true, const TCHAR *source = NULL, UINT32 level = 0xFFFFFFFF,
149 UINT32 idStart = 0, UINT32 idEnd = 0xFFFFFFFF);
150 LogParserRule(LogParserRule *src, LogParser *parser);
151 ~LogParserRule();
152
153 const TCHAR *getName() const { return m_name; }
154 bool isValid() const { return m_isValid; }
155
156 bool match(const TCHAR *line, LogParserCallback cb, UINT32 objectId, void *userArg);
157 bool matchEx(const TCHAR *source, UINT32 eventId, UINT32 level,
158 const TCHAR *line, LogParserCallback cb, UINT32 objectId, void *userArg);
159
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; }
165 bool isInverted() const { return m_isInverted; }
166
167 void setBreakFlag(bool flag) { m_breakOnMatch = flag; }
168 bool getBreakFlag() const { return m_breakOnMatch; }
169
170 const TCHAR *getContext() const { return m_context; }
171 const TCHAR *getContextToChange() const { return m_contextToChange; }
172 int getContextAction() const { return m_contextAction; }
173
174 void setDescription(const TCHAR *descr) { safe_free(m_description); m_description = (descr != NULL) ? _tcsdup(descr) : NULL; }
175 const TCHAR *getDescription() const { return CHECK_NULL_EX(m_description); }
176
177 void setSource(const TCHAR *source) { safe_free(m_source); m_source = (source != NULL) ? _tcsdup(source) : NULL; }
178 const TCHAR *getSource() const { return CHECK_NULL_EX(m_source); }
179
180 void setLevel(UINT32 level) { m_level = level; }
181 UINT32 getLevel() const { return m_level; }
182
183 void setIdRange(UINT32 start, UINT32 end) { m_idStart = start; m_idEnd = end; }
184 QWORD getIdRange() const { return ((QWORD)m_idStart << 32) | (QWORD)m_idEnd; }
185
186 void setRepeatInterval(int repeatInterval) { m_repeatInterval = repeatInterval; }
187 int getRepeatInterval() const { return m_repeatInterval; }
188
189 void setRepeatCount(int repeatCount) { m_repeatCount = repeatCount; }
190 int getRepeatCount() const { return m_repeatCount; }
191
192 void setRepeatReset(bool resetRepeat) { m_resetRepeat = resetRepeat; }
193 bool isRepeatReset() const { return m_resetRepeat; }
194
195 const TCHAR *getRegexpSource() const { return CHECK_NULL(m_regexp); }
196
197 int getCheckCount(UINT32 objectId = 0) const;
198 int getMatchCount(UINT32 objectId = 0) const;
199
200 void restoreCounters(const LogParserRule *rule);
201 };
202
203 /**
204 * Log parser class
205 */
206 class LIBNXLP_EXPORTABLE LogParser
207 {
208 friend bool LogParserRule::matchInternal(bool, const TCHAR *, UINT32, UINT32, const TCHAR *, LogParserCallback, UINT32, void *);
209
210 private:
211 ObjectArray<LogParserRule> *m_rules;
212 StringMap m_contexts;
213 StringMap m_macros;
214 LogParserCallback m_cb;
215 void *m_userArg;
216 TCHAR *m_fileName;
217 int m_fileEncoding;
218 bool m_preallocatedFile;
219 StringList m_exclusionSchedules;
220 TCHAR *m_name;
221 CODE_TO_TEXT *m_eventNameList;
222 bool (*m_eventResolver)(const TCHAR *, UINT32 *);
223 THREAD m_thread; // Associated thread
224 int m_recordsProcessed;
225 int m_recordsMatched;
226 bool m_processAllRules;
227 bool m_suspended;
228 int m_traceLevel;
229 void (*m_traceCallback)(int, const TCHAR *, va_list);
230 LogParserStatus m_status;
231 #ifdef _WIN32
232 TCHAR *m_marker;
233 #endif
234
235 const TCHAR *checkContext(LogParserRule *rule);
236 void trace(int level, const TCHAR *format, ...);
237 bool matchLogRecord(bool hasAttributes, const TCHAR *source, UINT32 eventId, UINT32 level, const TCHAR *line, UINT32 objectId);
238
239 bool isExclusionPeriod();
240
241 const LogParserRule *findRuleByName(const TCHAR *name) const;
242
243 int getCharSize() const;
244
245 void setStatus(LogParserStatus status) { m_status = status; }
246
247 #ifdef _WIN32
248 void parseEvent(EVENTLOGRECORD *rec);
249
250 bool monitorEventLogV6(CONDITION stopCondition);
251 bool monitorEventLogV4(CONDITION stopCondition);
252
253 time_t readLastProcessedRecordTimestamp();
254 #endif
255
256 public:
257 LogParser();
258 LogParser(const LogParser *src);
259 ~LogParser();
260
261 static ObjectArray<LogParser> *createFromXml(const char *xml, int xmlLen = -1,
262 TCHAR *errorText = NULL, int errBufSize = 0, bool (*eventResolver)(const TCHAR *, UINT32 *) = NULL);
263
264 const TCHAR *getName() const { return m_name; }
265 const TCHAR *getFileName() const { return m_fileName; }
266 int getFileEncoding() const { return m_fileEncoding; }
267 LogParserStatus getStatus() const { return m_status; }
268 const TCHAR *getStatusText() const;
269 bool isFilePreallocated() const { return m_preallocatedFile; }
270
271 void setName(const TCHAR *name);
272 void setFileName(const TCHAR *name);
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
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);
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; }
285 void setEventNameResolver(bool (*cb)(const TCHAR *, UINT32 *)) { m_eventResolver = cb; }
286 UINT32 resolveEventName(const TCHAR *name, UINT32 defVal = 0);
287
288 void addExclusionSchedule(const TCHAR *schedule) { m_exclusionSchedules.add(schedule); }
289
290 void addMacro(const TCHAR *name, const TCHAR *value);
291 const TCHAR *getMacro(const TCHAR *name);
292
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);
295
296 int getProcessedRecordsCount() const { return m_recordsProcessed; }
297 int getMatchedRecordsCount() const { return m_recordsMatched; }
298
299 int getTraceLevel() const { return m_traceLevel; }
300 void setTraceLevel(int level) { m_traceLevel = level; }
301 void setTraceCallback(void (*cb)(int, const TCHAR *, va_list)) { m_traceCallback = cb; }
302
303 bool monitorFile(CONDITION stopCondition, bool readFromCurrPos = true);
304 #ifdef _WIN32
305 bool monitorEventLog(CONDITION stopCondition, const TCHAR *markerPrefix);
306 void saveLastProcessedRecordTimestamp(time_t timestamp);
307 #endif
308
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; }
311
312 void restoreCounters(const LogParser *parser);
313
314 void suspend();
315 void resume();
316 };
317
318 /**
319 * Init log parser library
320 */
321 void LIBNXLP_EXPORTABLE InitLogParserLibrary();
322
323 /**
324 * Cleanup event log parsig library
325 */
326 void LIBNXLP_EXPORTABLE CleanupLogParserLibrary();
327
328 /**
329 * Set trace callback for log parser library
330 */
331 void LIBNXLP_EXPORTABLE SetLogParserTraceCallback(void (* traceCallback)(int, const TCHAR *, va_list));
332
333 #endif