]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/sent.c
2e17b84d9b5d1c225ddbab14b80fa3302bae33a8
[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;
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.bv_val = "0";
103         bv.bv_len = 1;
104         attr_merge_one( e, monitor_ad_desc, &bv, NULL );
105         
106         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
107         e->e_private = ( void * )mp;
108         mp->mp_next = e_tmp;
109         mp->mp_children = NULL;
110         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
111         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
112                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
113
114         if ( monitor_cache_add( mi, e ) ) {
115 #ifdef NEW_LOGGING
116                 LDAP_LOG( OPERATION, CRIT,
117                         "monitor_subsys_sent_init: "
118                         "unable to add entry 'cn=Entries,%s'\n",
119                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
120 #else
121                 Debug( LDAP_DEBUG_ANY,
122                         "monitor_subsys_sent_init: "
123                         "unable to add entry 'cn=Entries,%s'\n%s%s",
124                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
125                         "", "" );
126 #endif
127                 return( -1 );
128         }
129         
130         e_tmp = e;
131
132         /*
133          * Referrals
134          */
135         snprintf( buf, sizeof( buf ),
136                         "dn: cn=Referrals,%s\n"
137                         SLAPD_MONITOR_OBJECTCLASSES
138                         "cn: Referrals\n",
139                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val );
140
141         e = str2entry( buf );
142         if ( e == NULL ) {
143 #ifdef NEW_LOGGING
144                 LDAP_LOG( OPERATION, CRIT,
145                         "monitor_subsys_sent_init: "
146                         "unable to create entry 'cn=Referrals,%s'\n",
147                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
148 #else
149                 Debug( LDAP_DEBUG_ANY,
150                         "monitor_subsys_sent_init: "
151                         "unable to create entry 'cn=Referrals,%s'\n%s%s",
152                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
153                         "", "" );
154 #endif
155                 return( -1 );
156         }
157
158         bv.bv_val = "0";
159         bv.bv_len = 1;
160         attr_merge_one( e, monitor_ad_desc, &bv, NULL );
161         
162         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
163         e->e_private = ( void * )mp;
164         mp->mp_next = e_tmp;
165         mp->mp_children = NULL;
166         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
167         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
168                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
169
170         if ( monitor_cache_add( mi, e ) ) {
171 #ifdef NEW_LOGGING
172                 LDAP_LOG( OPERATION, CRIT,
173                         "monitor_subsys_sent_init: "
174                         "unable to add entry 'cn=Referrals,%s'\n",
175                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
176 #else
177                 Debug( LDAP_DEBUG_ANY,
178                         "monitor_subsys_sent_init: "
179                         "unable to add entry 'cn=Referrals,%s'\n%s%s",
180                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
181                         "", "" );
182 #endif
183                 return( -1 );
184         }
185         
186         e_tmp = e;
187
188         /*
189          * PDU
190          */
191         snprintf( buf, sizeof( buf ),
192                         "dn: cn=PDU,%s\n"
193                         SLAPD_MONITOR_OBJECTCLASSES
194                         "cn: PDU\n",
195                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val );
196
197         e = str2entry( buf );
198         if ( e == NULL ) {
199 #ifdef NEW_LOGGING
200                 LDAP_LOG( OPERATION, CRIT,
201                         "monitor_subsys_sent_init: "
202                         "unable to create entry 'cn=PDU,%s'\n",
203                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
204 #else
205                 Debug( LDAP_DEBUG_ANY,
206                         "monitor_subsys_sent_init: "
207                         "unable to create entry 'cn=PDU,%s'\n%s%s",
208                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
209                         "", "" );
210 #endif
211                 return( -1 );
212         }
213
214         bv.bv_val = "0";
215         bv.bv_len = 1;
216         attr_merge_one( e, monitor_ad_desc, &bv, NULL );
217         
218         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
219         e->e_private = ( void * )mp;
220         mp->mp_next = e_tmp;
221         mp->mp_children = NULL;
222         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
223         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
224                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
225
226         if ( monitor_cache_add( mi, e ) ) {
227 #ifdef NEW_LOGGING
228                 LDAP_LOG( OPERATION, CRIT,
229                         "monitor_subsys_sent_init: "
230                         "unable to add entry 'cn=PDU,%s'\n",
231                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
232 #else
233                 Debug( LDAP_DEBUG_ANY,
234                         "monitor_subsys_sent_init: "
235                         "unable to add entry 'cn=PDU,%s'\n%s%s",
236                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
237                         "", "" );
238 #endif
239                 return( -1 );
240         }
241         
242         e_tmp = e;
243
244         /*
245          * Bytes
246          */
247         snprintf( buf, sizeof( buf ),
248                         "dn: cn=Bytes,%s\n"
249                         SLAPD_MONITOR_OBJECTCLASSES
250                         "cn: Bytes\n",
251                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val );
252
253         e = str2entry( buf );
254         if ( e == NULL ) {
255 #ifdef NEW_LOGGING
256                 LDAP_LOG( OPERATION, CRIT,
257                         "monitor_subsys_sent_init: "
258                         "unable to create entry 'cn=Bytes,%s'\n",
259                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
260 #else
261                 Debug( LDAP_DEBUG_ANY,
262                         "monitor_subsys_sent_init: "
263                         "unable to create entry 'cn=Bytes,%s'\n%s%s",
264                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
265                         "", "" );
266 #endif
267                 return( -1 );
268         }
269
270         bv.bv_val = "0";
271         bv.bv_len = 1;
272         attr_merge_one( e, monitor_ad_desc, &bv, NULL );
273         
274         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
275         e->e_private = ( void * )mp;
276         mp->mp_next = e_tmp;
277         mp->mp_children = NULL;
278         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
279         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
280                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
281
282         if ( monitor_cache_add( mi, e ) ) {
283 #ifdef NEW_LOGGING
284                 LDAP_LOG( OPERATION, CRIT,
285                         "monitor_subsys_sent_init: "
286                         "unable to add entry 'cn=Bytes,%s'\n",
287                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
288 #else
289                 Debug( LDAP_DEBUG_ANY,
290                         "monitor_subsys_sent_init: "
291                         "unable to add entry 'cn=Bytes,%s'\n%s%s",
292                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
293                         "", "" );
294 #endif
295                 return( -1 );
296         }
297         
298         e_tmp = e;
299
300         mp = ( struct monitorentrypriv * )e_sent->e_private;
301         mp->mp_children = e_tmp;
302
303         monitor_cache_release( mi, e_sent );
304
305         return( 0 );
306 }
307
308 int
309 monitor_subsys_sent_update(
310         Operation               *op,
311         Entry                   *e
312 )
313 {
314         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
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