]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
Use SLAP_MONITOR(be) to check flag
[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                 BackendInfo     *bi;
77
78                 be = &backendDB[i];
79
80                 bi = be->bd_info;
81
82                 if ( strcmp( be->bd_info->bi_type, "over" ) == 0 ) {
83                         oi = (slap_overinfo *)be->bd_info;
84                         bi = oi->oi_orig;
85                 }
86
87                 /* Subordinates are not exposed as their own naming context */
88                 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
89                         continue;
90                 }
91
92                 snprintf( buf, sizeof( buf ),
93                                 "dn: cn=Database %d,%s\n"
94                                 "objectClass: %s\n"
95                                 "structuralObjectClass: %s\n"
96                                 "cn: Database %d\n"
97                                 "description: This object contains the type of the database.\n"
98                                 "%s: %s\n"
99                                 "createTimestamp: %s\n"
100                                 "modifyTimestamp: %s\n",
101                                 i,
102                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
103                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
104                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
105                                 i,
106                                 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
107                                 bi->bi_type,
108                                 mi->mi_startTime.bv_val,
109                                 mi->mi_startTime.bv_val );
110                 
111                 e = str2entry( buf );
112                 if ( e == NULL ) {
113 #ifdef NEW_LOGGING
114                         LDAP_LOG( OPERATION, CRIT,
115                                 "monitor_subsys_database_init: "
116                                 "unable to create entry 'cn=Database %d,%s'\n",
117                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
118 #else
119                         Debug( LDAP_DEBUG_ANY,
120                                 "monitor_subsys_database_init: "
121                                 "unable to create entry 'cn=Database %d,%s'\n%s",
122                                 i, 
123                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
124                                 "" );
125 #endif
126                         return( -1 );
127                 }
128                 
129                 if ( SLAP_MONITOR(be) ) {
130                         attr_merge( e, slap_schema.si_ad_monitorContext,
131                                         be->be_suffix, be->be_nsuffix );
132                         attr_merge( e_database, slap_schema.si_ad_monitorContext,
133                                         be->be_suffix, be->be_nsuffix );
134                 } else {
135                         attr_merge( e, slap_schema.si_ad_namingContexts,
136                                         be->be_suffix, be->be_nsuffix );
137                         attr_merge( e_database, slap_schema.si_ad_namingContexts,
138                                         be->be_suffix, be->be_nsuffix );
139                 }
140
141                 if ( oi != NULL ) {
142                         slap_overinst *on = oi->oi_list;
143
144                         for ( ; on; on = on->on_next ) {
145                                 struct berval           bv;
146                                 slap_overinst           *on2;
147                                 
148                                 bv.bv_val = on->on_bi.bi_type;
149                                 bv.bv_len = strlen( bv.bv_val );
150                                 attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
151                                                 &bv, NULL );
152
153                                 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
154                                         if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
155                                                 break;
156                                         }
157                                 }
158                                 assert( on2 );
159
160                                 snprintf( buf, sizeof( buf ), 
161                                         "cn=Overlay %d,%s", 
162                                         j, monitor_subsys[SLAPD_MONITOR_OVERLAY].mss_dn.bv_val );
163                                 bv.bv_val = buf;
164                                 bv.bv_len = strlen( buf );
165                                 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
166                                                 &bv, NULL );
167                         }
168                 }
169
170 #if defined(SLAPD_LDAP) 
171                 if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
172                         struct ldapinfo         *li = (struct ldapinfo *)be->be_private;
173                         struct berval           bv;
174
175                         bv.bv_val = li->url;
176                         bv.bv_len = strlen( bv.bv_val );
177                         attr_merge_normalize_one( e, mi->mi_ad_labeledURI,
178                                         &bv, NULL );
179                 }
180 #endif /* defined(SLAPD_LDAP) */
181
182                 for ( j = nBackendInfo; j--; ) {
183                         if ( backendInfo[ j ].bi_type == bi->bi_type ) {
184                                 struct berval           bv;
185
186                                 snprintf( buf, sizeof( buf ), 
187                                         "cn=Backend %d,%s", 
188                                         j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
189                                 bv.bv_val = buf;
190                                 bv.bv_len = strlen( buf );
191                                 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
192                                                 &bv, NULL );
193                                 break;
194                         }
195                 }
196                 /* we must find it! */
197                 assert( j >= 0 );
198
199                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
200                 e->e_private = ( void * )mp;
201                 mp->mp_next = e_tmp;
202                 mp->mp_children = NULL;
203                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
204                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
205                         | MONITOR_F_SUB;
206
207                 if ( monitor_cache_add( mi, e ) ) {
208 #ifdef NEW_LOGGING
209                         LDAP_LOG( OPERATION, CRIT,
210                                 "monitor_subsys_database_init: "
211                                 "unable to add entry 'cn=Database %d,%s'\n",
212                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
213 #else
214                         Debug( LDAP_DEBUG_ANY,
215                                 "monitor_subsys_database_init: "
216                                 "unable to add entry 'cn=Database %d,%s'\n",
217                                 i, 
218                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
219                                 0 );
220 #endif
221                         return( -1 );
222                 }
223
224 #if defined(LDAP_SLAPI)
225                 monitor_back_add_plugin( be, e );
226 #endif /* defined(LDAP_SLAPI) */
227
228                 e_tmp = e;
229         }
230         
231         mp = ( struct monitorentrypriv * )e_database->e_private;
232         mp->mp_children = e_tmp;
233
234         monitor_cache_release( mi, e_database );
235
236         return( 0 );
237 }
238
239 #if defined(LDAP_SLAPI)
240 static int
241 monitor_back_add_plugin( Backend *be, Entry *e_database )
242 {
243         Slapi_PBlock            *pCurrentPB; 
244         int                     i, rc = LDAP_SUCCESS;
245         struct monitorinfo      *mi = ( struct monitorinfo * )be->be_private;
246
247         if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
248                 /*
249                  * LDAP_OTHER is returned if no plugins are installed
250                  */
251                 rc = LDAP_OTHER;
252                 goto done;
253         }
254
255         i = 0;
256         do {
257                 Slapi_PluginDesc        *srchdesc;
258                 char                    buf[ BACKMONITOR_BUFSIZE ];
259                 struct berval           bv;
260
261                 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
262                                 &srchdesc );
263                 if ( rc != LDAP_SUCCESS ) {
264                         goto done;
265                 }
266
267                 snprintf( buf, sizeof(buf),
268                                 "plugin %d name: %s; "
269                                 "vendor: %s; "
270                                 "version: %s; "
271                                 "description: %s", 
272                                 i,
273                                 srchdesc->spd_id,
274                                 srchdesc->spd_vendor,
275                                 srchdesc->spd_version,
276                                 srchdesc->spd_description );
277
278                 bv.bv_val = buf;
279                 bv.bv_len = strlen( buf );
280                 attr_merge_normalize_one( e_database,
281                                 mi->mi_ad_monitoredInfo, &bv, NULL );
282
283                 i++;
284
285         } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
286                         && ( pCurrentPB != NULL ) );
287
288 done:
289         return rc;
290 }
291 #endif /* defined(LDAP_SLAPI) */