]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/monitor.c
Sync with HEAD for OL 2.4.5
[openldap] / servers / slapd / back-bdb / monitor.c
1 /* monitor.c - monitor bdb backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2007 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
23 #include <ac/errno.h>
24 #include <sys/stat.h>
25 #include "lutil.h"
26 #include "back-bdb.h"
27
28 #include "../back-monitor/back-monitor.h"
29
30 #include "config.h"
31
32 static ObjectClass              *oc_olmBDBDatabase;
33
34 static AttributeDescription     *ad_olmBDBEntryCache,
35         *ad_olmBDBDNCache, *ad_olmBDBIDLCache,
36         *ad_olmDbDirectory;
37
38 #ifdef BDB_MONITOR_IDX
39 static int
40 bdb_monitor_idx_entry_add(
41         struct bdb_info *bdb,
42         Entry           *e );
43
44 static AttributeDescription     *ad_olmBDBNotIndexed;
45 #endif /* BDB_MONITOR_IDX */
46
47 /*
48  * NOTE: there's some confusion in monitor OID arc;
49  * by now, let's consider:
50  * 
51  * Subsystems monitor attributes        1.3.6.1.4.1.4203.666.1.55.0
52  * Databases monitor attributes         1.3.6.1.4.1.4203.666.1.55.0.1
53  * BDB database monitor attributes      1.3.6.1.4.1.4203.666.1.55.0.1.1
54  *
55  * Subsystems monitor objectclasses     1.3.6.1.4.1.4203.666.3.16.0
56  * Databases monitor objectclasses      1.3.6.1.4.1.4203.666.3.16.0.1
57  * BDB database monitor objectclasses   1.3.6.1.4.1.4203.666.3.16.0.1.1
58  */
59
60 static struct {
61         char                    *name;
62         char                    *oid;
63 }               s_oid[] = {
64         { "olmBDBAttributes",                   "olmDatabaseAttributes:1" },
65         { "olmBDBObjectClasses",                "olmDatabaseObjectClasses:1" },
66
67         { NULL }
68 };
69
70 static struct {
71         char                    *desc;
72         AttributeDescription    **ad;
73 }               s_at[] = {
74         { "( olmBDBAttributes:1 "
75                 "NAME ( 'olmBDBEntryCache' ) "
76                 "DESC 'Number of items in Entry Cache' "
77                 "SUP monitorCounter "
78                 "NO-USER-MODIFICATION "
79                 "USAGE dSAOperation )",
80                 &ad_olmBDBEntryCache },
81
82         { "( olmBDBAttributes:2 "
83                 "NAME ( 'olmBDBDNCache' ) "
84                 "DESC 'Number of items in DN Cache' "
85                 "SUP monitorCounter "
86                 "NO-USER-MODIFICATION "
87                 "USAGE dSAOperation )",
88                 &ad_olmBDBDNCache },
89
90         { "( olmBDBAttributes:3 "
91                 "NAME ( 'olmBDBIDLCache' ) "
92                 "DESC 'Number of items in IDL Cache' "
93                 "SUP monitorCounter "
94                 "NO-USER-MODIFICATION "
95                 "USAGE dSAOperation )",
96                 &ad_olmBDBIDLCache },
97
98         { "( olmBDBAttributes:4 "
99                 "NAME ( 'olmDbDirectory' ) "
100                 "DESC 'Path name of the directory "
101                         "where the database environment resides' "
102                 "SUP monitoredInfo "
103                 "NO-USER-MODIFICATION "
104                 "USAGE dSAOperation )",
105                 &ad_olmDbDirectory },
106
107 #ifdef BDB_MONITOR_IDX
108         { "( olmBDBAttributes:5 "
109                 "NAME ( 'olmBDBNotIndexed' ) "
110                 "DESC 'Missing indexes resulting from candidate selection' "
111                 "SUP monitoredInfo "
112                 "NO-USER-MODIFICATION "
113                 "USAGE dSAOperation )",
114                 &ad_olmBDBNotIndexed },
115 #endif /* BDB_MONITOR_IDX */
116
117         { NULL }
118 };
119
120 static struct {
121         char            *desc;
122         ObjectClass     **oc;
123 }               s_oc[] = {
124         /* augments an existing object, so it must be AUXILIARY
125          * FIXME: derive from some ABSTRACT "monitoredEntity"? */
126         { "( olmBDBObjectClasses:1 "
127                 "NAME ( 'olmBDBDatabase' ) "
128                 "SUP top AUXILIARY "
129                 "MAY ( "
130                         "olmBDBEntryCache "
131                         "$ olmBDBDNCache "
132                         "$ olmBDBIDLCache "
133                         "$ olmDbDirectory "
134 #ifdef BDB_MONITOR_IDX
135                         "$ olmBDBNotIndexed "
136 #endif /* BDB_MONITOR_IDX */
137                         ") )",
138                 &oc_olmBDBDatabase },
139
140         { NULL }
141 };
142
143 static int
144 bdb_monitor_update(
145         Operation       *op,
146         SlapReply       *rs,
147         Entry           *e,
148         void            *priv )
149 {
150         struct bdb_info         *bdb = (struct bdb_info *) priv;
151         Attribute               *a;
152
153         char                    buf[ BUFSIZ ];
154         struct berval           bv;
155
156         assert( ad_olmBDBEntryCache != NULL );
157
158         a = attr_find( e->e_attrs, ad_olmBDBEntryCache );
159         assert( a != NULL );
160         bv.bv_val = buf;
161         bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_cursize );
162         ber_bvreplace( &a->a_vals[ 0 ], &bv );
163
164         a = attr_find( e->e_attrs, ad_olmBDBDNCache );
165         assert( a != NULL );
166         bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_eiused );
167         ber_bvreplace( &a->a_vals[ 0 ], &bv );
168
169         a = attr_find( e->e_attrs, ad_olmBDBIDLCache );
170         assert( a != NULL );
171         bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_idl_cache_size );
172         ber_bvreplace( &a->a_vals[ 0 ], &bv );
173         
174 #ifdef BDB_MONITOR_IDX
175         bdb_monitor_idx_entry_add( bdb, e );
176 #endif /* BDB_MONITOR_IDX */
177
178         return SLAP_CB_CONTINUE;
179 }
180
181 #if 0   /* uncomment if required */
182 static int
183 bdb_monitor_modify(
184         Operation       *op,
185         SlapReply       *rs,
186         Entry           *e,
187         void            *priv )
188 {
189         return SLAP_CB_CONTINUE;
190 }
191 #endif
192
193 static int
194 bdb_monitor_free(
195         Entry           *e,
196         void            **priv )
197 {
198         struct berval   values[ 2 ];
199         Modification    mod = { 0 };
200
201         const char      *text;
202         char            textbuf[ SLAP_TEXT_BUFLEN ];
203
204         int             i, rc;
205
206         /* NOTE: if slap_shutdown != 0, priv might have already been freed */
207         *priv = NULL;
208
209         /* Remove objectClass */
210         mod.sm_op = LDAP_MOD_DELETE;
211         mod.sm_desc = slap_schema.si_ad_objectClass;
212         mod.sm_values = values;
213         values[ 0 ] = oc_olmBDBDatabase->soc_cname;
214         BER_BVZERO( &values[ 1 ] );
215
216         rc = modify_delete_values( e, &mod, 1, &text,
217                 textbuf, sizeof( textbuf ) );
218         /* don't care too much about return code... */
219
220         /* remove attrs */
221         for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
222                 mod.sm_desc = *s_at[ i ].ad;
223                 mod.sm_values = NULL;
224                 rc = modify_delete_values( e, &mod, 1, &text,
225                         textbuf, sizeof( textbuf ) );
226                 /* don't care too much about return code... */
227         }
228         
229         return SLAP_CB_CONTINUE;
230 }
231
232 #define bdb_monitor_initialize  BDB_SYMBOL(monitor_initialize)
233
234 /*
235  * call from within bdb_initialize()
236  */
237 static int
238 bdb_monitor_initialize( void )
239 {
240         int             i, code;
241         ConfigArgs c;
242         char    *argv[ 3 ];
243
244         static int      bdb_monitor_initialized = 0;
245
246         if ( backend_info( "monitor" ) == NULL ) {
247                 return -1;
248         }
249
250         if ( bdb_monitor_initialized++ ) {
251                 return 0;
252         }
253
254         /* register schema here */
255
256         argv[ 0 ] = "back-bdb/back-hdb monitor";
257         c.argv = argv;
258         c.argc = 3;
259         c.fname = argv[0];
260
261         for ( i = 0; s_oid[ i ].name; i++ ) {
262                 c.lineno = i;
263                 argv[ 1 ] = s_oid[ i ].name;
264                 argv[ 2 ] = s_oid[ i ].oid;
265
266                 if ( parse_oidm( &c, 0, NULL ) != 0 ) {
267                         Debug( LDAP_DEBUG_ANY,
268                                 "bdb_monitor_initialize: unable to add "
269                                 "objectIdentifier \"%s=%s\"\n",
270                                 s_oid[ i ].name, s_oid[ i ].oid, 0 );
271                         return 1;
272                 }
273         }
274
275         for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
276                 code = register_at( s_at[ i ].desc, s_at[ i ].ad, 1 );
277                 if ( code != LDAP_SUCCESS ) {
278                         Debug( LDAP_DEBUG_ANY,
279                                 "bdb_monitor_initialize: register_at failed\n",
280                                 0, 0, 0 );
281                 }
282         }
283
284         for ( i = 0; s_oc[ i ].desc != NULL; i++ ) {
285                 code = register_oc( s_oc[ i ].desc, s_oc[ i ].oc, 1 );
286                 if ( code != LDAP_SUCCESS ) {
287                         Debug( LDAP_DEBUG_ANY,
288                                 "bdb_monitor_initialize: register_oc failed\n",
289                                 0, 0, 0 );
290                 }
291         }
292
293         return 0;
294 }
295
296 /*
297  * call from within bdb_db_init()
298  */
299 int
300 bdb_monitor_db_init( BackendDB *be )
301 {
302         struct bdb_info         *bdb = (struct bdb_info *) be->be_private;
303
304         if ( SLAP_GLUE_SUBORDINATE( be ) ) {
305                 return 0;
306         }
307
308         if ( bdb_monitor_initialize() == LDAP_SUCCESS ) {
309                 /* monitoring in back-bdb is on by default */
310                 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
311         }
312
313 #ifdef BDB_MONITOR_IDX
314         bdb->bi_idx = NULL;
315         ldap_pvt_thread_mutex_init( &bdb->bi_idx_mutex );
316 #endif /* BDB_MONITOR_IDX */
317
318         bdb->bi_monitor.bdm_scope = -1;
319
320         return 0;
321 }
322
323 /*
324  * call from within bdb_db_open()
325  */
326 int
327 bdb_monitor_db_open( BackendDB *be )
328 {
329         struct bdb_info         *bdb = (struct bdb_info *) be->be_private;
330         Attribute               *a, *next;
331         monitor_callback_t      *cb = NULL;
332         struct berval           suffix, *filter, *base;
333         char                    *ptr;
334         int                     rc = 0;
335         BackendInfo             *mi;
336         monitor_extra_t         *mbe;
337
338         if ( !SLAP_DBMONITORING( be ) ) {
339                 return 0;
340         }
341
342         if ( SLAP_GLUE_SUBORDINATE( be ) ) {
343                 return 0;
344         }
345
346         mi = backend_info( "monitor" );
347         if ( !mi || !mi->bi_extra ) {
348                 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
349                 return 0;
350         }
351         mbe = mi->bi_extra;
352
353         /* don't bother if monitor is not configured */
354         if ( !mbe->is_configured() ) {
355                 static int warning = 0;
356
357                 if ( warning++ == 0 ) {
358                         Debug( LDAP_DEBUG_ANY, "bdb_monitor_open: "
359                                 "monitoring disabled; "
360                                 "configure monitor database to enable\n",
361                                 0, 0, 0 );
362                 }
363
364                 return 0;
365         }
366
367         if ( bdb->bi_monitor.bdm_scope == -1 ) {
368                 bdb->bi_monitor.bdm_scope = LDAP_SCOPE_ONELEVEL;
369         }
370         base = &bdb->bi_monitor.bdm_nbase;
371         BER_BVSTR( base, "cn=databases,cn=monitor" );
372         filter = &bdb->bi_monitor.bdm_filter;
373         BER_BVZERO( filter );
374
375         suffix.bv_len = ldap_bv2escaped_filter_value_len( &be->be_nsuffix[ 0 ] );
376         if ( suffix.bv_len == be->be_nsuffix[ 0 ].bv_len ) {
377                 suffix = be->be_nsuffix[ 0 ];
378
379         } else {
380                 ldap_bv2escaped_filter_value( &be->be_nsuffix[ 0 ], &suffix );
381         }
382
383         if ( BER_BVISEMPTY( &suffix ) ) {
384                 /* frontend also has empty suffix, sigh! */
385                 filter->bv_len = STRLENOF( "(&(namingContexts:distinguishedNameMatch:=" )
386                         + suffix.bv_len + STRLENOF( ")(!(cn=frontend)))" );
387                 ptr = filter->bv_val = ch_malloc( filter->bv_len + 1 );
388                 ptr = lutil_strcopy( ptr, "(&(namingContexts:distinguishedNameMatch:=" );
389                 ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
390                 ptr = lutil_strcopy( ptr, ")(!(cn=frontend)))" );
391
392         } else {
393                 /* just look for the naming context */
394                 filter->bv_len = STRLENOF( "(namingContexts:distinguishedNameMatch:=" )
395                         + suffix.bv_len + STRLENOF( ")" );
396                 ptr = filter->bv_val = ch_malloc( filter->bv_len + 1 );
397                 ptr = lutil_strcopy( ptr, "(namingContexts:distinguishedNameMatch:=" );
398                 ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
399                 ptr = lutil_strcopy( ptr, ")" );
400         }
401         ptr[ 0 ] = '\0';
402         assert( filter->bv_len == ptr - filter->bv_val );
403         
404         if ( suffix.bv_val != be->be_nsuffix[ 0 ].bv_val ) {
405                 ch_free( suffix.bv_val );
406         }
407
408         /* alloc as many as required (plus 1 for objectClass) */
409         a = attrs_alloc( 1 + 4 );
410         if ( a == NULL ) {
411                 rc = 1;
412                 goto cleanup;
413         }
414
415         a->a_desc = slap_schema.si_ad_objectClass;
416         value_add_one( &a->a_vals, &oc_olmBDBDatabase->soc_cname );
417         a->a_nvals = a->a_vals;
418         next = a->a_next;
419
420         {
421                 struct berval   bv = BER_BVC( "0" );
422
423                 next->a_desc = ad_olmBDBEntryCache;
424                 value_add_one( &next->a_vals, &bv );
425                 next->a_nvals = next->a_vals;
426                 next = next->a_next;
427
428                 next->a_desc = ad_olmBDBDNCache;
429                 value_add_one( &next->a_vals, &bv );
430                 next->a_nvals = next->a_vals;
431                 next = next->a_next;
432
433                 next->a_desc = ad_olmBDBIDLCache;
434                 value_add_one( &next->a_vals, &bv );
435                 next->a_nvals = next->a_vals;
436                 next = next->a_next;
437         }
438
439         {
440                 struct berval   bv, nbv;
441                 ber_len_t       pathlen = 0, len = 0;
442                 char            path[ PATH_MAX ] = { '\0' };
443                 char            *fname = bdb->bi_dbenv_home,
444                                 *ptr;
445
446                 len = strlen( fname );
447                 if ( fname[ 0 ] != '/' ) {
448                         /* get full path name */
449                         getcwd( path, sizeof( path ) );
450                         pathlen = strlen( path );
451
452                         if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
453                                 fname += 2;
454                                 len -= 2;
455                         }
456                 }
457
458                 bv.bv_len = pathlen + STRLENOF( "/" ) + len;
459                 ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
460                 if ( pathlen ) {
461                         ptr = lutil_strncopy( ptr, path, pathlen );
462                         ptr[ 0 ] = '/';
463                         ptr++;
464                 }
465                 ptr = lutil_strncopy( ptr, fname, len );
466                 if ( ptr[ -1 ] != '/' ) {
467                         ptr[ 0 ] = '/';
468                         ptr++;
469                 }
470                 ptr[ 0 ] = '\0';
471                 
472                 attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
473
474                 next->a_desc = ad_olmDbDirectory;
475                 next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
476                 next->a_vals[ 0 ] = bv;
477
478                 if ( BER_BVISNULL( &nbv ) ) {
479                         next->a_nvals = next->a_vals;
480
481                 } else {
482                         next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
483                         next->a_nvals[ 0 ] = nbv;
484                 }
485
486                 next = next->a_next;
487         }
488
489         cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
490         cb->mc_update = bdb_monitor_update;
491 #if 0   /* uncomment if required */
492         cb->mc_modify = bdb_monitor_modify;
493 #endif
494         cb->mc_free = bdb_monitor_free;
495         cb->mc_private = (void *)bdb;
496
497         /* make sure the database is registered; then add monitor attributes */
498         rc = mbe->register_database( be );
499         if ( rc == 0 ) {
500                 rc = mbe->register_entry_attrs( NULL, a, cb,
501                         base, bdb->bi_monitor.bdm_scope, filter );
502         }
503
504 cleanup:;
505         if ( rc != 0 ) {
506                 if ( cb != NULL ) {
507                         ch_free( cb );
508                         cb = NULL;
509                 }
510
511                 if ( a != NULL ) {
512                         attrs_free( a );
513                         a = NULL;
514                 }
515
516                 if ( !BER_BVISNULL( filter ) ) {
517                         ch_free( filter->bv_val );
518                         BER_BVZERO( filter );
519                 }
520         }
521
522         /* store for cleanup */
523         bdb->bi_monitor.bdm_cb = (void *)cb;
524
525         /* we don't need to keep track of the attributes, because
526          * bdb_monitor_free() takes care of everything */
527         if ( a != NULL ) {
528                 attrs_free( a );
529         }
530
531         return rc;
532 }
533
534 /*
535  * call from within bdb_db_close()
536  */
537 int
538 bdb_monitor_db_close( BackendDB *be )
539 {
540         struct bdb_info         *bdb = (struct bdb_info *) be->be_private;
541
542         if ( SLAP_GLUE_SUBORDINATE( be ) ) {
543                 return 0;
544         }
545
546         if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
547                 BackendInfo             *mi = backend_info( "monitor" );
548                 monitor_extra_t         *mbe;
549
550                 if ( mi && &mi->bi_extra ) {
551                         mbe = mi->bi_extra;
552                         mbe->unregister_entry_callback( NULL,
553                                 (monitor_callback_t *)bdb->bi_monitor.bdm_cb,
554                                 &bdb->bi_monitor.bdm_nbase,
555                                 bdb->bi_monitor.bdm_scope,
556                                 &bdb->bi_monitor.bdm_filter );
557                 }
558
559                 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
560                         ch_free( bdb->bi_monitor.bdm_filter.bv_val );
561                 }
562
563                 memset( &bdb->bi_monitor, 0, sizeof( bdb->bi_monitor ) );
564         }
565
566         return 0;
567 }
568
569 /*
570  * call from within bdb_db_destroy()
571  */
572 int
573 bdb_monitor_db_destroy( BackendDB *be )
574 {
575         if ( SLAP_GLUE_SUBORDINATE( be ) ) {
576                 return 0;
577         }
578
579 #ifdef BDB_MONITOR_IDX
580         {
581                 struct bdb_info         *bdb = (struct bdb_info *) be->be_private;
582
583                 /* TODO: free tree */
584                 ldap_pvt_thread_mutex_destroy( &bdb->bi_idx_mutex );
585                 avl_free( bdb->bi_idx, ch_free );
586         }
587 #endif /* BDB_MONITOR_IDX */
588
589         return 0;
590 }
591
592 #ifdef BDB_MONITOR_IDX
593
594 #define BDB_MONITOR_IDX_TYPES   (4)
595
596 typedef struct monitor_idx_t monitor_idx_t;
597
598 struct monitor_idx_t {
599         AttributeDescription    *idx_ad;
600         unsigned long           idx_count[BDB_MONITOR_IDX_TYPES];
601 };
602
603 static int
604 bdb_monitor_bitmask2key( slap_mask_t bitmask )
605 {
606         int     key;
607
608         for ( key = 0; key < 8*sizeof(slap_mask_t) && !( bitmask & 0x1U ); key++ ) {
609                 bitmask >>= 1;
610         }
611
612         return key;
613 }
614
615 static struct berval idxbv[] = {
616         BER_BVC( "present=" ),
617         BER_BVC( "equality=" ),
618         BER_BVC( "approx=" ),
619         BER_BVC( "substr=" ),
620         BER_BVNULL
621 };
622
623 static ber_len_t
624 bdb_monitor_idx2len( monitor_idx_t *idx )
625 {
626         int             i;
627         ber_len_t       len = 0;
628
629         for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
630                 if ( idx->idx_count[ i ] != 0 ) {
631                         len += idxbv[i].bv_len;
632                 }
633         }
634
635         return len;
636 }
637
638 static int
639 monitor_idx_cmp( const void *p1, const void *p2 )
640 {
641         const monitor_idx_t     *idx1 = (const monitor_idx_t *)p1;
642         const monitor_idx_t     *idx2 = (const monitor_idx_t *)p2;
643
644         return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad );
645 }
646
647 static int
648 monitor_idx_dup( void *p1, void *p2 )
649 {
650         monitor_idx_t   *idx1 = (monitor_idx_t *)p1;
651         monitor_idx_t   *idx2 = (monitor_idx_t *)p2;
652
653         return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad ) == 0 ? -1 : 0;
654 }
655
656 int
657 bdb_monitor_idx_add(
658         struct bdb_info         *bdb,
659         AttributeDescription    *desc,
660         slap_mask_t             type )
661 {
662         monitor_idx_t           idx_dummy = { 0 },
663                                 *idx;
664         int                     rc = 0, key;
665
666         idx_dummy.idx_ad = desc;
667         key = bdb_monitor_bitmask2key( type ) - 1;
668         if ( key >= BDB_MONITOR_IDX_TYPES ) {
669                 /* invalid index type */
670                 return -1;
671         }
672
673         ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );
674
675         idx = (monitor_idx_t *)avl_find( bdb->bi_idx,
676                 (caddr_t)&idx_dummy, monitor_idx_cmp );
677         if ( idx == NULL ) {
678                 idx = (monitor_idx_t *)ch_calloc( sizeof( monitor_idx_t ), 1 );
679                 idx->idx_ad = desc;
680                 idx->idx_count[ key ] = 1;
681
682                 switch ( avl_insert( &bdb->bi_idx, (caddr_t)idx, 
683                         monitor_idx_cmp, monitor_idx_dup ) )
684                 {
685                 case 0:
686                         break;
687
688                 default:
689                         ch_free( idx );
690                         rc = -1;
691                 }
692
693         } else {
694                 idx->idx_count[ key ]++;
695         }
696
697         ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );
698
699         return rc;
700 }
701
702 static int
703 bdb_monitor_idx_apply( void *v_idx, void *v_valp )
704 {
705         monitor_idx_t   *idx = (monitor_idx_t *)v_idx;
706         BerVarray       *valp = (BerVarray *)v_valp;
707
708         struct berval   bv;
709         char            *ptr;
710         char            count_buf[ BDB_MONITOR_IDX_TYPES ][ SLAP_TEXT_BUFLEN ];
711         ber_len_t       count_len[ BDB_MONITOR_IDX_TYPES ],
712                         idx_len;
713         int             i, num = 0;
714
715         idx_len = bdb_monitor_idx2len( idx );
716
717         bv.bv_len = 0;
718         for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
719                 if ( idx->idx_count[ i ] == 0 ) {
720                         continue;
721                 }
722
723                 count_len[ i ] = snprintf( count_buf[ i ],
724                         sizeof( count_buf[ i ] ), "%lu", idx->idx_count[ i ] );
725                 bv.bv_len += count_len[ i ];
726                 num++;
727         }
728
729         bv.bv_len += idx->idx_ad->ad_cname.bv_len
730                 + num
731                 + idx_len;
732         ptr = bv.bv_val = ch_malloc( bv.bv_len + 1 );
733         ptr = lutil_strcopy( ptr, idx->idx_ad->ad_cname.bv_val );
734         for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
735                 if ( idx->idx_count[ i ] == 0 ) {
736                         continue;
737                 }
738
739                 ptr[ 0 ] = '#';
740                 ++ptr;
741                 ptr = lutil_strcopy( ptr, idxbv[ i ].bv_val );
742                 ptr = lutil_strcopy( ptr, count_buf[ i ] );
743         }
744
745         ber_bvarray_add( valp, &bv );
746
747         return 0;
748 }
749
750 static int
751 bdb_monitor_idx_entry_add(
752         struct bdb_info *bdb,
753         Entry           *e )
754 {
755         BerVarray       vals = NULL;
756         Attribute       *a;
757
758         a = attr_find( e->e_attrs, ad_olmBDBNotIndexed );
759
760         ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );
761
762         avl_apply( bdb->bi_idx, bdb_monitor_idx_apply,
763                 &vals, -1, AVL_INORDER );
764
765         ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );
766
767         if ( vals != NULL ) {
768                 if ( a != NULL ) {
769                         assert( a->a_nvals == a->a_vals );
770
771                         ber_bvarray_free( a->a_vals );
772
773                 } else {
774                         Attribute       **ap;
775
776                         for ( ap = &e->e_attrs; *ap != NULL; ap = &(*ap)->a_next )
777                                 ;
778                         *ap = attr_alloc( ad_olmBDBNotIndexed );
779                         a = *ap;
780                 }
781                 a->a_vals = vals;
782                 a->a_nvals = a->a_vals;
783         }
784
785         return 0;
786 }
787
788 #endif /* BDB_MONITOR_IDX */