summaryrefslogtreecommitdiff
path: root/src/pmlogger/src/gram.y
blob: 9a28d8abf335bccb3d1f87908a93d5a1352b215f (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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
 * Copyright (c) 2013-2014 Red Hat.
 * Copyright (c) 1995-2001 Silicon Graphics, Inc.  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.
 */

/* 
 * There is a shift/reduced conflict reported by yacc when it cannot
 * decide whatever it should take 'optinst' route or 'access' one in
 * the following sutiation:
 *
 * log on once foo [access] all
 *
 * This conflict considered to be benign, since yacc takes 'the right' option
 * if optinst is supplied. To work around the issue of access been treated 
 * as an option, enclose the list of metrics in the curly braces, i.e.
 *
 * log on once {foo} [access] all 
 */

%{
#include "pmapi.h"
#include "impl.h"
#include "logger.h"

int		mystate = GLOBAL;	/* config file parser state */

__pmHashCtl	pm_hash;
task_t		*tasklist;

static task_t	*tp;
static int	numinst;
static int	*intlist;
static char	**extlist;
static int	state;			/* logging state, current block */
static char	*metricName;		/* current metric, current block */

typedef struct _hl {
    struct _hl	*hl_next;
    char	*hl_name;
    int		hl_line;
} hostlist_t;

static hostlist_t	*hl_root;
static hostlist_t	*hl_last;
static hostlist_t	*hlp;
static hostlist_t	*prevhlp;
static int		opmask;		/* operations mask */
static int		specmask;	/* specifications mask */
static int		allow;		/* host allow/disallow state */

static int lookup_metric_name(const char *);
static void activate_new_metric(const char *);
static void activate_cached_metric(const char *, int);
static task_t *findtask(int, struct timeval *);

%}
%union {
    long lval;
    char * str;
}

%expect 1

%term	LSQB
	RSQB
	COMMA
	LBRACE
	RBRACE
	COLON
	SEMICOLON

	LOG
	MANDATORY ADVISORY
	ON OFF MAYBE
	EVERY ONCE DEFAULT
	MSEC SECOND MINUTE HOUR

	ACCESS ENQUIRE ALLOW DISALLOW ALL EXCEPT

%token<str>  NAME STRING IPSPEC HOSTNAME URL
%token<lval> NUMBER

%type<lval> frequency timeunits action
%type<str>  hostspec
%%

config		: specopt accessopt
		;

specopt		: spec
		| /* nothing */
		;

spec		: stmt
		| spec stmt
		;

stmt		: dowhat somemetrics				
		{
		    mystate = GLOBAL;
		    if (tp->t_numvalid)
			linkback(tp);
		    state = 0;
                }
		;

dowhat		: logopt action		
		{
		    struct timeval delta;

		    delta.tv_sec = $2 / 1000;
		    delta.tv_usec = 1000 * ($2 % 1000);

		    /*
		     * Search for an existing task for this state/interval;
		     * only allocate and setup a new task if none exists.
		     */
		    if ((tp = findtask(state, &delta)) == NULL) {
			if ((tp = (task_t *)calloc(1, sizeof(task_t))) == NULL) {
			    char emess[256];
			    snprintf(emess, sizeof(emess), "malloc failed: %s", osstrerror());
			    yyerror(emess);
			} else {
			    tp->t_delta = delta;
			    tp->t_state = state;
			    tp->t_next = tasklist;
			    tasklist = tp;
			}
		    }
		    state = 0;
		}
		;

logopt		: LOG 
		| /* nothing */
		;

action		: cntrl ON frequency	
		{ 
		    char emess[256];
                    if ($3 < 0) {
			snprintf(emess, sizeof(emess),
				"Logging delta (%ld msec) must be positive",$3);
			yyerror(emess);
		    }
		    else if ($3 >  PMLC_MAX_DELTA) {
			snprintf(emess, sizeof(emess),
				"Logging delta (%ld msec) cannot be bigger "
				"than %d msec", $3, PMLC_MAX_DELTA);
			yyerror(emess);
		    }

                    PMLC_SET_ON(state, 1); 
                    $$ = $3;
                }
		| cntrl OFF			{ PMLC_SET_ON(state, 0);$$ = 0;}
		| MANDATORY MAYBE
		{
                    PMLC_SET_MAND(state, 0);
                    PMLC_SET_ON(state, 0);
                    PMLC_SET_MAYBE(state, 1);
                    $$ = 0;
                }
		;

cntrl		: MANDATORY			{ PMLC_SET_MAND(state, 1); }
		| ADVISORY			{ PMLC_SET_MAND(state, 0); }
		| /*nothing == advisory*/	{ PMLC_SET_MAND(state, 0); }
			;

frequency	: everyopt NUMBER timeunits	{ $$ = $2*$3; }
		| ONCE				{ $$ = 0; }
		| DEFAULT						
		{ 
                    extern struct timeval delta; /* default logging interval */
                    $$ = delta.tv_sec*1000 + delta.tv_usec/1000;
                }
		;

everyopt	: EVERY
		| /* nothing */
		;

timeunits	: MSEC		{ $$ = 1; }
		| SECOND	{ $$ = 1000; }
		| MINUTE	{ $$ = 60000; }
		| HOUR		{ $$ = 3600000; }
		;

somemetrics	: LBRACE { mystate = INSPEC; } metriclist RBRACE
		| metricspec
		;

metriclist	: metricspec
		| metriclist metricspec
		| metriclist COMMA metricspec
		;

metricspec	: NAME
		{
                    if ((metricName = strdup($1)) == NULL) {
			char emess[256];
			snprintf(emess, sizeof(emess), "malloc failed: %s", osstrerror());
                        yyerror(emess);
		    }
                }
		optinst
		{
		    int index, sts;

		    /*
		     * search names for previously seen metrics for this task
		     * (note that name may be non-terminal in the PMNS here);
		     * if already found in this task, skip namespace PDUs.
		     */
		    if ((index = lookup_metric_name(metricName)) < 0) {
			if ((sts = pmTraversePMNS(metricName, activate_new_metric)) < 0 ) {
			    char emess[256];
			    snprintf(emess, sizeof(emess),
				    "Problem with lookup for metric \"%s\" "
				    "... logging not activated", metricName);
			    yywarn(emess);
			    fprintf(stderr, "Reason: %s\n", pmErrStr(sts));
			}
		    }
		    else {	/* name is cached already, handle instances */
			activate_cached_metric(metricName, index);
		    }
		    freeinst(&numinst, intlist, extlist);
		    free(metricName);
		}
		;

optinst		: LSQB instancelist RSQB
		| /* nothing */
		;

instancelist	: instance
		| instance instancelist
		| instance COMMA instancelist
		;

instance	: NAME		{ buildinst(&numinst, &intlist, &extlist, -1, $1); }
		| NUMBER	{ buildinst(&numinst, &intlist, &extlist, $1, NULL); }
		| STRING	{ buildinst(&numinst, &intlist, &extlist, -1, $1); }
		;

accessopt	: LSQB ACCESS RSQB ctllist
		| /* nothing */
		;

ctllist		: ctl
		| ctl ctllist
		;
		
ctl		: allow hostlist COLON operation SEMICOLON
		{
                    prevhlp = NULL;
                    for (hlp = hl_root; hlp != NULL; hlp = hlp->hl_next) {
			int sts;

                        if (prevhlp != NULL) {
                            free(prevhlp->hl_name);
                            free(prevhlp);
                        }
                        sts = __pmAccAddHost(hlp->hl_name, specmask, 
                                             opmask, 0);
                        if (sts < 0) {
                            fprintf(stderr, "error was on line %d\n", 
                                hlp->hl_line);
                            YYABORT;
                        }
                        prevhlp = hlp;
                    }
                    if (prevhlp != NULL) {
                        free(prevhlp->hl_name);
                        free(prevhlp);
                    }
                    opmask = 0;
                    specmask = 0;
                    hl_root = hl_last = NULL;
                }
		;

allow		: ALLOW			{ allow = 1; }
		| DISALLOW		{ allow = 0; }
		;

hostlist	: host
		| host COMMA hostlist
		;

host		: hostspec
		{
		    size_t sz = sizeof(hostlist_t);

                    hlp = (hostlist_t *)malloc(sz);
                    if (hlp == NULL) {
                        __pmNoMem("adding new host", sz, PM_FATAL_ERR);
                    }
                    if (hl_last != NULL) {
                        hl_last->hl_next = hlp;
                        hl_last = hlp;
                    }
                    else
                        hl_root = hl_last = hlp;
                    hlp->hl_next = NULL;
                    hlp->hl_name = strdup($1);
                    hlp->hl_line = lineno;
                }
		;

hostspec	: IPSPEC
		| URL
		| HOSTNAME
		| NAME
		;

operation	: operlist
		{
                    specmask = opmask;
                    if (allow)
                        opmask = ~opmask;
                }
		| ALL			
		{
                    specmask = PM_OP_ALL;
                    if (allow)
                        opmask = PM_OP_NONE;
                    else
                        opmask = PM_OP_ALL;
                }
		| ALL EXCEPT operlist
		{
                    specmask = PM_OP_ALL;
                    if (!allow)
                        opmask = ~opmask;
                }
		;

operlist	: op
		| op COMMA operlist
		;

op		: ADVISORY		{ opmask |= PM_OP_LOG_ADV; }
		| MANDATORY		{ opmask |= PM_OP_LOG_MAND; }
		| ENQUIRE		{ opmask |= PM_OP_LOG_ENQ; }
		;

%%

/*
 * Search the cache for previously seen metrics for active task.
 * Returns -1 if not found, else an index into tp->t_namelist.
 */
static int
lookup_metric_name(const char *name)
{
    int		j;

    for (j = 0; j < tp->t_numpmid; j++)
	if (strcmp(tp->t_namelist[j], name) == 0)
	    return j;
    return -1;
}

/*
 * Assumed calling context ...
 *	tp		the correct task for the requested metric
 *	numinst		number of instances associated with this request
 *	extlist[]	external instance names if numinst > 0
 *	intlist[]	internal instance identifier if numinst > 0 and
 *			corresponding extlist[] entry is NULL
 */
static void
activate_cached_metric(const char *name, int index)
{
    int		sts = 0;
    int		inst;
    int		i;
    int		j;
    int		skip = 0;
    pmID	pmid;
    pmDesc	*dp;
    optreq_t	*rqp;
    char	emess[1024];

    /*
     * need new malloc'd pmDesc, even if metric found in cache, as
     * the fetchctl keeps its own (non-realloc-movable!) pointer.
     */
    dp = (pmDesc *)malloc(sizeof(pmDesc));
    if (dp == NULL)
	goto nomem;

    if (index < 0) {
	if ((sts = pmLookupName(1, (char **)&name, &pmid)) < 0 || pmid == PM_ID_NULL) {
	    snprintf(emess, sizeof(emess),
		    "Metric \"%s\" is unknown ... not logged", name);
	    goto snarf;
	}
	if ((sts = pmLookupDesc(pmid, dp)) < 0) {
	    snprintf(emess, sizeof(emess),
		    "Description unavailable for metric \"%s\" ... not logged",
		    name);
	    goto snarf;
	}
	tp->t_numpmid++;
	tp->t_namelist = (char **)realloc(tp->t_namelist, tp->t_numpmid * sizeof(char *));
	if (tp->t_namelist == NULL)
	    goto nomem;
	if ((tp->t_namelist[tp->t_numpmid-1] = strdup(name)) == NULL)
	    goto nomem;
	tp->t_pmidlist = (pmID *)realloc(tp->t_pmidlist, tp->t_numpmid * sizeof(pmID));
	if (tp->t_pmidlist == NULL)
	    goto nomem;
	tp->t_desclist = (pmDesc *)realloc(tp->t_desclist, tp->t_numpmid * sizeof(pmDesc));
	if (tp->t_desclist == NULL)
	    goto nomem;
	tp->t_pmidlist[tp->t_numpmid-1] = pmid;
	tp->t_desclist[tp->t_numpmid-1] = *dp;	/* struct assignment */
    }
    else {
	*dp = tp->t_desclist[index];
	pmid = tp->t_pmidlist[index];
    }

    rqp = (optreq_t *)calloc(1, sizeof(optreq_t));
    if (rqp == NULL)
	goto nomem;
    rqp->r_desc = dp;
    rqp->r_numinst = numinst;

    if (numinst) {
	/*
	 * malloc here, and keep ... gets buried in optFetch data structures
	 */
	rqp->r_instlist = (int *)malloc(numinst * sizeof(rqp->r_instlist[0]));
	if (rqp->r_instlist == NULL)
	    goto nomem;
	j = 0;
	for (i = 0; i < numinst; i++) {
	    if (extlist[i] != NULL) {
		sts = pmLookupInDom(dp->indom, extlist[i]);
		if (sts < 0) {
                    snprintf(emess, sizeof(emess),
			"Instance \"%s\" is not defined for the metric \"%s\"",
			extlist[i], name);
                    yywarn(emess);
		    rqp->r_numinst--;
		    continue;
		}
		inst = sts;
	    }
	    else {
		char	*p;
		sts = pmNameInDom(dp->indom, intlist[i], &p);
		if (sts < 0) {
                    snprintf(emess, sizeof(emess),
			"Instance \"%d\" is not defined for the metric \"%s\"",
			intlist[i], name);
                    yywarn(emess);
		    rqp->r_numinst--;
		    continue;
		}
		free(p);
		inst = intlist[i];
	    }
	    if ((sts = chk_one(tp, pmid, inst)) < 0) {
                snprintf(emess, sizeof(emess),
			"Incompatible request for metric \"%s\" "
			"and instance \"%s\"", name, extlist[i]);
                yywarn(emess);
                fprintf(stderr, "%s\n", chk_emess[-sts]);
                rqp->r_numinst--;
	    }
	    else if (sts == 0)
		rqp->r_instlist[j++] = inst;
	    else	/* already have this instance */
		skip = 1;
	}
	if (rqp->r_numinst == 0)
	    skip = 1;
    }
    else {
	if ((sts = chk_all(tp, pmid)) < 0) {
            snprintf(emess, sizeof(emess),
		    "Incompatible request for metric \"%s\"", name);
            yywarn(emess);

	    skip = 1;
	}
    }

    if (!skip) {
	__pmOptFetchAdd(&tp->t_fetch, rqp);
	if ((sts = __pmHashAdd(pmid, (void *)rqp, &pm_hash)) < 0) {
	    snprintf(emess, sizeof(emess), "__pmHashAdd failed "
		    "for metric \"%s\" ... logging not activated", name);
	    goto snarf;
	}
	tp->t_numvalid++;
    }
    else {
	free(dp);
	free(rqp);
    }
    return;

nomem:
    snprintf(emess, sizeof(emess), "malloc failed: %s", osstrerror());
    yyerror(emess);

snarf:
    yywarn(emess);
    fprintf(stderr, "Reason: %s\n", pmErrStr(sts));
    if (dp != NULL)
        free(dp);
    return;
}

static void
activate_new_metric(const char *name)
{
    activate_cached_metric(name, lookup_metric_name(name));
}

/*
 * Given a logging state and an interval, return a matching task
 * or NULL if none exists for that value pair.
 */
task_t *
findtask(int state, struct timeval *delta)
{
    task_t	*tp;

    for (tp = tasklist; tp != NULL; tp = tp->t_next) {
	if (state == tp->t_state &&
	    delta->tv_sec == tp->t_delta.tv_sec &&
	    delta->tv_usec == tp->t_delta.tv_usec)
	    break;
    }
    return tp;
}

/*
 * Complete the delayed processing of task elements, which can only
 * be done once all configuration file parsing is complete.
 */
void
yyend(void)
{
    for (tp = tasklist; tp != NULL; tp = tp->t_next) {
	if (tp->t_numvalid == 0)
	    continue;
	PMLC_SET_MAYBE(tp->t_state, 0);	/* clear req */
	if (PMLC_GET_ON(tp->t_state))
	    tp->t_afid = __pmAFregister(&tp->t_delta, (void *)tp, log_callback);
    }
}