2 ** NetXMS - Network Management System
4 ** Copyright (C) 2003-2012 Victor Kirhenshtein
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.
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.
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.
28 #ifdef LIBNXLP_EXPORTS
29 #define LIBNXLP_EXPORTABLE __declspec(dllexport)
31 #define LIBNXLP_EXPORTABLE __declspec(dllimport)
34 #define LIBNXLP_EXPORTABLE
37 #include <netxms-regex.h>
45 #define MAX_PARSER_STATUS_LEN 64
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")
57 #define CONTEXT_SET_MANUAL 0
58 #define CONTEXT_SET_AUTOMATIC 1
59 #define CONTEXT_CLEAR 2
69 #define LP_FCP_UCS2_LE 3
71 #define LP_FCP_UCS4_LE 5
76 * NetXMS event code, NetXMS event name, original text, source,
77 * original event ID (facility), original severity,
78 * number of capture groups, list of capture groups,
81 typedef void (* LogParserCallback
)(UINT32
, const TCHAR
*, const TCHAR
*, const TCHAR
*, UINT32
, UINT32
, int, TCHAR
**, UINT32
, int, void *);
83 class LIBNXLP_EXPORTABLE LogParser
;
88 class LIBNXLP_EXPORTABLE LogParserRule
90 friend class LogParser
;
107 TCHAR
*m_contextToChange
;
110 TCHAR
*m_description
;
111 int m_repeatInterval
;
113 IntegerArray
<time_t> *m_matchArray
;
116 bool matchInternal(bool extMode
, const TCHAR
*source
, UINT32 eventId
, UINT32 level
,
117 const TCHAR
*line
, LogParserCallback cb
, UINT32 objectId
, void *userArg
);
118 bool matchRepeatCount();
119 void expandMacros(const TCHAR
*regexp
, String
&out
);
122 LogParserRule(LogParser
*parser
,
123 const TCHAR
*regexp
, UINT32 eventCode
= 0, const TCHAR
*eventName
= NULL
,
124 int numParams
= 0, int repeatInterval
= 0, int repeatCount
= 0,
125 bool resetRepeat
= true, const TCHAR
*source
= NULL
, UINT32 level
= 0xFFFFFFFF,
126 UINT32 idStart
= 0, UINT32 idEnd
= 0xFFFFFFFF);
127 LogParserRule(LogParserRule
*src
, LogParser
*parser
);
130 bool isValid() { return m_isValid
; }
131 bool match(const TCHAR
*line
, LogParserCallback cb
, UINT32 objectId
, void *userArg
);
132 bool matchEx(const TCHAR
*source
, UINT32 eventId
, UINT32 level
,
133 const TCHAR
*line
, LogParserCallback cb
, UINT32 objectId
, void *userArg
);
135 void setContext(const TCHAR
*context
) { safe_free(m_context
); m_context
= (context
!= NULL
) ? _tcsdup(context
) : NULL
; }
136 void setContextToChange(const TCHAR
*context
) { safe_free(m_contextToChange
); m_contextToChange
= (context
!= NULL
) ? _tcsdup(context
) : NULL
; }
137 void setContextAction(int action
) { m_contextAction
= action
; }
139 void setInverted(bool flag
) { m_isInverted
= flag
; }
140 BOOL
isInverted() { return m_isInverted
; }
142 void setBreakFlag(bool flag
) { m_breakOnMatch
= flag
; }
143 BOOL
getBreakFlag() { return m_breakOnMatch
; }
145 const TCHAR
*getContext() { return m_context
; }
146 const TCHAR
*getContextToChange() { return m_contextToChange
; }
147 int getContextAction() { return m_contextAction
; }
149 void setDescription(const TCHAR
*descr
) { safe_free(m_description
); m_description
= (descr
!= NULL
) ? _tcsdup(descr
) : NULL
; }
150 const TCHAR
*getDescription() { return CHECK_NULL_EX(m_description
); }
152 void setSource(const TCHAR
*source
) { safe_free(m_source
); m_source
= (source
!= NULL
) ? _tcsdup(source
) : NULL
; }
153 const TCHAR
*getSource() { return CHECK_NULL_EX(m_source
); }
155 void setLevel(UINT32 level
) { m_level
= level
; }
156 UINT32
getLevel() { return m_level
; }
158 void setIdRange(UINT32 start
, UINT32 end
) { m_idStart
= start
; m_idEnd
= end
; }
159 QWORD
getIdRange() { return ((QWORD
)m_idStart
<< 32) | (QWORD
)m_idEnd
; }
161 void setRepeatInterval(int repeatInterval
) { m_repeatInterval
= repeatInterval
; }
162 int getRepeatInterval() { return m_repeatInterval
; }
164 void setRepeatCount(int repeatCount
) { m_repeatCount
= repeatCount
; }
165 int getRepeatCount() { return m_repeatCount
; }
167 void setRepeatReset(bool resetRepeat
) { m_resetRepeat
= resetRepeat
; }
168 bool isRepeatReset() { return m_resetRepeat
; }
170 const TCHAR
*getRegexpSource() { return CHECK_NULL(m_regexp
); }
176 class LIBNXLP_EXPORTABLE LogParser
178 friend bool LogParserRule::matchInternal(bool, const TCHAR
*, UINT32
, UINT32
, const TCHAR
*, LogParserCallback
, UINT32
, void *);
182 LogParserRule
**m_rules
;
183 StringMap m_contexts
;
185 LogParserCallback m_cb
;
190 CODE_TO_TEXT
*m_eventNameList
;
191 bool (*m_eventResolver
)(const TCHAR
*, UINT32
*);
192 THREAD m_thread
; // Associated thread
193 int m_recordsProcessed
;
194 int m_recordsMatched
;
195 bool m_processAllRules
;
197 void (*m_traceCallback
)(const TCHAR
*, va_list);
198 TCHAR m_status
[MAX_PARSER_STATUS_LEN
];
203 const TCHAR
*checkContext(LogParserRule
*rule
);
204 void trace(int level
, const TCHAR
*format
, ...);
205 bool matchLogRecord(bool hasAttributes
, const TCHAR
*source
, UINT32 eventId
, UINT32 level
, const TCHAR
*line
, UINT32 objectId
);
208 void parseEvent(EVENTLOGRECORD
*rec
);
210 bool monitorEventLogV6(CONDITION stopCondition
);
211 bool monitorEventLogV4(CONDITION stopCondition
);
213 time_t readLastProcessedRecordTimestamp();
218 LogParser(LogParser
*src
);
221 static ObjectArray
<LogParser
> *createFromXml(const char *xml
, int xmlLen
= -1,
222 TCHAR
*errorText
= NULL
, int errBufSize
= 0, bool (*eventResolver
)(const TCHAR
*, UINT32
*) = NULL
);
224 void setFileName(const TCHAR
*name
);
225 const TCHAR
*getFileName() { return m_fileName
; }
227 void setFileEncoding(int encoding
) { m_fileEncoding
= encoding
; }
228 int getFileEncoding() { return m_fileEncoding
; }
230 void setName(const TCHAR
*name
);
231 const TCHAR
*getName() { return m_name
; }
233 void setStatus(const TCHAR
*status
) { nx_strncpy(m_status
, status
, MAX_PARSER_STATUS_LEN
); }
234 const TCHAR
*getStatus() { return m_status
; }
236 void setThread(THREAD th
) { m_thread
= th
; }
237 THREAD
getThread() { return m_thread
; }
239 void setProcessAllFlag(bool flag
) { m_processAllRules
= flag
; }
240 bool getProcessAllFlag() { return m_processAllRules
; }
242 bool addRule(const TCHAR
*regexp
, UINT32 eventCode
= 0, const TCHAR
*eventName
= NULL
, int numParams
= 0, int repeatInterval
= 0, int repeatCount
= 0, bool resetRepeat
= true);
243 bool addRule(LogParserRule
*rule
);
244 void setCallback(LogParserCallback cb
) { m_cb
= cb
; }
245 void setUserArg(void *arg
) { m_userArg
= arg
; }
246 void setEventNameList(CODE_TO_TEXT
*ctt
) { m_eventNameList
= ctt
; }
247 void setEventNameResolver(bool (*cb
)(const TCHAR
*, UINT32
*)) { m_eventResolver
= cb
; }
248 UINT32
resolveEventName(const TCHAR
*name
, UINT32 defVal
= 0);
250 void addMacro(const TCHAR
*name
, const TCHAR
*value
);
251 const TCHAR
*getMacro(const TCHAR
*name
);
253 bool matchLine(const TCHAR
*line
, UINT32 objectId
= 0);
254 bool matchEvent(const TCHAR
*source
, UINT32 eventId
, UINT32 level
, const TCHAR
*line
, UINT32 objectId
= 0);
256 int getProcessedRecordsCount() { return m_recordsProcessed
; }
257 int getMatchedRecordsCount() { return m_recordsMatched
; }
259 int getTraceLevel() { return m_traceLevel
; }
260 void setTraceLevel(int level
) { m_traceLevel
= level
; }
261 void setTraceCallback(void (*cb
)(const TCHAR
*, va_list)) { m_traceCallback
= cb
; }
263 bool monitorFile(CONDITION stopCondition
, bool readFromCurrPos
= true);
265 bool monitorEventLog(CONDITION stopCondition
, const TCHAR
*markerPrefix
);
266 void saveLastProcessedRecordTimestamp(time_t timestamp
);
271 * Init log parser library
273 void LIBNXLP_EXPORTABLE
InitLogParserLibrary();
276 * Cleanup event log parsig library
278 void LIBNXLP_EXPORTABLE
CleanupLogParserLibrary();
281 * Set trace callback for log parser library
283 void LIBNXLP_EXPORTABLE
SetLogParserTraceCallback(void (* traceCallback
)(int, const TCHAR
*, va_list));