]> git.sur5r.net Git - openldap/blob - servers/slapd/monitor.c
Use calloc properly... could result in too few bytes being allocated.
[openldap] / servers / slapd / monitor.c
1 /*
2  * Copyright (c) 1995 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 /* Revision history
14  *
15  * 5-Jun-96     jeff.hodges@stanford.edu
16  *      Added locking of new_conn_mutex when traversing the c[] array.
17  *      Added locking of currenttime_mutex to protect call(s) to localtime().
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/socket.h>
25 #include <ac/string.h>
26 #include <ac/time.h>
27
28 #include "ldapconfig.h"
29 #include "slap.h"
30
31 #if defined( SLAPD_MONITOR_DN )
32
33 void
34 monitor_info( Connection *conn, Operation *op )
35 {
36         Entry           *e;
37         char            buf[BUFSIZ], buf2[22];
38         struct berval   val;
39         struct berval   *vals[2];
40         int             i, nconns, nwritewaiters, nreadwaiters;
41         struct tm       *ltm;
42         char            *p;
43
44         vals[0] = &val;
45         vals[1] = NULL;
46
47         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
48         /* initialize reader/writer lock */
49         e->e_attrs = NULL;
50         e->e_dn = ch_strdup( SLAPD_MONITOR_DN );
51         e->e_ndn = dn_normalize_case( ch_strdup(SLAPD_MONITOR_DN) );
52         e->e_private = NULL;
53
54         val.bv_val = Versionstr;
55         if (( p = strchr( Versionstr, '\n' )) == NULL ) {
56                 val.bv_len = strlen( Versionstr );
57         } else {
58                 val.bv_len = p - Versionstr;
59         }
60         attr_merge( e, "version", vals );
61
62         sprintf( buf, "%d", active_threads );
63         val.bv_val = buf;
64         val.bv_len = strlen( buf );
65         attr_merge( e, "threads", vals );
66
67         nconns = 0;
68         nwritewaiters = 0;
69         nreadwaiters = 0;
70
71         ldap_pvt_thread_mutex_lock( &new_conn_mutex );
72         for ( i = 0; i < dtblsize; i++ ) {
73                 if ( lber_pvt_sb_in_use(&(c[i].c_sb)) ) {
74                         nconns++;
75                         if ( c[i].c_writewaiter ) {
76                                 nwritewaiters++;
77                         }
78                         if ( c[i].c_gettingber ) {
79                                 nreadwaiters++;
80                         }
81                         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
82 #ifndef LDAP_LOCALTIME
83                         ltm = gmtime( &c[i].c_starttime );
84                         strftime( buf2, sizeof(buf2), "%Y%m%d%H%M%SZ", ltm );
85 #else
86                         ltm = localtime( &c[i].c_starttime );
87                         strftime( buf2, sizeof(buf2), "%y%m%d%H%M%SZ", ltm );
88 #endif
89                         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
90
91                         ldap_pvt_thread_mutex_lock( &c[i].c_dnmutex );
92                         sprintf( buf, "%d : %s : %d : %d : %s : %s%s%s%s", i,
93                             buf2, c[i].c_ops_received, c[i].c_ops_completed,
94                             c[i].c_cdn ? c[i].c_cdn : "NULLDN",
95                             c[i].c_gettingber ? "r" : "",
96                             c[i].c_writewaiter ? "w" : "",
97                             c[i].c_ops_executing ? "x" : "",
98                             c[i].c_ops_pending ? "p" : ""
99                         );
100                         ldap_pvt_thread_mutex_unlock( &c[i].c_dnmutex );
101                         val.bv_val = buf;
102                         val.bv_len = strlen( buf );
103                         attr_merge( e, "connection", vals );
104                 }
105         }
106         ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
107
108         sprintf( buf, "%d", nconns );
109         val.bv_val = buf;
110         val.bv_len = strlen( buf );
111         attr_merge( e, "currentconnections", vals );
112
113         sprintf( buf, "%d", num_conns );
114         val.bv_val = buf;
115         val.bv_len = strlen( buf );
116         attr_merge( e, "totalconnections", vals );
117
118         sprintf( buf, "%d", dtblsize );
119         val.bv_val = buf;
120         val.bv_len = strlen( buf );
121         attr_merge( e, "dtablesize", vals );
122
123         sprintf( buf, "%d", nwritewaiters );
124         val.bv_val = buf;
125         val.bv_len = strlen( buf );
126         attr_merge( e, "writewaiters", vals );
127
128         sprintf( buf, "%d", nreadwaiters );
129         val.bv_val = buf;
130         val.bv_len = strlen( buf );
131         attr_merge( e, "readwaiters", vals );
132
133         sprintf( buf, "%ld", ops_initiated );
134         val.bv_val = buf;
135         val.bv_len = strlen( buf );
136         attr_merge( e, "opsinitiated", vals );
137
138         sprintf( buf, "%ld", ops_completed );
139         val.bv_val = buf;
140         val.bv_len = strlen( buf );
141         attr_merge( e, "opscompleted", vals );
142
143         sprintf( buf, "%ld", num_entries_sent );
144         val.bv_val = buf;
145         val.bv_len = strlen( buf );
146         attr_merge( e, "entriessent", vals );
147
148         sprintf( buf, "%ld", num_bytes_sent );
149         val.bv_val = buf;
150         val.bv_len = strlen( buf );
151         attr_merge( e, "bytessent", vals );
152
153         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
154 #ifndef LDAP_LOCALTIME
155         ltm = gmtime( &currenttime );
156         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
157 #else
158         ltm = localtime( &currenttime );
159         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
160 #endif
161         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
162         val.bv_val = buf;
163         val.bv_len = strlen( buf );
164         attr_merge( e, "currenttime", vals );
165
166         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
167 #ifndef LDAP_LOCALTIME
168         ltm = gmtime( &starttime );
169         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
170 #else
171         ltm = localtime( &starttime );
172         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
173 #endif
174         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
175         val.bv_val = buf;
176         val.bv_len = strlen( buf );
177         attr_merge( e, "starttime", vals );
178
179         sprintf( buf, "%d", nbackends );
180         val.bv_val = buf;
181         val.bv_len = strlen( buf );
182         attr_merge( e, "nbackends", vals );
183
184 #ifdef HAVE_THREAD_CONCURRENCY
185         sprintf( buf, "%d", ldap_pvt_thread_get_concurrency() );
186         val.bv_val = buf;
187         val.bv_len = strlen( buf );
188         attr_merge( e, "concurrency", vals );
189 #endif
190
191         send_search_entry( &backends[0], conn, op, e, NULL, 0 );
192         send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL, 1 );
193
194         entry_free( e );
195 }
196
197 #endif /* slapd_monitor_dn */