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>
27 #include "../back-monitor/back-monitor.h"
29 static ObjectClass *oc_olmBDBDatabase;
31 static AttributeDescription *ad_olmBDBEntryCache,
32 *ad_olmBDBEntryInfo, *ad_olmBDBIDLCache,
36 * NOTE: there's some confusion in monitor OID arc;
37 * by now, let's consider:
39 * Subsystems monitor attributes 1.3.6.1.4.1.4203.666.1.55.0
40 * Databases monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1
41 * BDB database monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1.1
43 * Subsystems monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0
44 * Databases monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1
45 * BDB database monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1.1
52 { "olmBDBAttributes", "olmDatabaseAttributes:1" },
53 { "olmBDBObjectClasses", "olmDatabaseObjectClasses:1" },
61 AttributeDescription **ad;
63 { "olmBDBEntryCache", "( olmBDBAttributes:1 "
64 "NAME ( 'olmBDBEntryCache' ) "
65 "DESC 'Number of items in Entry Cache' "
67 "NO-USER-MODIFICATION "
68 "USAGE directoryOperation )",
69 &ad_olmBDBEntryCache },
71 { "olmBDBEntryInfo", "( olmBDBAttributes:2 "
72 "NAME ( 'olmBDBEntryInfo' ) "
73 "DESC 'Number of items in EntryInfo Cache' "
75 "NO-USER-MODIFICATION "
76 "USAGE directoryOperation )",
77 &ad_olmBDBEntryInfo },
79 { "olmBDBIDLCache", "( olmBDBAttributes:3 "
80 "NAME ( 'olmBDBIDLCache' ) "
81 "DESC 'Number of items in IDL Cache' "
83 "NO-USER-MODIFICATION "
84 "USAGE directoryOperation )",
87 { "olmDbDirectory", "( 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 )",
104 /* augments an existing object, so it must be AUXILIARY
105 * FIXME: derive from some ABSTRACT "monitoredEntity"? */
106 { "olmBDBDatabase", "( olmBDBObjectClasses:1 "
107 "NAME ( 'olmBDBDatabase' ) "
115 &oc_olmBDBDatabase },
127 struct bdb_info *bdb = (struct bdb_info *) priv;
133 assert( ad_olmBDBEntryCache != NULL );
135 a = attr_find( e->e_attrs, ad_olmBDBEntryCache );
138 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_cursize );
139 ber_bvreplace( &a->a_vals[ 0 ], &bv );
141 a = attr_find( e->e_attrs, ad_olmBDBEntryInfo );
143 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_eiused );
144 ber_bvreplace( &a->a_vals[ 0 ], &bv );
146 a = attr_find( e->e_attrs, ad_olmBDBIDLCache );
148 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_idl_cache_size );
149 ber_bvreplace( &a->a_vals[ 0 ], &bv );
151 return SLAP_CB_CONTINUE;
161 struct bdb_info *bdb = (struct bdb_info *) priv;
163 return SLAP_CB_CONTINUE;
171 struct bdb_info *bdb = (struct bdb_info *) priv;
173 struct berval values[ 2 ];
174 Modification mod = { 0 };
177 char textbuf[ SLAP_TEXT_BUFLEN ];
181 /* Remove objectClass */
182 mod.sm_op = LDAP_MOD_DELETE;
183 mod.sm_desc = slap_schema.si_ad_objectClass;
184 mod.sm_values = values;
185 values[ 0 ] = oc_olmBDBDatabase->soc_cname;
186 BER_BVZERO( &values[ 1 ] );
188 rc = modify_delete_values( e, &mod, 1, &text,
189 textbuf, sizeof( textbuf ) );
190 /* don't care too much about return code... */
193 for ( i = 0; s_at[ i ].name != NULL; i++ ) {
194 mod.sm_desc = *s_at[ i ].ad;
195 mod.sm_values = NULL;
196 rc = modify_delete_values( e, &mod, 1, &text,
197 textbuf, sizeof( textbuf ) );
198 /* don't care too much about return code... */
201 return SLAP_CB_CONTINUE;
205 * call from within bdb_initialize()
208 bdb_monitor_initialize( void )
213 static int bdb_monitor_initialized = 0;
215 /* register schema here; if compiled as dynamic object,
216 * must be loaded __after__ back_monitor.la */
218 if ( bdb_monitor_initialized++ ) {
222 for ( i = 0; s_oid[ i ].name; i++ ) {
225 argv[ 0 ] = "back-bdb/back-hdb monitor";
226 argv[ 1 ] = s_oid[ i ].name;
227 argv[ 2 ] = s_oid[ i ].oid;
229 if ( parse_oidm( argv[ 0 ], i, 3, argv, 0, NULL ) != 0 ) {
230 Debug( LDAP_DEBUG_ANY,
231 "bdb_monitor_initialize: unable to add "
232 "objectIdentifier \"%s=%s\"\n",
233 s_oid[ i ].name, s_oid[ i ].oid, 0 );
238 for ( i = 0; s_at[ i ].name != NULL; i++ ) {
239 LDAPAttributeType *at;
241 at = ldap_str2attributetype( s_at[ i ].desc,
242 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
244 Debug( LDAP_DEBUG_ANY,
245 "bdb_monitor_initialize: "
246 "AttributeType load failed: %s %s\n",
247 ldap_scherr2str( code ), err, 0 );
248 return LDAP_INVALID_SYNTAX;
251 code = at_add( at, 0, NULL, &err );
252 if ( code != LDAP_SUCCESS ) {
253 Debug( LDAP_DEBUG_ANY,
254 "bdb_monitor_initialize: "
255 "AttributeType load failed: %s %s\n",
256 scherr2str( code ), err, 0 );
257 code = LDAP_INVALID_SYNTAX;
261 code = slap_str2ad( s_at[ i ].name,
262 s_at[ i ].ad, &err );
263 if ( code != LDAP_SUCCESS ) {
264 Debug( LDAP_DEBUG_ANY,
265 "bdb_monitor_initialize: "
266 "unable to find AttributeDescription "
268 s_at[ i ].name, code, err );
269 code = LDAP_UNDEFINED_TYPE;
275 ldap_attributetype_free( at );
282 for ( i = 0; s_oc[ i ].name != NULL; i++ ) {
285 oc = ldap_str2objectclass( s_oc[ i ].desc,
286 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
288 Debug( LDAP_DEBUG_ANY,
289 "bdb_monitor_initialize: "
290 "ObjectClass load failed: %s %s\n",
291 ldap_scherr2str( code ), err, 0 );
292 return LDAP_INVALID_SYNTAX;
295 code = oc_add( oc, 0, NULL, &err );
296 if ( code != LDAP_SUCCESS ) {
297 Debug( LDAP_DEBUG_ANY,
298 "bdb_monitor_initialize: "
299 "ObjectClass load failed: %s %s\n",
300 scherr2str( code ), err, 0 );
301 code = LDAP_INVALID_SYNTAX;
305 *s_oc[ i ].oc = oc_find( s_oc[ i ].name );
306 if ( *s_oc[ i ].oc == NULL ) {
307 code = LDAP_UNDEFINED_TYPE;
308 Debug( LDAP_DEBUG_ANY,
309 "bdb_monitor_initialize: "
310 "unable to find objectClass \"%s\"\n",
311 s_oc[ i ].name, 0, 0 );
316 if ( code != LDAP_SUCCESS ) {
317 ldap_objectclass_free( oc );
328 * call from within bdb_db_init()
331 bdb_monitor_init( BackendDB *be )
337 * call from within bdb_db_open()
340 bdb_monitor_open( BackendDB *be )
342 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
344 monitor_callback_t *cb;
345 struct berval suffix, *filter, *base;
349 if ( !SLAP_DBMONITORING( be ) ) {
353 /* don't bother if monitor is not configured */
354 if ( !monitor_back_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 /* monitor_back_register_entry_attrs() with a NULL ndn,
368 * base="cn=Databases,cn=Monitor", scope=LDAP_SCOPE_ONE
369 * and filter="(namingContexts:distinguishedNameMatch:=<suffix>)" */
371 bdb->bi_monitor_cleanup.bdm_scope = LDAP_SCOPE_ONELEVEL;
372 base = &bdb->bi_monitor_cleanup.bdm_base;
373 BER_BVSTR( base, "cn=databases,cn=monitor" );
374 filter = &bdb->bi_monitor_cleanup.bdm_filter;
375 BER_BVZERO( filter );
377 suffix.bv_len = ldap_bv2escaped_filter_value_len( &be->be_nsuffix[ 0 ] );
378 if ( suffix.bv_len == be->be_nsuffix[ 0 ].bv_len ) {
379 suffix = be->be_nsuffix[ 0 ];
382 ldap_bv2escaped_filter_value( &be->be_nsuffix[ 0 ], &suffix );
385 filter->bv_len = STRLENOF( "(namingContexts:distinguishedNameMatch:=)" ) + suffix.bv_len;
386 ptr = filter->bv_val = ch_malloc( filter->bv_len + 1 );
387 ptr = lutil_strcopy( ptr, "(namingContexts:distinguishedNameMatch:=" );
388 ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
392 assert( filter->bv_len == ptr - filter->bv_val );
394 if ( suffix.bv_val != be->be_nsuffix[ 0 ].bv_val ) {
395 ch_free( suffix.bv_val );
398 /* alloc as many as required (plus 1 for objectClass) */
399 a = attrs_alloc( 1 + 4 );
405 a->a_desc = slap_schema.si_ad_objectClass;
406 value_add_one( &a->a_vals, &oc_olmBDBDatabase->soc_cname );
407 a->a_nvals = a->a_vals;
411 struct berval bv = BER_BVC( "0" );
413 next->a_desc = ad_olmBDBEntryCache;
414 value_add_one( &next->a_vals, &bv );
415 next->a_nvals = next->a_vals;
418 next->a_desc = ad_olmBDBEntryInfo;
419 value_add_one( &next->a_vals, &bv );
420 next->a_nvals = next->a_vals;
423 next->a_desc = ad_olmBDBIDLCache;
424 value_add_one( &next->a_vals, &bv );
425 next->a_nvals = next->a_vals;
430 struct berval bv, nbv;
431 ber_len_t pathlen = 0, len = 0;
432 char path[ PATH_MAX ] = { '\0' };
433 char *fname = bdb->bi_dbenv_home,
436 len = strlen( fname );
437 if ( fname[ 0 ] != '/' ) {
438 /* get full path name */
439 getcwd( path, sizeof( path ) );
440 pathlen = strlen( path );
442 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
448 bv.bv_len = pathlen + STRLENOF( "/" ) + len;
449 ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
451 ptr = lutil_strncopy( ptr, path, pathlen );
455 ptr = lutil_strncopy( ptr, fname, len );
456 if ( ptr[ -1 ] != '/' ) {
462 attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
464 next->a_desc = ad_olmDbDirectory;
465 next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
466 next->a_vals[ 0 ] = bv;
468 if ( BER_BVISNULL( &nbv ) ) {
469 next->a_nvals = next->a_vals;
472 next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
473 next->a_nvals[ 0 ] = nbv;
479 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
480 cb->mc_update = bdb_monitor_update;
481 cb->mc_modify = bdb_monitor_modify;
482 cb->mc_free = bdb_monitor_free;
483 cb->mc_private = (void *)bdb;
485 rc = monitor_back_register_entry_attrs( NULL, a, cb,
486 base, LDAP_SCOPE_ONELEVEL, filter );
500 if ( !BER_BVISNULL( filter ) ) {
501 ch_free( filter->bv_val );
502 BER_BVZERO( filter );
506 /* store for cleanup */
507 bdb->bi_monitor_cleanup.bdm_cb = (void *)cb;
509 /* we don't need to keep track of the attributes, because
510 * bdb_monitor_free() takes care of everything */
519 * call from within bdb_db_close()
522 bdb_monitor_close( BackendDB *be )
524 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
526 if ( !BER_BVISNULL( &bdb->bi_monitor_cleanup.bdm_filter ) ) {
527 monitor_back_unregister_entry_callback( NULL,
528 (monitor_callback_t *)bdb->bi_monitor_cleanup.bdm_cb,
529 &bdb->bi_monitor_cleanup.bdm_base,
530 bdb->bi_monitor_cleanup.bdm_scope,
531 &bdb->bi_monitor_cleanup.bdm_filter );
533 if ( !BER_BVISNULL( &bdb->bi_monitor_cleanup.bdm_filter ) ) {
534 ch_free( bdb->bi_monitor_cleanup.bdm_filter.bv_val );
537 memset( &bdb->bi_monitor_cleanup, 0, sizeof( bdb->bi_monitor_cleanup ) );
544 * call from within bdb_db_destroy()
547 bdb_monitor_destroy( BackendDB *be )