summaryrefslogtreecommitdiff
path: root/src/pmdas/logger/event.c
blob: 19ffda9457d46b84d5dd759b2a0013bc3a9420eb (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
 * Event support for the Logger PMDA
 *
 * Copyright (c) 2011-2012 Red Hat.
 * Copyright (c) 2011 Nathan Scott.  All rights reserved.
 * 
 * This program 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 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.
 */

#include "event.h"
#include "pmda.h"
#include "util.h"
#include <ctype.h>
#ifdef HAVE_REGEX_H
#include <regex.h>
#endif

static int numlogfiles;
static event_logfile_t *logfiles;

void
event_init(pmID pmid)
{
    char cmd[MAXPATHLEN];
    int	i, fd;

    for (i = 0; i < numlogfiles; i++) {
	size_t pathlen = strlen(logfiles[i].pathname);

	/*
	 * We support 2 kinds of PATHNAMEs:
	 * (1) Regular paths.  These paths are opened normally.
	 * (2) Pipes.  If the path ends in '|', the filename is
	 *     interpreted as a command which pipes input to us.
	 */
	if (logfiles[i].pathname[pathlen - 1] != '|') {
	    fd = open(logfiles[i].pathname, O_RDONLY|O_NONBLOCK);
	    if (fd < 0) {
		if (logfiles[i].fd >= 0)	/* log once only */
		    __pmNotifyErr(LOG_ERR, "open: %s - %s",
				logfiles[i].pathname, strerror(errno));
	    } else {
		if (fstat(fd, &logfiles[i].pathstat) < 0)
		    if (logfiles[i].fd >= 0)	/* log once only */
			__pmNotifyErr(LOG_ERR, "fstat: %s - %s",
				    logfiles[i].pathname, strerror(errno));
		lseek(fd, 0, SEEK_END);
	    }
	}
	else {
	    strncpy(cmd, logfiles[i].pathname, sizeof(cmd));
	    cmd[pathlen - 1] = '\0';	/* get rid of the '|' */
	    rstrip(cmd);	/* Remove all trailing whitespace. */
	    fd = start_cmd(cmd, &logfiles[i].pid);
	    if (fd < 0) {
		if (logfiles[i].fd >= 0)	/* log once only */
		    __pmNotifyErr(LOG_ERR, "pipe: %s - %s",
					logfiles[i].pathname, strerror(errno));
	    } else {
		if (fd > maxfd)
		    maxfd = fd;
		FD_SET(fd, &fds);
	    }
	}

	logfiles[i].fd = fd;		/* keep file descriptor (or error) */
	logfiles[i].pmid = pmid;	/* string param metric identifier */
	logfiles[i].queueid = pmdaEventNewQueue(logfiles[i].pmnsname, maxmem);
    }
}

void
event_shutdown(void)
{
    int i;

    __pmNotifyErr(LOG_INFO, "%s: Shutting down...", __FUNCTION__);

    for (i = 0; i < numlogfiles; i++) {
	if (logfiles[i].pid != 0) {
	    stop_cmd(logfiles[i].pid);
	    logfiles[i].pid = 0;
	}
	if (logfiles[i].fd > 0) {
	    close(logfiles[i].fd);
	    logfiles[i].fd = 0;
	}
    }
}

/*
 * Ensure given name (identifier) can be used as a namespace entry.
 */
static int
valid_pmns_name(char *name)
{
    if (!isalpha((int)name[0]))
	return 0;
    for (; *name != '\0'; name++)
	if (!isalnum((int)*name) && *name != '_')
	    return 0;
    return 1;
}

/*
 * Parse the configuration file and do initial data structure setup.
 */
int
event_config(const char *fname)
{
    FILE		*configFile;
    event_logfile_t	*logfile;
    int			sts = 0;
    size_t		len;
    char		line[MAXPATHLEN * 2];
    char		*ptr, *name, *noaccess;

    configFile = fopen(fname, "r");
    if (configFile == NULL) {
	__pmNotifyErr(LOG_ERR, "event_config: %s: %s", fname, strerror(errno));
	return -1;
    }

    while (!feof(configFile)) {
	if (fgets(line, sizeof(line), configFile) == NULL) {
	    if (feof(configFile))
		break;
	    __pmNotifyErr(LOG_ERR, "event_config: fgets: %s", strerror(errno));
	    sts = -1;
	    break;
	}

	/*
	 * fgets() puts the '\n' at the end of the buffer.  Remove
	 * it.  If it isn't there, that must mean that the line is
	 * longer than our buffer.
	 */
	len = strlen(line);
	if (len == 0)			/* Ignore empty strings. */
	    continue;
	if (line[len - 1] != '\n') { /* String must be too long */
	    __pmNotifyErr(LOG_ERR, "event_config: config line too long: '%s'",
			  line);
	    sts = -1;
	    break;
	}
	line[len - 1] = '\0';		/* Remove the '\n'. */

	/* Strip all trailing whitespace. */
	rstrip(line);

	/* If the string is now empty or a comment, just ignore the line. */
	len = strlen(line);
	if (len == 0)
	    continue;
	if (line[0] == '#')
	    continue;

	/* Skip past all leading whitespace to find the start of
	 * NAME. */
	ptr = name = lstrip(line);

	/* Now we need to split the line into 3 parts: NAME, ACCESS
	 * and PATHNAME.  NAME can't have whitespace in it, so look
	 * for the first non-whitespace. */
	while (*ptr != '\0' && ! isspace((int)*ptr)) {
	    ptr++;
	}
	/* If we're at the end, we didn't find any whitespace, so
	 * we've only got a NAME, with no ACCESS/PATHNAME. */
	if (*ptr == '\0') {
	    __pmNotifyErr(LOG_ERR, "event_config: badly formatted "
				   " configuration file line: '%s'", line);
	    sts = -1;
	    break;
	}
	/* Terminate NAME at the 1st whitespace. */
	*ptr++ = '\0';

	/* Make sure NAME isn't too long. */
	if (strlen(name) > MAXPATHLEN) {
	    __pmNotifyErr(LOG_ERR, "event_config: name too long: '%s'", name);
	    sts = -1;
	    break;
	}

	/* Make sure NAME is valid. */
	if (valid_pmns_name(name) == 0) {
	    __pmNotifyErr(LOG_ERR, "event_config: invalid name: '%s'", name);
	    sts = -1;
	    break;
	}

	/* Skip past any extra whitespace between NAME and ACCESS */
	ptr = noaccess = lstrip(ptr);

	/* Look for the next whitespace, and that terminate ACCESS */
	while (*ptr != '\0' && ! isspace((int)*ptr)) {
	    ptr++;
	}

	/* If we're at the end, we didn't find any whitespace, so
	 * we've only got NAME and ACCESS with no/PATHNAME. */
	if (*ptr == '\0') {
	    __pmNotifyErr(LOG_ERR, "event_config: badly formatted "
				   " configuration file line: '%s'", line);
	    sts = -1;
	    break;
	}
	/* Terminate ACCESS at the 1st whitespace. */
	*ptr++ = '\0';

	/* Skip past any extra whitespace between ACCESS and PATHNAME */
	ptr = lstrip(ptr);

	/* Make sure PATHNAME (the rest of the line) isn't too long. */
	if (strlen(ptr) > MAXPATHLEN) {
	    __pmNotifyErr(LOG_ERR, "event_config: path is too long: '%s'", ptr);
	    sts = -1;
	    break;
	}

	/* Now we've got a reasonable NAME/ACCESS/PATHNAME.  Save them. */
	len = (numlogfiles + 1) * sizeof(event_logfile_t);
	logfiles = realloc(logfiles, len);
	if (logfiles == NULL) {
	    __pmNoMem("event_config", len, PM_FATAL_ERR);
	    sts = -1;
	    break;
	}
	logfile = &logfiles[numlogfiles];
	memset(logfile, 0, sizeof(*logfile));
	logfile->noaccess = (noaccess[0] == 'y' || noaccess[0] == 'Y');
	strncpy(logfile->pmnsname, name, sizeof(logfile->pmnsname));
	logfile->pmnsname[sizeof(logfile->pmnsname)-1] = '\0';
	strncpy(logfile->pathname, ptr, sizeof(logfile->pathname));
	logfile->pathname[sizeof(logfile->pathname)-1] = '\0';
	/* remaining fields filled in after pmdaInit() is called. */
	numlogfiles++;

	if (pmDebug & DBG_TRACE_APPL0)
	    __pmNotifyErr(LOG_INFO, "event_config: new logfile %s (%s)",
				logfile->pathname, logfile->pmnsname);
    }

    fclose(configFile);
    if (sts < 0) {
	free(logfiles);
	return sts;
    }
    if (numlogfiles == 0) {
	__pmNotifyErr(LOG_ERR, "event_config: no valid log files found");
	return -1;
    }
    return numlogfiles;
}

static int
event_create(event_logfile_t *logfile)
{
    int j;
    char *s, *p;
    size_t offset;
    ssize_t bytes;
    struct timeval timestamp;

    static char *buffer;
    static int bufsize;

    /*
     * Using a static (global) event buffer to hold initial read.
     * The aim is to reduce memory allocation until we know we'll
     * need to keep something.
     */
    if (!buffer) {
	int	sts = 0;
	bufsize = 16 * getpagesize();
#ifdef HAVE_POSIX_MEMALIGN
	sts = posix_memalign((void **)&buffer, getpagesize(), bufsize);
#else
#ifdef HAVE_MEMALIGN
	buffer = (char *)memalign(getpagesize(), bufsize);
	if (buffer == NULL) sts = -1;
#else
	buffer = (char *)malloc(bufsize);
	if (buffer == NULL) sts = -1;
#endif
#endif
	if (sts != 0) {
	    __pmNotifyErr(LOG_ERR, "event buffer allocation failure");
	    return -1;
	}
    }

    offset = 0;
multiread:
    if (logfile->fd < 0)
    	return 0;
    bytes = read(logfile->fd, buffer + offset, bufsize - 1 - offset);
    /*
     * Ignore the error if:
     * - we've got EOF (0 bytes read)
     * - EBADF (fd isn't valid - most likely a closed pipe)
     * - EAGAIN/EWOULDBLOCK (fd is marked nonblocking and read would block)
     * - EINVAL/EISDIR (fd is a directory - config file botch)
     */
    if (bytes == 0)
	return 0;
    if (bytes < 0 && (errno == EBADF || errno == EISDIR || errno == EINVAL))
	return 0;
    if (bytes < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
	return 0;
    if (bytes > maxmem)
	return 0;
    if (bytes < 0) {
	__pmNotifyErr(LOG_ERR, "read failure on %s: %s",
		      logfile->pathname, strerror(errno));
	return -1;
    }

    gettimeofday(&timestamp, NULL);
    buffer[bufsize-1] = '\0';
    for (s = p = buffer, j = 0; *s != '\0' && j < bufsize-1; s++, j++) {
	if (*s != '\n')
	    continue;
	*s = '\0';
	bytes = (s+1) - p;
	pmdaEventQueueAppend(logfile->queueid, p, bytes, &timestamp);
	p = s + 1;
    }
    /* did we just do a full buffer read? */
    if (p == buffer) {
	char msg[64];
	__pmNotifyErr(LOG_ERR, "Ignoring long (%d bytes) line: \"%s\"", (int)
			bytes, __pmdaEventPrint(p, bytes, msg, sizeof(msg)));
    } else if (j == bufsize - 1) {
	offset = bufsize-1 - (p - buffer);
	memmove(buffer, p, offset);
	goto multiread;	/* read rest of line */
    }
    return 1;
}

void
event_refresh(void)
{
    struct event_logfile *logfile;
    struct stat pathstat;
    int i, fd, sts;

    for (i = 0; i < numlogfiles; i++) {
	logfile = &logfiles[i];

	if (logfile->pid > 0)	/* process pipe */
	    goto events;
	if (stat(logfile->pathname, &pathstat) < 0) {
	    if (logfile->fd >= 0) {
		close(logfile->fd);
		logfile->fd = -1;
	    }
	    memset(&logfile->pathstat, 0, sizeof(logfile->pathstat));
	} else {
	    /* reopen if no descriptor before, or log rotated (new file) */
	    if (logfile->fd < 0 ||
	        logfile->pathstat.st_ino != pathstat.st_ino ||
		logfile->pathstat.st_dev != pathstat.st_dev) {
		if (logfile->fd >= 0)
		    close(logfile->fd);
		fd = open(logfile->pathname, O_RDONLY|O_NONBLOCK);
		if (fd < 0 && logfile->fd >= 0)	/* log once */
		    __pmNotifyErr(LOG_ERR, "open: %s - %s",
				logfile->pathname, strerror(errno));
		logfile->fd = fd;
	    } else {
		if ((S_ISREG(pathstat.st_mode)) &&
		    (memcmp(&logfile->pathstat.st_mtime, &pathstat.st_mtime,
			    sizeof(pathstat.st_mtime))) == 0)
		    continue;
	    }
	    logfile->pathstat = pathstat;
events:
	    do {
		sts = event_create(logfile);
	    } while (sts != 0);
	}
    }
}

int
event_logcount(void)
{
    return numlogfiles;
}

int
event_queueid(int handle)
{
    if (handle < 0 || handle >= numlogfiles)
	return 0;

    /* if logfile unrestricted, allow this client access to this queue */
    if (logfiles[handle].noaccess == 0)
	pmdaEventSetAccess(pmdaGetContext(), logfiles[handle].queueid, 1);

    return logfiles[handle].queueid;
}

__uint64_t
event_pathsize(int handle)
{
    if (handle < 0 || handle >= numlogfiles)
	return 0;
    return logfiles[handle].pathstat.st_size;
}

const char *
event_pathname(int handle)
{
    if (handle < 0 || handle >= numlogfiles)
	return NULL;
    return logfiles[handle].pathname;
}

const char *
event_pmnsname(int handle)
{
    if (handle < 0 || handle >= numlogfiles)
	return NULL;
    return logfiles[handle].pmnsname;
}

pmID
event_pmid(int handle)
{
    if (handle < 0 || handle >= numlogfiles)
	return 0;
    return logfiles[handle].pmid;
}

int
event_decoder(int eventarray, void *buffer, size_t size,
		struct timeval *timestamp, void *data)
{
    int sts, handle = *(int *)data;
    pmID pmid = event_pmid(handle);
    pmAtomValue atom;

    sts = pmdaEventAddRecord(eventarray, timestamp, PM_EVENT_FLAG_POINT);
    if (sts < 0)
	return sts;
    atom.cp = buffer;
    sts = pmdaEventAddParam(eventarray, pmid, PM_TYPE_STRING, &atom);
    if (sts < 0)
	return sts;
    return 1;	/* simple decoder, added just one event array */
}

int
event_regex_apply(void *rp, void *data, size_t size)
{
    regex_t *regex = (regex_t *)rp;
    return regexec(regex, data, 0, NULL, 0) == REG_NOMATCH;
}

void
event_regex_release(void *rp)
{
    regex_t *regex = (regex_t *)rp;
    regfree(regex);
}

int
event_regex_alloc(const char *string, void **filter)
{
    regex_t *regex = malloc(sizeof(regex_t));

    if (regex == NULL)
	return -ENOMEM;
    if (regcomp(regex, string, REG_EXTENDED|REG_NOSUB) != 0) {
	free(regex);
	return PM_ERR_CONV;
    }
    *filter = (void *)regex;
    return 0;
}