]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/thread.c
12a674b3086431a3968bf5f316292a230d49b545
[openldap] / servers / slapd / back-monitor / thread.c
1 /* thread.c - deal with thread 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 /*
31 *  * initializes log subentry
32 *   */
33 int
34 monitor_subsys_thread_init(
35         BackendDB       *be,
36         monitorsubsys   *ms
37 )
38 {
39         struct monitorinfo      *mi;
40         struct monitorentrypriv *mp;
41         Entry                   *e, **ep, *e_thread;
42         static char             buf[ BACKMONITOR_BUFSIZE ];
43
44         mi = ( struct monitorinfo * )be->be_private;
45
46         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_thread ) ) {
47                 Debug( LDAP_DEBUG_ANY,
48                         "monitor_subsys_thread_init: unable to get entry \"%s\"\n",
49                         ms->mss_ndn.bv_val, 
50                         0, 0 );
51                 return( -1 );
52         }
53
54         mp = ( struct monitorentrypriv * )e_thread->e_private;
55         mp->mp_children = NULL;
56         ep = &mp->mp_children;
57
58         /*
59          * Max
60          */
61         snprintf( buf, sizeof( buf ),
62                         "dn: cn=Max,%s\n"
63                         "objectClass: %s\n"
64                         "structuralObjectClass: %s\n"
65                         "cn: Max\n"
66                         "%s: %d\n"
67                         "creatorsName: %s\n"
68                         "modifiersName: %s\n"
69                         "createTimestamp: %s\n"
70                         "modifyTimestamp: %s\n", 
71                         ms->mss_dn.bv_val,
72                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
73                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
74                         mi->mi_ad_monitoredInfo->ad_cname.bv_val,
75                         connection_pool_max,
76                         mi->mi_creatorsName.bv_val,
77                         mi->mi_creatorsName.bv_val,
78                         mi->mi_startTime.bv_val,
79                         mi->mi_startTime.bv_val );
80
81         e = str2entry( buf );
82         if ( e == NULL ) {
83                 Debug( LDAP_DEBUG_ANY,
84                         "monitor_subsys_thread_init: "
85                         "unable to create entry \"cn=Max,%s\"\n",
86                         ms->mss_ndn.bv_val, 0, 0 );
87                 return( -1 );
88         }
89         
90         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
91         e->e_private = ( void * )mp;
92         mp->mp_next = NULL;
93         mp->mp_children = NULL;
94         mp->mp_info = ms;
95         mp->mp_flags = ms->mss_flags \
96                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
97
98         if ( monitor_cache_add( mi, e ) ) {
99                 Debug( LDAP_DEBUG_ANY,
100                         "monitor_subsys_thread_init: "
101                         "unable to add entry \"cn=Max,%s\"\n",
102                         ms->mss_ndn.bv_val, 0, 0 );
103                 return( -1 );
104         }
105         
106         *ep = e;
107         ep = &mp->mp_next;
108
109         /*
110          * Backload
111          */
112         snprintf( buf, sizeof( buf ),
113                         "dn: cn=Backload,%s\n"
114                         "objectClass: %s\n"
115                         "structuralObjectClass: %s\n"
116                         "cn: Backload\n"
117                         "%s: 0\n"
118                         "creatorsName: %s\n"
119                         "modifiersName: %s\n"
120                         "createTimestamp: %s\n"
121                         "modifyTimestamp: %s\n",
122                         ms->mss_dn.bv_val,
123                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
124                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
125                         mi->mi_ad_monitoredInfo->ad_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_thread_init: "
135                         "unable to create entry \"cn=Backload,%s\"\n",
136                         ms->mss_ndn.bv_val, 0, 0 );
137                 return( -1 );
138         }
139
140         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
141         e->e_private = ( void * )mp;
142         mp->mp_next = NULL;
143         mp->mp_children = NULL;
144         mp->mp_info = ms;
145         mp->mp_flags = ms->mss_flags \
146                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
147
148         if ( monitor_cache_add( mi, e ) ) {
149                 Debug( LDAP_DEBUG_ANY,
150                         "monitor_subsys_thread_init: "
151                         "unable to add entry \"cn=Backload,%s\"\n",
152                         ms->mss_ndn.bv_val, 0, 0 );
153                 return( -1 );
154         }
155         
156         *ep = e;
157         ep = &mp->mp_next;
158
159         monitor_cache_release( mi, e_thread );
160
161         return( 0 );
162 }
163
164 int 
165 monitor_subsys_thread_update( 
166         Operation               *op,
167         Entry                   *e
168 )
169 {
170         struct monitorinfo      *mi =
171                 (struct monitorinfo *)op->o_bd->be_private;
172         Attribute               *a;
173         char                    buf[ BACKMONITOR_BUFSIZE ];
174         static struct berval    backload_bv = BER_BVC( "cn=backload" );
175         struct berval           rdn;
176         ber_len_t               len;
177
178         assert( mi != NULL );
179
180         dnRdn( &e->e_nname, &rdn );
181         if ( !dn_match( &rdn, &backload_bv ) ) {
182                 return 0;
183         }
184
185         a = attr_find( e->e_attrs, mi->mi_ad_monitoredInfo );
186         if ( a == NULL ) {
187                 return -1;
188         }
189
190         snprintf( buf, sizeof( buf ), "%d", 
191                         ldap_pvt_thread_pool_backload( &connection_pool ) );
192         len = strlen( buf );
193         if ( len > a->a_vals[ 0 ].bv_len ) {
194                 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
195         }
196         a->a_vals[ 0 ].bv_len = len;
197         AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
198
199         return( 0 );
200 }
201