]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
Sync with HEAD
[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-2003 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 int
36 monitor_subsys_database_init(
37         BackendDB       *be
38 )
39 {
40         struct monitorinfo      *mi;
41         Entry                   *e, *e_database, *e_tmp;
42         int                     i;
43         struct monitorentrypriv *mp;
44
45         assert( be != NULL );
46
47         mi = ( struct monitorinfo * )be->be_private;
48
49         if ( monitor_cache_get( mi, 
50                                 &monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn, 
51                                 &e_database ) ) {
52 #ifdef NEW_LOGGING
53                 LDAP_LOG( OPERATION, CRIT,
54                         "monitor_subsys_database_init: "
55                         "unable to get entry '%s'\n",
56                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0, 0 );
57 #else
58                 Debug( LDAP_DEBUG_ANY,
59                         "monitor_subsys_database_init: "
60                         "unable to get entry '%s'\n%s%s",
61                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 
62                         "", "" );
63 #endif
64                 return( -1 );
65         }
66
67         e_tmp = NULL;
68         for ( i = nBackendDB; i--; ) {
69                 char buf[ BACKMONITOR_BUFSIZE ];
70                 int j;
71
72                 be = &backendDB[i];
73
74                 /* Subordinates are not exposed as their own naming context */
75                 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
76                         continue;
77                 }
78
79                 snprintf( buf, sizeof( buf ),
80                                 "dn: cn=Database %d,%s\n"
81                                 "objectClass: %s\n"
82                                 "structuralObjectClass: %s\n"
83                                 "cn: Database %d\n"
84                                 "description: This object contains the type of the database.\n"
85                                 "%s: %s\n"
86                                 "createTimestamp: %s\n"
87                                 "modifyTimestamp: %s\n",
88                                 i,
89                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
90                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
91                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
92                                 i,
93                                 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
94                                 be->bd_info->bi_type,
95                                 mi->mi_startTime.bv_val,
96                                 mi->mi_startTime.bv_val );
97                 
98                 e = str2entry( buf );
99                 if ( e == NULL ) {
100 #ifdef NEW_LOGGING
101                         LDAP_LOG( OPERATION, CRIT,
102                                 "monitor_subsys_database_init: "
103                                 "unable to create entry 'cn=Database %d,%s'\n",
104                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
105 #else
106                         Debug( LDAP_DEBUG_ANY,
107                                 "monitor_subsys_database_init: "
108                                 "unable to create entry 'cn=Database %d,%s'\n%s",
109                                 i, 
110                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
111                                 "" );
112 #endif
113                         return( -1 );
114                 }
115                 
116                 if ( be->be_flags & SLAP_BFLAG_MONITOR ) {
117                         attr_merge( e, slap_schema.si_ad_monitorContext,
118                                         be->be_suffix, be->be_nsuffix );
119                         attr_merge( e_database, slap_schema.si_ad_monitorContext,
120                                         be->be_suffix, be->be_nsuffix );
121                 } else {
122                         attr_merge( e, slap_schema.si_ad_namingContexts,
123                                         be->be_suffix, be->be_nsuffix );
124                         attr_merge( e_database, slap_schema.si_ad_namingContexts,
125                                         be->be_suffix, be->be_nsuffix );
126                 }
127
128                 for ( j = nBackendInfo; j--; ) {
129                         if ( backendInfo[ j ].bi_type == be->bd_info->bi_type ) {
130                                 struct berval           bv;
131
132                                 snprintf( buf, sizeof( buf ), 
133                                         "cn=Backend %d,%s", 
134                                         j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
135                                 bv.bv_val = buf;
136                                 bv.bv_len = strlen( buf );
137                                 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
138                                                 &bv, NULL );
139                                 break;
140                         }
141                 }
142                 /* we must find it! */
143                 assert( j >= 0 );
144
145                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
146                 e->e_private = ( void * )mp;
147                 mp->mp_next = e_tmp;
148                 mp->mp_children = NULL;
149                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
150                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
151                         | MONITOR_F_SUB;
152
153                 if ( monitor_cache_add( mi, e ) ) {
154 #ifdef NEW_LOGGING
155                         LDAP_LOG( OPERATION, CRIT,
156                                 "monitor_subsys_database_init: "
157                                 "unable to add entry 'cn=Database %d,%s'\n",
158                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
159 #else
160                         Debug( LDAP_DEBUG_ANY,
161                                 "monitor_subsys_database_init: "
162                                 "unable to add entry 'cn=Database %d,%s'\n",
163                                 i, 
164                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
165                                 0 );
166 #endif
167                         return( -1 );
168                 }
169
170 #if defined(LDAP_SLAPI)
171                 monitor_back_add_plugin( be, e );
172 #endif /* defined(LDAP_SLAPI) */
173
174                 e_tmp = e;
175         }
176         
177         mp = ( struct monitorentrypriv * )e_database->e_private;
178         mp->mp_children = e_tmp;
179
180         monitor_cache_release( mi, e_database );
181
182         return( 0 );
183 }
184
185 #if defined(LDAP_SLAPI)
186 static int
187 monitor_back_add_plugin( Backend *be, Entry *e_database )
188 {
189         Slapi_PBlock            *pCurrentPB; 
190         int                     i, rc = LDAP_SUCCESS;
191         struct monitorinfo      *mi = ( struct monitorinfo * )be->be_private;
192
193         if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
194                 /*
195                  * LDAP_OTHER is returned if no plugins are installed
196                  */
197                 rc = LDAP_OTHER;
198                 goto done;
199         }
200
201         i = 0;
202         do {
203                 Slapi_PluginDesc        *srchdesc;
204                 char                    buf[ BACKMONITOR_BUFSIZE ];
205                 struct berval           bv;
206
207                 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
208                                 &srchdesc );
209                 if ( rc != LDAP_SUCCESS ) {
210                         goto done;
211                 }
212
213                 snprintf( buf, sizeof(buf),
214                                 "plugin %d name: %s; "
215                                 "vendor: %s; "
216                                 "version: %s; "
217                                 "description: %s", 
218                                 i,
219                                 srchdesc->spd_id,
220                                 srchdesc->spd_vendor,
221                                 srchdesc->spd_version,
222                                 srchdesc->spd_description );
223
224                 bv.bv_val = buf;
225                 bv.bv_len = strlen( buf );
226                 attr_merge_normalize_one( e_database,
227                                 mi->mi_ad_monitoredInfo, &bv, NULL );
228
229                 i++;
230
231         } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
232                         && ( pCurrentPB != NULL ) );
233
234 done:
235         return rc;
236 }
237 #endif /* defined(LDAP_SLAPI) */