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>.
22 #include <ac/string.h>
23 #include <ac/unistd.h>
24 #include <ac/stdlib.h>
29 #include "../back-monitor/back-monitor.h"
31 static ObjectClass *oc_olmBDBDatabase;
33 static AttributeDescription *ad_olmBDBEntryCache,
34 *ad_olmBDBEntryInfo, *ad_olmBDBIDLCache,
38 * NOTE: there's some confusion in monitor OID arc;
39 * by now, let's consider:
41 * Subsystems monitor attributes 1.3.6.1.4.1.4203.666.1.55.0
42 * Databases monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1
43 * BDB database monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1.1
45 * Subsystems monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0
46 * Databases monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1
47 * BDB database monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1.1
54 { "olmBDBAttributes", "olmDatabaseAttributes:1" },
55 { "olmBDBObjectClasses", "olmDatabaseObjectClasses:1" },
63 AttributeDescription **ad;
65 { "olmBDBEntryCache", "( olmBDBAttributes:1 "
66 "NAME ( 'olmBDBEntryCache' ) "
67 "DESC 'Number of items in Entry Cache' "
69 "NO-USER-MODIFICATION "
70 "USAGE directoryOperation )",
71 &ad_olmBDBEntryCache },
73 { "olmBDBEntryInfo", "( olmBDBAttributes:2 "
74 "NAME ( 'olmBDBEntryInfo' ) "
75 "DESC 'Number of items in EntryInfo Cache' "
77 "NO-USER-MODIFICATION "
78 "USAGE directoryOperation )",
79 &ad_olmBDBEntryInfo },
81 { "olmBDBIDLCache", "( olmBDBAttributes:3 "
82 "NAME ( 'olmBDBIDLCache' ) "
83 "DESC 'Number of items in IDL Cache' "
85 "NO-USER-MODIFICATION "
86 "USAGE directoryOperation )",
89 { "olmDbDirectory", "( olmBDBAttributes:4 "
90 "NAME ( 'olmDbDirectory' ) "
91 "DESC 'Path name of the directory "
92 "where the database environment resides' "
94 "NO-USER-MODIFICATION "
95 "USAGE directoryOperation )",
106 /* augments an existing object, so it must be AUXILIARY
107 * FIXME: derive from some ABSTRACT "monitoredEntity"? */
108 { "olmBDBDatabase", "( olmBDBObjectClasses:1 "
109 "NAME ( 'olmBDBDatabase' ) "
117 &oc_olmBDBDatabase },
129 struct bdb_info *bdb = (struct bdb_info *) priv;
135 assert( ad_olmBDBEntryCache != NULL );
137 a = attr_find( e->e_attrs, ad_olmBDBEntryCache );
140 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_cursize );
141 ber_bvreplace( &a->a_vals[ 0 ], &bv );
143 a = attr_find( e->e_attrs, ad_olmBDBEntryInfo );
145 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_eiused );
146 ber_bvreplace( &a->a_vals[ 0 ], &bv );
148 a = attr_find( e->e_attrs, ad_olmBDBIDLCache );
150 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_idl_cache_size );
151 ber_bvreplace( &a->a_vals[ 0 ], &bv );
153 return SLAP_CB_CONTINUE;
163 struct bdb_info *bdb = (struct bdb_info *) priv;
165 return SLAP_CB_CONTINUE;
173 struct bdb_info *bdb = (struct bdb_info *) priv;
175 struct berval values[ 2 ];
176 Modification mod = { 0 };
179 char textbuf[ SLAP_TEXT_BUFLEN ];
183 /* Remove objectClass */
184 mod.sm_op = LDAP_MOD_DELETE;
185 mod.sm_desc = slap_schema.si_ad_objectClass;
186 mod.sm_values = values;
187 values[ 0 ] = oc_olmBDBDatabase->soc_cname;
188 BER_BVZERO( &values[ 1 ] );
190 rc = modify_delete_values( e, &mod, 1, &text,
191 textbuf, sizeof( textbuf ) );
192 /* don't care too much about return code... */
195 for ( i = 0; s_at[ i ].name != NULL; i++ ) {
196 mod.sm_desc = *s_at[ i ].ad;
197 mod.sm_values = NULL;
198 rc = modify_delete_values( e, &mod, 1, &text,
199 textbuf, sizeof( textbuf ) );
200 /* don't care too much about return code... */
203 return SLAP_CB_CONTINUE;
207 * call from within bdb_initialize()
210 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 );
330 * call from within bdb_db_init()
333 bdb_monitor_init( BackendDB *be )
335 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
341 * call from within bdb_db_open()
344 bdb_monitor_open( BackendDB *be )
346 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
348 monitor_callback_t *cb = NULL;
349 struct berval suffix, *filter, *base;
353 if ( !SLAP_DBMONITORING( be ) ) {
357 /* don't bother if monitor is not configured */
358 if ( !monitor_back_is_configured() ) {
359 static int warning = 0;
361 if ( warning++ == 0 ) {
362 Debug( LDAP_DEBUG_ANY, "bdb_monitor_open: "
363 "monitoring disabled; "
364 "configure monitor database to enable\n",
371 bdb->bi_monitor.bdm_scope = LDAP_SCOPE_SUBORDINATE;
372 base = &bdb->bi_monitor.bdm_nbase;
373 BER_BVSTR( base, "cn=databases,cn=monitor" );
374 filter = &bdb->bi_monitor.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( "(&(monitoredInfo=" )
386 + strlen( be->bd_info->bi_type )
387 + STRLENOF( ")(namingContexts:distinguishedNameMatch:=" )
388 + suffix.bv_len + STRLENOF( "))" );
389 ptr = filter->bv_val = ch_malloc( filter->bv_len + 1 );
390 ptr = lutil_strcopy( ptr, "(&(monitoredInfo=" );
391 ptr = lutil_strcopy( ptr, be->bd_info->bi_type );
392 ptr = lutil_strcopy( ptr, ")(namingContexts:distinguishedNameMatch:=" );
393 ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
394 ptr = lutil_strcopy( ptr, "))" );
396 assert( filter->bv_len == ptr - filter->bv_val );
398 if ( suffix.bv_val != be->be_nsuffix[ 0 ].bv_val ) {
399 ch_free( suffix.bv_val );
402 /* alloc as many as required (plus 1 for objectClass) */
403 a = attrs_alloc( 1 + 4 );
409 a->a_desc = slap_schema.si_ad_objectClass;
410 value_add_one( &a->a_vals, &oc_olmBDBDatabase->soc_cname );
411 a->a_nvals = a->a_vals;
415 struct berval bv = BER_BVC( "0" );
417 next->a_desc = ad_olmBDBEntryCache;
418 value_add_one( &next->a_vals, &bv );
419 next->a_nvals = next->a_vals;
422 next->a_desc = ad_olmBDBEntryInfo;
423 value_add_one( &next->a_vals, &bv );
424 next->a_nvals = next->a_vals;
427 next->a_desc = ad_olmBDBIDLCache;
428 value_add_one( &next->a_vals, &bv );
429 next->a_nvals = next->a_vals;
434 struct berval bv, nbv;
435 ber_len_t pathlen = 0, len = 0;
436 char path[ PATH_MAX ] = { '\0' };
437 char *fname = bdb->bi_dbenv_home,
440 len = strlen( fname );
441 if ( fname[ 0 ] != '/' ) {
442 /* get full path name */
443 getcwd( path, sizeof( path ) );
444 pathlen = strlen( path );
446 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
452 bv.bv_len = pathlen + STRLENOF( "/" ) + len;
453 ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
455 ptr = lutil_strncopy( ptr, path, pathlen );
459 ptr = lutil_strncopy( ptr, fname, len );
460 if ( ptr[ -1 ] != '/' ) {
466 attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
468 next->a_desc = ad_olmDbDirectory;
469 next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
470 next->a_vals[ 0 ] = bv;
472 if ( BER_BVISNULL( &nbv ) ) {
473 next->a_nvals = next->a_vals;
476 next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
477 next->a_nvals[ 0 ] = nbv;
483 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
484 cb->mc_update = bdb_monitor_update;
485 cb->mc_modify = bdb_monitor_modify;
486 cb->mc_free = bdb_monitor_free;
487 cb->mc_private = (void *)bdb;
489 rc = monitor_back_register_entry_attrs( NULL, a, cb,
490 base, LDAP_SCOPE_SUBORDINATE, filter );
504 if ( !BER_BVISNULL( filter ) ) {
505 ch_free( filter->bv_val );
506 BER_BVZERO( filter );
510 /* store for cleanup */
511 bdb->bi_monitor.bdm_cb = (void *)cb;
513 /* we don't need to keep track of the attributes, because
514 * bdb_monitor_free() takes care of everything */
523 * call from within bdb_db_close()
526 bdb_monitor_close( BackendDB *be )
528 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
530 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
531 monitor_back_unregister_entry_callback( NULL,
532 (monitor_callback_t *)bdb->bi_monitor.bdm_cb,
533 &bdb->bi_monitor.bdm_nbase,
534 bdb->bi_monitor.bdm_scope,
535 &bdb->bi_monitor.bdm_filter );
537 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
538 ch_free( bdb->bi_monitor.bdm_filter.bv_val );
541 memset( &bdb->bi_monitor, 0, sizeof( bdb->bi_monitor ) );
548 * call from within bdb_db_destroy()
551 bdb_monitor_destroy( BackendDB *be )
556 #endif /* SLAPD_MONITOR */