summaryrefslogtreecommitdiffstats
path: root/output/ulogd_output_SYSLOG.c
blob: 591391b272546ddaabcded88dbb1f5a3052bedf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* ulogd_SYSLOG.c, Version $Revision$
 *
 * ulogd output target for real syslog() logging
 *
 * This target produces a syslog entries identical to the LOG target.
 *
 * (C) 2003-2005 by Harald Welte <laforge@gnumonks.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 
 *  as published by the Free Software Foundation
 *
 *  This program 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id$
 *
 */
#include "config.h"
#include <ulogd/ulogd.h>
#include <ulogd/common.h>
#include <ulogd/plugin.h>
#include <unistd.h>
#include <syslog.h>

static struct ulogd_key syslog_inp[] = {
	KEY(STRING, "print"),
};

static const struct config_keyset syslog_kset = {
	.num_ces = 2,
	.ces = {
		CONFIG_KEY_STR("facility",  "LOG_KERN"),
		CONFIG_KEY_STR("level", "LOG_NOTICE"),
	},
};

struct syslog_instance {
	int level;
	int facility;
};

static int syslog_interp(struct ulogd_pluginstance *upi, unsigned *flags)
{
	struct syslog_instance *li = upi_priv(upi);
	struct ulogd_key *res = upi->input.keys;

	if (res[0].source->flags & ULOGD_RETF_VALID)
		syslog(li->level | li->facility, "%s", key_str(&res[0]));

	return 0;
}

static int syslog_configure(struct ulogd_pluginstance *pi)
{
	struct syslog_instance *priv = upi_priv(pi);
	const char *facility, *level;

	facility = pi->config_kset->ces[0].u.string;
	level = pi->config_kset->ces[1].u.string;

	priv->facility = nv_get_value(nv_facility, FACILITY_LEN, facility);
	priv->level = nv_get_value(nv_level, LEVEL_LEN, level);
	if (priv->facility < 0 || priv->level < 0) {
		upi_log(pi, ULOGD_FATAL, "invalid syslog facility or level\n");
		return ULOGD_IRET_ERR;
	}

	return ULOGD_IRET_OK;
}

static int syslog_start(struct ulogd_pluginstance *pi)
{
	openlog("ulogd", LOG_NDELAY|LOG_PID, LOG_DAEMON);

	return 0;
}

static int
syslog_stop(struct ulogd_pluginstance *pi)
{
	closelog();

	return ULOGD_IRET_OK;
}

static struct ulogd_plugin syslog_plugin = {
	.name = "SYSLOG",
	.flags = ULOGD_PF_RECONF,
	.input = {
		.keys = syslog_inp,
		.num_keys = ARRAY_SIZE(syslog_inp),
		.type = ULOGD_DTYPE_PACKET | ULOGD_DTYPE_FLOW,
	},
	.output = {
		.type = ULOGD_DTYPE_SINK,
	},
	.config_kset	= &syslog_kset,
	.priv_size	= sizeof(struct syslog_instance),
	.configure	= syslog_configure,
	.start		= syslog_start,
	.stop		= syslog_stop,
	.interp		= syslog_interp,
	.rev		= ULOGD_PLUGIN_REVISION,
};

void __upi_ctor init(void);

void init(void)
{
	ulogd_register_plugin(&syslog_plugin);
}