1 /* monitor.c - monitor bdb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
28 #include "../back-monitor/back-monitor.h"
32 static ObjectClass *oc_olmBDBDatabase;
34 static AttributeDescription *ad_olmBDBEntryCache,
35 *ad_olmBDBEntryInfo, *ad_olmBDBIDLCache,
38 #ifdef BDB_MONITOR_IDX
40 bdb_monitor_idx_entry_add(
44 static AttributeDescription *ad_olmBDBNotIndexed;
45 #endif /* BDB_MONITOR_IDX */
48 * NOTE: there's some confusion in monitor OID arc;
49 * by now, let's consider:
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
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
64 { "olmBDBAttributes", "olmDatabaseAttributes:1" },
65 { "olmBDBObjectClasses", "olmDatabaseObjectClasses:1" },
72 AttributeDescription **ad;
74 { "( olmBDBAttributes:1 "
75 "NAME ( 'olmBDBEntryCache' ) "
76 "DESC 'Number of items in Entry Cache' "
78 "NO-USER-MODIFICATION "
79 "USAGE dSAOperation )",
80 &ad_olmBDBEntryCache },
82 { "( olmBDBAttributes:2 "
83 "NAME ( 'olmBDBEntryInfo' ) "
84 "DESC 'Number of items in EntryInfo Cache' "
86 "NO-USER-MODIFICATION "
87 "USAGE dSAOperation )",
88 &ad_olmBDBEntryInfo },
90 { "( olmBDBAttributes:3 "
91 "NAME ( 'olmBDBIDLCache' ) "
92 "DESC 'Number of items in IDL Cache' "
94 "NO-USER-MODIFICATION "
95 "USAGE dSAOperation )",
98 { "( olmBDBAttributes:4 "
99 "NAME ( 'olmDbDirectory' ) "
100 "DESC 'Path name of the directory "
101 "where the database environment resides' "
103 "NO-USER-MODIFICATION "
104 "USAGE dSAOperation )",
105 &ad_olmDbDirectory },
107 #ifdef BDB_MONITOR_IDX
108 { "( olmBDBAttributes:5 "
109 "NAME ( 'olmBDBNotIndexed' ) "
110 "DESC 'Missing indexes resulting from candidate selection' "
112 "NO-USER-MODIFICATION "
113 "USAGE dSAOperation )",
114 &ad_olmBDBNotIndexed },
115 #endif /* BDB_MONITOR_IDX */
124 /* augments an existing object, so it must be AUXILIARY
125 * FIXME: derive from some ABSTRACT "monitoredEntity"? */
126 { "( olmBDBObjectClasses:1 "
127 "NAME ( 'olmBDBDatabase' ) "
134 #ifdef BDB_MONITOR_IDX
135 "$ olmBDBNotIndexed "
136 #endif /* BDB_MONITOR_IDX */
138 &oc_olmBDBDatabase },
150 struct bdb_info *bdb = (struct bdb_info *) priv;
156 assert( ad_olmBDBEntryCache != NULL );
158 a = attr_find( e->e_attrs, ad_olmBDBEntryCache );
161 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_cursize );
162 ber_bvreplace( &a->a_vals[ 0 ], &bv );
164 a = attr_find( e->e_attrs, ad_olmBDBEntryInfo );
166 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_eiused );
167 ber_bvreplace( &a->a_vals[ 0 ], &bv );
169 a = attr_find( e->e_attrs, ad_olmBDBIDLCache );
171 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_idl_cache_size );
172 ber_bvreplace( &a->a_vals[ 0 ], &bv );
174 #ifdef BDB_MONITOR_IDX
175 bdb_monitor_idx_entry_add( bdb, e );
176 #endif /* BDB_MONITOR_IDX */
178 return SLAP_CB_CONTINUE;
181 #if 0 /* uncomment if required */
189 return SLAP_CB_CONTINUE;
198 struct berval values[ 2 ];
199 Modification mod = { 0 };
202 char textbuf[ SLAP_TEXT_BUFLEN ];
206 /* NOTE: if slap_shutdown != 0, priv might have already been freed */
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 ] );
216 rc = modify_delete_values( e, &mod, 1, &text,
217 textbuf, sizeof( textbuf ) );
218 /* don't care too much about return code... */
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... */
229 return SLAP_CB_CONTINUE;
232 #define bdb_monitor_initialize BDB_SYMBOL(monitor_initialize)
235 * call from within bdb_initialize()
238 bdb_monitor_initialize( void )
244 static int bdb_monitor_initialized = 0;
246 if ( backend_info( "monitor" ) == NULL ) {
250 if ( bdb_monitor_initialized++ ) {
254 /* register schema here */
256 argv[ 0 ] = "back-bdb/back-hdb monitor";
261 for ( i = 0; s_oid[ i ].name; i++ ) {
263 argv[ 1 ] = s_oid[ i ].name;
264 argv[ 2 ] = s_oid[ i ].oid;
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 );
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",
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",
297 * call from within bdb_db_init()
300 bdb_monitor_db_init( BackendDB *be )
302 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
304 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
308 if ( bdb_monitor_initialize() == LDAP_SUCCESS ) {
309 /* monitoring in back-bdb is on by default */
310 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
313 #ifdef BDB_MONITOR_IDX
315 ldap_pvt_thread_mutex_init( &bdb->bi_idx_mutex );
316 #endif /* BDB_MONITOR_IDX */
318 bdb->bi_monitor.bdm_scope = -1;
324 * call from within bdb_db_open()
327 bdb_monitor_db_open( BackendDB *be )
329 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
331 monitor_callback_t *cb = NULL;
332 struct berval suffix, *filter, *base;
336 monitor_extra_t *mbe;
338 if ( !SLAP_DBMONITORING( be ) ) {
342 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
346 mi = backend_info( "monitor" );
347 if ( !mi || !mi->bi_extra ) {
348 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
353 /* don't bother if monitor is not configured */
354 if ( !mbe->is_configured() ) {
355 static int warning = 0;
357 if ( warning++ == 0 ) {
358 Debug( LDAP_DEBUG_ANY, "bdb_monitor_open: "
359 "monitoring disabled; "
360 "configure monitor database to enable\n",
367 if ( bdb->bi_monitor.bdm_scope == -1 ) {
368 bdb->bi_monitor.bdm_scope = LDAP_SCOPE_ONELEVEL;
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 );
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 ];
380 ldap_bv2escaped_filter_value( &be->be_nsuffix[ 0 ], &suffix );
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)))" );
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, ")" );
402 assert( filter->bv_len == ptr - filter->bv_val );
404 if ( suffix.bv_val != be->be_nsuffix[ 0 ].bv_val ) {
405 ch_free( suffix.bv_val );
408 /* alloc as many as required (plus 1 for objectClass) */
409 a = attrs_alloc( 1 + 4 );
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;
421 struct berval bv = BER_BVC( "0" );
423 next->a_desc = ad_olmBDBEntryCache;
424 value_add_one( &next->a_vals, &bv );
425 next->a_nvals = next->a_vals;
428 next->a_desc = ad_olmBDBEntryInfo;
429 value_add_one( &next->a_vals, &bv );
430 next->a_nvals = next->a_vals;
433 next->a_desc = ad_olmBDBIDLCache;
434 value_add_one( &next->a_vals, &bv );
435 next->a_nvals = next->a_vals;
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,
446 len = strlen( fname );
447 if ( fname[ 0 ] != '/' ) {
448 /* get full path name */
449 getcwd( path, sizeof( path ) );
450 pathlen = strlen( path );
452 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
458 bv.bv_len = pathlen + STRLENOF( "/" ) + len;
459 ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
461 ptr = lutil_strncopy( ptr, path, pathlen );
465 ptr = lutil_strncopy( ptr, fname, len );
466 if ( ptr[ -1 ] != '/' ) {
472 attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
474 next->a_desc = ad_olmDbDirectory;
475 next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
476 next->a_vals[ 0 ] = bv;
478 if ( BER_BVISNULL( &nbv ) ) {
479 next->a_nvals = next->a_vals;
482 next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
483 next->a_nvals[ 0 ] = nbv;
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;
494 cb->mc_free = bdb_monitor_free;
495 cb->mc_private = (void *)bdb;
497 /* make sure the database is registered; then add monitor attributes */
498 rc = mbe->register_database( be );
500 rc = mbe->register_entry_attrs( NULL, a, cb,
501 base, bdb->bi_monitor.bdm_scope, filter );
516 if ( !BER_BVISNULL( filter ) ) {
517 ch_free( filter->bv_val );
518 BER_BVZERO( filter );
522 /* store for cleanup */
523 bdb->bi_monitor.bdm_cb = (void *)cb;
525 /* we don't need to keep track of the attributes, because
526 * bdb_monitor_free() takes care of everything */
535 * call from within bdb_db_close()
538 bdb_monitor_db_close( BackendDB *be )
540 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
542 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
546 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
547 BackendInfo *mi = backend_info( "monitor" );
548 monitor_extra_t *mbe;
550 if ( mi && &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 );
559 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
560 ch_free( bdb->bi_monitor.bdm_filter.bv_val );
563 memset( &bdb->bi_monitor, 0, sizeof( bdb->bi_monitor ) );
570 * call from within bdb_db_destroy()
573 bdb_monitor_db_destroy( BackendDB *be )
575 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
579 #ifdef BDB_MONITOR_IDX
581 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
583 /* TODO: free tree */
584 ldap_pvt_thread_mutex_destroy( &bdb->bi_idx_mutex );
585 avl_free( bdb->bi_idx, ch_free );
587 #endif /* BDB_MONITOR_IDX */
592 #ifdef BDB_MONITOR_IDX
593 typedef struct monitor_idx_t monitor_idx_t;
595 struct monitor_idx_t {
596 AttributeDescription *idx_ad;
597 slap_mask_t idx_type;
598 unsigned long idx_count;
602 monitor_idx_cmp( const void *p1, const void *p2 )
604 const monitor_idx_t *idx1 = (const monitor_idx_t *)p1;
605 const monitor_idx_t *idx2 = (const monitor_idx_t *)p2;
607 return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad );
611 monitor_idx_dup( void *p1, void *p2 )
613 monitor_idx_t *idx1 = (monitor_idx_t *)p1;
614 monitor_idx_t *idx2 = (monitor_idx_t *)p2;
616 return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad ) == 0 ? -1 : 0;
621 struct bdb_info *bdb,
622 AttributeDescription *desc,
625 monitor_idx_t idx_dummy = { 0 },
629 idx_dummy.idx_ad = desc;
630 if ( type == SLAP_INDEX_SUBSTR ) {
631 type = SLAP_INDEX_SUBSTR_DEFAULT;
634 ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );
636 idx = (monitor_idx_t *)avl_find( bdb->bi_idx,
637 (caddr_t)&idx_dummy, monitor_idx_cmp );
639 idx = (monitor_idx_t *)ch_malloc( sizeof( monitor_idx_t ) );
641 idx->idx_type = type;
644 switch ( avl_insert( &bdb->bi_idx, (caddr_t)idx,
645 monitor_idx_cmp, monitor_idx_dup ) )
656 idx->idx_type |= type;
660 ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );
666 bdb_monitor_idx_apply( void *v_idx, void *v_valp )
668 monitor_idx_t *idx = (monitor_idx_t *)v_idx;
669 BerVarray *valp = (BerVarray *)v_valp;
671 struct berval bv, index;
673 char count_buf[ SLAP_TEXT_BUFLEN ];
676 count_len = snprintf( count_buf, sizeof( count_buf ), "%lu", idx->idx_count );
677 slap_index2bvlen( idx->idx_type, &index );
679 bv.bv_len = idx->idx_ad->ad_cname.bv_len
683 ptr = bv.bv_val = ch_malloc( bv.bv_len + 1 );
684 ptr = lutil_strcopy( ptr, idx->idx_ad->ad_cname.bv_val );
687 ptr = lutil_strcopy( ptr, count_buf );
689 index.bv_val = ++ptr;
690 assert( index.bv_val + index.bv_len == bv.bv_val + bv.bv_len );
691 slap_index2bv( idx->idx_type, &index );
693 ber_bvarray_add( valp, &bv );
699 bdb_monitor_idx_entry_add(
700 struct bdb_info *bdb,
703 BerVarray vals = NULL;
706 a = attr_find( e->e_attrs, ad_olmBDBNotIndexed );
708 ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );
710 avl_apply( bdb->bi_idx, bdb_monitor_idx_apply,
711 &vals, -1, AVL_INORDER );
713 ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );
715 if ( vals != NULL ) {
717 assert( a->a_nvals == a->a_vals );
719 ber_bvarray_free( a->a_vals );
724 for ( ap = &e->e_attrs; *ap != NULL; ap = &(*ap)->a_next )
726 *ap = attr_alloc( ad_olmBDBNotIndexed );
730 a->a_nvals = a->a_vals;
736 #endif /* BDB_MONITOR_IDX */