]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/monitor.c
497510147c60dbb08f9f2b1ed4beac54c963b458
[openldap] / servers / slapd / back-mdb / monitor.c
1 /* monitor.c - monitor mdb backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2011 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
23 #include <ac/errno.h>
24 #include <sys/stat.h>
25 #include "lutil.h"
26 #include "back-mdb.h"
27
28 #include "../back-monitor/back-monitor.h"
29
30 #include "config.h"
31
32 static ObjectClass              *oc_olmMDBDatabase;
33
34 static AttributeDescription *ad_olmDbDirectory;
35
36 #ifdef MDB_MONITOR_IDX
37 static int
38 mdb_monitor_idx_entry_add(
39         struct mdb_info *mdb,
40         Entry           *e );
41
42 static AttributeDescription     *ad_olmDbNotIndexed;
43 #endif /* MDB_MONITOR_IDX */
44
45 /*
46  * NOTE: there's some confusion in monitor OID arc;
47  * by now, let's consider:
48  * 
49  * Subsystems monitor attributes        1.3.6.1.4.1.4203.666.1.55.0
50  * Databases monitor attributes         1.3.6.1.4.1.4203.666.1.55.0.1
51  * MDB database monitor attributes      1.3.6.1.4.1.4203.666.1.55.0.1.3
52  *
53  * Subsystems monitor objectclasses     1.3.6.1.4.1.4203.666.3.16.0
54  * Databases monitor objectclasses      1.3.6.1.4.1.4203.666.3.16.0.1
55  * MDB database monitor objectclasses   1.3.6.1.4.1.4203.666.3.16.0.1.3
56  */
57
58 static struct {
59         char                    *name;
60         char                    *oid;
61 }               s_oid[] = {
62         { "olmMDBAttributes",                   "olmDatabaseAttributes:1" },
63         { "olmMDBObjectClasses",                "olmDatabaseObjectClasses:1" },
64
65         { NULL }
66 };
67
68 static struct {
69         char                    *desc;
70         AttributeDescription    **ad;
71 }               s_at[] = {
72         { "( olmDatabaseAttributes:1 "
73                 "NAME ( 'olmDbDirectory' ) "
74                 "DESC 'Path name of the directory "
75                         "where the database environment resides' "
76                 "SUP monitoredInfo "
77                 "NO-USER-MODIFICATION "
78                 "USAGE dSAOperation )",
79                 &ad_olmDbDirectory },
80
81 #ifdef MDB_MONITOR_IDX
82         { "( olmDatabaseAttributes:2 "
83                 "NAME ( 'olmDbNotIndexed' ) "
84                 "DESC 'Missing indexes resulting from candidate selection' "
85                 "SUP monitoredInfo "
86                 "NO-USER-MODIFICATION "
87                 "USAGE dSAOperation )",
88                 &ad_olmDbNotIndexed },
89 #endif /* MDB_MONITOR_IDX */
90
91         { NULL }
92 };
93
94 static struct {
95         char            *desc;
96         ObjectClass     **oc;
97 }               s_oc[] = {
98         /* augments an existing object, so it must be AUXILIARY
99          * FIXME: derive from some ABSTRACT "monitoredEntity"? */
100         { "( olmMDBObjectClasses:2 "
101                 "NAME ( 'olmMDBDatabase' ) "
102                 "SUP top AUXILIARY "
103                 "MAY ( "
104                         "olmDbDirectory "
105 #ifdef MDB_MONITOR_IDX
106                         "$ olmDbNotIndexed "
107 #endif /* MDB_MONITOR_IDX */
108                         ") )",
109                 &oc_olmMDBDatabase },
110
111         { NULL }
112 };
113
114 static int
115 mdb_monitor_update(
116         Operation       *op,
117         SlapReply       *rs,
118         Entry           *e,
119         void            *priv )
120 {
121         struct mdb_info         *mdb = (struct mdb_info *) priv;
122
123 #ifdef MDB_MONITOR_IDX
124         mdb_monitor_idx_entry_add( mdb, e );
125 #endif /* MDB_MONITOR_IDX */
126
127         return SLAP_CB_CONTINUE;
128 }
129
130 #if 0   /* uncomment if required */
131 static int
132 mdb_monitor_modify(
133         Operation       *op,
134         SlapReply       *rs,
135         Entry           *e,
136         void            *priv )
137 {
138         return SLAP_CB_CONTINUE;
139 }
140 #endif
141
142 static int
143 mdb_monitor_free(
144         Entry           *e,
145         void            **priv )
146 {
147         struct berval   values[ 2 ];
148         Modification    mod = { 0 };
149
150         const char      *text;
151         char            textbuf[ SLAP_TEXT_BUFLEN ];
152
153         int             i, rc;
154
155         /* NOTE: if slap_shutdown != 0, priv might have already been freed */
156         *priv = NULL;
157
158         /* Remove objectClass */
159         mod.sm_op = LDAP_MOD_DELETE;
160         mod.sm_desc = slap_schema.si_ad_objectClass;
161         mod.sm_values = values;
162         mod.sm_numvals = 1;
163         values[ 0 ] = oc_olmMDBDatabase->soc_cname;
164         BER_BVZERO( &values[ 1 ] );
165
166         rc = modify_delete_values( e, &mod, 1, &text,
167                 textbuf, sizeof( textbuf ) );
168         /* don't care too much about return code... */
169
170         /* remove attrs */
171         mod.sm_values = NULL;
172         mod.sm_numvals = 0;
173         for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
174                 mod.sm_desc = *s_at[ i ].ad;
175                 rc = modify_delete_values( e, &mod, 1, &text,
176                         textbuf, sizeof( textbuf ) );
177                 /* don't care too much about return code... */
178         }
179         
180         return SLAP_CB_CONTINUE;
181 }
182
183 /*
184  * call from within mdb_initialize()
185  */
186 static int
187 mdb_monitor_initialize( void )
188 {
189         int             i, code;
190         ConfigArgs c;
191         char    *argv[ 3 ];
192
193         static int      mdb_monitor_initialized = 0;
194
195         /* set to 0 when successfully initialized; otherwise, remember failure */
196         static int      mdb_monitor_initialized_failure = 1;
197
198         if ( mdb_monitor_initialized++ ) {
199                 return mdb_monitor_initialized_failure;
200         }
201
202         if ( backend_info( "monitor" ) == NULL ) {
203                 return -1;
204         }
205
206         /* register schema here */
207
208         argv[ 0 ] = "back-mdb monitor";
209         c.argv = argv;
210         c.argc = 3;
211         c.fname = argv[0];
212
213         for ( i = 0; s_oid[ i ].name; i++ ) {
214                 c.lineno = i;
215                 argv[ 1 ] = s_oid[ i ].name;
216                 argv[ 2 ] = s_oid[ i ].oid;
217
218                 if ( parse_oidm( &c, 0, NULL ) != 0 ) {
219                         Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(mdb_monitor_initialize)
220                                 ": unable to add "
221                                 "objectIdentifier \"%s=%s\"\n",
222                                 s_oid[ i ].name, s_oid[ i ].oid, 0 );
223                         return 2;
224                 }
225         }
226
227         for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
228                 code = register_at( s_at[ i ].desc, s_at[ i ].ad, 1 );
229                 if ( code != LDAP_SUCCESS ) {
230                         Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(mdb_monitor_initialize)
231                                 ": register_at failed for attributeType (%s)\n",
232                                 s_at[ i ].desc, 0, 0 );
233                         return 3;
234
235                 } else {
236                         (*s_at[ i ].ad)->ad_type->sat_flags |= SLAP_AT_HIDE;
237                 }
238         }
239
240         for ( i = 0; s_oc[ i ].desc != NULL; i++ ) {
241                 code = register_oc( s_oc[ i ].desc, s_oc[ i ].oc, 1 );
242                 if ( code != LDAP_SUCCESS ) {
243                         Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(mdb_monitor_initialize)
244                                 ": register_oc failed for objectClass (%s)\n",
245                                 s_oc[ i ].desc, 0, 0 );
246                         return 4;
247
248                 } else {
249                         (*s_oc[ i ].oc)->soc_flags |= SLAP_OC_HIDE;
250                 }
251         }
252
253         return ( mdb_monitor_initialized_failure = LDAP_SUCCESS );
254 }
255
256 /*
257  * call from within mdb_db_init()
258  */
259 int
260 mdb_monitor_db_init( BackendDB *be )
261 {
262         struct mdb_info         *mdb = (struct mdb_info *) be->be_private;
263
264         if ( mdb_monitor_initialize() == LDAP_SUCCESS ) {
265                 /* monitoring in back-mdb is on by default */
266                 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
267         }
268
269 #ifdef MDB_MONITOR_IDX
270         mdb->mi_idx = NULL;
271         ldap_pvt_thread_mutex_init( &mdb->mi_idx_mutex );
272 #endif /* MDB_MONITOR_IDX */
273
274         return 0;
275 }
276
277 /*
278  * call from within mdb_db_open()
279  */
280 int
281 mdb_monitor_db_open( BackendDB *be )
282 {
283         struct mdb_info         *mdb = (struct mdb_info *) be->be_private;
284         Attribute               *a, *next;
285         monitor_callback_t      *cb = NULL;
286         int                     rc = 0;
287         BackendInfo             *mi;
288         monitor_extra_t         *mbe;
289         struct berval dummy = BER_BVC("");
290
291         if ( !SLAP_DBMONITORING( be ) ) {
292                 return 0;
293         }
294
295         mi = backend_info( "monitor" );
296         if ( !mi || !mi->bi_extra ) {
297                 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
298                 return 0;
299         }
300         mbe = mi->bi_extra;
301
302         /* don't bother if monitor is not configured */
303         if ( !mbe->is_configured() ) {
304                 static int warning = 0;
305
306                 if ( warning++ == 0 ) {
307                         Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(mdb_monitor_db_open)
308                                 ": monitoring disabled; "
309                                 "configure monitor database to enable\n",
310                                 0, 0, 0 );
311                 }
312
313                 return 0;
314         }
315
316         /* alloc as many as required (plus 1 for objectClass) */
317         a = attrs_alloc( 1 + 1 );
318         if ( a == NULL ) {
319                 rc = 1;
320                 goto cleanup;
321         }
322
323         a->a_desc = slap_schema.si_ad_objectClass;
324         attr_valadd( a, &oc_olmMDBDatabase->soc_cname, NULL, 1 );
325         next = a->a_next;
326
327         {
328                 struct berval   bv, nbv;
329                 ber_len_t       pathlen = 0, len = 0;
330                 char            path[ MAXPATHLEN ] = { '\0' };
331                 char            *fname = mdb->mi_dbenv_home,
332                                 *ptr;
333
334                 len = strlen( fname );
335                 if ( fname[ 0 ] != '/' ) {
336                         /* get full path name */
337                         getcwd( path, sizeof( path ) );
338                         pathlen = strlen( path );
339
340                         if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
341                                 fname += 2;
342                                 len -= 2;
343                         }
344                 }
345
346                 bv.bv_len = pathlen + STRLENOF( "/" ) + len;
347                 ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
348                 if ( pathlen ) {
349                         ptr = lutil_strncopy( ptr, path, pathlen );
350                         ptr[ 0 ] = '/';
351                         ptr++;
352                 }
353                 ptr = lutil_strncopy( ptr, fname, len );
354                 if ( ptr[ -1 ] != '/' ) {
355                         ptr[ 0 ] = '/';
356                         ptr++;
357                 }
358                 ptr[ 0 ] = '\0';
359                 
360                 attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
361
362                 next->a_desc = ad_olmDbDirectory;
363                 next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
364                 next->a_vals[ 0 ] = bv;
365                 next->a_numvals = 1;
366
367                 if ( BER_BVISNULL( &nbv ) ) {
368                         next->a_nvals = next->a_vals;
369
370                 } else {
371                         next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
372                         next->a_nvals[ 0 ] = nbv;
373                 }
374
375                 next = next->a_next;
376         }
377
378         cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
379         cb->mc_update = mdb_monitor_update;
380 #if 0   /* uncomment if required */
381         cb->mc_modify = mdb_monitor_modify;
382 #endif
383         cb->mc_free = mdb_monitor_free;
384         cb->mc_private = (void *)mdb;
385
386         /* make sure the database is registered; then add monitor attributes */
387         rc = mbe->register_database( be, &mdb->mi_monitor.mdm_ndn );
388         if ( rc == 0 ) {
389                 rc = mbe->register_entry_attrs( &mdb->mi_monitor.mdm_ndn, a, cb,
390                         &dummy, 0, &dummy );
391         }
392
393 cleanup:;
394         if ( rc != 0 ) {
395                 if ( cb != NULL ) {
396                         ch_free( cb );
397                         cb = NULL;
398                 }
399
400                 if ( a != NULL ) {
401                         attrs_free( a );
402                         a = NULL;
403                 }
404         }
405
406         /* store for cleanup */
407         mdb->mi_monitor.mdm_cb = (void *)cb;
408
409         /* we don't need to keep track of the attributes, because
410          * mdb_monitor_free() takes care of everything */
411         if ( a != NULL ) {
412                 attrs_free( a );
413         }
414
415         return rc;
416 }
417
418 /*
419  * call from within mdb_db_close()
420  */
421 int
422 mdb_monitor_db_close( BackendDB *be )
423 {
424         struct mdb_info         *mdb = (struct mdb_info *) be->be_private;
425
426         if ( !BER_BVISNULL( &mdb->mi_monitor.mdm_ndn ) ) {
427                 BackendInfo             *mi = backend_info( "monitor" );
428                 monitor_extra_t         *mbe;
429
430                 if ( mi && &mi->bi_extra ) {
431                         mbe = mi->bi_extra;
432                         mbe->unregister_entry_callback( &mdb->mi_monitor.mdm_ndn,
433                                 (monitor_callback_t *)mdb->mi_monitor.mdm_cb,
434                                 NULL, 0, NULL );
435                 }
436
437                 memset( &mdb->mi_monitor, 0, sizeof( mdb->mi_monitor ) );
438         }
439
440         return 0;
441 }
442
443 /*
444  * call from within mdb_db_destroy()
445  */
446 int
447 mdb_monitor_db_destroy( BackendDB *be )
448 {
449 #ifdef MDB_MONITOR_IDX
450         struct mdb_info         *mdb = (struct mdb_info *) be->be_private;
451
452         /* TODO: free tree */
453         ldap_pvt_thread_mutex_destroy( &mdb->mi_idx_mutex );
454         avl_free( mdb->mi_idx, ch_free );
455 #endif /* MDB_MONITOR_IDX */
456
457         return 0;
458 }
459
460 #ifdef MDB_MONITOR_IDX
461
462 #define MDB_MONITOR_IDX_TYPES   (4)
463
464 typedef struct monitor_idx_t monitor_idx_t;
465
466 struct monitor_idx_t {
467         AttributeDescription    *idx_ad;
468         unsigned long           idx_count[MDB_MONITOR_IDX_TYPES];
469 };
470
471 static int
472 mdb_monitor_bitmask2key( slap_mask_t bitmask )
473 {
474         int     key;
475
476         for ( key = 0; key < 8 * (int)sizeof(slap_mask_t) && !( bitmask & 0x1U );
477                         key++ )
478                 bitmask >>= 1;
479
480         return key;
481 }
482
483 static struct berval idxbv[] = {
484         BER_BVC( "present=" ),
485         BER_BVC( "equality=" ),
486         BER_BVC( "approx=" ),
487         BER_BVC( "substr=" ),
488         BER_BVNULL
489 };
490
491 static ber_len_t
492 mdb_monitor_idx2len( monitor_idx_t *idx )
493 {
494         int             i;
495         ber_len_t       len = 0;
496
497         for ( i = 0; i < MDB_MONITOR_IDX_TYPES; i++ ) {
498                 if ( idx->idx_count[ i ] != 0 ) {
499                         len += idxbv[i].bv_len;
500                 }
501         }
502
503         return len;
504 }
505
506 static int
507 monitor_idx_cmp( const void *p1, const void *p2 )
508 {
509         const monitor_idx_t     *idx1 = (const monitor_idx_t *)p1;
510         const monitor_idx_t     *idx2 = (const monitor_idx_t *)p2;
511
512         return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad );
513 }
514
515 static int
516 monitor_idx_dup( void *p1, void *p2 )
517 {
518         monitor_idx_t   *idx1 = (monitor_idx_t *)p1;
519         monitor_idx_t   *idx2 = (monitor_idx_t *)p2;
520
521         return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad ) == 0 ? -1 : 0;
522 }
523
524 int
525 mdb_monitor_idx_add(
526         struct mdb_info         *mdb,
527         AttributeDescription    *desc,
528         slap_mask_t             type )
529 {
530         monitor_idx_t           idx_dummy = { 0 },
531                                 *idx;
532         int                     rc = 0, key;
533
534         idx_dummy.idx_ad = desc;
535         key = mdb_monitor_bitmask2key( type ) - 1;
536         if ( key >= MDB_MONITOR_IDX_TYPES ) {
537                 /* invalid index type */
538                 return -1;
539         }
540
541         ldap_pvt_thread_mutex_lock( &mdb->mi_idx_mutex );
542
543         idx = (monitor_idx_t *)avl_find( mdb->mi_idx,
544                 (caddr_t)&idx_dummy, monitor_idx_cmp );
545         if ( idx == NULL ) {
546                 idx = (monitor_idx_t *)ch_calloc( sizeof( monitor_idx_t ), 1 );
547                 idx->idx_ad = desc;
548                 idx->idx_count[ key ] = 1;
549
550                 switch ( avl_insert( &mdb->mi_idx, (caddr_t)idx, 
551                         monitor_idx_cmp, monitor_idx_dup ) )
552                 {
553                 case 0:
554                         break;
555
556                 default:
557                         ch_free( idx );
558                         rc = -1;
559                 }
560
561         } else {
562                 idx->idx_count[ key ]++;
563         }
564
565         ldap_pvt_thread_mutex_unlock( &mdb->mi_idx_mutex );
566
567         return rc;
568 }
569
570 static int
571 mdb_monitor_idx_apply( void *v_idx, void *v_valp )
572 {
573         monitor_idx_t   *idx = (monitor_idx_t *)v_idx;
574         BerVarray       *valp = (BerVarray *)v_valp;
575
576         struct berval   bv;
577         char            *ptr;
578         char            count_buf[ MDB_MONITOR_IDX_TYPES ][ SLAP_TEXT_BUFLEN ];
579         ber_len_t       count_len[ MDB_MONITOR_IDX_TYPES ],
580                         idx_len;
581         int             i, num = 0;
582
583         idx_len = mdb_monitor_idx2len( idx );
584
585         bv.bv_len = 0;
586         for ( i = 0; i < MDB_MONITOR_IDX_TYPES; i++ ) {
587                 if ( idx->idx_count[ i ] == 0 ) {
588                         continue;
589                 }
590
591                 count_len[ i ] = snprintf( count_buf[ i ],
592                         sizeof( count_buf[ i ] ), "%lu", idx->idx_count[ i ] );
593                 bv.bv_len += count_len[ i ];
594                 num++;
595         }
596
597         bv.bv_len += idx->idx_ad->ad_cname.bv_len
598                 + num
599                 + idx_len;
600         ptr = bv.bv_val = ch_malloc( bv.bv_len + 1 );
601         ptr = lutil_strcopy( ptr, idx->idx_ad->ad_cname.bv_val );
602         for ( i = 0; i < MDB_MONITOR_IDX_TYPES; i++ ) {
603                 if ( idx->idx_count[ i ] == 0 ) {
604                         continue;
605                 }
606
607                 ptr[ 0 ] = '#';
608                 ++ptr;
609                 ptr = lutil_strcopy( ptr, idxbv[ i ].bv_val );
610                 ptr = lutil_strcopy( ptr, count_buf[ i ] );
611         }
612
613         ber_bvarray_add( valp, &bv );
614
615         return 0;
616 }
617
618 static int
619 mdb_monitor_idx_entry_add(
620         struct mdb_info *mdb,
621         Entry           *e )
622 {
623         BerVarray       vals = NULL;
624         Attribute       *a;
625
626         a = attr_find( e->e_attrs, ad_olmDbNotIndexed );
627
628         ldap_pvt_thread_mutex_lock( &mdb->mi_idx_mutex );
629
630         avl_apply( mdb->mi_idx, mdb_monitor_idx_apply,
631                 &vals, -1, AVL_INORDER );
632
633         ldap_pvt_thread_mutex_unlock( &mdb->mi_idx_mutex );
634
635         if ( vals != NULL ) {
636                 if ( a != NULL ) {
637                         assert( a->a_nvals == a->a_vals );
638
639                         ber_bvarray_free( a->a_vals );
640
641                 } else {
642                         Attribute       **ap;
643
644                         for ( ap = &e->e_attrs; *ap != NULL; ap = &(*ap)->a_next )
645                                 ;
646                         *ap = attr_alloc( ad_olmDbNotIndexed );
647                         a = *ap;
648                 }
649                 a->a_vals = vals;
650                 a->a_nvals = a->a_vals;
651         }
652
653         return 0;
654 }
655
656 #endif /* MDB_MONITOR_IDX */