]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/sent.c
3d8b9a0ded54294819549eb1531f6e22eda67f2a
[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         monitor_subsys_t        *ms
54 )
55 {
56         monitor_info_t  *mi;
57         
58         Entry           **ep, *e_sent;
59         monitor_entry_t *mp;
60         int                     i;
61
62         assert( be != NULL );
63
64         mi = ( monitor_info_t * )be->be_private;
65
66         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_sent ) ) {
67                 Debug( LDAP_DEBUG_ANY,
68                         "monitor_subsys_sent_init: "
69                         "unable to get entry \"%s\"\n",
70                         ms->mss_ndn.bv_val, 0, 0 );
71                 return( -1 );
72         }
73
74         mp = ( monitor_entry_t * )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           nrdn, 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                                 ms->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                                 ms->mss_ndn.bv_val, 0 );
109                         return( -1 );
110                 }
111
112                 /* steal normalized RDN */
113                 dnRdn( &e->e_nname, &nrdn );
114                 ber_dupbv( &monitor_sent[ i ].nrdn, &nrdn );
115         
116                 BER_BVSTR( &bv, "0" );
117                 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
118         
119                 mp = monitor_entrypriv_create();
120                 if ( mp == NULL ) {
121                         return -1;
122                 }
123                 e->e_private = ( void * )mp;
124                 mp->mp_info = ms;
125                 mp->mp_flags = ms->mss_flags \
126                         | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
127
128                 if ( monitor_cache_add( mi, e ) ) {
129                         Debug( LDAP_DEBUG_ANY,
130                                 "monitor_subsys_sent_init: "
131                                 "unable to add entry \"%s,%s\"\n",
132                                 monitor_sent[ i ].rdn.bv_val,
133                                 ms->mss_ndn.bv_val, 0 );
134                         return( -1 );
135                 }
136         
137                 *ep = e;
138                 ep = &mp->mp_next;
139         }
140
141         monitor_cache_release( mi, e_sent );
142
143         return( 0 );
144 }
145
146 int
147 monitor_subsys_sent_update(
148         Operation               *op,
149         Entry                   *e
150 )
151 {
152         monitor_info_t  *mi = ( monitor_info_t *)op->o_bd->be_private;
153         
154         struct berval           nrdn;
155         ldap_pvt_mp_t           n;
156         Attribute               *a;
157         int                     i;
158
159         assert( mi );
160         assert( e );
161
162         dnRdn( &e->e_nname, &nrdn );
163
164         for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
165                 if ( dn_match( &nrdn, &monitor_sent[ i ].nrdn ) ) {
166                         break;
167                 }
168         }
169
170         if ( i == MONITOR_SENT_LAST ) {
171                 return 0;
172         }
173
174         ldap_pvt_thread_mutex_lock(&slap_counters.sc_sent_mutex);
175         switch ( i ) {
176         case MONITOR_SENT_ENTRIES:
177                 ldap_pvt_mp_init_set( n, slap_counters.sc_entries );
178                 break;
179
180         case MONITOR_SENT_REFERRALS:
181                 ldap_pvt_mp_init_set( n, slap_counters.sc_refs );
182                 break;
183
184         case MONITOR_SENT_PDU:
185                 ldap_pvt_mp_init_set( n, slap_counters.sc_pdu );
186                 break;
187
188         case MONITOR_SENT_BYTES:
189                 ldap_pvt_mp_init_set( n, slap_counters.sc_bytes );
190                 break;
191
192         default:
193                 assert(0);
194         }
195         ldap_pvt_thread_mutex_unlock(&slap_counters.sc_sent_mutex);
196         
197         a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
198         assert( a );
199
200         /* NOTE: no minus sign is allowed in the counters... */
201         UI2BV( &a->a_vals[ 0 ], n );
202         ldap_pvt_mp_clear( n );
203
204         return 0;
205 }
206