]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/sent.c
f996f9ec558c154616db6d6e4b93d5df225c5352
[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 enum {
31         MONITOR_SENT_BYTES = 0,
32         MONITOR_SENT_PDU,
33         MONITOR_SENT_ENTRIES,
34         MONITOR_SENT_REFERRALS,
35
36         MONITOR_SENT_LAST
37 };
38
39 struct monitor_sent_t {
40         struct berval   rdn;
41         struct berval   nrdn;
42 } monitor_sent[] = {
43         { BER_BVC("cn=Bytes"),          BER_BVNULL },
44         { BER_BVC("cn=PDU"),            BER_BVNULL },
45         { BER_BVC("cn=Entries"),        BER_BVNULL },
46         { BER_BVC("cn=Referrals"),      BER_BVNULL },
47         { BER_BVNULL,                   BER_BVNULL }
48 };
49
50 int
51 monitor_subsys_sent_init(
52         BackendDB               *be
53 )
54 {
55         struct monitorinfo      *mi;
56         
57         Entry                   **ep, *e_sent;
58         struct monitorentrypriv *mp;
59         int                     i;
60
61         assert( be != NULL );
62
63         mi = ( struct monitorinfo * )be->be_private;
64
65         if ( monitor_cache_get( mi,
66                         &monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn, &e_sent ) ) {
67                 Debug( LDAP_DEBUG_ANY,
68                         "monitor_subsys_sent_init: "
69                         "unable to get entry \"%s\"\n",
70                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0, 0 );
71                 return( -1 );
72         }
73
74         mp = ( struct monitorentrypriv * )e_sent->e_private;
75         mp->mp_children = NULL;
76         ep = &mp->mp_children;
77
78         for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
79                 char                    buf[ BACKMONITOR_BUFSIZE ];
80                 struct berval           rdn, bv;
81                 Entry                   *e;
82
83                 snprintf( buf, sizeof( buf ),
84                                 "dn: %s,%s\n"
85                                 "objectClass: %s\n"
86                                 "structuralObjectClass: %s\n"
87                                 "cn: %s\n"
88                                 "creatorsName: %s\n"
89                                 "modifiersName: %s\n"
90                                 "createTimestamp: %s\n"
91                                 "modifyTimestamp: %s\n",
92                                 monitor_sent[i].rdn.bv_val,
93                                 monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val,
94                                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
95                                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
96                                 &monitor_sent[i].rdn.bv_val[STRLENOF( "cn=" )],
97                                 mi->mi_creatorsName.bv_val,
98                                 mi->mi_creatorsName.bv_val,
99                                 mi->mi_startTime.bv_val,
100                                 mi->mi_startTime.bv_val );
101
102                 e = str2entry( buf );
103                 if ( e == NULL ) {
104                         Debug( LDAP_DEBUG_ANY,
105                                 "monitor_subsys_sent_init: "
106                                 "unable to create entry \"%s,%s\"\n",
107                                 monitor_sent[i].rdn.bv_val,
108                                 monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0 );
109                         return( -1 );
110                 }
111
112                 /* steal normalized RDN */
113                 dnRdn( &e->e_nname, &rdn );
114                 ber_dupbv( &monitor_sent[i].nrdn, &rdn );
115         
116                 BER_BVSTR( &bv, "0" );
117                 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
118         
119                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
120                 e->e_private = ( void * )mp;
121                 mp->mp_next = NULL;
122                 mp->mp_children = NULL;
123                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
124                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
125                         | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
126
127                 if ( monitor_cache_add( mi, e ) ) {
128                         Debug( LDAP_DEBUG_ANY,
129                                 "monitor_subsys_sent_init: "
130                                 "unable to add entry \"%s,%s\"\n",
131                                 monitor_sent[i].rdn.bv_val,
132                                 monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val, 0 );
133                         return( -1 );
134                 }
135         
136                 *ep = e;
137                 ep = &mp->mp_next;
138         }
139
140         monitor_cache_release( mi, e_sent );
141
142         return( 0 );
143 }
144
145 int
146 monitor_subsys_sent_update(
147         Operation               *op,
148         Entry                   *e
149 )
150 {
151         struct monitorinfo      *mi = 
152                 (struct monitorinfo *)op->o_bd->be_private;
153         
154         struct berval           rdn;
155 #ifdef HAVE_GMP
156         mpz_t                   n;
157 #else /* ! HAVE_GMP */
158         unsigned long           n;
159 #endif /* ! HAVE_GMP */
160         Attribute               *a;
161         int                     i;
162
163         assert( mi );
164         assert( e );
165
166         dnRdn( &e->e_nname, &rdn );
167
168         for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
169                 if ( dn_match( &rdn, &monitor_sent[i].nrdn ) ) {
170                         break;
171                 }
172         }
173
174         if ( i == MONITOR_SENT_LAST ) {
175                 return 0;
176         }
177
178         ldap_pvt_thread_mutex_lock(&slap_counters.sc_sent_mutex);
179         switch ( i ) {
180         case MONITOR_SENT_ENTRIES:
181 #ifdef HAVE_GMP
182                 mpz_init_set( n, slap_counters.sc_entries );
183 #else /* ! HAVE_GMP */
184                 n = slap_counters.sc_entries;
185 #endif /* ! HAVE_GMP */
186                 break;
187
188         case MONITOR_SENT_REFERRALS:
189 #ifdef HAVE_GMP
190                 mpz_init_set( n, slap_counters.sc_refs );
191 #else /* ! HAVE_GMP */
192                 n = slap_counters.sc_refs;
193 #endif /* ! HAVE_GMP */
194                 break;
195
196         case MONITOR_SENT_PDU:
197 #ifdef HAVE_GMP
198                 mpz_init_set( n, slap_counters.sc_pdu );
199 #else /* ! HAVE_GMP */
200                 n = slap_counters.sc_pdu;
201 #endif /* ! HAVE_GMP */
202                 break;
203
204         case MONITOR_SENT_BYTES:
205 #ifdef HAVE_GMP
206                 mpz_init_set( n, slap_counters.sc_bytes );
207 #else /* ! HAVE_GMP */
208                 n = slap_counters.sc_bytes;
209 #endif /* ! HAVE_GMP */
210                 break;
211
212         default:
213                 assert(0);
214         }
215         ldap_pvt_thread_mutex_unlock(&slap_counters.sc_sent_mutex);
216         
217         a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
218         assert( a );
219
220         /* NOTE: no minus sign is allowed in the counters... */
221         UI2BV( &a->a_vals[ 0 ], n );
222 #ifdef HAVE_GMP
223         mpz_clear( n );
224 #endif /* HAVE_GMP */
225
226         return 0;
227 }
228