1 /* monitor.c - monitor bdb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 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_olmBDBDNCache, *ad_olmBDBIDLCache,
38 #ifdef BDB_MONITOR_IDX
40 bdb_monitor_idx_entry_add(
44 static AttributeDescription *ad_olmDbNotIndexed;
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 ( 'olmBDBDNCache' ) "
84 "DESC 'Number of items in DN Cache' "
86 "NO-USER-MODIFICATION "
87 "USAGE dSAOperation )",
90 { "( olmBDBAttributes:3 "
91 "NAME ( 'olmBDBIDLCache' ) "
92 "DESC 'Number of items in IDL Cache' "
94 "NO-USER-MODIFICATION "
95 "USAGE dSAOperation )",
98 { "( olmDatabaseAttributes:1 "
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 { "( olmDatabaseAttributes:2 "
109 "NAME ( 'olmDbNotIndexed' ) "
110 "DESC 'Missing indexes resulting from candidate selection' "
112 "NO-USER-MODIFICATION "
113 "USAGE dSAOperation )",
114 &ad_olmDbNotIndexed },
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
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 ), "%lu", bdb->bi_cache.c_cursize );
162 ber_bvreplace( &a->a_vals[ 0 ], &bv );
164 a = attr_find( e->e_attrs, ad_olmBDBDNCache );
166 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", 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 ), "%lu", 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;
214 values[ 0 ] = oc_olmBDBDatabase->soc_cname;
215 BER_BVZERO( &values[ 1 ] );
217 rc = modify_delete_values( e, &mod, 1, &text,
218 textbuf, sizeof( textbuf ) );
219 /* don't care too much about return code... */
222 mod.sm_values = NULL;
224 for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
225 mod.sm_desc = *s_at[ i ].ad;
226 rc = modify_delete_values( e, &mod, 1, &text,
227 textbuf, sizeof( textbuf ) );
228 /* don't care too much about return code... */
231 return SLAP_CB_CONTINUE;
234 #define bdb_monitor_initialize BDB_SYMBOL(monitor_initialize)
237 * call from within bdb_initialize()
240 bdb_monitor_initialize( void )
246 static int bdb_monitor_initialized = 0;
248 /* set to 0 when successfully initialized; otherwise, remember failure */
249 static int bdb_monitor_initialized_failure = 1;
251 if ( bdb_monitor_initialized++ ) {
252 return bdb_monitor_initialized_failure;
255 if ( backend_info( "monitor" ) == NULL ) {
259 /* register schema here */
261 argv[ 0 ] = "back-bdb/back-hdb monitor";
266 for ( i = 0; s_oid[ i ].name; i++ ) {
268 argv[ 1 ] = s_oid[ i ].name;
269 argv[ 2 ] = s_oid[ i ].oid;
271 if ( parse_oidm( &c, 0, NULL ) != 0 ) {
272 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_initialize)
274 "objectIdentifier \"%s=%s\"\n",
275 s_oid[ i ].name, s_oid[ i ].oid, 0 );
280 for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
281 code = register_at( s_at[ i ].desc, s_at[ i ].ad, 1 );
282 if ( code != LDAP_SUCCESS ) {
283 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_initialize)
284 ": register_at failed for attributeType (%s)\n",
285 s_at[ i ].desc, 0, 0 );
289 (*s_at[ i ].ad)->ad_type->sat_flags |= SLAP_AT_HIDE;
293 for ( i = 0; s_oc[ i ].desc != NULL; i++ ) {
294 code = register_oc( s_oc[ i ].desc, s_oc[ i ].oc, 1 );
295 if ( code != LDAP_SUCCESS ) {
296 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_initialize)
297 ": register_oc failed for objectClass (%s)\n",
298 s_oc[ i ].desc, 0, 0 );
302 (*s_oc[ i ].oc)->soc_flags |= SLAP_OC_HIDE;
306 return ( bdb_monitor_initialized_failure = LDAP_SUCCESS );
310 * call from within bdb_db_init()
313 bdb_monitor_db_init( BackendDB *be )
315 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
317 if ( bdb_monitor_initialize() == LDAP_SUCCESS ) {
318 /* monitoring in back-bdb is on by default */
319 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
322 #ifdef BDB_MONITOR_IDX
324 ldap_pvt_thread_mutex_init( &bdb->bi_idx_mutex );
325 #endif /* BDB_MONITOR_IDX */
331 * call from within bdb_db_open()
334 bdb_monitor_db_open( BackendDB *be )
336 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
338 monitor_callback_t *cb = NULL;
341 monitor_extra_t *mbe;
342 struct berval dummy = BER_BVC("");
344 if ( !SLAP_DBMONITORING( be ) ) {
348 mi = backend_info( "monitor" );
349 if ( !mi || !mi->bi_extra ) {
350 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
355 /* don't bother if monitor is not configured */
356 if ( !mbe->is_configured() ) {
357 static int warning = 0;
359 if ( warning++ == 0 ) {
360 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_db_open)
361 ": monitoring disabled; "
362 "configure monitor database to enable\n",
369 /* alloc as many as required (plus 1 for objectClass) */
370 a = attrs_alloc( 1 + 4 );
376 a->a_desc = slap_schema.si_ad_objectClass;
377 attr_valadd( a, &oc_olmBDBDatabase->soc_cname, NULL, 1 );
381 struct berval bv = BER_BVC( "0" );
383 next->a_desc = ad_olmBDBEntryCache;
384 attr_valadd( next, &bv, NULL, 1 );
387 next->a_desc = ad_olmBDBDNCache;
388 attr_valadd( next, &bv, NULL, 1 );
391 next->a_desc = ad_olmBDBIDLCache;
392 attr_valadd( next, &bv, NULL, 1 );
397 struct berval bv, nbv;
398 ber_len_t pathlen = 0, len = 0;
399 char path[ MAXPATHLEN ] = { '\0' };
400 char *fname = bdb->bi_dbenv_home,
403 len = strlen( fname );
404 if ( fname[ 0 ] != '/' ) {
405 /* get full path name */
406 getcwd( path, sizeof( path ) );
407 pathlen = strlen( path );
409 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
415 bv.bv_len = pathlen + STRLENOF( "/" ) + len;
416 ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
418 ptr = lutil_strncopy( ptr, path, pathlen );
422 ptr = lutil_strncopy( ptr, fname, len );
423 if ( ptr[ -1 ] != '/' ) {
429 attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
431 next->a_desc = ad_olmDbDirectory;
432 next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
433 next->a_vals[ 0 ] = bv;
436 if ( BER_BVISNULL( &nbv ) ) {
437 next->a_nvals = next->a_vals;
440 next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
441 next->a_nvals[ 0 ] = nbv;
447 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
448 cb->mc_update = bdb_monitor_update;
449 #if 0 /* uncomment if required */
450 cb->mc_modify = bdb_monitor_modify;
452 cb->mc_free = bdb_monitor_free;
453 cb->mc_private = (void *)bdb;
455 /* make sure the database is registered; then add monitor attributes */
456 rc = mbe->register_database( be, &bdb->bi_monitor.bdm_ndn );
458 rc = mbe->register_entry_attrs( &bdb->bi_monitor.bdm_ndn, a, cb,
475 /* store for cleanup */
476 bdb->bi_monitor.bdm_cb = (void *)cb;
478 /* we don't need to keep track of the attributes, because
479 * bdb_monitor_free() takes care of everything */
488 * call from within bdb_db_close()
491 bdb_monitor_db_close( BackendDB *be )
493 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
495 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_ndn ) ) {
496 BackendInfo *mi = backend_info( "monitor" );
497 monitor_extra_t *mbe;
499 if ( mi && &mi->bi_extra ) {
501 mbe->unregister_entry_callback( &bdb->bi_monitor.bdm_ndn,
502 (monitor_callback_t *)bdb->bi_monitor.bdm_cb,
506 memset( &bdb->bi_monitor, 0, sizeof( bdb->bi_monitor ) );
513 * call from within bdb_db_destroy()
516 bdb_monitor_db_destroy( BackendDB *be )
518 #ifdef BDB_MONITOR_IDX
519 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
521 /* TODO: free tree */
522 ldap_pvt_thread_mutex_destroy( &bdb->bi_idx_mutex );
523 avl_free( bdb->bi_idx, ch_free );
524 #endif /* BDB_MONITOR_IDX */
529 #ifdef BDB_MONITOR_IDX
531 #define BDB_MONITOR_IDX_TYPES (4)
533 typedef struct monitor_idx_t monitor_idx_t;
535 struct monitor_idx_t {
536 AttributeDescription *idx_ad;
537 unsigned long idx_count[BDB_MONITOR_IDX_TYPES];
541 bdb_monitor_bitmask2key( slap_mask_t bitmask )
545 for ( key = 0; key < 8 * (int)sizeof(slap_mask_t) && !( bitmask & 0x1U );
552 static struct berval idxbv[] = {
553 BER_BVC( "present=" ),
554 BER_BVC( "equality=" ),
555 BER_BVC( "approx=" ),
556 BER_BVC( "substr=" ),
561 bdb_monitor_idx2len( monitor_idx_t *idx )
566 for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
567 if ( idx->idx_count[ i ] != 0 ) {
568 len += idxbv[i].bv_len;
576 monitor_idx_cmp( const void *p1, const void *p2 )
578 const monitor_idx_t *idx1 = (const monitor_idx_t *)p1;
579 const monitor_idx_t *idx2 = (const monitor_idx_t *)p2;
581 return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad );
585 monitor_idx_dup( void *p1, void *p2 )
587 monitor_idx_t *idx1 = (monitor_idx_t *)p1;
588 monitor_idx_t *idx2 = (monitor_idx_t *)p2;
590 return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad ) == 0 ? -1 : 0;
595 struct bdb_info *bdb,
596 AttributeDescription *desc,
599 monitor_idx_t idx_dummy = { 0 },
603 idx_dummy.idx_ad = desc;
604 key = bdb_monitor_bitmask2key( type ) - 1;
605 if ( key >= BDB_MONITOR_IDX_TYPES ) {
606 /* invalid index type */
610 ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );
612 idx = (monitor_idx_t *)avl_find( bdb->bi_idx,
613 (caddr_t)&idx_dummy, monitor_idx_cmp );
615 idx = (monitor_idx_t *)ch_calloc( sizeof( monitor_idx_t ), 1 );
617 idx->idx_count[ key ] = 1;
619 switch ( avl_insert( &bdb->bi_idx, (caddr_t)idx,
620 monitor_idx_cmp, monitor_idx_dup ) )
631 idx->idx_count[ key ]++;
634 ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );
640 bdb_monitor_idx_apply( void *v_idx, void *v_valp )
642 monitor_idx_t *idx = (monitor_idx_t *)v_idx;
643 BerVarray *valp = (BerVarray *)v_valp;
647 char count_buf[ BDB_MONITOR_IDX_TYPES ][ SLAP_TEXT_BUFLEN ];
648 ber_len_t count_len[ BDB_MONITOR_IDX_TYPES ],
652 idx_len = bdb_monitor_idx2len( idx );
655 for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
656 if ( idx->idx_count[ i ] == 0 ) {
660 count_len[ i ] = snprintf( count_buf[ i ],
661 sizeof( count_buf[ i ] ), "%lu", idx->idx_count[ i ] );
662 bv.bv_len += count_len[ i ];
666 bv.bv_len += idx->idx_ad->ad_cname.bv_len
669 ptr = bv.bv_val = ch_malloc( bv.bv_len + 1 );
670 ptr = lutil_strcopy( ptr, idx->idx_ad->ad_cname.bv_val );
671 for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
672 if ( idx->idx_count[ i ] == 0 ) {
678 ptr = lutil_strcopy( ptr, idxbv[ i ].bv_val );
679 ptr = lutil_strcopy( ptr, count_buf[ i ] );
682 ber_bvarray_add( valp, &bv );
688 bdb_monitor_idx_entry_add(
689 struct bdb_info *bdb,
692 BerVarray vals = NULL;
695 a = attr_find( e->e_attrs, ad_olmDbNotIndexed );
697 ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );
699 avl_apply( bdb->bi_idx, bdb_monitor_idx_apply,
700 &vals, -1, AVL_INORDER );
702 ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );
704 if ( vals != NULL ) {
706 assert( a->a_nvals == a->a_vals );
708 ber_bvarray_free( a->a_vals );
713 for ( ap = &e->e_attrs; *ap != NULL; ap = &(*ap)->a_next )
715 *ap = attr_alloc( ad_olmDbNotIndexed );
719 a->a_nvals = a->a_vals;
725 #endif /* BDB_MONITOR_IDX */