monitor.c 15.4 KB
Newer Older
1
2
3
4
/* monitor.c - monitor bdb backend */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Copyright 2000-2008 The OpenLDAP Foundation.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */

#include "portable.h"

#include <stdio.h>
#include <ac/string.h>
#include <ac/unistd.h>
#include <ac/stdlib.h>
#include <ac/errno.h>
#include <sys/stat.h>
#include "lutil.h"
#include "back-bdb.h"
27

28
29
#include "../back-monitor/back-monitor.h"

30
31
#include "config.h"

32
33
static ObjectClass		*oc_olmBDBDatabase;

34
static AttributeDescription	*ad_olmBDBEntryCache,
35
	*ad_olmBDBDNCache, *ad_olmBDBIDLCache,
36
	*ad_olmDbDirectory;
37

38
39
40
41
42
43
44
45
46
#ifdef BDB_MONITOR_IDX
static int
bdb_monitor_idx_entry_add(
	struct bdb_info	*bdb,
	Entry		*e );

static AttributeDescription	*ad_olmBDBNotIndexed;
#endif /* BDB_MONITOR_IDX */

47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * NOTE: there's some confusion in monitor OID arc;
 * by now, let's consider:
 * 
 * Subsystems monitor attributes	1.3.6.1.4.1.4203.666.1.55.0
 * Databases monitor attributes		1.3.6.1.4.1.4203.666.1.55.0.1
 * BDB database monitor attributes	1.3.6.1.4.1.4203.666.1.55.0.1.1
 *
 * Subsystems monitor objectclasses	1.3.6.1.4.1.4203.666.3.16.0
 * Databases monitor objectclasses	1.3.6.1.4.1.4203.666.3.16.0.1
 * BDB database monitor objectclasses	1.3.6.1.4.1.4203.666.3.16.0.1.1
 */

60
61
62
63
64
65
66
67
68
69
static struct {
	char			*name;
	char			*oid;
}		s_oid[] = {
	{ "olmBDBAttributes",			"olmDatabaseAttributes:1" },
	{ "olmBDBObjectClasses",		"olmDatabaseObjectClasses:1" },

	{ NULL }
};

70
71
72
73
static struct {
	char			*desc;
	AttributeDescription	**ad;
}		s_at[] = {
74
	{ "( olmBDBAttributes:1 "
75
76
77
78
		"NAME ( 'olmBDBEntryCache' ) "
		"DESC 'Number of items in Entry Cache' "
		"SUP monitorCounter "
		"NO-USER-MODIFICATION "
79
		"USAGE dSAOperation )",
80
81
		&ad_olmBDBEntryCache },

82
	{ "( olmBDBAttributes:2 "
83
84
		"NAME ( 'olmBDBDNCache' ) "
		"DESC 'Number of items in DN Cache' "
85
86
		"SUP monitorCounter "
		"NO-USER-MODIFICATION "
87
		"USAGE dSAOperation )",
88
		&ad_olmBDBDNCache },
89

90
	{ "( olmBDBAttributes:3 "
91
92
93
94
		"NAME ( 'olmBDBIDLCache' ) "
		"DESC 'Number of items in IDL Cache' "
		"SUP monitorCounter "
		"NO-USER-MODIFICATION "
95
		"USAGE dSAOperation )",
96
		&ad_olmBDBIDLCache },
97

98
	{ "( olmBDBAttributes:4 "
99
100
101
102
103
		"NAME ( 'olmDbDirectory' ) "
		"DESC 'Path name of the directory "
			"where the database environment resides' "
		"SUP monitoredInfo "
		"NO-USER-MODIFICATION "
104
		"USAGE dSAOperation )",
105
106
		&ad_olmDbDirectory },

107
108
109
110
111
112
113
114
115
116
#ifdef BDB_MONITOR_IDX
	{ "( olmBDBAttributes:5 "
		"NAME ( 'olmBDBNotIndexed' ) "
		"DESC 'Missing indexes resulting from candidate selection' "
		"SUP monitoredInfo "
		"NO-USER-MODIFICATION "
		"USAGE dSAOperation )",
		&ad_olmBDBNotIndexed },
#endif /* BDB_MONITOR_IDX */

117
118
119
120
121
122
123
	{ NULL }
};

static struct {
	char		*desc;
	ObjectClass	**oc;
}		s_oc[] = {
124
125
	/* augments an existing object, so it must be AUXILIARY
	 * FIXME: derive from some ABSTRACT "monitoredEntity"? */
126
	{ "( olmBDBObjectClasses:1 "
127
		"NAME ( 'olmBDBDatabase' ) "
128
		"SUP top AUXILIARY "
129
		"MAY ( "
130
			"olmBDBEntryCache "
131
			"$ olmBDBDNCache "
132
133
			"$ olmBDBIDLCache "
			"$ olmDbDirectory "
134
135
136
#ifdef BDB_MONITOR_IDX
			"$ olmBDBNotIndexed "
#endif /* BDB_MONITOR_IDX */
137
138
139
140
141
142
			") )",
		&oc_olmBDBDatabase },

	{ NULL }
};

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
static int
bdb_monitor_update(
	Operation	*op,
	SlapReply	*rs,
	Entry		*e,
	void		*priv )
{
	struct bdb_info		*bdb = (struct bdb_info *) priv;
	Attribute		*a;

	char			buf[ BUFSIZ ];
	struct berval		bv;

	assert( ad_olmBDBEntryCache != NULL );

	a = attr_find( e->e_attrs, ad_olmBDBEntryCache );
	assert( a != NULL );
	bv.bv_val = buf;
	bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_cursize );
	ber_bvreplace( &a->a_vals[ 0 ], &bv );

164
	a = attr_find( e->e_attrs, ad_olmBDBDNCache );
165
166
167
168
169
170
171
172
173
	assert( a != NULL );
	bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_eiused );
	ber_bvreplace( &a->a_vals[ 0 ], &bv );

	a = attr_find( e->e_attrs, ad_olmBDBIDLCache );
	assert( a != NULL );
	bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_idl_cache_size );
	ber_bvreplace( &a->a_vals[ 0 ], &bv );
	
174
175
176
177
#ifdef BDB_MONITOR_IDX
	bdb_monitor_idx_entry_add( bdb, e );
#endif /* BDB_MONITOR_IDX */

178
179
180
	return SLAP_CB_CONTINUE;
}

181
#if 0	/* uncomment if required */
182
183
184
185
186
187
188
189
190
static int
bdb_monitor_modify(
	Operation	*op,
	SlapReply	*rs,
	Entry		*e,
	void		*priv )
{
	return SLAP_CB_CONTINUE;
}
191
#endif
192
193
194
195

static int
bdb_monitor_free(
	Entry		*e,
196
	void		**priv )
197
198
199
200
201
202
203
204
205
{
	struct berval	values[ 2 ];
	Modification	mod = { 0 };

	const char	*text;
	char		textbuf[ SLAP_TEXT_BUFLEN ];

	int		i, rc;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
206
	/* NOTE: if slap_shutdown != 0, priv might have already been freed */
207
	*priv = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
208

209
210
211
212
	/* Remove objectClass */
	mod.sm_op = LDAP_MOD_DELETE;
	mod.sm_desc = slap_schema.si_ad_objectClass;
	mod.sm_values = values;
213
	mod.sm_numvals = 1;
214
215
216
217
218
219
220
221
	values[ 0 ] = oc_olmBDBDatabase->soc_cname;
	BER_BVZERO( &values[ 1 ] );

	rc = modify_delete_values( e, &mod, 1, &text,
		textbuf, sizeof( textbuf ) );
	/* don't care too much about return code... */

	/* remove attrs */
222
223
	mod.sm_values = NULL;
	mod.sm_numvals = 0;
224
	for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
225
226
227
228
229
230
231
232
233
		mod.sm_desc = *s_at[ i ].ad;
		rc = modify_delete_values( e, &mod, 1, &text,
			textbuf, sizeof( textbuf ) );
		/* don't care too much about return code... */
	}
	
	return SLAP_CB_CONTINUE;
}

234
235
#define	bdb_monitor_initialize	BDB_SYMBOL(monitor_initialize)

236
237
238
/*
 * call from within bdb_initialize()
 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
239
static int
240
241
242
bdb_monitor_initialize( void )
{
	int		i, code;
243
244
	ConfigArgs c;
	char	*argv[ 3 ];
245
246
247

	static int	bdb_monitor_initialized = 0;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
248
	if ( backend_info( "monitor" ) == NULL ) {
249
		return -1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
250
	}
251
252
253
254
255

	if ( bdb_monitor_initialized++ ) {
		return 0;
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
256
257
	/* register schema here */

258
259
260
261
262
	argv[ 0 ] = "back-bdb/back-hdb monitor";
	c.argv = argv;
	c.argc = 3;
	c.fname = argv[0];

263
	for ( i = 0; s_oid[ i ].name; i++ ) {
264
		c.lineno = i;
265
266
267
		argv[ 1 ] = s_oid[ i ].name;
		argv[ 2 ] = s_oid[ i ].oid;

268
		if ( parse_oidm( &c, 0, NULL ) != 0 ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
269
270
			Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_initialize)
				": unable to add "
271
272
273
274
275
276
				"objectIdentifier \"%s=%s\"\n",
				s_oid[ i ].name, s_oid[ i ].oid, 0 );
			return 1;
		}
	}

277
278
	for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
		code = register_at( s_at[ i ].desc, s_at[ i ].ad, 1 );
279
		if ( code != LDAP_SUCCESS ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
280
281
			Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_initialize)
				": register_at failed\n",
282
				0, 0, 0 );
283
		}
284
		(*s_at[ i ].ad)->ad_type->sat_flags |= SLAP_AT_HIDE;
285
286
	}

287
288
	for ( i = 0; s_oc[ i ].desc != NULL; i++ ) {
		code = register_oc( s_oc[ i ].desc, s_oc[ i ].oc, 1 );
289
		if ( code != LDAP_SUCCESS ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
290
291
			Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_initialize)
				": register_oc failed\n",
292
				0, 0, 0 );
293
		}
294
		(*s_oc[ i ].oc)->soc_flags |= SLAP_OC_HIDE;
295
296
297
298
299
300
301
302
303
	}

	return 0;
}

/*
 * call from within bdb_db_init()
 */
int
Pierangelo Masarati's avatar
Pierangelo Masarati committed
304
bdb_monitor_db_init( BackendDB *be )
305
{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
306
307
	struct bdb_info		*bdb = (struct bdb_info *) be->be_private;

308
309
310
311
	if ( SLAP_GLUE_SUBORDINATE( be ) ) {
		return 0;
	}

312
	if ( bdb_monitor_initialize() == LDAP_SUCCESS ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
313
		/* monitoring in back-bdb is on by default */
314
315
		SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
	}
316

317
318
319
320
321
#ifdef BDB_MONITOR_IDX
	bdb->bi_idx = NULL;
	ldap_pvt_thread_mutex_init( &bdb->bi_idx_mutex );
#endif /* BDB_MONITOR_IDX */

322
323
324
325
326
327
328
	return 0;
}

/*
 * call from within bdb_db_open()
 */
int
Pierangelo Masarati's avatar
Pierangelo Masarati committed
329
bdb_monitor_db_open( BackendDB *be )
330
331
332
{
	struct bdb_info		*bdb = (struct bdb_info *) be->be_private;
	Attribute		*a, *next;
Howard Chu's avatar
Howard Chu committed
333
	monitor_callback_t	*cb = NULL;
334
	int			rc = 0;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
335
336
	BackendInfo		*mi;
	monitor_extra_t		*mbe;
337
	struct berval dummy = BER_BVC("");
338

339
340
341
	if ( !SLAP_DBMONITORING( be ) ) {
		return 0;
	}
342

343
344
345
346
	if ( SLAP_GLUE_SUBORDINATE( be ) ) {
		return 0;
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
347
348
349
350
	mi = backend_info( "monitor" );
	if ( !mi || !mi->bi_extra ) {
		SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
		return 0;
351
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
352
	mbe = mi->bi_extra;
353

354
	/* don't bother if monitor is not configured */
355
	if ( !mbe->is_configured() ) {
356
357
358
		static int warning = 0;

		if ( warning++ == 0 ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
359
360
			Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_db_open)
				": monitoring disabled; "
361
362
363
364
365
366
367
				"configure monitor database to enable\n",
				0, 0, 0 );
		}

		return 0;
	}

368
	/* alloc as many as required (plus 1 for objectClass) */
369
	a = attrs_alloc( 1 + 4 );
370
371
372
373
374
375
	if ( a == NULL ) {
		rc = 1;
		goto cleanup;
	}

	a->a_desc = slap_schema.si_ad_objectClass;
376
	attr_valadd( a, &oc_olmBDBDatabase->soc_cname, NULL, 1 );
377
378
379
380
381
	next = a->a_next;

	{
		struct berval	bv = BER_BVC( "0" );

382
		next->a_desc = ad_olmBDBEntryCache;
383
		attr_valadd( next, &bv, NULL, 1 );
384
385
		next = next->a_next;

386
		next->a_desc = ad_olmBDBDNCache;
387
		attr_valadd( next, &bv, NULL, 1 );
388
389
390
		next = next->a_next;

		next->a_desc = ad_olmBDBIDLCache;
391
		attr_valadd( next, &bv, NULL, 1 );
392
393
394
395
396
397
		next = next->a_next;
	}

	{
		struct berval	bv, nbv;
		ber_len_t	pathlen = 0, len = 0;
398
		char		path[ MAXPATHLEN ] = { '\0' };
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
		char		*fname = bdb->bi_dbenv_home,
				*ptr;

		len = strlen( fname );
		if ( fname[ 0 ] != '/' ) {
			/* get full path name */
			getcwd( path, sizeof( path ) );
			pathlen = strlen( path );

			if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
				fname += 2;
				len -= 2;
			}
		}

		bv.bv_len = pathlen + STRLENOF( "/" ) + len;
		ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
		if ( pathlen ) {
			ptr = lutil_strncopy( ptr, path, pathlen );
			ptr[ 0 ] = '/';
			ptr++;
		}
		ptr = lutil_strncopy( ptr, fname, len );
		if ( ptr[ -1 ] != '/' ) {
			ptr[ 0 ] = '/';
			ptr++;
		}
		ptr[ 0 ] = '\0';
		
		attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );

		next->a_desc = ad_olmDbDirectory;
		next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
		next->a_vals[ 0 ] = bv;
433
		next->a_numvals = 1;
434
435
436
437
438
439
440
441
442
443

		if ( BER_BVISNULL( &nbv ) ) {
			next->a_nvals = next->a_vals;

		} else {
			next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
			next->a_nvals[ 0 ] = nbv;
		}

		next = next->a_next;
444
445
446
447
	}

	cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
	cb->mc_update = bdb_monitor_update;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
448
#if 0	/* uncomment if required */
449
	cb->mc_modify = bdb_monitor_modify;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
450
#endif
451
452
453
	cb->mc_free = bdb_monitor_free;
	cb->mc_private = (void *)bdb;

454
	/* make sure the database is registered; then add monitor attributes */
455
	rc = mbe->register_database( be, &bdb->bi_monitor.bdm_ndn );
456
	if ( rc == 0 ) {
457
458
		rc = mbe->register_entry_attrs( &bdb->bi_monitor.bdm_ndn, a, cb,
			&dummy, 0, &dummy );
459
	}
460
461
462
463
464

cleanup:;
	if ( rc != 0 ) {
		if ( cb != NULL ) {
			ch_free( cb );
465
466
467
468
469
470
			cb = NULL;
		}

		if ( a != NULL ) {
			attrs_free( a );
			a = NULL;
471
		}
472
473
	}

474
	/* store for cleanup */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
475
	bdb->bi_monitor.bdm_cb = (void *)cb;
476
477
478

	/* we don't need to keep track of the attributes, because
	 * bdb_monitor_free() takes care of everything */
479
480
	if ( a != NULL ) {
		attrs_free( a );
481
	}
482

483
484
485
486
487
488
489
	return rc;
}

/*
 * call from within bdb_db_close()
 */
int
Pierangelo Masarati's avatar
Pierangelo Masarati committed
490
bdb_monitor_db_close( BackendDB *be )
491
{
492
493
	struct bdb_info		*bdb = (struct bdb_info *) be->be_private;

494
495
496
497
	if ( SLAP_GLUE_SUBORDINATE( be ) ) {
		return 0;
	}

498
	if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_ndn ) ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
499
500
501
502
503
		BackendInfo		*mi = backend_info( "monitor" );
		monitor_extra_t		*mbe;

		if ( mi && &mi->bi_extra ) {
			mbe = mi->bi_extra;
504
			mbe->unregister_entry_callback( &bdb->bi_monitor.bdm_ndn,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
505
				(monitor_callback_t *)bdb->bi_monitor.bdm_cb,
506
				NULL, 0, NULL );
507
508
		}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
509
		memset( &bdb->bi_monitor, 0, sizeof( bdb->bi_monitor ) );
510
511
	}

512
513
514
515
516
517
518
	return 0;
}

/*
 * call from within bdb_db_destroy()
 */
int
Pierangelo Masarati's avatar
Pierangelo Masarati committed
519
bdb_monitor_db_destroy( BackendDB *be )
520
{
521
522
523
524
525
526
527
528
529
530
531
532
533
534
	if ( SLAP_GLUE_SUBORDINATE( be ) ) {
		return 0;
	}

#ifdef BDB_MONITOR_IDX
	{
		struct bdb_info		*bdb = (struct bdb_info *) be->be_private;

		/* TODO: free tree */
		ldap_pvt_thread_mutex_destroy( &bdb->bi_idx_mutex );
		avl_free( bdb->bi_idx, ch_free );
	}
#endif /* BDB_MONITOR_IDX */

535
536
	return 0;
}
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734

#ifdef BDB_MONITOR_IDX

#define BDB_MONITOR_IDX_TYPES	(4)

typedef struct monitor_idx_t monitor_idx_t;

struct monitor_idx_t {
	AttributeDescription	*idx_ad;
	unsigned long		idx_count[BDB_MONITOR_IDX_TYPES];
};

static int
bdb_monitor_bitmask2key( slap_mask_t bitmask )
{
	int	key;

	for ( key = 0; key < 8*sizeof(slap_mask_t) && !( bitmask & 0x1U ); key++ ) {
		bitmask >>= 1;
	}

	return key;
}

static struct berval idxbv[] = {
	BER_BVC( "present=" ),
	BER_BVC( "equality=" ),
	BER_BVC( "approx=" ),
	BER_BVC( "substr=" ),
	BER_BVNULL
};

static ber_len_t
bdb_monitor_idx2len( monitor_idx_t *idx )
{
	int		i;
	ber_len_t	len = 0;

	for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
		if ( idx->idx_count[ i ] != 0 ) {
			len += idxbv[i].bv_len;
		}
	}

	return len;
}

static int
monitor_idx_cmp( const void *p1, const void *p2 )
{
	const monitor_idx_t	*idx1 = (const monitor_idx_t *)p1;
	const monitor_idx_t	*idx2 = (const monitor_idx_t *)p2;

	return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad );
}

static int
monitor_idx_dup( void *p1, void *p2 )
{
	monitor_idx_t	*idx1 = (monitor_idx_t *)p1;
	monitor_idx_t	*idx2 = (monitor_idx_t *)p2;

	return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad ) == 0 ? -1 : 0;
}

int
bdb_monitor_idx_add(
	struct bdb_info		*bdb,
	AttributeDescription	*desc,
	slap_mask_t		type )
{
	monitor_idx_t		idx_dummy = { 0 },
				*idx;
	int			rc = 0, key;

	idx_dummy.idx_ad = desc;
	key = bdb_monitor_bitmask2key( type ) - 1;
	if ( key >= BDB_MONITOR_IDX_TYPES ) {
		/* invalid index type */
		return -1;
	}

	ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );

	idx = (monitor_idx_t *)avl_find( bdb->bi_idx,
		(caddr_t)&idx_dummy, monitor_idx_cmp );
	if ( idx == NULL ) {
		idx = (monitor_idx_t *)ch_calloc( sizeof( monitor_idx_t ), 1 );
		idx->idx_ad = desc;
		idx->idx_count[ key ] = 1;

		switch ( avl_insert( &bdb->bi_idx, (caddr_t)idx, 
			monitor_idx_cmp, monitor_idx_dup ) )
		{
		case 0:
			break;

		default:
			ch_free( idx );
			rc = -1;
		}

	} else {
		idx->idx_count[ key ]++;
	}

	ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );

	return rc;
}

static int
bdb_monitor_idx_apply( void *v_idx, void *v_valp )
{
	monitor_idx_t	*idx = (monitor_idx_t *)v_idx;
	BerVarray	*valp = (BerVarray *)v_valp;

	struct berval	bv;
	char		*ptr;
	char		count_buf[ BDB_MONITOR_IDX_TYPES ][ SLAP_TEXT_BUFLEN ];
	ber_len_t	count_len[ BDB_MONITOR_IDX_TYPES ],
			idx_len;
	int		i, num = 0;

	idx_len = bdb_monitor_idx2len( idx );

	bv.bv_len = 0;
	for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
		if ( idx->idx_count[ i ] == 0 ) {
			continue;
		}

		count_len[ i ] = snprintf( count_buf[ i ],
			sizeof( count_buf[ i ] ), "%lu", idx->idx_count[ i ] );
		bv.bv_len += count_len[ i ];
		num++;
	}

	bv.bv_len += idx->idx_ad->ad_cname.bv_len
		+ num
		+ idx_len;
	ptr = bv.bv_val = ch_malloc( bv.bv_len + 1 );
	ptr = lutil_strcopy( ptr, idx->idx_ad->ad_cname.bv_val );
	for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
		if ( idx->idx_count[ i ] == 0 ) {
			continue;
		}

		ptr[ 0 ] = '#';
		++ptr;
		ptr = lutil_strcopy( ptr, idxbv[ i ].bv_val );
		ptr = lutil_strcopy( ptr, count_buf[ i ] );
	}

	ber_bvarray_add( valp, &bv );

	return 0;
}

static int
bdb_monitor_idx_entry_add(
	struct bdb_info	*bdb,
	Entry		*e )
{
	BerVarray	vals = NULL;
	Attribute	*a;

	a = attr_find( e->e_attrs, ad_olmBDBNotIndexed );

	ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );

	avl_apply( bdb->bi_idx, bdb_monitor_idx_apply,
		&vals, -1, AVL_INORDER );

	ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );

	if ( vals != NULL ) {
		if ( a != NULL ) {
			assert( a->a_nvals == a->a_vals );

			ber_bvarray_free( a->a_vals );

		} else {
			Attribute	**ap;

			for ( ap = &e->e_attrs; *ap != NULL; ap = &(*ap)->a_next )
				;
			*ap = attr_alloc( ad_olmBDBNotIndexed );
			a = *ap;
		}
		a->a_vals = vals;
		a->a_nvals = a->a_vals;
	}

	return 0;
}

#endif /* BDB_MONITOR_IDX */