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" },
62 AttributeDescription **ad;
64 { "olmBDBEntryCache", "( olmBDBAttributes:1 "
65 "NAME ( 'olmBDBEntryCache' ) "
66 "DESC 'Number of items in Entry Cache' "
68 "NO-USER-MODIFICATION "
69 "USAGE directoryOperation )",
70 &ad_olmBDBEntryCache },
72 { "olmBDBEntryInfo", "( olmBDBAttributes:2 "
73 "NAME ( 'olmBDBEntryInfo' ) "
74 "DESC 'Number of items in EntryInfo Cache' "
76 "NO-USER-MODIFICATION "
77 "USAGE directoryOperation )",
78 &ad_olmBDBEntryInfo },
80 { "olmBDBIDLCache", "( olmBDBAttributes:3 "
81 "NAME ( 'olmBDBIDLCache' ) "
82 "DESC 'Number of items in IDL Cache' "
84 "NO-USER-MODIFICATION "
85 "USAGE directoryOperation )",
88 { "olmDbDirectory", "( olmBDBAttributes:4 "
89 "NAME ( 'olmDbDirectory' ) "
90 "DESC 'Path name of the directory "
91 "where the database environment resides' "
93 "NO-USER-MODIFICATION "
94 "USAGE directoryOperation )",
105 /* augments an existing object, so it must be AUXILIARY
106 * FIXME: derive from some ABSTRACT "monitoredEntity"? */
107 { "olmBDBDatabase", "( olmBDBObjectClasses:1 "
108 "NAME ( 'olmBDBDatabase' ) "
116 &oc_olmBDBDatabase },
128 struct bdb_info *bdb = (struct bdb_info *) priv;
134 assert( ad_olmBDBEntryCache != NULL );
136 a = attr_find( e->e_attrs, ad_olmBDBEntryCache );
139 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_cursize );
140 ber_bvreplace( &a->a_vals[ 0 ], &bv );
142 a = attr_find( e->e_attrs, ad_olmBDBEntryInfo );
144 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_eiused );
145 ber_bvreplace( &a->a_vals[ 0 ], &bv );
147 a = attr_find( e->e_attrs, ad_olmBDBIDLCache );
149 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_idl_cache_size );
150 ber_bvreplace( &a->a_vals[ 0 ], &bv );
152 return SLAP_CB_CONTINUE;
162 return SLAP_CB_CONTINUE;
170 struct berval values[ 2 ];
171 Modification mod = { 0 };
174 char textbuf[ SLAP_TEXT_BUFLEN ];
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 ].name != 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 )
211 static int bdb_monitor_initialized = 0;
213 bi = backend_info("monitor");
217 /* register schema here */
219 if ( bdb_monitor_initialized++ ) {
223 for ( i = 0; s_oid[ i ].name; i++ ) {
226 argv[ 0 ] = "back-bdb/back-hdb monitor";
227 argv[ 1 ] = s_oid[ i ].name;
228 argv[ 2 ] = s_oid[ i ].oid;
230 if ( parse_oidm( argv[ 0 ], i, 3, argv, 0, NULL ) != 0 ) {
231 Debug( LDAP_DEBUG_ANY,
232 "bdb_monitor_initialize: unable to add "
233 "objectIdentifier \"%s=%s\"\n",
234 s_oid[ i ].name, s_oid[ i ].oid, 0 );
239 for ( i = 0; s_at[ i ].name != NULL; i++ ) {
240 LDAPAttributeType *at;
242 at = ldap_str2attributetype( s_at[ i ].desc,
243 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
245 Debug( LDAP_DEBUG_ANY,
246 "bdb_monitor_initialize: "
247 "AttributeType load failed: %s %s\n",
248 ldap_scherr2str( code ), err, 0 );
249 return LDAP_INVALID_SYNTAX;
252 code = at_add( at, 0, NULL, &err );
253 if ( code != LDAP_SUCCESS ) {
254 Debug( LDAP_DEBUG_ANY,
255 "bdb_monitor_initialize: "
256 "AttributeType load failed: %s %s\n",
257 scherr2str( code ), err, 0 );
258 code = LDAP_INVALID_SYNTAX;
262 code = slap_str2ad( s_at[ i ].name,
263 s_at[ i ].ad, &err );
264 if ( code != LDAP_SUCCESS ) {
265 Debug( LDAP_DEBUG_ANY,
266 "bdb_monitor_initialize: "
267 "unable to find AttributeDescription "
269 s_at[ i ].name, code, err );
270 code = LDAP_UNDEFINED_TYPE;
276 ldap_attributetype_free( at );
283 for ( i = 0; s_oc[ i ].name != NULL; i++ ) {
286 oc = ldap_str2objectclass( s_oc[ i ].desc,
287 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
289 Debug( LDAP_DEBUG_ANY,
290 "bdb_monitor_initialize: "
291 "ObjectClass load failed: %s %s\n",
292 ldap_scherr2str( code ), err, 0 );
293 return LDAP_INVALID_SYNTAX;
296 code = oc_add( oc, 0, NULL, &err );
297 if ( code != LDAP_SUCCESS ) {
298 Debug( LDAP_DEBUG_ANY,
299 "bdb_monitor_initialize: "
300 "ObjectClass load failed: %s %s\n",
301 scherr2str( code ), err, 0 );
302 code = LDAP_INVALID_SYNTAX;
306 *s_oc[ i ].oc = oc_find( s_oc[ i ].name );
307 if ( *s_oc[ i ].oc == NULL ) {
308 code = LDAP_UNDEFINED_TYPE;
309 Debug( LDAP_DEBUG_ANY,
310 "bdb_monitor_initialize: "
311 "unable to find objectClass \"%s\"\n",
312 s_oc[ i ].name, 0, 0 );
317 if ( code != LDAP_SUCCESS ) {
318 ldap_objectclass_free( oc );
329 * call from within bdb_db_init()
332 bdb_monitor_init( BackendDB *be )
334 if ( bdb_monitor_initialize() == LDAP_SUCCESS ) {
335 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
342 * call from within bdb_db_open()
345 bdb_monitor_open( BackendDB *be )
347 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
349 monitor_callback_t *cb = NULL;
350 struct berval suffix, *filter, *base;
353 monitor_extra_t *mbe;
355 if ( !SLAP_DBMONITORING( be ) ) {
360 BackendInfo *mi = backend_info( "monitor" );
361 if ( !mi || !mi->bi_extra ) {
362 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
368 /* don't bother if monitor is not configured */
369 if ( !mbe->is_configured() ) {
370 static int warning = 0;
372 if ( warning++ == 0 ) {
373 Debug( LDAP_DEBUG_ANY, "bdb_monitor_open: "
374 "monitoring disabled; "
375 "configure monitor database to enable\n",
382 bdb->bi_monitor.bdm_scope = LDAP_SCOPE_SUBORDINATE;
383 base = &bdb->bi_monitor.bdm_nbase;
384 BER_BVSTR( base, "cn=databases,cn=monitor" );
385 filter = &bdb->bi_monitor.bdm_filter;
386 BER_BVZERO( filter );
388 suffix.bv_len = ldap_bv2escaped_filter_value_len( &be->be_nsuffix[ 0 ] );
389 if ( suffix.bv_len == be->be_nsuffix[ 0 ].bv_len ) {
390 suffix = be->be_nsuffix[ 0 ];
393 ldap_bv2escaped_filter_value( &be->be_nsuffix[ 0 ], &suffix );
396 filter->bv_len = STRLENOF( "(&(monitoredInfo=" )
397 + strlen( be->bd_info->bi_type )
398 + STRLENOF( ")(namingContexts:distinguishedNameMatch:=" )
399 + suffix.bv_len + STRLENOF( "))" );
400 ptr = filter->bv_val = ch_malloc( filter->bv_len + 1 );
401 ptr = lutil_strcopy( ptr, "(&(monitoredInfo=" );
402 ptr = lutil_strcopy( ptr, be->bd_info->bi_type );
403 ptr = lutil_strcopy( ptr, ")(namingContexts:distinguishedNameMatch:=" );
404 ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
405 ptr = lutil_strcopy( ptr, "))" );
407 assert( filter->bv_len == ptr - filter->bv_val );
409 if ( suffix.bv_val != be->be_nsuffix[ 0 ].bv_val ) {
410 ch_free( suffix.bv_val );
413 /* alloc as many as required (plus 1 for objectClass) */
414 a = attrs_alloc( 1 + 4 );
420 a->a_desc = slap_schema.si_ad_objectClass;
421 value_add_one( &a->a_vals, &oc_olmBDBDatabase->soc_cname );
422 a->a_nvals = a->a_vals;
426 struct berval bv = BER_BVC( "0" );
428 next->a_desc = ad_olmBDBEntryCache;
429 value_add_one( &next->a_vals, &bv );
430 next->a_nvals = next->a_vals;
433 next->a_desc = ad_olmBDBEntryInfo;
434 value_add_one( &next->a_vals, &bv );
435 next->a_nvals = next->a_vals;
438 next->a_desc = ad_olmBDBIDLCache;
439 value_add_one( &next->a_vals, &bv );
440 next->a_nvals = next->a_vals;
445 struct berval bv, nbv;
446 ber_len_t pathlen = 0, len = 0;
447 char path[ PATH_MAX ] = { '\0' };
448 char *fname = bdb->bi_dbenv_home,
451 len = strlen( fname );
452 if ( fname[ 0 ] != '/' ) {
453 /* get full path name */
454 getcwd( path, sizeof( path ) );
455 pathlen = strlen( path );
457 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
463 bv.bv_len = pathlen + STRLENOF( "/" ) + len;
464 ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
466 ptr = lutil_strncopy( ptr, path, pathlen );
470 ptr = lutil_strncopy( ptr, fname, len );
471 if ( ptr[ -1 ] != '/' ) {
477 attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
479 next->a_desc = ad_olmDbDirectory;
480 next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
481 next->a_vals[ 0 ] = bv;
483 if ( BER_BVISNULL( &nbv ) ) {
484 next->a_nvals = next->a_vals;
487 next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
488 next->a_nvals[ 0 ] = nbv;
494 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
495 cb->mc_update = bdb_monitor_update;
496 cb->mc_modify = bdb_monitor_modify;
497 cb->mc_free = bdb_monitor_free;
498 cb->mc_private = (void *)bdb;
500 rc = mbe->register_entry_attrs( NULL, a, cb,
501 base, LDAP_SCOPE_SUBORDINATE, filter );
515 if ( !BER_BVISNULL( filter ) ) {
516 ch_free( filter->bv_val );
517 BER_BVZERO( filter );
521 /* store for cleanup */
522 bdb->bi_monitor.bdm_cb = (void *)cb;
524 /* we don't need to keep track of the attributes, because
525 * bdb_monitor_free() takes care of everything */
534 * call from within bdb_db_close()
537 bdb_monitor_close( BackendDB *be )
539 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
541 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
542 BackendInfo *mi = backend_info( "monitor" );
543 monitor_extra_t *mbe;
545 if ( !mi || !mi->bi_extra )
548 mbe->unregister_entry_callback( NULL,
549 (monitor_callback_t *)bdb->bi_monitor.bdm_cb,
550 &bdb->bi_monitor.bdm_nbase,
551 bdb->bi_monitor.bdm_scope,
552 &bdb->bi_monitor.bdm_filter );
554 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
555 ch_free( bdb->bi_monitor.bdm_filter.bv_val );
558 memset( &bdb->bi_monitor, 0, sizeof( bdb->bi_monitor ) );
565 * call from within bdb_db_destroy()
568 bdb_monitor_destroy( BackendDB *be )