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