]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/sent.c
unifdef -UNEW_LOGGING
[openldap] / servers / slapd / back-monitor / sent.c
1 /* sent.c - deal with data sent subsystem */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26
27 #include "slap.h"
28 #include "back-monitor.h"
29
30 int
31 monitor_subsys_sent_init(
32         BackendDB               *be
33 )
34 {
35         struct monitorinfo      *mi;
36         
37         Entry                   *e, *e_tmp, *e_sent;
38         struct monitorentrypriv *mp;
39         char                    buf[ BACKMONITOR_BUFSIZE ];
40         struct berval           bv;
41
42         assert( be != NULL );
43
44         mi = ( struct monitorinfo * )be->be_private;
45
46         if ( monitor_cache_get( mi,
47                         &monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn, &e_sent ) ) {
48                 Debug( LDAP_DEBUG_ANY,
49                         "monitor_subsys_sent_init: "
50                         "unable to get entry '%s'\n%s%s",
51                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 
52                         "", "" );
53                 return( -1 );
54         }
55
56         e_tmp = NULL;
57
58         /*
59          * Entries
60          */
61         snprintf( buf, sizeof( buf ),
62                         "dn: cn=Entries,%s\n"
63                         "objectClass: %s\n"
64                         "structuralObjectClass: %s\n"
65                         "cn: Entries\n"
66                         "creatorsName: %s\n"
67                         "modifiersName: %s\n"
68                         "createTimestamp: %s\n"
69                         "modifyTimestamp: %s\n",
70                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val,
71                         mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
72                         mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
73                         mi->mi_creatorsName.bv_val,
74                         mi->mi_creatorsName.bv_val,
75                         mi->mi_startTime.bv_val,
76                         mi->mi_startTime.bv_val );
77
78         e = str2entry( buf );
79         if ( e == NULL ) {
80                 Debug( LDAP_DEBUG_ANY,
81                         "monitor_subsys_sent_init: "
82                         "unable to create entry 'cn=Entries,%s'\n%s%s",
83                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
84                         "", "" );
85                 return( -1 );
86         }
87         
88         bv.bv_val = "0";
89         bv.bv_len = 1;
90         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
91         
92         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
93         e->e_private = ( void * )mp;
94         mp->mp_next = e_tmp;
95         mp->mp_children = NULL;
96         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
97         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
98                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
99
100         if ( monitor_cache_add( mi, e ) ) {
101                 Debug( LDAP_DEBUG_ANY,
102                         "monitor_subsys_sent_init: "
103                         "unable to add entry 'cn=Entries,%s'\n%s%s",
104                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
105                         "", "" );
106                 return( -1 );
107         }
108         
109         e_tmp = e;
110
111         /*
112          * Referrals
113          */
114         snprintf( buf, sizeof( buf ),
115                         "dn: cn=Referrals,%s\n"
116                         "objectClass: %s\n"
117                         "structuralObjectClass: %s\n"
118                         "cn: Referrals\n"
119                         "creatorsName: %s\n"
120                         "modifiersName: %s\n"
121                         "createTimestamp: %s\n"
122                         "modifyTimestamp: %s\n",
123                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val,
124                         mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
125                         mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
126                         mi->mi_creatorsName.bv_val,
127                         mi->mi_creatorsName.bv_val,
128                         mi->mi_startTime.bv_val,
129                         mi->mi_startTime.bv_val );
130
131         e = str2entry( buf );
132         if ( e == NULL ) {
133                 Debug( LDAP_DEBUG_ANY,
134                         "monitor_subsys_sent_init: "
135                         "unable to create entry 'cn=Referrals,%s'\n%s%s",
136                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
137                         "", "" );
138                 return( -1 );
139         }
140
141         bv.bv_val = "0";
142         bv.bv_len = 1;
143         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
144         
145         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
146         e->e_private = ( void * )mp;
147         mp->mp_next = e_tmp;
148         mp->mp_children = NULL;
149         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
150         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
151                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
152
153         if ( monitor_cache_add( mi, e ) ) {
154                 Debug( LDAP_DEBUG_ANY,
155                         "monitor_subsys_sent_init: "
156                         "unable to add entry 'cn=Referrals,%s'\n%s%s",
157                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
158                         "", "" );
159                 return( -1 );
160         }
161         
162         e_tmp = e;
163
164         /*
165          * PDU
166          */
167         snprintf( buf, sizeof( buf ),
168                         "dn: cn=PDU,%s\n"
169                         "objectClass: %s\n"
170                         "structuralObjectClass: %s\n"
171                         "cn: PDU\n"
172                         "creatorsName: %s\n"
173                         "modifiersName: %s\n"
174                         "createTimestamp: %s\n"
175                         "modifyTimestamp: %s\n",
176                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val,
177                         mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
178                         mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
179                         mi->mi_creatorsName.bv_val,
180                         mi->mi_creatorsName.bv_val,
181                         mi->mi_startTime.bv_val,
182                         mi->mi_startTime.bv_val );
183
184         e = str2entry( buf );
185         if ( e == NULL ) {
186                 Debug( LDAP_DEBUG_ANY,
187                         "monitor_subsys_sent_init: "
188                         "unable to create entry 'cn=PDU,%s'\n%s%s",
189                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
190                         "", "" );
191                 return( -1 );
192         }
193
194         bv.bv_val = "0";
195         bv.bv_len = 1;
196         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
197         
198         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
199         e->e_private = ( void * )mp;
200         mp->mp_next = e_tmp;
201         mp->mp_children = NULL;
202         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
203         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
204                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
205
206         if ( monitor_cache_add( mi, e ) ) {
207                 Debug( LDAP_DEBUG_ANY,
208                         "monitor_subsys_sent_init: "
209                         "unable to add entry 'cn=PDU,%s'\n%s%s",
210                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
211                         "", "" );
212                 return( -1 );
213         }
214         
215         e_tmp = e;
216
217         /*
218          * Bytes
219          */
220         snprintf( buf, sizeof( buf ),
221                         "dn: cn=Bytes,%s\n"
222                         "objectClass: %s\n"
223                         "structuralObjectClass: %s\n"
224                         "cn: Bytes\n"
225                         "creatorsName: %s\n"
226                         "modifiersName: %s\n"
227                         "createTimestamp: %s\n"
228                         "modifyTimestamp: %s\n",
229                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val,
230                         mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
231                         mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
232                         mi->mi_creatorsName.bv_val,
233                         mi->mi_creatorsName.bv_val,
234                         mi->mi_startTime.bv_val,
235                         mi->mi_startTime.bv_val );
236
237         e = str2entry( buf );
238         if ( e == NULL ) {
239                 Debug( LDAP_DEBUG_ANY,
240                         "monitor_subsys_sent_init: "
241                         "unable to create entry 'cn=Bytes,%s'\n%s%s",
242                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
243                         "", "" );
244                 return( -1 );
245         }
246
247         bv.bv_val = "0";
248         bv.bv_len = 1;
249         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
250         
251         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
252         e->e_private = ( void * )mp;
253         mp->mp_next = e_tmp;
254         mp->mp_children = NULL;
255         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
256         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
257                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
258
259         if ( monitor_cache_add( mi, e ) ) {
260                 Debug( LDAP_DEBUG_ANY,
261                         "monitor_subsys_sent_init: "
262                         "unable to add entry 'cn=Bytes,%s'\n%s%s",
263                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
264                         "", "" );
265                 return( -1 );
266         }
267         
268         e_tmp = e;
269
270         mp = ( struct monitorentrypriv * )e_sent->e_private;
271         mp->mp_children = e_tmp;
272
273         monitor_cache_release( mi, e_sent );
274
275         return( 0 );
276 }
277
278 int
279 monitor_subsys_sent_update(
280         Operation               *op,
281         Entry                   *e
282 )
283 {
284         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
285         long            n = -1;
286
287         assert( mi );
288         assert( e );
289         
290         if ( strncasecmp( e->e_ndn, "cn=entries", 
291                                 sizeof("cn=entries")-1 ) == 0 ) {
292                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
293                 n = num_entries_sent;
294                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
295
296         } else if ( strncasecmp( e->e_ndn, "cn=referrals", 
297                                 sizeof("cn=referrals")-1 ) == 0 ) {
298                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
299                 n = num_refs_sent;
300                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
301
302         } else if ( strncasecmp( e->e_ndn, "cn=pdu", 
303                                 sizeof("cn=pdu")-1 ) == 0 ) {
304                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
305                 n = num_pdu_sent;
306                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
307
308         } else if ( strncasecmp( e->e_ndn, "cn=bytes", 
309                                 sizeof("cn=bytes")-1 ) == 0 ) {
310                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
311                 n = num_bytes_sent;
312                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
313         }
314
315         if ( n != -1 ) {
316                 Attribute       *a;
317                 char            buf[] = "+9223372036854775807L";
318
319                 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter);
320                 if ( a == NULL ) {
321                         return( -1 );
322                 }
323
324                 snprintf( buf, sizeof( buf ), "%ld", n );
325                 free( a->a_vals[ 0 ].bv_val );
326                 ber_str2bv( buf, 0, 1, &a->a_vals[ 0 ] );
327         }
328
329         return( 0 );
330 }
331