]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/operation.c
66ab03b7e9213e02b110653f0b55364bb247e554
[openldap] / servers / slapd / back-monitor / operation.c
1 /* operation.c - deal with operation 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 #ifndef _BACK_MONITOR_H_
50 #define _BACK_MONITOR_H_
51
52 #include <ldap_pvt.h>
53 #include <ldap_pvt_thread.h>
54 #include <avl.h>
55 #include <slap.h>
56
57 LDAP_BEGIN_DECL
58
59 /*
60  * The cache maps DNs to Entries.
61 /*
62  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
63  * 
64  * This work has beed deveolped for the OpenLDAP Foundation 
65  * in the hope that it may be useful to the Open Source community, 
66  * but WITHOUT ANY WARRANTY.
67  * 
68  * Permission is granted to anyone to use this software for any purpose
69  * on any computer system, and to alter it and redistribute it, subject
70  * to the following restrictions:
71  * 
72  * 1. The author and SysNet s.n.c. are not responsible for the consequences
73  *    of use of this software, no matter how awful, even if they arise from
74  *    flaws in it.
75  * 
76  * 2. The origin of this software must not be misrepresented, either by
77  *    explicit claim or by omission.  Since few users ever read sources,
78  *    credits should appear in the documentation.
79  * 
80  * 3. Altered versions must be plainly marked as such, and must not be
81  *    misrepresented as being the original software.  Since few users
82  *    ever read sources, credits should appear in the documentation.
83  *    SysNet s.n.c. cannot be responsible for the consequences of the
84  *    alterations.
85  * 
86  * 4. This notice may not be removed or altered.
87  */
88
89 #include "portable.h"
90
91 #include <stdio.h>
92 #include <ac/string.h>
93
94 #include "slap.h"
95 #include "back-monitor.h"
96 #include "lber_pvt.h"
97
98 static struct berval 
99         bv_op[] = {
100                 BER_BVC( "Bind" ),
101                 BER_BVC( "Unbind" ),
102                 BER_BVC( "Add" ),
103                 BER_BVC( "Delete" ),
104                 BER_BVC( "Modrdn" ),
105                 BER_BVC( "Modify" ),
106                 BER_BVC( "Compare" ),
107                 BER_BVC( "Search" ),
108                 BER_BVC( "Abandon" ),
109                 BER_BVC( "Extended" ),
110                 { 0, NULL }
111         };
112
113 int
114 monitor_subsys_ops_init(
115         BackendDB               *be
116 )
117 {
118         struct monitorinfo      *mi;
119         
120         Entry                   *e, *e_tmp, *e_op;
121         struct monitorentrypriv *mp;
122         char                    buf[ BACKMONITOR_BUFSIZE ];
123         int                     i;
124
125         assert( be != NULL );
126
127         mi = ( struct monitorinfo * )be->be_private;
128
129         if ( monitor_cache_get( mi,
130                         &monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn, &e_op ) ) {
131 #ifdef NEW_LOGGING
132                 LDAP_LOG( OPERATION, CRIT,
133                         "monitor_subsys_ops_init: "
134                         "unable to get entry '%s'\n",
135                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0, 0 );
136 #else
137                 Debug( LDAP_DEBUG_ANY,
138                         "monitor_subsys_ops_init: "
139                         "unable to get entry '%s'\n%s%s",
140                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 
141                         "", "" );
142 #endif
143                 return( -1 );
144         }
145
146         e_tmp = NULL;
147
148         for ( i = SLAP_OP_LAST; i-- > 0; ) {
149
150                 /*
151                  * Initiated ops
152                  */
153                 snprintf( buf, sizeof( buf ),
154                                 "dn: cn=%s,%s\n"
155                                 "objectClass: %s\n"
156                                 "structuralObjectClass: %s\n"
157                                 "cn: %s\n"
158                                 "%s: 0\n"
159                                 "%s: 0\n"
160                                 "createTimestamp: %s\n"
161                                 "modifyTimestamp: %s\n",
162                                 bv_op[ i ].bv_val,
163                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_dn.bv_val,
164                                 mi->mi_oc_monitorOperation->soc_cname.bv_val,
165                                 mi->mi_oc_monitorOperation->soc_cname.bv_val,
166                                 bv_op[ i ].bv_val,
167                                 mi->mi_ad_monitorOpInitiated->ad_cname.bv_val,
168                                 mi->mi_ad_monitorOpCompleted->ad_cname.bv_val,
169                                 mi->mi_startTime.bv_val,
170                                 mi->mi_startTime.bv_val );
171
172                 e = str2entry( buf );
173                 if ( e == NULL ) {
174 #ifdef NEW_LOGGING
175                         LDAP_LOG( OPERATION, CRIT,
176                                 "monitor_subsys_ops_init: "
177                                 "unable to create entry 'cn=%s,%s'\n",
178                                 bv_op[ i ].bv_val,
179                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
180 #else
181                         Debug( LDAP_DEBUG_ANY,
182                                 "monitor_subsys_ops_init: "
183                                 "unable to create entry 'cn=%s,%s'\n",
184                                 bv_op[ i ].bv_val,
185                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
186 #endif
187                         return( -1 );
188                 }
189         
190                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
191                 e->e_private = ( void * )mp;
192                 mp->mp_next = e_tmp;
193                 mp->mp_children = NULL;
194                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_OPS];
195                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_OPS].mss_flags \
196                         | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
197
198                 if ( monitor_cache_add( mi, e ) ) {
199 #ifdef NEW_LOGGING
200                         LDAP_LOG( OPERATION, CRIT,
201                                 "monitor_subsys_ops_init: "
202                                 "unable to add entry 'cn=%s,%s'\n",
203                                 bv_op[ i ].bv_val,
204                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
205 #else
206                         Debug( LDAP_DEBUG_ANY,
207                                 "monitor_subsys_ops_init: "
208                                 "unable to add entry 'cn=%s,%s'\n",
209                                 bv_op[ i ].bv_val,
210                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
211 #endif
212                         return( -1 );
213                 }
214         
215                 e_tmp = e;
216         }
217
218         mp = ( struct monitorentrypriv * )e_op->e_private;
219         mp->mp_children = e_tmp;
220
221         monitor_cache_release( mi, e_op );
222
223         return( 0 );
224 }
225
226 int
227 monitor_subsys_ops_update(
228         Operation               *op,
229         Entry                   *e
230 )
231 {
232         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
233         long            nInitiated = -1, nCompleted = -1;
234         char            *rdnvalue;
235         int             i;
236         Attribute       *a;
237         char            buf[] = "+9223372036854775807L";
238
239         assert( mi );
240         assert( e );
241
242         rdnvalue = e->e_dn + ( sizeof( "cn=" ) - 1 );
243
244         for (i = 0; i < SLAP_OP_LAST; i++ ) {
245                 if ( strncmp( rdnvalue, bv_op[ i ].bv_val, 
246                                         bv_op[ i ].bv_len ) == 0 ) {
247                         nInitiated = num_ops_initiated_[ i ];
248                         nCompleted = num_ops_completed_[ i ];
249                         break;
250                 }
251         }
252
253         if ( i == SLAP_OP_LAST ) {
254                 return( 0 );
255         }
256
257         a = attr_find( e->e_attrs, mi->mi_ad_monitorOpInitiated );
258         assert ( a != NULL );
259         snprintf( buf, sizeof( buf ), "%ld", nInitiated );
260         free( a->a_vals[ 0 ].bv_val );
261         ber_str2bv( buf, 0, 1, &a->a_vals[ 0 ] );
262
263         a = attr_find( e->e_attrs, mi->mi_ad_monitorOpCompleted );
264         assert ( a != NULL );
265         snprintf( buf, sizeof( buf ), "%ld", nCompleted );
266         free( a->a_vals[ 0 ].bv_val );
267         ber_str2bv( buf, 0, 1, &a->a_vals[ 0 ] );
268
269         return( 0 );
270 }
271