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