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