From 14989708b551081380a96c25ece225e3db4d99a1 Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Tue, 20 Oct 2015 11:51:29 +0200 Subject: [PATCH] log_systemd: module to provide logging to systemd journal from config file --- modules/log_systemd/Makefile | 25 ++++ modules/log_systemd/README | 87 ++++++++++++ modules/log_systemd/doc/Makefile | 4 + modules/log_systemd/doc/log_systemd.xml | 37 +++++ modules/log_systemd/doc/log_systemd_admin.xml | 84 +++++++++++ modules/log_systemd/log_systemd_mod.c | 133 ++++++++++++++++++ 6 files changed, 370 insertions(+) create mode 100644 modules/log_systemd/Makefile create mode 100644 modules/log_systemd/README create mode 100644 modules/log_systemd/doc/Makefile create mode 100644 modules/log_systemd/doc/log_systemd.xml create mode 100644 modules/log_systemd/doc/log_systemd_admin.xml create mode 100644 modules/log_systemd/log_systemd_mod.c diff --git a/modules/log_systemd/Makefile b/modules/log_systemd/Makefile new file mode 100644 index 00000000000..97fb9e9a654 --- /dev/null +++ b/modules/log_systemd/Makefile @@ -0,0 +1,25 @@ +# +# WARNING: do not run this directly, it should be run by the master Makefile + +include ../../Makefile.defs +auto_gen= +NAME=log_systemd.so + +ifeq ($(CROSS_COMPILE),) +SYSTEMD_BUILDER=$(shell \ + if pkg-config --exists libsystemd; then \ + echo 'pkg-config libsystemd'; \ + fi) +endif + +ifneq ($(SYSTEMD_BUILDER),) + DEFS += $(shell $(SYSTEMD_BUILDER) --cflags) + LIBS += $(shell $(SYSTEMD_BUILDER) --libs) +else + DEFS += -I$(LOCALBASE)/include + LIBS= -L$(LOCALBASE)/lib -lsystemd +endif + +DEFS+=-DKAMAILIO_MOD_INTERFACE + +include ../../Makefile.modules diff --git a/modules/log_systemd/README b/modules/log_systemd/README new file mode 100644 index 00000000000..739f405e855 --- /dev/null +++ b/modules/log_systemd/README @@ -0,0 +1,87 @@ +LOG_SYSTEMD Module + +Daniel-Constantin Mierla + + + +Edited by + +Daniel-Constantin Mierla + + + + Copyright © 2015 asipto.com + __________________________________________________________________ + + Table of Contents + + 1. Admin Guide + + 1. Overview + 2. Dependencies + + 2.1. Kamailio Modules + 2.2. External Libraries or Applications + + 3. Functions + + 3.1. sd_journal_print(level, text) + + List of Examples + + 1.1. sd_journal_print usage + +Chapter 1. Admin Guide + + Table of Contents + + 1. Overview + 2. Dependencies + + 2.1. Kamailio Modules + 2.2. External Libraries or Applications + + 3. Functions + + 3.1. sd_journal_print(level, text) + +1. Overview + + This module provides logging to systemd journal from configuration + file. + +2. Dependencies + + 2.1. Kamailio Modules + 2.2. External Libraries or Applications + +2.1. Kamailio Modules + + The following modules must be loaded before this module: + * none. + +2.2. External Libraries or Applications + + The following libraries or applications must be installed before + running Kamailio with this module loaded: + * libsystemd + +3. Functions + + 3.1. sd_journal_print(level, text) + +3.1. sd_journal_print(level, text) + + Print the text in the systemd journal at the provided level parameter. + + Both parameters can contain variables. + + For more, see the manual page for function sd_journal_print() in C + library libsystemd. + + This function can be used from ANY_ROUTE. + + Example 1.1. sd_journal_print usage +... + sd_journal_print("LOG_INFO", "R-URI is $ru\n"); +... diff --git a/modules/log_systemd/doc/Makefile b/modules/log_systemd/doc/Makefile new file mode 100644 index 00000000000..b931ea82723 --- /dev/null +++ b/modules/log_systemd/doc/Makefile @@ -0,0 +1,4 @@ +docs = log_systemd.xml + +docbook_dir = ../../../docbook +include $(docbook_dir)/Makefile.module diff --git a/modules/log_systemd/doc/log_systemd.xml b/modules/log_systemd/doc/log_systemd.xml new file mode 100644 index 00000000000..0f9dbf5ee6c --- /dev/null +++ b/modules/log_systemd/doc/log_systemd.xml @@ -0,0 +1,37 @@ + + + +%docentities; + +]> + + + + LOG_SYSTEMD Module + sip-router.org + + + Daniel-Constantin + Mierla + miconda@gmail.com + + + Daniel-Constantin + Mierla + miconda@gmail.com + + + + 2015 + asipto.com + + + + + + + + diff --git a/modules/log_systemd/doc/log_systemd_admin.xml b/modules/log_systemd/doc/log_systemd_admin.xml new file mode 100644 index 00000000000..11d3295131a --- /dev/null +++ b/modules/log_systemd/doc/log_systemd_admin.xml @@ -0,0 +1,84 @@ + + + +%docentities; + +]> + + + + + &adminguide; + +
+ Overview + + This module provides logging to systemd journal from configuration + file. + +
+ +
+ Dependencies +
+ &kamailio; Modules + + The following modules must be loaded before this module: + + + + none. + + + + +
+
+ External Libraries or Applications + + The following libraries or applications must be installed before running + &kamailio; with this module loaded: + + + + libsystemd + + + + +
+
+ +
+ Functions +
+ + <function moreinfo="none">sd_journal_print(level, text)</function> + + + Print the text in the systemd journal at the provided level parameter. + + + Both parameters can contain variables. + + + For more, see the manual page for function sd_journal_print() in C library libsystemd. + + + This function can be used from ANY_ROUTE. + + + <function>sd_journal_print</function> usage + +... + sd_journal_print("LOG_INFO", "R-URI is $ru\n"); +... + + +
+
+
+ diff --git a/modules/log_systemd/log_systemd_mod.c b/modules/log_systemd/log_systemd_mod.c new file mode 100644 index 00000000000..f84d057a4cf --- /dev/null +++ b/modules/log_systemd/log_systemd_mod.c @@ -0,0 +1,133 @@ +/** + * Copyright (C) 2015 Daniel-Constantin Mierla (asipto.com) + * + * This file is part of Kamailio, a free SIP server. + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version + * + * + * This file is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include +#include +#include +#include +#include + +#include "../../sr_module.h" +#include "../../dprint.h" +#include "../../ut.h" +#include "../../mod_fix.h" + +MODULE_VERSION + +static int mod_init(void); +static int child_init(int); +static void mod_destroy(void); + +static int w_sd_journal_print(struct sip_msg* msg, char* lev, char* txt); + + +static cmd_export_t cmds[]={ + {"sd_journal_print", (cmd_function)w_sd_journal_print, 2, fixup_spve_spve, + 0, ANY_ROUTE}, + {0, 0, 0, 0, 0, 0} +}; + +static param_export_t params[]={ + {0, 0, 0} +}; + +struct module_exports exports = { + "log_systemd", + DEFAULT_DLFLAGS, /* dlopen flags */ + cmds, + params, + 0, + 0, /* exported MI functions */ + 0, /* exported pseudo-variables */ + 0, /* extra processes */ + mod_init, /* module initialization function */ + 0, /* response function */ + mod_destroy, /* destroy function */ + child_init /* per child init function */ +}; + + + +/** + * init module function + */ +static int mod_init(void) +{ + return 0; +} + +/** + * @brief Initialize async module children + */ +static int child_init(int rank) +{ + return 0; +} +/** + * destroy module function + */ +static void mod_destroy(void) +{ +} + +/** + * + */ +static int w_sd_journal_print(struct sip_msg* msg, char* lev, char* txt) +{ + str slev; + str stxt; + int ilev; + + if(fixup_get_svalue(msg, (gparam_t*)lev, &slev)!=0) { + LM_ERR("unable to get level parameter\n"); + return -1; + } + + if(fixup_get_svalue(msg, (gparam_t*)txt, &stxt)!=0) { + LM_ERR("unable to get text parameter\n"); + return -1; + } + + /* one of LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, + * LOG_NOTICE, LOG_INFO, LOG_DEBUG, as defined in syslog.h, see syslog(3) */ + ilev = LOG_DEBUG; + if(slev.len==9 && strncasecmp(slev.s, "LOG_EMERG", slev.len)==0) { + ilev = LOG_EMERG; + } else if(slev.len==9 && strncasecmp(slev.s, "LOG_ALERT", slev.len)==0) { + ilev = LOG_ALERT; + } else if(slev.len==8 && strncasecmp(slev.s, "LOG_CRIT", slev.len)==0) { + ilev = LOG_CRIT; + } else if(slev.len==7 && strncasecmp(slev.s, "LOG_ERR", slev.len)==0) { + ilev = LOG_ERR; + } else if(slev.len==11 && strncasecmp(slev.s, "LOG_WARNING", slev.len)==0) { + ilev = LOG_WARNING; + } else if(slev.len==10 && strncasecmp(slev.s, "LOG_NOTICE", slev.len)==0) { + ilev = LOG_NOTICE; + } else if(slev.len==8 && strncasecmp(slev.s, "LOG_INFO", slev.len)==0) { + ilev = LOG_INFO; + } + + sd_journal_print(ilev, "%.*s", stxt.len, stxt.s); + + return 1; +}