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
47 #define BDB_MONITOR_SCHEMA_AD "1.3.6.1.4.1.4203.666.1.55.0.1.1"
48 #define BDB_MONITOR_SCHEMA_OC "1.3.6.1.4.1.4203.666.3.16.0.1.1"
53 AttributeDescription **ad;
55 { "olmBDBEntryCache", "( " BDB_MONITOR_SCHEMA_AD ".1 "
56 "NAME ( 'olmBDBEntryCache' ) "
57 "DESC 'Number of items in Entry Cache' "
59 "NO-USER-MODIFICATION "
60 "USAGE directoryOperation )",
61 &ad_olmBDBEntryCache },
63 { "olmBDBEntryInfo", "( " BDB_MONITOR_SCHEMA_AD ".2 "
64 "NAME ( 'olmBDBEntryInfo' ) "
65 "DESC 'Number of items in EntryInfo Cache' "
67 "NO-USER-MODIFICATION "
68 "USAGE directoryOperation )",
69 &ad_olmBDBEntryInfo },
71 { "olmBDBIDLCache", "( " BDB_MONITOR_SCHEMA_AD ".3 "
72 "NAME ( 'olmBDBIDLCache' ) "
73 "DESC 'Number of items in IDL Cache' "
75 "NO-USER-MODIFICATION "
76 "USAGE directoryOperation )",
79 { "olmDbDirectory", "( " BDB_MONITOR_SCHEMA_AD ".4 "
80 "NAME ( 'olmDbDirectory' ) "
81 "DESC 'Path name of the directory "
82 "where the database environment resides' "
84 "NO-USER-MODIFICATION "
85 "USAGE directoryOperation )",
96 /* augments an existing object, so it must be AUXILIARY
97 * FIXME: derive from some ABSTRACT "monitoredEntity"? */
98 { "olmBDBDatabase", "( " BDB_MONITOR_SCHEMA_OC ".1 "
99 "NAME ( 'olmBDBDatabase' ) "
107 &oc_olmBDBDatabase },
119 struct bdb_info *bdb = (struct bdb_info *) priv;
125 assert( ad_olmBDBEntryCache != NULL );
127 a = attr_find( e->e_attrs, ad_olmBDBEntryCache );
130 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_cursize );
131 ber_bvreplace( &a->a_vals[ 0 ], &bv );
133 a = attr_find( e->e_attrs, ad_olmBDBEntryInfo );
135 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_eiused );
136 ber_bvreplace( &a->a_vals[ 0 ], &bv );
138 a = attr_find( e->e_attrs, ad_olmBDBIDLCache );
140 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_idl_cache_size );
141 ber_bvreplace( &a->a_vals[ 0 ], &bv );
143 return SLAP_CB_CONTINUE;
153 struct bdb_info *bdb = (struct bdb_info *) priv;
155 return SLAP_CB_CONTINUE;
163 struct bdb_info *bdb = (struct bdb_info *) priv;
165 struct berval values[ 2 ];
166 Modification mod = { 0 };
169 char textbuf[ SLAP_TEXT_BUFLEN ];
173 /* Remove objectClass */
174 mod.sm_op = LDAP_MOD_DELETE;
175 mod.sm_desc = slap_schema.si_ad_objectClass;
176 mod.sm_values = values;
177 values[ 0 ] = oc_olmBDBDatabase->soc_cname;
178 BER_BVZERO( &values[ 1 ] );
180 rc = modify_delete_values( e, &mod, 1, &text,
181 textbuf, sizeof( textbuf ) );
182 /* don't care too much about return code... */
185 for ( i = 0; s_at[ i ].name != NULL; i++ ) {
186 mod.sm_desc = *s_at[ i ].ad;
187 mod.sm_values = NULL;
188 rc = modify_delete_values( e, &mod, 1, &text,
189 textbuf, sizeof( textbuf ) );
190 /* don't care too much about return code... */
193 return SLAP_CB_CONTINUE;
197 * call from within bdb_initialize()
200 bdb_monitor_initialize( void )
205 static int bdb_monitor_initialized = 0;
207 /* register schema here; if compiled as dynamic object,
208 * must be loaded __after__ back_monitor.la */
210 if ( bdb_monitor_initialized++ ) {
214 for ( i = 0; s_at[ i ].name != NULL; i++ ) {
215 LDAPAttributeType *at;
217 at = ldap_str2attributetype( s_at[ i ].desc,
218 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
220 Debug( LDAP_DEBUG_ANY,
221 "bdb_monitor_initialize: "
222 "AttributeType load failed: %s %s\n",
223 ldap_scherr2str( code ), err, 0 );
224 return LDAP_INVALID_SYNTAX;
227 code = at_add( at, 0, NULL, &err );
228 if ( code != LDAP_SUCCESS ) {
229 Debug( LDAP_DEBUG_ANY,
230 "bdb_monitor_initialize: "
231 "AttributeType load failed: %s %s\n",
232 scherr2str( code ), err, 0 );
233 code = LDAP_INVALID_SYNTAX;
237 code = slap_str2ad( s_at[ i ].name,
238 s_at[ i ].ad, &err );
239 if ( code != LDAP_SUCCESS ) {
240 Debug( LDAP_DEBUG_ANY,
241 "bdb_monitor_initialize: "
242 "unable to find AttributeDescription "
244 s_at[ i ].name, code, err );
245 code = LDAP_UNDEFINED_TYPE;
251 ldap_attributetype_free( at );
258 for ( i = 0; s_oc[ i ].name != NULL; i++ ) {
261 oc = ldap_str2objectclass( s_oc[ i ].desc,
262 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
264 Debug( LDAP_DEBUG_ANY,
265 "bdb_monitor_initialize: "
266 "ObjectClass load failed: %s %s\n",
267 ldap_scherr2str( code ), err, 0 );
268 return LDAP_INVALID_SYNTAX;
271 code = oc_add( oc, 0, NULL, &err );
272 if ( code != LDAP_SUCCESS ) {
273 Debug( LDAP_DEBUG_ANY,
274 "bdb_monitor_initialize: "
275 "ObjectClass load failed: %s %s\n",
276 scherr2str( code ), err, 0 );
277 code = LDAP_INVALID_SYNTAX;
281 *s_oc[ i ].oc = oc_find( s_oc[ i ].name );
282 if ( *s_oc[ i ].oc == NULL ) {
283 code = LDAP_UNDEFINED_TYPE;
284 Debug( LDAP_DEBUG_ANY,
285 "bdb_monitor_initialize: "
286 "unable to find objectClass \"%s\"\n",
287 s_oc[ i ].name, 0, 0 );
292 if ( code != LDAP_SUCCESS ) {
293 ldap_objectclass_free( oc );
304 * call from within bdb_db_init()
307 bdb_monitor_init( BackendDB *be )
313 * call from within bdb_db_open()
316 bdb_monitor_open( BackendDB *be )
318 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
320 monitor_callback_t *cb;
321 struct berval suffix, *filter, *base;
325 bdb->bi_monitor_cleanup.bdm_scope = LDAP_SCOPE_ONELEVEL;
326 base = &bdb->bi_monitor_cleanup.bdm_base;
327 BER_BVSTR( base, "cn=databases,cn=monitor" );
328 filter = &bdb->bi_monitor_cleanup.bdm_filter;
329 BER_BVZERO( filter );
331 /* don't bother if monitor is not configured */
332 if ( !monitor_back_is_configured() ) {
333 static int warning = 0;
335 if ( warning++ == 0 ) {
336 Debug( LDAP_DEBUG_ANY, "bdb_monitor_open: "
337 "monitoring disabled; "
338 "configure monitor database to enable\n",
345 /* monitor_back_register_entry_attrs() with a NULL ndn,
346 * base="cn=Databases,cn=Monitor", scope=LDAP_SCOPE_ONE
347 * and filter="(namingContexts:distinguishedNameMatch:=<suffix>)" */
349 suffix.bv_len = ldap_bv2escaped_filter_value_len( &be->be_nsuffix[ 0 ] );
350 if ( suffix.bv_len == be->be_nsuffix[ 0 ].bv_len ) {
351 suffix = be->be_nsuffix[ 0 ];
354 ldap_bv2escaped_filter_value( &be->be_nsuffix[ 0 ], &suffix );
357 filter->bv_len = STRLENOF( "(namingContexts:distinguishedNameMatch:=)" ) + suffix.bv_len;
358 ptr = filter->bv_val = ch_malloc( filter->bv_len + 1 );
359 ptr = lutil_strcopy( ptr, "(namingContexts:distinguishedNameMatch:=" );
360 ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
364 assert( filter->bv_len == ptr - filter->bv_val );
366 if ( suffix.bv_val != be->be_nsuffix[ 0 ].bv_val ) {
367 ch_free( suffix.bv_val );
370 /* alloc as many as required (plus 1 for objectClass) */
371 a = attrs_alloc( 1 + 4 );
377 a->a_desc = slap_schema.si_ad_objectClass;
378 value_add_one( &a->a_vals, &oc_olmBDBDatabase->soc_cname );
379 a->a_nvals = a->a_vals;
383 struct berval bv = BER_BVC( "0" );
385 next->a_desc = ad_olmBDBEntryCache;
386 value_add_one( &next->a_vals, &bv );
387 next->a_nvals = next->a_vals;
390 next->a_desc = ad_olmBDBEntryInfo;
391 value_add_one( &next->a_vals, &bv );
392 next->a_nvals = next->a_vals;
395 next->a_desc = ad_olmBDBIDLCache;
396 value_add_one( &next->a_vals, &bv );
397 next->a_nvals = next->a_vals;
402 struct berval bv, nbv;
403 ber_len_t pathlen = 0, len = 0;
404 char path[ PATH_MAX ] = { '\0' };
405 char *fname = bdb->bi_dbenv_home,
408 len = strlen( fname );
409 if ( fname[ 0 ] != '/' ) {
410 /* get full path name */
411 getcwd( path, sizeof( path ) );
412 pathlen = strlen( path );
414 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
420 bv.bv_len = pathlen + STRLENOF( "/" ) + len;
421 ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
423 ptr = lutil_strncopy( ptr, path, pathlen );
427 ptr = lutil_strncopy( ptr, fname, len );
428 if ( ptr[ -1 ] != '/' ) {
434 attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
436 next->a_desc = ad_olmDbDirectory;
437 next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
438 next->a_vals[ 0 ] = bv;
440 if ( BER_BVISNULL( &nbv ) ) {
441 next->a_nvals = next->a_vals;
444 next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
445 next->a_nvals[ 0 ] = nbv;
451 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
452 cb->mc_update = bdb_monitor_update;
453 cb->mc_modify = bdb_monitor_modify;
454 cb->mc_free = bdb_monitor_free;
455 cb->mc_private = (void *)bdb;
457 rc = monitor_back_register_entry_attrs( NULL, a, cb,
458 base, LDAP_SCOPE_ONELEVEL, filter );
472 if ( !BER_BVISNULL( filter ) ) {
473 ch_free( filter->bv_val );
474 BER_BVZERO( filter );
478 /* store for cleanup */
479 bdb->bi_monitor_cleanup.bdm_cb = (void *)cb;
481 /* we don't need to keep track of the attributes, because
482 * bdb_monitor_free() takes care of everything */
491 * call from within bdb_db_close()
494 bdb_monitor_close( BackendDB *be )
496 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
498 if ( !BER_BVISNULL( &bdb->bi_monitor_cleanup.bdm_filter ) ) {
499 monitor_back_unregister_entry_callback( NULL,
500 (monitor_callback_t *)bdb->bi_monitor_cleanup.bdm_cb,
501 &bdb->bi_monitor_cleanup.bdm_base,
502 bdb->bi_monitor_cleanup.bdm_scope,
503 &bdb->bi_monitor_cleanup.bdm_filter );
505 if ( !BER_BVISNULL( &bdb->bi_monitor_cleanup.bdm_filter ) ) {
506 ch_free( bdb->bi_monitor_cleanup.bdm_filter.bv_val );
509 memset( &bdb->bi_monitor_cleanup, 0, sizeof( bdb->bi_monitor_cleanup ) );
516 * call from within bdb_db_destroy()
519 bdb_monitor_destroy( BackendDB *be )