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_olmBDBCounter;
40 struct bdb_info *bdb = (struct bdb_info *) priv;
43 /* NOTE: dummy code that increments a olmBDBCounter
44 * any time it's called; replace with something useful */
49 assert( ad_olmBDBCounter != NULL );
51 a = attr_find( e->e_attrs, ad_olmBDBCounter );
53 lutil_atoul( &u, a->a_vals[ 0 ].bv_val );
56 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", u );
57 ber_bvreplace( &a->a_vals[ 0 ], &bv );
59 return SLAP_CB_CONTINUE;
69 struct bdb_info *bdb = (struct bdb_info *) priv;
71 return SLAP_CB_CONTINUE;
79 struct bdb_info *bdb = (struct bdb_info *) priv;
81 return SLAP_CB_CONTINUE;
85 * NOTE: there's some confusion in monitor OID arc;
86 * by now, let's consider:
88 * Subsystems monitor attributes 1.3.6.1.4.1.4203.666.1.55.0
89 * Databases monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1
90 * BDB database monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1.1
92 * Subsystems monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0
93 * Databases monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1
94 * BDB database monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1.1
96 #define BDB_MONITOR_SCHEMA_AD "1.3.6.1.4.1.4203.666.1.55.0.1.1"
97 #define BDB_MONITOR_SCHEMA_OC "1.3.6.1.4.1.4203.666.3.16.0.1.1"
102 AttributeDescription **ad;
104 { "olmBDBCounter", "( " BDB_MONITOR_SCHEMA_AD ".1 "
105 "NAME ( 'olmBDBCounter' ) "
106 "DESC 'A dummy counter' "
107 "SUP monitorCounter "
108 "NO-USER-MODIFICATION "
109 "USAGE directoryOperation )",
120 { "olmBDBDatabase", "( " BDB_MONITOR_SCHEMA_OC ".1 "
121 "NAME ( 'olmBDBDatabase' ) "
122 "SUP monitoredObject STRUCTURAL "
126 &oc_olmBDBDatabase },
132 * call from within bdb_initialize()
135 bdb_monitor_initialize( void )
140 static int bdb_monitor_initialized = 0;
142 /* register schema here; if compiled as dynamic object,
143 * must be loaded __after__ back_monitor.la */
145 if ( bdb_monitor_initialized++ ) {
149 for ( i = 0; s_at[ i ].name != NULL; i++ ) {
150 LDAPAttributeType *at;
152 at = ldap_str2attributetype( s_at[ i ].desc,
153 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
155 Debug( LDAP_DEBUG_ANY,
156 "bdb_monitor_initialize: "
157 "AttributeType load failed: %s %s\n",
158 ldap_scherr2str( code ), err, 0 );
159 return LDAP_INVALID_SYNTAX;
162 code = at_add( at, 0, NULL, &err );
163 if ( code != LDAP_SUCCESS ) {
164 Debug( LDAP_DEBUG_ANY,
165 "bdb_monitor_initialize: "
166 "AttributeType load failed: %s %s\n",
167 scherr2str( code ), err, 0 );
168 code = LDAP_INVALID_SYNTAX;
172 code = slap_str2ad( s_at[ i ].name,
173 s_at[ i ].ad, &err );
174 if ( code != LDAP_SUCCESS ) {
175 Debug( LDAP_DEBUG_ANY,
176 "bdb_monitor_initialize: "
177 "unable to find AttributeDescription "
179 s_at[ i ].name, code, err );
180 code = LDAP_UNDEFINED_TYPE;
186 ldap_attributetype_free( at );
193 for ( i = 0; s_oc[ i ].name != NULL; i++ ) {
196 oc = ldap_str2objectclass( s_oc[ i ].desc,
197 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
199 Debug( LDAP_DEBUG_ANY,
200 "bdb_monitor_initialize: "
201 "ObjectClass load failed: %s %s\n",
202 ldap_scherr2str( code ), err, 0 );
203 return LDAP_INVALID_SYNTAX;
206 code = oc_add( oc, 0, NULL, &err );
207 if ( code != LDAP_SUCCESS ) {
208 Debug( LDAP_DEBUG_ANY,
209 "bdb_monitor_initialize: "
210 "ObjectClass load failed: %s %s\n",
211 scherr2str( code ), err, 0 );
212 code = LDAP_INVALID_SYNTAX;
216 *s_oc[ i ].oc = oc_find( s_oc[ i ].name );
217 if ( *s_oc[ i ].oc == NULL ) {
218 code = LDAP_UNDEFINED_TYPE;
219 Debug( LDAP_DEBUG_ANY,
220 "bdb_monitor_initialize: "
221 "unable to find objectClass \"%s\"\n",
222 s_oc[ i ].name, 0, 0 );
227 if ( code != LDAP_SUCCESS ) {
228 ldap_objectclass_free( oc );
239 * call from within bdb_db_init()
242 bdb_monitor_init( BackendDB *be )
248 * call from within bdb_db_open()
251 bdb_monitor_open( BackendDB *be )
253 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
255 monitor_callback_t *cb;
256 struct berval base = BER_BVC( "cn=databases,cn=monitor" );
257 struct berval suffix, filter;
261 /* monitor_back_register_entry_attrs() with a NULL ndn,
262 * base="cn=Databases,cn=Monitor", scope=LDAP_SCOPE_ONE
263 * and filter="(namingContexts:distinguishedNameMatch:=<suffix>)" */
265 suffix.bv_len = ldap_bv2escaped_filter_value_len( &be->be_nsuffix[ 0 ] );
266 if ( suffix.bv_len == be->be_nsuffix[ 0 ].bv_len ) {
267 suffix = be->be_nsuffix[ 0 ];
270 ldap_bv2escaped_filter_value( &be->be_nsuffix[ 0 ], &suffix );
273 filter.bv_len = STRLENOF( "(namingContexts:distinguishedNameMatch:=)" ) + suffix.bv_len;
274 ptr = filter.bv_val = ch_malloc( filter.bv_len + 1 );
275 ptr = lutil_strcopy( ptr, "(namingContexts:distinguishedNameMatch:=" );
276 ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
280 assert( filter.bv_len == ptr - filter.bv_val );
282 if ( suffix.bv_val != be->be_nsuffix[ 0 ].bv_val ) {
283 ch_free( suffix.bv_val );
286 /* alloc as many as required (plus 1 for objectClass) */
287 a = attrs_alloc( 1 + 1 );
293 a->a_desc = slap_schema.si_ad_objectClass;
295 value_add_one( &a->a_vals, &oc_olmBDBDatabase->soc_cname );
296 a->a_nvals = a->a_vals;
299 /* NOTE: dummy code that increments a olmBDBCounter
300 * any time it's called; replace with something useful */
302 struct berval bv = BER_BVC( "0" );
304 next->a_desc = ad_olmBDBCounter;
306 value_add_one( &next->a_vals, &bv );
307 next->a_nvals = next->a_vals;
311 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
312 cb->mc_update = bdb_monitor_update;
313 cb->mc_modify = bdb_monitor_modify;
314 cb->mc_free = bdb_monitor_free;
315 cb->mc_private = (void *)bdb;
317 rc = monitor_back_register_entry_attrs( NULL,
318 a, cb, &base, LDAP_SCOPE_ONELEVEL, &filter );
327 if ( !BER_BVISNULL( &filter ) ) {
328 ch_free( filter.bv_val );
339 * call from within bdb_db_close()
342 bdb_monitor_close( BackendDB *be )
348 * call from within bdb_db_destroy()
351 bdb_monitor_destroy( BackendDB *be )