summaryrefslogtreecommitdiff
path: root/plugins/imkmsg/kmsg.c
blob: f1815f25f7760cc86cfa427050d3c26d52b8b713 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* imkmsg driver for Linux /dev/kmsg structured logging
 *
 * This contains Linux-specific functionality to read /dev/kmsg
 * For a general overview, see head comment in imkmsg.c.
 * This is heavily based on imklog bsd.c file.
 *
 * Copyright 2008-2012 Adiscon GmbH
 *
 * This file is part of rsyslog.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 *       -or-
 *       see COPYING.ASL20 in the source distribution
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifdef HAVE_CONFIG_H
#	include "config.h"
#endif
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#ifdef  OS_LINUX
#include <sys/klog.h>
#endif
#include <json/json.h>

#include "rsyslog.h"
#include "srUtils.h"
#include "debug.h"
#include "imkmsg.h"

/* globals */
static int	fklog = -1;	/* kernel log fd */

#ifndef _PATH_KLOG
#	define _PATH_KLOG "/dev/kmsg"
#endif

/* submit a message to imkmsg Syslog() API. In this function, we parse
 * necessary information from kernel log line, and make json string
 * from the rest.
 */
static void
submitSyslog(uchar *buf)
{
	long offs = 0;
	struct timeval tv;
	long int timestamp = 0;
	struct timespec monotonic;
	struct timespec realtime;
	char name[1024];
	char value[1024];
	char msg[1024];
	int priority = 0;
	long int sequnum = 0;
	struct json_object *json = NULL, *jval;

	/* create new json object */
	json = json_object_new_object();

	/* get priority */
	for (; isdigit(*buf); buf++) {
		priority += (priority * 10) + (*buf - '0');
	}
	buf++;

	/* get messages sequence number and add it to json */
	for (; isdigit(*buf); buf++) {
		sequnum = (sequnum * 10) + (*buf - '0');
	}
	buf++; /* skip , */
	jval = json_object_new_int(sequnum);
	json_object_object_add(json, "sequnum", jval);

	/* get timestamp */
	for (; isdigit(*buf); buf++) {
		timestamp += (timestamp * 10) + (*buf - '0');
	}

	while (*buf != ';') {
		buf++; /* skip everything till the first ; */
	} 
	buf++; /* skip ; */

	/* get message */
	offs = 0;
	for (; *buf != '\n' && *buf != '\0'; buf++, offs++) {
		msg[offs] = *buf;
	}
	msg[offs] = '\0';
	jval = json_object_new_string((char*)msg);
	json_object_object_add(json, "msg", jval);

	if (*buf != '\0') /* message has appended properties, skip \n */
		buf++;

	while (*buf) {
		/* get name of the property */
		buf++; /* skip ' ' */
		offs = 0;
		for (; *buf != '=' && *buf != ' '; buf++, offs++) {
			name[offs] = *buf;
		}
		name[offs] = '\0';
		buf++; /* skip = or ' ' */;

		offs = 0;
		for (; *buf != '\n' && *buf != '\0'; buf++, offs++) {
			value[offs] = *buf;
		}
		value[offs] = '\0';
		if (*buf != '\0') {
			buf++; /* another property, skip \n */
		}

		jval = json_object_new_string((char*)value);
		json_object_object_add(json, name, jval);
	}

	/* calculate timestamp */
	clock_gettime(CLOCK_MONOTONIC, &monotonic);
	clock_gettime(CLOCK_REALTIME, &realtime);
	tv.tv_sec = realtime.tv_sec + ((timestamp / 1000000l) - monotonic.tv_sec);
	tv.tv_usec = (realtime.tv_nsec + ((timestamp / 1000000000l) - monotonic.tv_nsec)) / 1000;

	Syslog(priority, (uchar *)msg, &tv, json);
}


/* open the kernel log - will be called inside the willRun() imkmsg entry point
 */
rsRetVal
klogWillRun(modConfData_t *pModConf)
{
	char errmsg[2048];
	int r;
	DEFiRet;

	fklog = open(_PATH_KLOG, O_RDONLY, 0);
	if (fklog < 0) {
		imkmsgLogIntMsg(RS_RET_ERR_OPEN_KLOG, "imkmsg: cannot open kernel log(%s): %s.",
			_PATH_KLOG, rs_strerror_r(errno, errmsg, sizeof(errmsg)));
		ABORT_FINALIZE(RS_RET_ERR_OPEN_KLOG);
	}

	/* Set level of kernel console messaging.. */
	if(pModConf->console_log_level != -1) {
		r = klogctl(8, NULL, pModConf->console_log_level);
		if(r != 0) {
			imkmsgLogIntMsg(LOG_WARNING, "imkmsg: cannot set console log level: %s",
				rs_strerror_r(errno, errmsg, sizeof(errmsg)));
			/* make sure we do not try to re-set! */
			pModConf->console_log_level = -1;
		}
	}

finalize_it:
	RETiRet;
}

/* Read kernel log while data are available, each read() reads one
 * record of printk buffer.
 */
static void
readkmsg(void)
{
	int i;
	uchar pRcv[8192+1];
	char errmsg[2048];

	for (;;) {
		dbgprintf("imkmsg waiting for kernel log line\n");

		/* every read() from the opened device node receives one record of the printk buffer */
		i = read(fklog, pRcv, 8192);

		if (i > 0) {
			/* successful read of message of nonzero length */
			pRcv[i] = '\0';
		} else if (i == -EPIPE) {
			imkmsgLogIntMsg(LOG_WARNING,
					"imkmsg: some messages in circular buffer got overwritten");
			continue;
		} else {
			/* something went wrong - error or zero length message */
			if (i < 0 && errno != EINTR && errno != EAGAIN) {
				/* error occured */
				imkmsgLogIntMsg(LOG_ERR,
				       "imkmsg: error reading kernel log - shutting down: %s",
					rs_strerror_r(errno, errmsg, sizeof(errmsg)));
				fklog = -1;
			}
			break;
		}

		submitSyslog(pRcv);
	}
}


/* to be called in the module's AfterRun entry point
 * rgerhards, 2008-04-09
 */
rsRetVal klogAfterRun(modConfData_t *pModConf)
{
	DEFiRet;
	if(fklog != -1)
		close(fklog);
	/* Turn on logging of messages to console, but only if a log level was speficied */
	if(pModConf->console_log_level != -1)
		klogctl(7, NULL, 0);
	RETiRet;
}


/* to be called in the module's WillRun entry point, this is the main
 * "message pull" mechanism.
 * rgerhards, 2008-04-09
 */
rsRetVal klogLogKMsg(modConfData_t __attribute__((unused)) *pModConf)
{
	DEFiRet;
	readkmsg();
	RETiRet;
}


/* provide the (system-specific) default facility for internal messages
 * rgerhards, 2008-04-14
 */
int
klogFacilIntMsg(void)
{
	return LOG_SYSLOG;
}