]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
3442106bb0fea7641e209ac9a0cdf2c881f40d5d
[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 2001-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26
27 #include "slap.h"
28 #include "back-monitor.h"
29
30 #if defined(LDAP_SLAPI)
31 #include "slapi.h"
32 static int monitor_back_add_plugin( Backend *be, Entry *e );
33 #endif /* defined(LDAP_SLAPI) */
34
35 #if defined(SLAPD_LDAP) 
36 #include "../back-ldap/back-ldap.h"
37 #endif /* defined(SLAPD_LDAP) */
38
39 int
40 monitor_subsys_database_init(
41         BackendDB       *be
42 )
43 {
44         struct monitorinfo      *mi;
45         Entry                   *e, *e_database, *e_tmp;
46         int                     i;
47         struct monitorentrypriv *mp;
48
49         assert( be != NULL );
50
51         mi = ( struct monitorinfo * )be->be_private;
52
53         if ( monitor_cache_get( mi, 
54                                 &monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn, 
55                                 &e_database ) ) {
56 #ifdef NEW_LOGGING
57                 LDAP_LOG( OPERATION, CRIT,
58                         "monitor_subsys_database_init: "
59                         "unable to get entry '%s'\n",
60                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0, 0 );
61 #else
62                 Debug( LDAP_DEBUG_ANY,
63                         "monitor_subsys_database_init: "
64                         "unable to get entry '%s'\n%s%s",
65                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 
66                         "", "" );
67 #endif
68                 return( -1 );
69         }
70
71         e_tmp = NULL;
72         for ( i = nBackendDB; i--; ) {
73                 char            buf[ BACKMONITOR_BUFSIZE ];
74                 int             j;
75                 slap_overinfo   *oi = NULL;
76
77                 be = &backendDB[i];
78
79                 if ( strcmp( be->bd_info->bi_type, "over" ) == 0 ) {
80                         oi = (slap_overinfo *)be->bd_info;
81
82                         be = &oi->oi_bd;
83                 }
84
85                 /* Subordinates are not exposed as their own naming context */
86                 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
87                         continue;
88                 }
89
90                 snprintf( buf, sizeof( buf ),
91                                 "dn: cn=Database %d,%s\n"
92                                 "objectClass: %s\n"
93                                 "structuralObjectClass: %s\n"
94                                 "cn: Database %d\n"
95                                 "description: This object contains the type of the database.\n"
96                                 "%s: %s\n"
97                                 "createTimestamp: %s\n"
98                                 "modifyTimestamp: %s\n",
99                                 i,
100                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
101                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
102                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
103                                 i,
104                                 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
105                                 be->bd_info->bi_type,
106                                 mi->mi_startTime.bv_val,
107                                 mi->mi_startTime.bv_val );
108                 
109                 e = str2entry( buf );
110                 if ( e == NULL ) {
111 #ifdef NEW_LOGGING
112                         LDAP_LOG( OPERATION, CRIT,
113                                 "monitor_subsys_database_init: "
114                                 "unable to create entry 'cn=Database %d,%s'\n",
115                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
116 #else
117                         Debug( LDAP_DEBUG_ANY,
118                                 "monitor_subsys_database_init: "
119                                 "unable to create entry 'cn=Database %d,%s'\n%s",
120                                 i, 
121                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
122                                 "" );
123 #endif
124                         return( -1 );
125                 }
126                 
127                 if ( be->be_flags & SLAP_BFLAG_MONITOR ) {
128                         attr_merge( e, slap_schema.si_ad_monitorContext,
129                                         be->be_suffix, be->be_nsuffix );
130                         attr_merge( e_database, slap_schema.si_ad_monitorContext,
131                                         be->be_suffix, be->be_nsuffix );
132                 } else {
133                         attr_merge( e, slap_schema.si_ad_namingContexts,
134                                         be->be_suffix, be->be_nsuffix );
135                         attr_merge( e_database, slap_schema.si_ad_namingContexts,
136                                         be->be_suffix, be->be_nsuffix );
137                 }
138
139                 if ( oi != NULL ) {
140                         slap_overinst *on = oi->oi_list;
141
142                         for ( ; on; on = on->on_next ) {
143                                 struct berval           bv;
144                                 slap_overinst           *on2;
145                                 
146                                 bv.bv_val = on->on_bi.bi_type;
147                                 bv.bv_len = strlen( bv.bv_val );
148                                 attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
149                                                 &bv, NULL );
150
151                                 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
152                                         if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
153                                                 break;
154                                         }
155                                 }
156                                 assert( on2 );
157
158                                 snprintf( buf, sizeof( buf ), 
159                                         "cn=Overlay %d,%s", 
160                                         j, monitor_subsys[SLAPD_MONITOR_OVERLAY].mss_dn.bv_val );
161                                 bv.bv_val = buf;
162                                 bv.bv_len = strlen( buf );
163                                 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
164                                                 &bv, NULL );
165                         }
166                 }
167
168 #if defined(SLAPD_LDAP) 
169                 if ( strcmp( be->bd_info->bi_type, "ldap" ) == 0 ) {
170                         struct ldapinfo         *li = (struct ldapinfo *)be->be_private;
171                         struct berval           bv;
172
173                         bv.bv_val = li->url;
174                         bv.bv_len = strlen( bv.bv_val );
175                         attr_merge_normalize_one( e, mi->mi_ad_labeledURI,
176                                         &bv, NULL );
177                 }
178 #endif /* defined(SLAPD_LDAP) */
179
180                 for ( j = nBackendInfo; j--; ) {
181                         if ( backendInfo[ j ].bi_type == be->bd_info->bi_type ) {
182                                 struct berval           bv;
183
184                                 snprintf( buf, sizeof( buf ), 
185                                         "cn=Backend %d,%s", 
186                                         j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
187                                 bv.bv_val = buf;
188                                 bv.bv_len = strlen( buf );
189                                 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
190                                                 &bv, NULL );
191                                 break;
192                         }
193                 }
194                 /* we must find it! */
195                 assert( j >= 0 );
196
197                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
198                 e->e_private = ( void * )mp;
199                 mp->mp_next = e_tmp;
200                 mp->mp_children = NULL;
201                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
202                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
203                         | MONITOR_F_SUB;
204
205                 if ( monitor_cache_add( mi, e ) ) {
206 #ifdef NEW_LOGGING
207                         LDAP_LOG( OPERATION, CRIT,
208                                 "monitor_subsys_database_init: "
209                                 "unable to add entry 'cn=Database %d,%s'\n",
210                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
211 #else
212                         Debug( LDAP_DEBUG_ANY,
213                                 "monitor_subsys_database_init: "
214                                 "unable to add entry 'cn=Database %d,%s'\n",
215                                 i, 
216                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
217                                 0 );
218 #endif
219                         return( -1 );
220                 }
221
222 #if defined(LDAP_SLAPI)
223                 monitor_back_add_plugin( be, e );
224 #endif /* defined(LDAP_SLAPI) */
225
226                 e_tmp = e;
227         }
228         
229         mp = ( struct monitorentrypriv * )e_database->e_private;
230         mp->mp_children = e_tmp;
231
232         monitor_cache_release( mi, e_database );
233
234         return( 0 );
235 }
236
237 #if defined(LDAP_SLAPI)
238 static int
239 monitor_back_add_plugin( Backend *be, Entry *e_database )
240 {
241         Slapi_PBlock            *pCurrentPB; 
242         int                     i, rc = LDAP_SUCCESS;
243         struct monitorinfo      *mi = ( struct monitorinfo * )be->be_private;
244
245         if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
246                 /*
247                  * LDAP_OTHER is returned if no plugins are installed
248                  */
249                 rc = LDAP_OTHER;
250                 goto done;
251         }
252
253         i = 0;
254         do {
255                 Slapi_PluginDesc        *srchdesc;
256                 char                    buf[ BACKMONITOR_BUFSIZE ];
257                 struct berval           bv;
258
259                 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
260                                 &srchdesc );
261                 if ( rc != LDAP_SUCCESS ) {
262                         goto done;
263                 }
264
265                 snprintf( buf, sizeof(buf),
266                                 "plugin %d name: %s; "
267                                 "vendor: %s; "
268                                 "version: %s; "
269                                 "description: %s", 
270                                 i,
271                                 srchdesc->spd_id,
272                                 srchdesc->spd_vendor,
273                                 srchdesc->spd_version,
274                                 srchdesc->spd_description );
275
276                 bv.bv_val = buf;
277                 bv.bv_len = strlen( buf );
278                 attr_merge_normalize_one( e_database,
279                                 mi->mi_ad_monitoredInfo, &bv, NULL );
280
281                 i++;
282
283         } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
284                         && ( pCurrentPB != NULL ) );
285
286 done:
287         return rc;
288 }
289 #endif /* defined(LDAP_SLAPI) */