1 /* monitor.c - monitor bdb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2006 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"
30 static ObjectClass *oc_olmBDBDatabase;
32 static AttributeDescription *ad_olmBDBEntryCache,
33 *ad_olmBDBEntryInfo, *ad_olmBDBIDLCache,
37 * NOTE: there's some confusion in monitor OID arc;
38 * by now, let's consider:
40 * Subsystems monitor attributes 1.3.6.1.4.1.4203.666.1.55.0
41 * Databases monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1
42 * BDB database monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1.1
44 * Subsystems monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0
45 * Databases monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1
46 * BDB database monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1.1
53 { "olmBDBAttributes", "olmDatabaseAttributes:1" },
54 { "olmBDBObjectClasses", "olmDatabaseObjectClasses:1" },
61 AttributeDescription **ad;
63 { "( olmBDBAttributes:1 "
64 "NAME ( 'olmBDBEntryCache' ) "
65 "DESC 'Number of items in Entry Cache' "
67 "NO-USER-MODIFICATION "
68 "USAGE directoryOperation )",
69 &ad_olmBDBEntryCache },
71 { "( olmBDBAttributes:2 "
72 "NAME ( 'olmBDBEntryInfo' ) "
73 "DESC 'Number of items in EntryInfo Cache' "
75 "NO-USER-MODIFICATION "
76 "USAGE directoryOperation )",
77 &ad_olmBDBEntryInfo },
79 { "( olmBDBAttributes:3 "
80 "NAME ( 'olmBDBIDLCache' ) "
81 "DESC 'Number of items in IDL Cache' "
83 "NO-USER-MODIFICATION "
84 "USAGE directoryOperation )",
87 { "( olmBDBAttributes:4 "
88 "NAME ( 'olmDbDirectory' ) "
89 "DESC 'Path name of the directory "
90 "where the database environment resides' "
92 "NO-USER-MODIFICATION "
93 "USAGE directoryOperation )",
103 /* augments an existing object, so it must be AUXILIARY
104 * FIXME: derive from some ABSTRACT "monitoredEntity"? */
105 { "( olmBDBObjectClasses:1 "
106 "NAME ( 'olmBDBDatabase' ) "
114 &oc_olmBDBDatabase },
126 struct bdb_info *bdb = (struct bdb_info *) priv;
132 assert( ad_olmBDBEntryCache != NULL );
134 a = attr_find( e->e_attrs, ad_olmBDBEntryCache );
137 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_cursize );
138 ber_bvreplace( &a->a_vals[ 0 ], &bv );
140 a = attr_find( e->e_attrs, ad_olmBDBEntryInfo );
142 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_eiused );
143 ber_bvreplace( &a->a_vals[ 0 ], &bv );
145 a = attr_find( e->e_attrs, ad_olmBDBIDLCache );
147 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_idl_cache_size );
148 ber_bvreplace( &a->a_vals[ 0 ], &bv );
150 return SLAP_CB_CONTINUE;
160 return SLAP_CB_CONTINUE;
168 struct berval values[ 2 ];
169 Modification mod = { 0 };
172 char textbuf[ SLAP_TEXT_BUFLEN ];
176 /* NOTE: if slap_shutdown != 0, priv might have already been freed */
178 /* Remove objectClass */
179 mod.sm_op = LDAP_MOD_DELETE;
180 mod.sm_desc = slap_schema.si_ad_objectClass;
181 mod.sm_values = values;
182 values[ 0 ] = oc_olmBDBDatabase->soc_cname;
183 BER_BVZERO( &values[ 1 ] );
185 rc = modify_delete_values( e, &mod, 1, &text,
186 textbuf, sizeof( textbuf ) );
187 /* don't care too much about return code... */
190 for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
191 mod.sm_desc = *s_at[ i ].ad;
192 mod.sm_values = NULL;
193 rc = modify_delete_values( e, &mod, 1, &text,
194 textbuf, sizeof( textbuf ) );
195 /* don't care too much about return code... */
198 return SLAP_CB_CONTINUE;
202 * call from within bdb_initialize()
205 bdb_monitor_initialize( void )
209 static int bdb_monitor_initialized = 0;
211 if ( backend_info( "monitor" ) == NULL ) {
215 if ( bdb_monitor_initialized++ ) {
219 /* register schema here */
221 for ( i = 0; s_oid[ i ].name; i++ ) {
224 argv[ 0 ] = "back-bdb/back-hdb monitor";
225 argv[ 1 ] = s_oid[ i ].name;
226 argv[ 2 ] = s_oid[ i ].oid;
228 if ( parse_oidm( argv[ 0 ], i, 3, argv, 0, NULL ) != 0 ) {
229 Debug( LDAP_DEBUG_ANY,
230 "bdb_monitor_initialize: unable to add "
231 "objectIdentifier \"%s=%s\"\n",
232 s_oid[ i ].name, s_oid[ i ].oid, 0 );
237 for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
238 code = register_at( s_at[ i ].desc, s_at[ i ].ad, 1 );
239 if ( code != LDAP_SUCCESS ) {
240 Debug( LDAP_DEBUG_ANY,
241 "bdb_monitor_initialize: register_at failed\n",
246 for ( i = 0; s_oc[ i ].desc != NULL; i++ ) {
247 code = register_oc( s_oc[ i ].desc, s_oc[ i ].oc, 1 );
248 if ( code != LDAP_SUCCESS ) {
249 Debug( LDAP_DEBUG_ANY,
250 "bdb_monitor_initialize: register_oc failed\n",
259 * call from within bdb_db_init()
262 bdb_monitor_db_init( BackendDB *be )
264 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
266 if ( bdb_monitor_initialize() == LDAP_SUCCESS ) {
267 /* monitoring in back-bdb is on by default */
268 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
271 bdb->bi_monitor.bdm_scope = -1;
277 * call from within bdb_db_open()
280 bdb_monitor_db_open( BackendDB *be )
282 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
284 monitor_callback_t *cb = NULL;
285 struct berval suffix, *filter, *base;
289 monitor_extra_t *mbe;
291 if ( !SLAP_DBMONITORING( be ) ) {
295 mi = backend_info( "monitor" );
296 if ( !mi || !mi->bi_extra ) {
297 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
302 /* don't bother if monitor is not configured */
303 if ( !mbe->is_configured() ) {
304 static int warning = 0;
306 if ( warning++ == 0 ) {
307 Debug( LDAP_DEBUG_ANY, "bdb_monitor_open: "
308 "monitoring disabled; "
309 "configure monitor database to enable\n",
316 if ( bdb->bi_monitor.bdm_scope == -1 ) {
317 bdb->bi_monitor.bdm_scope = LDAP_SCOPE_ONELEVEL;
319 base = &bdb->bi_monitor.bdm_nbase;
320 BER_BVSTR( base, "cn=databases,cn=monitor" );
321 filter = &bdb->bi_monitor.bdm_filter;
322 BER_BVZERO( filter );
324 suffix.bv_len = ldap_bv2escaped_filter_value_len( &be->be_nsuffix[ 0 ] );
325 if ( suffix.bv_len == be->be_nsuffix[ 0 ].bv_len ) {
326 suffix = be->be_nsuffix[ 0 ];
329 ldap_bv2escaped_filter_value( &be->be_nsuffix[ 0 ], &suffix );
332 /* just look for the naming context */
333 filter->bv_len = STRLENOF( "(namingContexts:distinguishedNameMatch:=" )
334 + suffix.bv_len + STRLENOF( ")" );
335 ptr = filter->bv_val = ch_malloc( filter->bv_len + 1 );
336 ptr = lutil_strcopy( ptr, "(namingContexts:distinguishedNameMatch:=" );
337 ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
338 ptr = lutil_strcopy( ptr, ")" );
340 assert( filter->bv_len == ptr - filter->bv_val );
342 if ( suffix.bv_val != be->be_nsuffix[ 0 ].bv_val ) {
343 ch_free( suffix.bv_val );
346 /* alloc as many as required (plus 1 for objectClass) */
347 a = attrs_alloc( 1 + 4 );
353 a->a_desc = slap_schema.si_ad_objectClass;
354 value_add_one( &a->a_vals, &oc_olmBDBDatabase->soc_cname );
355 a->a_nvals = a->a_vals;
359 struct berval bv = BER_BVC( "0" );
361 next->a_desc = ad_olmBDBEntryCache;
362 value_add_one( &next->a_vals, &bv );
363 next->a_nvals = next->a_vals;
366 next->a_desc = ad_olmBDBEntryInfo;
367 value_add_one( &next->a_vals, &bv );
368 next->a_nvals = next->a_vals;
371 next->a_desc = ad_olmBDBIDLCache;
372 value_add_one( &next->a_vals, &bv );
373 next->a_nvals = next->a_vals;
378 struct berval bv, nbv;
379 ber_len_t pathlen = 0, len = 0;
380 char path[ PATH_MAX ] = { '\0' };
381 char *fname = bdb->bi_dbenv_home,
384 len = strlen( fname );
385 if ( fname[ 0 ] != '/' ) {
386 /* get full path name */
387 getcwd( path, sizeof( path ) );
388 pathlen = strlen( path );
390 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
396 bv.bv_len = pathlen + STRLENOF( "/" ) + len;
397 ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
399 ptr = lutil_strncopy( ptr, path, pathlen );
403 ptr = lutil_strncopy( ptr, fname, len );
404 if ( ptr[ -1 ] != '/' ) {
410 attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
412 next->a_desc = ad_olmDbDirectory;
413 next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
414 next->a_vals[ 0 ] = bv;
416 if ( BER_BVISNULL( &nbv ) ) {
417 next->a_nvals = next->a_vals;
420 next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
421 next->a_nvals[ 0 ] = nbv;
427 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
428 cb->mc_update = bdb_monitor_update;
429 #if 0 /* uncomment if required */
430 cb->mc_modify = bdb_monitor_modify;
432 cb->mc_free = bdb_monitor_free;
433 cb->mc_private = (void *)bdb;
435 rc = mbe->register_entry_attrs( NULL, a, cb,
436 base, bdb->bi_monitor.bdm_scope, filter );
450 if ( !BER_BVISNULL( filter ) ) {
451 ch_free( filter->bv_val );
452 BER_BVZERO( filter );
456 /* store for cleanup */
457 bdb->bi_monitor.bdm_cb = (void *)cb;
459 /* we don't need to keep track of the attributes, because
460 * bdb_monitor_free() takes care of everything */
469 * call from within bdb_db_close()
472 bdb_monitor_db_close( BackendDB *be )
474 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
476 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
477 BackendInfo *mi = backend_info( "monitor" );
478 monitor_extra_t *mbe;
480 if ( mi && &mi->bi_extra ) {
482 mbe->unregister_entry_callback( NULL,
483 (monitor_callback_t *)bdb->bi_monitor.bdm_cb,
484 &bdb->bi_monitor.bdm_nbase,
485 bdb->bi_monitor.bdm_scope,
486 &bdb->bi_monitor.bdm_filter );
489 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
490 ch_free( bdb->bi_monitor.bdm_filter.bv_val );
493 memset( &bdb->bi_monitor, 0, sizeof( bdb->bi_monitor ) );
500 * call from within bdb_db_destroy()
503 bdb_monitor_db_destroy( BackendDB *be )