]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/operation.c
73ae9f83e483c1361ac4c54b76423a2ece011918
[openldap] / servers / slapd / back-monitor / operation.c
1 /* operation.c - deal with operation subsystem */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
9  * This work has beed deveolped for the OpenLDAP Foundation 
10  * in the hope that it may be useful to the Open Source community, 
11  * but WITHOUT ANY WARRANTY.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author and SysNet s.n.c. are not responsible for the consequences
18  *    of use of this software, no matter how awful, even if they arise from
19  *    flaws in it.
20  * 
21  * 2. The origin of this software must not be misrepresented, either by
22  *    explicit claim or by omission.  Since few users ever read sources,
23  *    credits should appear in the documentation.
24  * 
25  * 3. Altered versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.  Since few users
27  *    ever read sources, credits should appear in the documentation.
28  *    SysNet s.n.c. cannot be responsible for the consequences of the
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include "slap.h"
39 #include "back-monitor.h"
40
41 int
42 monitor_subsys_ops_init(
43         BackendDB               *be
44 )
45 {
46         struct monitorinfo      *mi;
47         
48         Entry                   *e, *e_tmp, *e_op;
49         struct monitorentrypriv *mp;
50         char                    buf[1024];
51         struct berval           bv[2];
52
53         assert( be != NULL );
54
55         mi = ( struct monitorinfo * )be->be_private;
56
57         if ( monitor_cache_get( mi,
58                         &monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn, &e_op ) ) {
59 #ifdef NEW_LOGGING
60                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
61                         "monitor_subsys_ops_init: "
62                         "unable to get entry '%s'\n",
63                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val ));
64 #else
65                 Debug( LDAP_DEBUG_ANY,
66                         "monitor_subsys_ops_init: "
67                         "unable to get entry '%s'\n%s%s",
68                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 
69                         "", "" );
70 #endif
71                 return( -1 );
72         }
73
74         e_tmp = NULL;
75
76         /*
77          * Initiated ops
78          */
79         snprintf( buf, sizeof( buf ),
80                         "dn: cn=Initiated,%s\n"
81                         "objectClass: top\n"
82                         "objectClass: LDAPsubEntry\n"
83 #ifdef SLAPD_MONITORSUBENTRY
84                         "objectClass: monitorSubEntry\n"
85 #else /* !SLAPD_MONITORSUBENTRY */
86                         "objectClass: extensibleObject\n"
87 #endif /* !SLAPD_MONITORSUBENTRY */
88                         "cn: Initiated\n",
89                         monitor_subsys[SLAPD_MONITOR_OPS].mss_dn.bv_val );
90
91         e = str2entry( buf );
92         if ( e == NULL ) {
93 #ifdef NEW_LOGGING
94                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
95                         "monitor_subsys_ops_init: "
96                         "unable to create entry 'cn=Initiated,%s'\n",
97                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val ));
98 #else
99                 Debug( LDAP_DEBUG_ANY,
100                         "monitor_subsys_ops_init: "
101                         "unable to create entry 'cn=Initiated,%s'\n%s%s",
102                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val,
103                         "", "" );
104 #endif
105                 return( -1 );
106         }
107         
108         bv[1].bv_val = NULL;
109         bv[0].bv_val = "0";
110         bv[0].bv_len = 1;
111         attr_merge( e, monitor_ad_desc, bv );
112         
113         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
114         e->e_private = ( void * )mp;
115         mp->mp_next = e_tmp;
116         mp->mp_children = NULL;
117         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_OPS];
118         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_OPS].mss_flags \
119                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
120
121         if ( monitor_cache_add( mi, e ) ) {
122 #ifdef NEW_LOGGING
123                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
124                         "monitor_subsys_ops_init: "
125                         "unable to add entry 'cn=Initiated,%s'\n",
126                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val ));
127 #else
128                 Debug( LDAP_DEBUG_ANY,
129                         "monitor_subsys_ops_init: "
130                         "unable to add entry 'cn=Initiated,%s'\n%s%s",
131                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val,
132                         "", "" );
133 #endif
134                 return( -1 );
135         }
136         
137         e_tmp = e;
138
139         /*
140          * Completed ops
141          */
142         snprintf( buf, sizeof( buf ),
143                         "dn: cn=Completed,%s\n"
144                         "objectClass: top\n"
145                         "objectClass: LDAPsubEntry\n"
146 #ifdef SLAPD_MONITORSUBENTRY
147                         "objectClass: monitorSubEntry\n"
148 #else /* !SLAPD_MONITORSUBENTRY */
149                         "objectClass: extensibleObject\n"
150 #endif /* !SLAPD_MONITORSUBENTRY */
151                         "cn: Completed\n",
152                         monitor_subsys[SLAPD_MONITOR_OPS].mss_dn.bv_val );
153
154         e = str2entry( buf );
155         if ( e == NULL ) {
156 #ifdef NEW_LOGGING
157                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
158                         "monitor_subsys_ops_init: "
159                         "unable to create entry 'cn=Completed,%s'\n",
160                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val ));
161 #else
162                 Debug( LDAP_DEBUG_ANY,
163                         "monitor_subsys_ops_init: "
164                         "unable to create entry 'cn=Completed,%s'\n%s%s",
165                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val,
166                         "", "" );
167 #endif
168                 return( -1 );
169         }
170
171         bv[0].bv_val = "0";
172         bv[0].bv_len = 1;
173         attr_merge( e, monitor_ad_desc, bv );
174         
175         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
176         e->e_private = ( void * )mp;
177         mp->mp_next = e_tmp;
178         mp->mp_children = NULL;
179         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_OPS];
180         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_OPS].mss_flags \
181                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
182
183         if ( monitor_cache_add( mi, e ) ) {
184 #ifdef NEW_LOGGING
185                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
186                         "monitor_subsys_ops_init: "
187                         "unable to add entry 'cn=Completed,%s'\n",
188                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val ));
189 #else
190                 Debug( LDAP_DEBUG_ANY,
191                         "monitor_subsys_ops_init: "
192                         "unable to add entry 'cn=Completed,%s'\n%s%s",
193                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val,
194                         "", "" );
195 #endif
196                 return( -1 );
197         }
198         
199         e_tmp = e;
200
201         mp = ( struct monitorentrypriv * )e_op->e_private;
202         mp->mp_children = e_tmp;
203
204         monitor_cache_release( mi, e_op );
205
206         return( 0 );
207 }
208
209 int
210 monitor_subsys_ops_update(
211         struct monitorinfo      *mi,
212         Entry                   *e
213 )
214 {
215         long            n = -1;
216
217         assert( mi );
218         assert( e );
219         
220         if ( strncasecmp( e->e_ndn, "CN=INITIATED", 
221                                 sizeof("CN=INITIATED")-1 ) == 0 ) {
222                 ldap_pvt_thread_mutex_lock(&num_ops_mutex);
223                 n = num_ops_initiated;
224                 ldap_pvt_thread_mutex_unlock(&num_ops_mutex);
225
226         } else if ( strncasecmp( e->e_ndn, "CN=COMPLETED", 
227                                 sizeof("CN=COMPLETED")-1 ) == 0 ) {
228                 ldap_pvt_thread_mutex_lock(&num_ops_mutex);
229                 n = num_ops_completed;
230                 ldap_pvt_thread_mutex_unlock(&num_ops_mutex);
231         }
232
233         if ( n != -1 ) {
234                 Attribute       *a;
235                 char            buf[16];
236
237                 a = attr_find( e->e_attrs, monitor_ad_desc );
238                 if ( a == NULL ) {
239                         return( -1 );
240                 }
241
242                 snprintf( buf, sizeof( buf ), "%ld", n );
243                 free( a->a_vals[ 0 ].bv_val );
244                 ber_str2bv( buf, 0, 1, a->a_vals );
245         }
246
247         return( 0 );
248 }
249