]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/sent.c
Fix LBER_ERROR vs. -1 confusion.
[openldap] / servers / slapd / back-monitor / sent.c
1 /* sent.c - deal with data sent subsystem */
2 /*
3  * Copyright 1998-2003 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_sent_init(
43         BackendDB               *be
44 )
45 {
46         struct monitorinfo      *mi;
47         
48         Entry                   *e, *e_tmp, *e_sent;
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_SENT].mss_ndn, &e_sent ) ) {
59 #ifdef NEW_LOGGING
60                 LDAP_LOG( OPERATION, CRIT,
61                         "monitor_subsys_sent_init: "
62                         "unable to get entry '%s'\n",
63                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
64 #else
65                 Debug( LDAP_DEBUG_ANY,
66                         "monitor_subsys_sent_init: "
67                         "unable to get entry '%s'\n%s%s",
68                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 
69                         "", "" );
70 #endif
71                 return( -1 );
72         }
73
74         e_tmp = NULL;
75
76         /*
77          * Entries
78          */
79         snprintf( buf, sizeof( buf ),
80                         "dn: cn=Entries,%s\n"
81                         SLAPD_MONITOR_OBJECTCLASSES
82                         "cn: Entries\n",
83                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val );
84
85         e = str2entry( buf );
86         if ( e == NULL ) {
87 #ifdef NEW_LOGGING
88                 LDAP_LOG( OPERATION, CRIT,
89                         "monitor_subsys_sent_init: "
90                         "unable to create entry 'cn=Entries,%s'\n",
91                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
92 #else
93                 Debug( LDAP_DEBUG_ANY,
94                         "monitor_subsys_sent_init: "
95                         "unable to create entry 'cn=Entries,%s'\n%s%s",
96                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
97                         "", "" );
98 #endif
99                 return( -1 );
100         }
101         
102         bv[1].bv_val = NULL;
103         bv[0].bv_val = "0";
104         bv[0].bv_len = 1;
105         attr_merge( e, monitor_ad_desc, bv );
106         
107         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
108         e->e_private = ( void * )mp;
109         mp->mp_next = e_tmp;
110         mp->mp_children = NULL;
111         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
112         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
113                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
114
115         if ( monitor_cache_add( mi, e ) ) {
116 #ifdef NEW_LOGGING
117                 LDAP_LOG( OPERATION, CRIT,
118                         "monitor_subsys_sent_init: "
119                         "unable to add entry 'cn=Entries,%s'\n",
120                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
121 #else
122                 Debug( LDAP_DEBUG_ANY,
123                         "monitor_subsys_sent_init: "
124                         "unable to add entry 'cn=Entries,%s'\n%s%s",
125                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
126                         "", "" );
127 #endif
128                 return( -1 );
129         }
130         
131         e_tmp = e;
132
133         /*
134          * Referrals
135          */
136         snprintf( buf, sizeof( buf ),
137                         "dn: cn=Referrals,%s\n"
138                         SLAPD_MONITOR_OBJECTCLASSES
139                         "cn: Referrals\n",
140                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val );
141
142         e = str2entry( buf );
143         if ( e == NULL ) {
144 #ifdef NEW_LOGGING
145                 LDAP_LOG( OPERATION, CRIT,
146                         "monitor_subsys_sent_init: "
147                         "unable to create entry 'cn=Referrals,%s'\n",
148                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
149 #else
150                 Debug( LDAP_DEBUG_ANY,
151                         "monitor_subsys_sent_init: "
152                         "unable to create entry 'cn=Referrals,%s'\n%s%s",
153                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
154                         "", "" );
155 #endif
156                 return( -1 );
157         }
158
159         bv[0].bv_val = "0";
160         bv[0].bv_len = 1;
161         attr_merge( e, monitor_ad_desc, bv );
162         
163         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
164         e->e_private = ( void * )mp;
165         mp->mp_next = e_tmp;
166         mp->mp_children = NULL;
167         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
168         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
169                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
170
171         if ( monitor_cache_add( mi, e ) ) {
172 #ifdef NEW_LOGGING
173                 LDAP_LOG( OPERATION, CRIT,
174                         "monitor_subsys_sent_init: "
175                         "unable to add entry 'cn=Referrals,%s'\n",
176                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
177 #else
178                 Debug( LDAP_DEBUG_ANY,
179                         "monitor_subsys_sent_init: "
180                         "unable to add entry 'cn=Referrals,%s'\n%s%s",
181                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
182                         "", "" );
183 #endif
184                 return( -1 );
185         }
186         
187         e_tmp = e;
188
189         /*
190          * PDU
191          */
192         snprintf( buf, sizeof( buf ),
193                         "dn: cn=PDU,%s\n"
194                         SLAPD_MONITOR_OBJECTCLASSES
195                         "cn: PDU\n",
196                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val );
197
198         e = str2entry( buf );
199         if ( e == NULL ) {
200 #ifdef NEW_LOGGING
201                 LDAP_LOG( OPERATION, CRIT,
202                         "monitor_subsys_sent_init: "
203                         "unable to create entry 'cn=PDU,%s'\n",
204                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
205 #else
206                 Debug( LDAP_DEBUG_ANY,
207                         "monitor_subsys_sent_init: "
208                         "unable to create entry 'cn=PDU,%s'\n%s%s",
209                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
210                         "", "" );
211 #endif
212                 return( -1 );
213         }
214
215         bv[0].bv_val = "0";
216         bv[0].bv_len = 1;
217         attr_merge( e, monitor_ad_desc, bv );
218         
219         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
220         e->e_private = ( void * )mp;
221         mp->mp_next = e_tmp;
222         mp->mp_children = NULL;
223         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
224         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
225                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
226
227         if ( monitor_cache_add( mi, e ) ) {
228 #ifdef NEW_LOGGING
229                 LDAP_LOG( OPERATION, CRIT,
230                         "monitor_subsys_sent_init: "
231                         "unable to add entry 'cn=PDU,%s'\n",
232                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
233 #else
234                 Debug( LDAP_DEBUG_ANY,
235                         "monitor_subsys_sent_init: "
236                         "unable to add entry 'cn=PDU,%s'\n%s%s",
237                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
238                         "", "" );
239 #endif
240                 return( -1 );
241         }
242         
243         e_tmp = e;
244
245         /*
246          * Bytes
247          */
248         snprintf( buf, sizeof( buf ),
249                         "dn: cn=Bytes,%s\n"
250                         SLAPD_MONITOR_OBJECTCLASSES
251                         "cn: Bytes\n",
252                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val );
253
254         e = str2entry( buf );
255         if ( e == NULL ) {
256 #ifdef NEW_LOGGING
257                 LDAP_LOG( OPERATION, CRIT,
258                         "monitor_subsys_sent_init: "
259                         "unable to create entry 'cn=Bytes,%s'\n",
260                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
261 #else
262                 Debug( LDAP_DEBUG_ANY,
263                         "monitor_subsys_sent_init: "
264                         "unable to create entry 'cn=Bytes,%s'\n%s%s",
265                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
266                         "", "" );
267 #endif
268                 return( -1 );
269         }
270
271         bv[0].bv_val = "0";
272         bv[0].bv_len = 1;
273         attr_merge( e, monitor_ad_desc, bv );
274         
275         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
276         e->e_private = ( void * )mp;
277         mp->mp_next = e_tmp;
278         mp->mp_children = NULL;
279         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
280         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
281                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
282
283         if ( monitor_cache_add( mi, e ) ) {
284 #ifdef NEW_LOGGING
285                 LDAP_LOG( OPERATION, CRIT,
286                         "monitor_subsys_sent_init: "
287                         "unable to add entry 'cn=Bytes,%s'\n",
288                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
289 #else
290                 Debug( LDAP_DEBUG_ANY,
291                         "monitor_subsys_sent_init: "
292                         "unable to add entry 'cn=Bytes,%s'\n%s%s",
293                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
294                         "", "" );
295 #endif
296                 return( -1 );
297         }
298         
299         e_tmp = e;
300
301         mp = ( struct monitorentrypriv * )e_sent->e_private;
302         mp->mp_children = e_tmp;
303
304         monitor_cache_release( mi, e_sent );
305
306         return( 0 );
307 }
308
309 int
310 monitor_subsys_sent_update(
311         struct monitorinfo      *mi,
312         Entry                   *e
313 )
314 {
315         long            n = -1;
316
317         assert( mi );
318         assert( e );
319         
320         if ( strncasecmp( e->e_ndn, "cn=entries", 
321                                 sizeof("cn=entries")-1 ) == 0 ) {
322                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
323                 n = num_entries_sent;
324                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
325
326         } else if ( strncasecmp( e->e_ndn, "cn=referrals", 
327                                 sizeof("cn=referrals")-1 ) == 0 ) {
328                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
329                 n = num_refs_sent;
330                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
331
332         } else if ( strncasecmp( e->e_ndn, "cn=pdu", 
333                                 sizeof("cn=pdu")-1 ) == 0 ) {
334                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
335                 n = num_pdu_sent;
336                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
337
338         } else if ( strncasecmp( e->e_ndn, "cn=bytes", 
339                                 sizeof("cn=bytes")-1 ) == 0 ) {
340                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
341                 n = num_bytes_sent;
342                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
343         }
344
345         if ( n != -1 ) {
346                 Attribute       *a;
347                 char            buf[16];
348
349                 a = attr_find( e->e_attrs, monitor_ad_desc );
350                 if ( a == NULL ) {
351                         return( -1 );
352                 }
353
354                 snprintf( buf, sizeof( buf ), "%ld", n );
355                 free( a->a_vals[ 0 ].bv_val );
356                 ber_str2bv( buf, 0, 1, a->a_vals );
357         }
358
359         return( 0 );
360 }
361