summaryrefslogtreecommitdiffstats
path: root/output/ulogd_output_LOGEMU.c
blob: f82b10dff4db849fec73608d1e9085932b85b1e7 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* ulogd_LOGEMU.c, Version $Revision$
 *
 * ulogd output target for syslog logging emulation
 *
 * This target produces a file which looks the same like the syslog-entries
 * of the LOG target.
 *
 * (C) 2000-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/conffile.h>
#include <ulogd/plugin.h>
#include <unistd.h>
#include <string.h>
#include <time.h>

#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX	256
#endif

#ifndef ULOGD_LOGEMU_DEFAULT
#define ULOGD_LOGEMU_DEFAULT	"/var/log/ulogd.syslogemu"
#endif

#ifndef ULOGD_LOGEMU_SYNC_DEFAULT
#define ULOGD_LOGEMU_SYNC_DEFAULT	0
#endif

static char hostname[HOST_NAME_MAX+1];

static struct ulogd_key logemu_inp[] = {
	KEY(STRING, "print"),
	KEY_FLAGS(UINT32, "oob.time.sec", ULOGD_KEYF_OPTIONAL),
};

static const struct config_keyset logemu_kset = {
	.num_ces = 2,
	.ces = {
		{
			.key 	 = "file",
			.type	 = CONFIG_TYPE_STRING,
			.options = CONFIG_OPT_NONE,
			.u	 = { .string = ULOGD_LOGEMU_DEFAULT },
		},
		{
			.key	 = "sync",
			.type	 = CONFIG_TYPE_INT,
			.options = CONFIG_OPT_NONE,
			.u	 = { .value = ULOGD_LOGEMU_SYNC_DEFAULT },
		},
	},
};

struct logemu_instance {
	FILE *of;
};

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

	if (res[0].u.source->flags & ULOGD_RETF_VALID) {
		char *timestr;
		char *tmp;
		time_t now;

		if (res[1].u.source && (res[1].u.source->flags & ULOGD_RETF_VALID))
			now = (time_t) key_src_u32(&res[1]);
		else
			now = time(NULL);

		timestr = ctime(&now) + 4;
		if ((tmp = strchr(timestr, '\n')))
			*tmp = '\0';

		fprintf(li->of, "%.15s %s %s", timestr, hostname, key_src_str(&res[0]));

		if (upi->config_kset->ces[1].u.value)
			fflush(li->of);
	}

	return 0;
}

static int start_logemu(struct ulogd_pluginstance *pi)
{
	struct logemu_instance *li = upi_priv(pi);
	char *tmp;

	upi_log(pi, ULOGD_INFO, "starting logemu\n");

#ifdef DEBUG_LOGEMU
	li->of = stdout;
#else
	upi_log(pi, ULOGD_DEBUG, "opening file: %s\n",
		  pi->config_kset->ces[0].u.string);
	li->of = fopen(pi->config_kset->ces[0].u.string, "a");
	if (!li->of) {
		upi_log(pi, ULOGD_FATAL, "can't open syslogemu: %m\n");
		return errno;
	}		
#endif

	if (gethostname(hostname, sizeof(hostname)) < 0) {
		upi_log(pi, ULOGD_FATAL, "get hostname: %m\n");
		return -EINVAL;
	}

	/* truncate hostname */
	if ((tmp = strchr(hostname, '.')))
		*tmp = '\0';

	return 0;
}

static int fini_logemu(struct ulogd_pluginstance *pi) {
	struct logemu_instance *li = upi_priv(pi);

	if (li->of != stdout)
		fclose(li->of);

	return 0;
}

static struct ulogd_plugin logemu_plugin = { 
	.name = "LOGEMU",
	.input = {
		.keys = logemu_inp,
		.num_keys = ARRAY_SIZE(logemu_inp),
		.type = ULOGD_DTYPE_PACKET | ULOGD_DTYPE_FLOW,
	},
	.output = {
		.type = ULOGD_DTYPE_SINK,
	},
	.config_kset 	= &logemu_kset,
	.priv_size 	= sizeof(struct logemu_instance),
	.start	 	= &start_logemu,
	.stop	 	= &fini_logemu,

	.interp 	= &_output_logemu, 
	.rev		= ULOGD_PLUGIN_REVISION,
};

void __upi_ctor init(void);

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