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