|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* @file liblognorm.h
|
|
Packit |
1422b7 |
* @brief The public liblognorm API.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* Functions other than those defined here MUST not be called by
|
|
Packit |
1422b7 |
* a liblognorm "user" application.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* This file is meant to be included by applications using liblognorm.
|
|
Packit |
1422b7 |
* For lognorm library files themselves, include "lognorm.h".
|
|
Packit |
1422b7 |
*//**
|
|
Packit |
1422b7 |
* @mainpage
|
|
Packit |
1422b7 |
* Liblognorm is an easy to use and fast samples-based log normalization
|
|
Packit |
1422b7 |
* library.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* It can be passed a stream of arbitrary log messages, one at a time, and for
|
|
Packit |
1422b7 |
* each message it will output well-defined name-value pairs and a set of
|
|
Packit |
1422b7 |
* tags describing the message.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* For further details, see it's initial announcement available at
|
|
Packit |
1422b7 |
* http://blog.gerhards.net/2010/10/introducing-liblognorm.html
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* The public interface of this library is describe in liblognorm.h.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* Liblognorm fully supports Unicode. Like most Linux tools, it operates
|
|
Packit |
1422b7 |
* on UTF-8 natively, called "passive mode". This was decided because we
|
|
Packit |
1422b7 |
* so can keep the size of data structures small while still supporting
|
|
Packit |
1422b7 |
* all of the world's languages (actually more than when we did UCS-2).
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* At the technical level, we can handle UTF-8 multibyte sequences transparently.
|
|
Packit |
1422b7 |
* Liblognorm needs to look at a few US-ASCII characters to do the
|
|
Packit |
1422b7 |
* sample base parsing (things to indicate fields), so this is no
|
|
Packit |
1422b7 |
* issue. Inside the parse tree, a multibyte sequence can simple be processed
|
|
Packit |
1422b7 |
* as if it were a sequence of different characters that make up a their
|
|
Packit |
1422b7 |
* own symbol each. In fact, this even allows for somewhat greater parsing
|
|
Packit |
1422b7 |
* speed.
|
|
Packit |
1422b7 |
*//*
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* liblognorm - a fast samples-based log normalization library
|
|
Packit |
1422b7 |
* Copyright 2010-2017 by Rainer Gerhards and Adiscon GmbH.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* Modified by Pavel Levshin (pavel@levshin.spb.ru) in 2013
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* This file is part of liblognorm.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* This library is free software; you can redistribute it and/or
|
|
Packit |
1422b7 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
1422b7 |
* License as published by the Free Software Foundation; either
|
|
Packit |
1422b7 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
1422b7 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
1422b7 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
1422b7 |
* Lesser General Public License for more details.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
1422b7 |
* License along with this library; if not, write to the Free Software
|
|
Packit |
1422b7 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* A copy of the LGPL v2.1 can be found in the file "COPYING" in this distribution.
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
#ifndef LIBLOGNORM_H_INCLUDED
|
|
Packit |
1422b7 |
#define LIBLOGNORM_H_INCLUDED
|
|
Packit |
1422b7 |
#include <stdlib.h> /* we need size_t */
|
|
Packit |
1422b7 |
#include <json.h>
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/* error codes */
|
|
Packit |
1422b7 |
#define LN_NOMEM -1
|
|
Packit |
1422b7 |
#define LN_INVLDFDESCR -1
|
|
Packit |
1422b7 |
#define LN_BADCONFIG -250
|
|
Packit |
1422b7 |
#define LN_BADPARSERSTATE -500
|
|
Packit |
1422b7 |
#define LN_WRONGPARSER -1000
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
#define LN_RB_LINE_TOO_LONG -1001
|
|
Packit |
1422b7 |
#define LN_OVER_SIZE_LIMIT -1002
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* The library context descriptor.
|
|
Packit |
1422b7 |
* This is used to permit multiple independent instances of the
|
|
Packit |
1422b7 |
* library to be called within a single program. This is most
|
|
Packit |
1422b7 |
* useful for plugin-based architectures.
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
typedef struct ln_ctx_s* ln_ctx;
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/* API */
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* Return library version string.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* Returns the version of the currently used library.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @return Zero-Terminated library version string.
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
/* Note: this MUST NOT be inline to make sure the actual library
|
|
Packit |
1422b7 |
* has the right version, not just what was used to compile!
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
const char *ln_version(void);
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* Return if library is build with advanced statistics
|
|
Packit |
1422b7 |
* activated.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @return 1 if advanced stats are active, 0 if not
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
int ln_hasAdvancedStats(void);
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* Initialize a library context.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* To prevent memory leaks, ln_exitCtx() must be called on a library
|
|
Packit |
1422b7 |
* context that is no longer needed.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @return new library context or NULL if an error occured
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
ln_ctx ln_initCtx(void);
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* Inherit control attributes from a library context.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* This does not copy the parse-tree, but does copy
|
|
Packit |
1422b7 |
* behaviour-controling attributes such as enableRegex.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* Just as with ln_initCtx, ln_exitCtx() must be called on a library
|
|
Packit |
1422b7 |
* context that is no longer needed.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @return new library context or NULL if an error occured
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
ln_ctx ln_inherittedCtx(ln_ctx parent);
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* Discard a library context.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* Free's the ressources associated with the given library context. It
|
|
Packit |
1422b7 |
* MUST NOT be accessed after calling this function.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @param ctx The context to be discarded.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @return Returns zero on success, something else otherwise.
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
int ln_exitCtx(ln_ctx ctx);
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/* binary values, so that we can "or" them together */
|
|
Packit |
1422b7 |
#define LN_CTXOPT_ALLOW_REGEX 0x01 /**< permit regex matching */
|
|
Packit |
1422b7 |
#define LN_CTXOPT_ADD_EXEC_PATH 0x02 /**< add exec_path attribute (time-consuming!) */
|
|
Packit |
1422b7 |
#define LN_CTXOPT_ADD_ORIGINALMSG 0x04 /**< always add original message to output
|
|
Packit |
1422b7 |
(not just in error case) */
|
|
Packit |
1422b7 |
#define LN_CTXOPT_ADD_RULE 0x08 /**< add mockup rule */
|
|
Packit |
1422b7 |
#define LN_CTXOPT_ADD_RULE_LOCATION 0x10 /**< add rule location (file, lineno) to metadata */
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* Set options on ctx.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @param ctx The context to be modified.
|
|
Packit |
1422b7 |
* @param opts a potentially or-ed list of options, see LN_CTXOPT_*
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
void
|
|
Packit |
1422b7 |
ln_setCtxOpts(ln_ctx ctx, unsigned opts);
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* Set a debug message handler (callback).
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* Liblognorm can provide helpful information for debugging
|
|
Packit |
1422b7 |
* - it's internal processing
|
|
Packit |
1422b7 |
* - the way a log message is being normalized
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* It does so by emiting "interesting" information about its processing
|
|
Packit |
1422b7 |
* at various stages. A caller can obtain this information by registering
|
|
Packit |
1422b7 |
* an entry point. When done so, liblognorm will call the entry point
|
|
Packit |
1422b7 |
* whenever it has something to emit. Note that debugging can be rather
|
|
Packit |
1422b7 |
* verbose.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* The callback will be called with the following three parameters in that order:
|
|
Packit |
1422b7 |
* - the caller-provided cookie
|
|
Packit |
1422b7 |
* - a zero-terminated string buffer
|
|
Packit |
1422b7 |
* - the length of the string buffer, without the trailing NUL byte
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @note
|
|
Packit |
1422b7 |
* The provided callback function must not call any liblognorm
|
|
Packit |
1422b7 |
* APIs except when specifically flagged as safe for calling by a debug
|
|
Packit |
1422b7 |
* callback handler.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @param[in] ctx The library context to apply callback to.
|
|
Packit |
1422b7 |
* @param[in] cb The function to be called for debugging
|
|
Packit |
1422b7 |
* @param[in] cookie Opaque cookie to be passed down to debug handler. Can be
|
|
Packit |
1422b7 |
* used for some state tracking by the caller. This is defined as
|
|
Packit |
1422b7 |
* void* to support pointers. To play it safe, a pointer should be
|
|
Packit |
1422b7 |
* passed (but advantorous folks may also use an unsigned).
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @return Returns zero on success, something else otherwise.
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
int ln_setDebugCB(ln_ctx ctx, void (*cb)(void*, const char*, size_t), void *cookie);
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* Set a error message handler (callback).
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* If set, this is used to emit error messages of interest to the user, e.g.
|
|
Packit |
1422b7 |
* on failures during rulebase load. It is suggested that a caller uses this
|
|
Packit |
1422b7 |
* feedback to aid its users in resolving issues.
|
|
Packit |
1422b7 |
* Its semantics are otherwise exactly the same like ln_setDebugCB().
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
int ln_setErrMsgCB(ln_ctx ctx, void (*cb)(void*, const char*, size_t), void *cookie);
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* enable or disable debug mode.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @param[in] ctx context
|
|
Packit |
1422b7 |
* @param[in] b boolean 0 - disable debug mode, 1 - enable debug mode
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
void ln_enableDebug(ln_ctx ctx, int i);
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* Load a (log) sample file.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* The file must contain log samples in syntactically correct format. Samples are added
|
|
Packit |
1422b7 |
* to set already loaded in the current context. If there is a sample with duplicate
|
|
Packit |
1422b7 |
* semantics, this sample will be ignored. Most importantly, this can \b not be used
|
|
Packit |
1422b7 |
* to change tag assignments for a given sample.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @param[in] ctx The library context to apply callback to.
|
|
Packit |
1422b7 |
* @param[in] file Name of file to be loaded.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @return Returns zero on success, something else otherwise.
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
int ln_loadSamples(ln_ctx ctx, const char *file);
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* Load a rulebase via a string.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* Note: this can only load v2 samples, v1 is NOT supported.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @param[in] ctx The library context to apply callback to.
|
|
Packit |
1422b7 |
* @param[in] string The string with the actual rulebase.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @return Returns zero on success, something else otherwise.
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
int ln_loadSamplesFromString(ln_ctx ctx, const char *string);
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
/**
|
|
Packit |
1422b7 |
* Normalize a message.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* This is the main library entry point. It is called with a message
|
|
Packit |
1422b7 |
* to normalize and will return a normalized in-memory representation
|
|
Packit |
1422b7 |
* of it.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* If an error occurs, the function returns -1. In that case, an
|
|
Packit |
1422b7 |
* in-memory event representation has been generated if event is
|
|
Packit |
1422b7 |
* non-NULL. In that case, the event contains further error details in
|
|
Packit |
1422b7 |
* normalized form.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @note
|
|
Packit |
1422b7 |
* This function works on byte-counted strings and as such is able to
|
|
Packit |
1422b7 |
* process NUL bytes if they occur inside the message. On the other hand,
|
|
Packit |
1422b7 |
* this means the the correct messages size, \b excluding the NUL byte,
|
|
Packit |
1422b7 |
* must be provided.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @param[in] ctx The library context to use.
|
|
Packit |
1422b7 |
* @param[in] str The message string (see note above).
|
|
Packit |
1422b7 |
* @param[in] strLen The length of the message in bytes.
|
|
Packit |
1422b7 |
* @param[out] json_p A new event record or NULL if an error occured. Must be
|
|
Packit |
1422b7 |
* destructed if no longer needed.
|
|
Packit |
1422b7 |
*
|
|
Packit |
1422b7 |
* @return Returns zero on success, something else otherwise.
|
|
Packit |
1422b7 |
*/
|
|
Packit |
1422b7 |
int ln_normalize(ln_ctx ctx, const char *str, const size_t strLen, struct json_object **json_p);
|
|
Packit |
1422b7 |
|
|
Packit |
1422b7 |
#endif /* #ifndef LOGNORM_H_INCLUDED */
|