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>
30 #include "../back-monitor/back-monitor.h"
32 static ObjectClass *oc_olmBDBDatabase;
34 static AttributeDescription *ad_olmBDBEntryCache,
35 *ad_olmBDBEntryInfo, *ad_olmBDBIDLCache,
39 * NOTE: there's some confusion in monitor OID arc;
40 * by now, let's consider:
42 * Subsystems monitor attributes 1.3.6.1.4.1.4203.666.1.55.0
43 * Databases monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1
44 * BDB database monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1.1
46 * Subsystems monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0
47 * Databases monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1
48 * BDB database monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1.1
55 { "olmBDBAttributes", "olmDatabaseAttributes:1" },
56 { "olmBDBObjectClasses", "olmDatabaseObjectClasses:1" },
64 AttributeDescription **ad;
66 { "olmBDBEntryCache", "( olmBDBAttributes:1 "
67 "NAME ( 'olmBDBEntryCache' ) "
68 "DESC 'Number of items in Entry Cache' "
70 "NO-USER-MODIFICATION "
71 "USAGE directoryOperation )",
72 &ad_olmBDBEntryCache },
74 { "olmBDBEntryInfo", "( olmBDBAttributes:2 "
75 "NAME ( 'olmBDBEntryInfo' ) "
76 "DESC 'Number of items in EntryInfo Cache' "
78 "NO-USER-MODIFICATION "
79 "USAGE directoryOperation )",
80 &ad_olmBDBEntryInfo },
82 { "olmBDBIDLCache", "( olmBDBAttributes:3 "
83 "NAME ( 'olmBDBIDLCache' ) "
84 "DESC 'Number of items in IDL Cache' "
86 "NO-USER-MODIFICATION "
87 "USAGE directoryOperation )",
90 { "olmDbDirectory", "( olmBDBAttributes:4 "
91 "NAME ( 'olmDbDirectory' ) "
92 "DESC 'Path name of the directory "
93 "where the database environment resides' "
95 "NO-USER-MODIFICATION "
96 "USAGE directoryOperation )",
107 /* augments an existing object, so it must be AUXILIARY
108 * FIXME: derive from some ABSTRACT "monitoredEntity"? */
109 { "olmBDBDatabase", "( olmBDBObjectClasses:1 "
110 "NAME ( 'olmBDBDatabase' ) "
118 &oc_olmBDBDatabase },
130 struct bdb_info *bdb = (struct bdb_info *) priv;
136 assert( ad_olmBDBEntryCache != NULL );
138 a = attr_find( e->e_attrs, ad_olmBDBEntryCache );
141 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_cursize );
142 ber_bvreplace( &a->a_vals[ 0 ], &bv );
144 a = attr_find( e->e_attrs, ad_olmBDBEntryInfo );
146 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_eiused );
147 ber_bvreplace( &a->a_vals[ 0 ], &bv );
149 a = attr_find( e->e_attrs, ad_olmBDBIDLCache );
151 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_idl_cache_size );
152 ber_bvreplace( &a->a_vals[ 0 ], &bv );
154 return SLAP_CB_CONTINUE;
164 return SLAP_CB_CONTINUE;
172 struct berval values[ 2 ];
173 Modification mod = { 0 };
176 char textbuf[ SLAP_TEXT_BUFLEN ];
180 /* Remove objectClass */
181 mod.sm_op = LDAP_MOD_DELETE;
182 mod.sm_desc = slap_schema.si_ad_objectClass;
183 mod.sm_values = values;
184 values[ 0 ] = oc_olmBDBDatabase->soc_cname;
185 BER_BVZERO( &values[ 1 ] );
187 rc = modify_delete_values( e, &mod, 1, &text,
188 textbuf, sizeof( textbuf ) );
189 /* don't care too much about return code... */
192 for ( i = 0; s_at[ i ].name != NULL; i++ ) {
193 mod.sm_desc = *s_at[ i ].ad;
194 mod.sm_values = NULL;
195 rc = modify_delete_values( e, &mod, 1, &text,
196 textbuf, sizeof( textbuf ) );
197 /* don't care too much about return code... */
200 return SLAP_CB_CONTINUE;
203 #endif /* SLAPD_MONITOR */
206 * call from within bdb_initialize()
209 bdb_monitor_initialize( void )
215 static int bdb_monitor_initialized = 0;
217 /* register schema here; if compiled as dynamic object,
218 * must be loaded __after__ back_monitor.la */
220 if ( bdb_monitor_initialized++ ) {
224 for ( i = 0; s_oid[ i ].name; i++ ) {
227 argv[ 0 ] = "back-bdb/back-hdb monitor";
228 argv[ 1 ] = s_oid[ i ].name;
229 argv[ 2 ] = s_oid[ i ].oid;
231 if ( parse_oidm( argv[ 0 ], i, 3, argv, 0, NULL ) != 0 ) {
232 Debug( LDAP_DEBUG_ANY,
233 "bdb_monitor_initialize: unable to add "
234 "objectIdentifier \"%s=%s\"\n",
235 s_oid[ i ].name, s_oid[ i ].oid, 0 );
240 for ( i = 0; s_at[ i ].name != NULL; i++ ) {
241 LDAPAttributeType *at;
243 at = ldap_str2attributetype( s_at[ i ].desc,
244 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
246 Debug( LDAP_DEBUG_ANY,
247 "bdb_monitor_initialize: "
248 "AttributeType load failed: %s %s\n",
249 ldap_scherr2str( code ), err, 0 );
250 return LDAP_INVALID_SYNTAX;
253 code = at_add( at, 0, NULL, &err );
254 if ( code != LDAP_SUCCESS ) {
255 Debug( LDAP_DEBUG_ANY,
256 "bdb_monitor_initialize: "
257 "AttributeType load failed: %s %s\n",
258 scherr2str( code ), err, 0 );
259 code = LDAP_INVALID_SYNTAX;
263 code = slap_str2ad( s_at[ i ].name,
264 s_at[ i ].ad, &err );
265 if ( code != LDAP_SUCCESS ) {
266 Debug( LDAP_DEBUG_ANY,
267 "bdb_monitor_initialize: "
268 "unable to find AttributeDescription "
270 s_at[ i ].name, code, err );
271 code = LDAP_UNDEFINED_TYPE;
277 ldap_attributetype_free( at );
284 for ( i = 0; s_oc[ i ].name != NULL; i++ ) {
287 oc = ldap_str2objectclass( s_oc[ i ].desc,
288 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
290 Debug( LDAP_DEBUG_ANY,
291 "bdb_monitor_initialize: "
292 "ObjectClass load failed: %s %s\n",
293 ldap_scherr2str( code ), err, 0 );
294 return LDAP_INVALID_SYNTAX;
297 code = oc_add( oc, 0, NULL, &err );
298 if ( code != LDAP_SUCCESS ) {
299 Debug( LDAP_DEBUG_ANY,
300 "bdb_monitor_initialize: "
301 "ObjectClass load failed: %s %s\n",
302 scherr2str( code ), err, 0 );
303 code = LDAP_INVALID_SYNTAX;
307 *s_oc[ i ].oc = oc_find( s_oc[ i ].name );
308 if ( *s_oc[ i ].oc == NULL ) {
309 code = LDAP_UNDEFINED_TYPE;
310 Debug( LDAP_DEBUG_ANY,
311 "bdb_monitor_initialize: "
312 "unable to find objectClass \"%s\"\n",
313 s_oc[ i ].name, 0, 0 );
318 if ( code != LDAP_SUCCESS ) {
319 ldap_objectclass_free( oc );
325 #endif /* SLAPD_MONITOR */
331 * call from within bdb_db_init()
334 bdb_monitor_init( BackendDB *be )
337 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
338 #endif /* SLAPD_MONITOR */
344 * call from within bdb_db_open()
347 bdb_monitor_open( BackendDB *be )
350 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
352 monitor_callback_t *cb = NULL;
353 struct berval suffix, *filter, *base;
357 if ( !SLAP_DBMONITORING( be ) ) {
361 /* don't bother if monitor is not configured */
362 if ( !monitor_back_is_configured() ) {
363 static int warning = 0;
365 if ( warning++ == 0 ) {
366 Debug( LDAP_DEBUG_ANY, "bdb_monitor_open: "
367 "monitoring disabled; "
368 "configure monitor database to enable\n",
375 bdb->bi_monitor.bdm_scope = LDAP_SCOPE_SUBORDINATE;
376 base = &bdb->bi_monitor.bdm_nbase;
377 BER_BVSTR( base, "cn=databases,cn=monitor" );
378 filter = &bdb->bi_monitor.bdm_filter;
379 BER_BVZERO( filter );
381 suffix.bv_len = ldap_bv2escaped_filter_value_len( &be->be_nsuffix[ 0 ] );
382 if ( suffix.bv_len == be->be_nsuffix[ 0 ].bv_len ) {
383 suffix = be->be_nsuffix[ 0 ];
386 ldap_bv2escaped_filter_value( &be->be_nsuffix[ 0 ], &suffix );
389 filter->bv_len = STRLENOF( "(&(monitoredInfo=" )
390 + strlen( be->bd_info->bi_type )
391 + STRLENOF( ")(namingContexts:distinguishedNameMatch:=" )
392 + suffix.bv_len + STRLENOF( "))" );
393 ptr = filter->bv_val = ch_malloc( filter->bv_len + 1 );
394 ptr = lutil_strcopy( ptr, "(&(monitoredInfo=" );
395 ptr = lutil_strcopy( ptr, be->bd_info->bi_type );
396 ptr = lutil_strcopy( ptr, ")(namingContexts:distinguishedNameMatch:=" );
397 ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
398 ptr = lutil_strcopy( ptr, "))" );
400 assert( filter->bv_len == ptr - filter->bv_val );
402 if ( suffix.bv_val != be->be_nsuffix[ 0 ].bv_val ) {
403 ch_free( suffix.bv_val );
406 /* alloc as many as required (plus 1 for objectClass) */
407 a = attrs_alloc( 1 + 4 );
413 a->a_desc = slap_schema.si_ad_objectClass;
414 value_add_one( &a->a_vals, &oc_olmBDBDatabase->soc_cname );
415 a->a_nvals = a->a_vals;
419 struct berval bv = BER_BVC( "0" );
421 next->a_desc = ad_olmBDBEntryCache;
422 value_add_one( &next->a_vals, &bv );
423 next->a_nvals = next->a_vals;
426 next->a_desc = ad_olmBDBEntryInfo;
427 value_add_one( &next->a_vals, &bv );
428 next->a_nvals = next->a_vals;
431 next->a_desc = ad_olmBDBIDLCache;
432 value_add_one( &next->a_vals, &bv );
433 next->a_nvals = next->a_vals;
438 struct berval bv, nbv;
439 ber_len_t pathlen = 0, len = 0;
440 char path[ PATH_MAX ] = { '\0' };
441 char *fname = bdb->bi_dbenv_home,
444 len = strlen( fname );
445 if ( fname[ 0 ] != '/' ) {
446 /* get full path name */
447 getcwd( path, sizeof( path ) );
448 pathlen = strlen( path );
450 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
456 bv.bv_len = pathlen + STRLENOF( "/" ) + len;
457 ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
459 ptr = lutil_strncopy( ptr, path, pathlen );
463 ptr = lutil_strncopy( ptr, fname, len );
464 if ( ptr[ -1 ] != '/' ) {
470 attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
472 next->a_desc = ad_olmDbDirectory;
473 next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
474 next->a_vals[ 0 ] = bv;
476 if ( BER_BVISNULL( &nbv ) ) {
477 next->a_nvals = next->a_vals;
480 next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
481 next->a_nvals[ 0 ] = nbv;
487 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
488 cb->mc_update = bdb_monitor_update;
489 cb->mc_modify = bdb_monitor_modify;
490 cb->mc_free = bdb_monitor_free;
491 cb->mc_private = (void *)bdb;
493 rc = monitor_back_register_entry_attrs( NULL, a, cb,
494 base, LDAP_SCOPE_SUBORDINATE, filter );
508 if ( !BER_BVISNULL( filter ) ) {
509 ch_free( filter->bv_val );
510 BER_BVZERO( filter );
514 /* store for cleanup */
515 bdb->bi_monitor.bdm_cb = (void *)cb;
517 /* we don't need to keep track of the attributes, because
518 * bdb_monitor_free() takes care of everything */
524 #else /* !SLAPD_MONITOR */
526 #endif /* SLAPD_MONITOR */
530 * call from within bdb_db_close()
533 bdb_monitor_close( BackendDB *be )
536 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
538 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
539 monitor_back_unregister_entry_callback( NULL,
540 (monitor_callback_t *)bdb->bi_monitor.bdm_cb,
541 &bdb->bi_monitor.bdm_nbase,
542 bdb->bi_monitor.bdm_scope,
543 &bdb->bi_monitor.bdm_filter );
545 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
546 ch_free( bdb->bi_monitor.bdm_filter.bv_val );
549 memset( &bdb->bi_monitor, 0, sizeof( bdb->bi_monitor ) );
551 #endif /* SLAPD_MONITOR */
557 * call from within bdb_db_destroy()
560 bdb_monitor_destroy( BackendDB *be )