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 )
333 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
339 * call from within bdb_db_open()
342 bdb_monitor_open( BackendDB *be )
344 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
346 monitor_callback_t *cb;
347 struct berval suffix, *filter, *base;
351 if ( !SLAP_DBMONITORING( be ) ) {
355 /* don't bother if monitor is not configured */
356 if ( !monitor_back_is_configured() ) {
357 static int warning = 0;
359 if ( warning++ == 0 ) {
360 Debug( LDAP_DEBUG_ANY, "bdb_monitor_open: "
361 "monitoring disabled; "
362 "configure monitor database to enable\n",
369 bdb->bi_monitor.bdm_scope = LDAP_SCOPE_SUBORDINATE;
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 filter->bv_len = STRLENOF( "(&(monitoredInfo=" )
384 + strlen( be->bd_info->bi_type )
385 + STRLENOF( ")(namingContexts:distinguishedNameMatch:=" )
386 + suffix.bv_len + STRLENOF( "))" );
387 ptr = filter->bv_val = ch_malloc( filter->bv_len + 1 );
388 ptr = lutil_strcopy( ptr, "(&(monitoredInfo=" );
389 ptr = lutil_strcopy( ptr, be->bd_info->bi_type );
390 ptr = lutil_strcopy( ptr, ")(namingContexts:distinguishedNameMatch:=" );
391 ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
392 ptr = lutil_strcopy( ptr, "))" );
394 assert( filter->bv_len == ptr - filter->bv_val );
396 if ( suffix.bv_val != be->be_nsuffix[ 0 ].bv_val ) {
397 ch_free( suffix.bv_val );
400 /* alloc as many as required (plus 1 for objectClass) */
401 a = attrs_alloc( 1 + 4 );
407 a->a_desc = slap_schema.si_ad_objectClass;
408 value_add_one( &a->a_vals, &oc_olmBDBDatabase->soc_cname );
409 a->a_nvals = a->a_vals;
413 struct berval bv = BER_BVC( "0" );
415 next->a_desc = ad_olmBDBEntryCache;
416 value_add_one( &next->a_vals, &bv );
417 next->a_nvals = next->a_vals;
420 next->a_desc = ad_olmBDBEntryInfo;
421 value_add_one( &next->a_vals, &bv );
422 next->a_nvals = next->a_vals;
425 next->a_desc = ad_olmBDBIDLCache;
426 value_add_one( &next->a_vals, &bv );
427 next->a_nvals = next->a_vals;
432 struct berval bv, nbv;
433 ber_len_t pathlen = 0, len = 0;
434 char path[ PATH_MAX ] = { '\0' };
435 char *fname = bdb->bi_dbenv_home,
438 len = strlen( fname );
439 if ( fname[ 0 ] != '/' ) {
440 /* get full path name */
441 getcwd( path, sizeof( path ) );
442 pathlen = strlen( path );
444 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
450 bv.bv_len = pathlen + STRLENOF( "/" ) + len;
451 ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
453 ptr = lutil_strncopy( ptr, path, pathlen );
457 ptr = lutil_strncopy( ptr, fname, len );
458 if ( ptr[ -1 ] != '/' ) {
464 attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
466 next->a_desc = ad_olmDbDirectory;
467 next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
468 next->a_vals[ 0 ] = bv;
470 if ( BER_BVISNULL( &nbv ) ) {
471 next->a_nvals = next->a_vals;
474 next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
475 next->a_nvals[ 0 ] = nbv;
481 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
482 cb->mc_update = bdb_monitor_update;
483 cb->mc_modify = bdb_monitor_modify;
484 cb->mc_free = bdb_monitor_free;
485 cb->mc_private = (void *)bdb;
487 rc = monitor_back_register_entry_attrs( NULL, a, cb,
488 base, LDAP_SCOPE_SUBORDINATE, filter );
502 if ( !BER_BVISNULL( filter ) ) {
503 ch_free( filter->bv_val );
504 BER_BVZERO( filter );
508 /* store for cleanup */
509 bdb->bi_monitor.bdm_cb = (void *)cb;
511 /* we don't need to keep track of the attributes, because
512 * bdb_monitor_free() takes care of everything */
521 * call from within bdb_db_close()
524 bdb_monitor_close( BackendDB *be )
526 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
528 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
529 monitor_back_unregister_entry_callback( NULL,
530 (monitor_callback_t *)bdb->bi_monitor.bdm_cb,
531 &bdb->bi_monitor.bdm_nbase,
532 bdb->bi_monitor.bdm_scope,
533 &bdb->bi_monitor.bdm_filter );
535 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
536 ch_free( bdb->bi_monitor.bdm_filter.bv_val );
539 memset( &bdb->bi_monitor, 0, sizeof( bdb->bi_monitor ) );
546 * call from within bdb_db_destroy()
549 bdb_monitor_destroy( BackendDB *be )