]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
0874552e30a9f9a756b2d4390d9b10122239881b
[openldap] / servers / slapd / back-monitor / database.c
1 /* database.c - deals with database subsystem */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
9  * This work has beed deveolped for the OpenLDAP Foundation 
10  * in the hope that it may be useful to the Open Source community, 
11  * but WITHOUT ANY WARRANTY.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author and SysNet s.n.c. are not responsible for the consequences
18  *    of use of this software, no matter how awful, even if they arise from
19  *    flaws in it.
20  * 
21  * 2. The origin of this software must not be misrepresented, either by
22  *    explicit claim or by omission.  Since few users ever read sources,
23  *    credits should appear in the documentation.
24  * 
25  * 3. Altered versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.  Since few users
27  *    ever read sources, credits should appear in the documentation.
28  *    SysNet s.n.c. cannot be responsible for the consequences of the
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37 #include <ac/string.h>
38
39 #include "slap.h"
40 #include "back-monitor.h"
41
42 #if defined(LDAP_SLAPI)
43 #include "slapi.h"
44 static int monitor_back_add_plugin( Backend *be, Entry *e );
45 #endif /* defined(LDAP_SLAPI) */
46
47 int
48 monitor_subsys_database_init(
49         BackendDB       *be
50 )
51 {
52         struct monitorinfo      *mi;
53         Entry                   *e, *e_database, *e_tmp;
54         int                     i;
55         struct monitorentrypriv *mp;
56
57         assert( be != NULL );
58
59         mi = ( struct monitorinfo * )be->be_private;
60
61         if ( monitor_cache_get( mi, 
62                                 &monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn, 
63                                 &e_database ) ) {
64 #ifdef NEW_LOGGING
65                 LDAP_LOG( OPERATION, CRIT,
66                         "monitor_subsys_database_init: "
67                         "unable to get entry '%s'\n",
68                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0, 0 );
69 #else
70                 Debug( LDAP_DEBUG_ANY,
71                         "monitor_subsys_database_init: "
72                         "unable to get entry '%s'\n%s%s",
73                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 
74                         "", "" );
75 #endif
76                 return( -1 );
77         }
78
79         e_tmp = NULL;
80         for ( i = nBackendDB; i--; ) {
81                 char buf[ BACKMONITOR_BUFSIZE ];
82                 int j;
83
84                 be = &backendDB[i];
85
86                 /* Subordinates are not exposed as their own naming context */
87                 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
88                         continue;
89                 }
90
91                 snprintf( buf, sizeof( buf ),
92                                 "dn: cn=Database %d,%s\n"
93                                 "objectClass: %s\n"
94                                 "structuralObjectClass: %s\n"
95                                 "cn: Database %d\n"
96                                 "description: This object contains the type of the database.\n"
97                                 "%s: %s\n"
98                                 "createTimestamp: %s\n"
99                                 "modifyTimestamp: %s\n",
100                                 i,
101                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
102                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
103                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
104                                 i,
105                                 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
106                                 be->bd_info->bi_type,
107                                 mi->mi_startTime.bv_val,
108                                 mi->mi_startTime.bv_val );
109                 
110                 e = str2entry( buf );
111                 if ( e == NULL ) {
112 #ifdef NEW_LOGGING
113                         LDAP_LOG( OPERATION, CRIT,
114                                 "monitor_subsys_database_init: "
115                                 "unable to create entry 'cn=Database %d,%s'\n",
116                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
117 #else
118                         Debug( LDAP_DEBUG_ANY,
119                                 "monitor_subsys_database_init: "
120                                 "unable to create entry 'cn=Database %d,%s'\n%s",
121                                 i, 
122                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
123                                 "" );
124 #endif
125                         return( -1 );
126                 }
127                 
128                 if ( be->be_flags & SLAP_BFLAG_MONITOR ) {
129                         attr_merge( e, slap_schema.si_ad_monitorContext,
130                                         be->be_suffix, be->be_nsuffix );
131                         attr_merge( e_database, slap_schema.si_ad_monitorContext,
132                                         be->be_suffix, be->be_nsuffix );
133                 } else {
134                         attr_merge( e, slap_schema.si_ad_namingContexts,
135                                         be->be_suffix, be->be_nsuffix );
136                         attr_merge( e_database, slap_schema.si_ad_namingContexts,
137                                         be->be_suffix, be->be_nsuffix );
138                 }
139
140                 for ( j = nBackendInfo; j--; ) {
141                         if ( backendInfo[ j ].bi_type == be->bd_info->bi_type ) {
142                                 struct berval           bv;
143
144                                 snprintf( buf, sizeof( buf ), 
145                                         "cn=Backend %d,%s", 
146                                         j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
147                                 bv.bv_val = buf;
148                                 bv.bv_len = strlen( buf );
149                                 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
150                                                 &bv, NULL );
151                                 break;
152                         }
153                 }
154                 /* we must find it! */
155                 assert( j >= 0 );
156
157                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
158                 e->e_private = ( void * )mp;
159                 mp->mp_next = e_tmp;
160                 mp->mp_children = NULL;
161                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
162                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
163                         | MONITOR_F_SUB;
164
165                 if ( monitor_cache_add( mi, e ) ) {
166 #ifdef NEW_LOGGING
167                         LDAP_LOG( OPERATION, CRIT,
168                                 "monitor_subsys_database_init: "
169                                 "unable to add entry 'cn=Database %d,%s'\n",
170                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
171 #else
172                         Debug( LDAP_DEBUG_ANY,
173                                 "monitor_subsys_database_init: "
174                                 "unable to add entry 'cn=Database %d,%s'\n",
175                                 i, 
176                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
177                                 0 );
178 #endif
179                         return( -1 );
180                 }
181
182 #if defined(LDAP_SLAPI)
183                 monitor_back_add_plugin( be, e );
184 #endif /* defined(LDAP_SLAPI) */
185
186                 e_tmp = e;
187         }
188         
189         mp = ( struct monitorentrypriv * )e_database->e_private;
190         mp->mp_children = e_tmp;
191
192         monitor_cache_release( mi, e_database );
193
194         return( 0 );
195 }
196
197 #if defined(LDAP_SLAPI)
198 static int
199 monitor_back_add_plugin( Backend *be, Entry *e_database )
200 {
201         Slapi_PBlock            *pCurrentPB; 
202         int                     i, rc = LDAP_SUCCESS;
203         struct monitorinfo      *mi = ( struct monitorinfo * )be->be_private;
204
205         if ( slapi_x_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
206                 /*
207                  * LDAP_OTHER is returned if no plugins are installed
208                  */
209                 rc = LDAP_OTHER;
210                 goto done;
211         }
212
213         i = 0;
214         do {
215                 Slapi_PluginDesc        *srchdesc;
216                 char                    buf[ BACKMONITOR_BUFSIZE ];
217                 struct berval           bv;
218
219                 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
220                                 &srchdesc );
221                 if ( rc != LDAP_SUCCESS ) {
222                         goto done;
223                 }
224
225                 snprintf( buf, sizeof(buf),
226                                 "plugin %d name: %s; "
227                                 "vendor: %s; "
228                                 "version: %s; "
229                                 "description: %s", 
230                                 i,
231                                 srchdesc->spd_id,
232                                 srchdesc->spd_vendor,
233                                 srchdesc->spd_version,
234                                 srchdesc->spd_description );
235
236                 bv.bv_val = buf;
237                 bv.bv_len = strlen( buf );
238                 attr_merge_normalize_one( e_database,
239                                 mi->mi_ad_monitoredInfo, &bv, NULL );
240
241                 i++;
242
243         } while ( ( slapi_x_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
244                         && ( pCurrentPB != NULL ) );
245
246 done:
247         return rc;
248 }
249 #endif /* defined(LDAP_SLAPI) */