]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/operation.c
284224d378d4876edb6c10a2e3aa60f9c493826a
[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 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 #include "lber_pvt.h"
30
31 static struct berval 
32         bv_op[] = {
33                 BER_BVC( "Bind" ),
34                 BER_BVC( "Unbind" ),
35                 BER_BVC( "Add" ),
36                 BER_BVC( "Delete" ),
37                 BER_BVC( "Modrdn" ),
38                 BER_BVC( "Modify" ),
39                 BER_BVC( "Compare" ),
40                 BER_BVC( "Search" ),
41                 BER_BVC( "Abandon" ),
42                 BER_BVC( "Extended" ),
43                 BER_BVNULL
44         };
45
46 int
47 monitor_subsys_ops_init(
48         BackendDB               *be
49 )
50 {
51         struct monitorinfo      *mi;
52         
53         Entry                   *e, *e_tmp, *e_op;
54         struct monitorentrypriv *mp;
55         char                    buf[ BACKMONITOR_BUFSIZE ];
56         int                     i;
57         struct berval           bv_zero = BER_BVC("0");
58
59         assert( be != NULL );
60
61         mi = ( struct monitorinfo * )be->be_private;
62
63         if ( monitor_cache_get( mi,
64                         &monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn, &e_op ) ) {
65 #ifdef NEW_LOGGING
66                 LDAP_LOG( OPERATION, CRIT,
67                         "monitor_subsys_ops_init: "
68                         "unable to get entry '%s'\n",
69                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0, 0 );
70 #else
71                 Debug( LDAP_DEBUG_ANY,
72                         "monitor_subsys_ops_init: "
73                         "unable to get entry '%s'\n%s%s",
74                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 
75                         "", "" );
76 #endif
77                 return( -1 );
78         }
79
80         attr_merge_one( e_op, mi->mi_ad_monitorOpInitiated, &bv_zero, NULL );
81         attr_merge_one( e_op, mi->mi_ad_monitorOpCompleted, &bv_zero, NULL );
82
83         e_tmp = NULL;
84
85         for ( i = SLAP_OP_LAST; i-- > 0; ) {
86
87                 /*
88                  * Initiated ops
89                  */
90                 snprintf( buf, sizeof( buf ),
91                                 "dn: cn=%s,%s\n"
92                                 "objectClass: %s\n"
93                                 "structuralObjectClass: %s\n"
94                                 "cn: %s\n"
95                                 "%s: 0\n"
96                                 "%s: 0\n"
97                                 "creatorsName: %s\n"
98                                 "modifiersName: %s\n"
99                                 "createTimestamp: %s\n"
100                                 "modifyTimestamp: %s\n",
101                                 bv_op[ i ].bv_val,
102                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_dn.bv_val,
103                                 mi->mi_oc_monitorOperation->soc_cname.bv_val,
104                                 mi->mi_oc_monitorOperation->soc_cname.bv_val,
105                                 bv_op[ i ].bv_val,
106                                 mi->mi_ad_monitorOpInitiated->ad_cname.bv_val,
107                                 mi->mi_ad_monitorOpCompleted->ad_cname.bv_val,
108                                 mi->mi_creatorsName.bv_val,
109                                 mi->mi_creatorsName.bv_val,
110                                 mi->mi_startTime.bv_val,
111                                 mi->mi_startTime.bv_val );
112
113                 e = str2entry( buf );
114                 if ( e == NULL ) {
115 #ifdef NEW_LOGGING
116                         LDAP_LOG( OPERATION, CRIT,
117                                 "monitor_subsys_ops_init: "
118                                 "unable to create entry 'cn=%s,%s'\n",
119                                 bv_op[ i ].bv_val,
120                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
121 #else
122                         Debug( LDAP_DEBUG_ANY,
123                                 "monitor_subsys_ops_init: "
124                                 "unable to create entry 'cn=%s,%s'\n",
125                                 bv_op[ i ].bv_val,
126                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
127 #endif
128                         return( -1 );
129                 }
130         
131                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
132                 e->e_private = ( void * )mp;
133                 mp->mp_next = e_tmp;
134                 mp->mp_children = NULL;
135                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_OPS];
136                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_OPS].mss_flags \
137                         | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
138
139                 if ( monitor_cache_add( mi, e ) ) {
140 #ifdef NEW_LOGGING
141                         LDAP_LOG( OPERATION, CRIT,
142                                 "monitor_subsys_ops_init: "
143                                 "unable to add entry 'cn=%s,%s'\n",
144                                 bv_op[ i ].bv_val,
145                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
146 #else
147                         Debug( LDAP_DEBUG_ANY,
148                                 "monitor_subsys_ops_init: "
149                                 "unable to add entry 'cn=%s,%s'\n",
150                                 bv_op[ i ].bv_val,
151                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
152 #endif
153                         return( -1 );
154                 }
155         
156                 e_tmp = e;
157         }
158
159         mp = ( struct monitorentrypriv * )e_op->e_private;
160         mp->mp_children = e_tmp;
161
162         monitor_cache_release( mi, e_op );
163
164         return( 0 );
165 }
166
167 int
168 monitor_subsys_ops_update(
169         Operation               *op,
170         Entry                   *e
171 )
172 {
173         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
174         long            nInitiated = 0, nCompleted = 0;
175         char            *rdnvalue;
176         int             i;
177         Attribute       *a;
178         char            buf[] = "+9223372036854775807L";
179
180         assert( mi );
181         assert( e );
182
183         rdnvalue = e->e_dn + ( sizeof( "cn=" ) - 1 );
184
185         if ( strncmp( rdnvalue, SLAPD_MONITOR_OPS_NAME,
186                                 sizeof( SLAPD_MONITOR_OPS_NAME ) - 1 ) == 0 )
187         {
188                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
189                         nInitiated += num_ops_initiated_[ i ];
190                         nCompleted += num_ops_completed_[ i ];
191                 }
192                 
193         } else {
194                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
195                         if ( strncmp( rdnvalue, bv_op[ i ].bv_val, 
196                                                 bv_op[ i ].bv_len ) == 0 )
197                         {
198                                 nInitiated = num_ops_initiated_[ i ];
199                                 nCompleted = num_ops_completed_[ i ];
200                                 break;
201                         }
202                 }
203
204                 if ( i == SLAP_OP_LAST ) {
205                         /* not found ... */
206                         return( 0 );
207                 }
208         }
209
210         a = attr_find( e->e_attrs, mi->mi_ad_monitorOpInitiated );
211         assert ( a != NULL );
212         snprintf( buf, sizeof( buf ), "%ld", nInitiated );
213         free( a->a_vals[ 0 ].bv_val );
214         ber_str2bv( buf, 0, 1, &a->a_vals[ 0 ] );
215
216         a = attr_find( e->e_attrs, mi->mi_ad_monitorOpCompleted );
217         assert ( a != NULL );
218         snprintf( buf, sizeof( buf ), "%ld", nCompleted );
219         free( a->a_vals[ 0 ].bv_val );
220         ber_str2bv( buf, 0, 1, &a->a_vals[ 0 ] );
221
222         return( 0 );
223 }
224