1 /* Copyright (c) 1998, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
32 /* We use this to make sure the receiver is the same. */
33 static const char compilation
[21] = __DATE__
" " __TIME__
;
35 /* Statistic data for one database. */
42 unsigned long int postimeout
;
43 unsigned long int negtimeout
;
45 unsigned long int poshit
;
46 unsigned long int neghit
;
47 unsigned long int posmiss
;
48 unsigned long int negmiss
;
50 unsigned long int nentries
;
51 unsigned long int maxnentries
;
52 unsigned long int maxnsearched
;
54 unsigned long int rdlockdelayed
;
55 unsigned long int wrlockdelayed
;
58 /* Record for transmitting statistics. */
61 char version
[sizeof (compilation
)];
64 unsigned long int client_queued
;
66 struct dbstat dbs
[lastdb
];
71 send_stats (int fd
, struct database dbs
[lastdb
])
76 memcpy (data
.version
, compilation
, sizeof (compilation
));
77 data
.debug_level
= debug_level
;
78 data
.runtime
= time (NULL
) - start_time
;
79 data
.client_queued
= client_queued
;
82 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
84 data
.dbs
[cnt
].enabled
= dbs
[cnt
].enabled
;
85 data
.dbs
[cnt
].check_file
= dbs
[cnt
].check_file
;
86 data
.dbs
[cnt
].module
= dbs
[cnt
].module
;
87 data
.dbs
[cnt
].postimeout
= dbs
[cnt
].postimeout
;
88 data
.dbs
[cnt
].negtimeout
= dbs
[cnt
].negtimeout
;
89 data
.dbs
[cnt
].poshit
= dbs
[cnt
].poshit
;
90 data
.dbs
[cnt
].neghit
= dbs
[cnt
].neghit
;
91 data
.dbs
[cnt
].posmiss
= dbs
[cnt
].posmiss
;
92 data
.dbs
[cnt
].negmiss
= dbs
[cnt
].negmiss
;
93 data
.dbs
[cnt
].nentries
= dbs
[cnt
].nentries
;
94 data
.dbs
[cnt
].maxnentries
= dbs
[cnt
].maxnentries
;
95 data
.dbs
[cnt
].maxnsearched
= dbs
[cnt
].maxnsearched
;
96 data
.dbs
[cnt
].rdlockdelayed
= dbs
[cnt
].rdlockdelayed
;
97 data
.dbs
[cnt
].wrlockdelayed
= dbs
[cnt
].wrlockdelayed
;
100 if (TEMP_FAILURE_RETRY (write (fd
, &data
, sizeof (data
))) != sizeof (data
))
103 dbg_log (_("cannot write statistics: %s"),
104 strerror_r (errno
, buf
, sizeof (buf
)));
110 receive_print_stats (void)
112 struct statdata data
;
117 uid_t uid
= getuid ();
119 /* Find out whether there is another user but root allowed to
120 request statistics. */
123 /* User specified? */
124 if(stat_user
== NULL
|| stat_uid
!= uid
)
126 if (stat_user
!= NULL
)
127 error (EXIT_FAILURE
, 0,
128 _("Only root or %s is allowed to use this option!"),
131 error (EXIT_FAILURE
, 0,
132 _("Only root is allowed to use this option!"));
136 /* Open a socket to the running nscd. */
137 fd
= nscd_open_socket ();
139 error (EXIT_FAILURE
, 0, _("nscd not running!\n"));
141 /* Send the request. */
142 req
.version
= NSCD_VERSION
;
145 nbytes
= TEMP_FAILURE_RETRY (write (fd
, &req
, sizeof (request_header
)));
146 if (nbytes
!= sizeof (request_header
))
150 error (EXIT_FAILURE
, err
, _("write incomplete"));
153 /* Read as much data as we expect. */
154 if (TEMP_FAILURE_RETRY (read (fd
, &data
, sizeof (data
))) != sizeof (data
)
155 || (memcmp (data
.version
, compilation
, sizeof (compilation
)) != 0
156 /* Yes, this is an assignment! */
157 && (errno
= EINVAL
)))
159 /* Not the right version. */
162 error (EXIT_FAILURE
, err
, _("cannot read statistics data"));
165 printf (_("nscd configuration:\n\n%15d server debug level\n"),
168 /* We know that we can simply subtract time_t values. */
169 unsigned long int diff
= data
.runtime
;
170 unsigned int ndays
= 0;
171 unsigned int nhours
= 0;
172 unsigned int nmins
= 0;
173 if (diff
> 24 * 60 * 60)
175 ndays
= diff
/ (24 * 60 * 60);
176 diff
%= 24 * 60 * 60;
180 nhours
= diff
/ (60 * 60);
189 printf (_("%3ud %2uh %2um %2lus server runtime\n"),
190 ndays
, nhours
, nmins
, diff
);
191 else if (nhours
!= 0)
192 printf (_(" %2uh %2um %2lus server runtime\n"), nhours
, nmins
, diff
);
194 printf (_(" %2um %2lus server runtime\n"), nmins
, diff
);
196 printf (_(" %2lus server runtime\n"), diff
);
198 printf (_("%15lu number of times clients had to wait\n"),
201 for (i
= 0; i
< lastdb
; ++i
)
203 unsigned long int hit
= data
.dbs
[i
].poshit
+ data
.dbs
[i
].neghit
;
204 unsigned long int all
= hit
+ data
.dbs
[i
].posmiss
+ data
.dbs
[i
].negmiss
;
205 const char *enabled
= nl_langinfo (data
.dbs
[i
].enabled
? YESSTR
: NOSTR
);
206 const char *check_file
= nl_langinfo (data
.dbs
[i
].check_file
209 if (enabled
[0] == '\0')
210 /* The locale does not provide this information so we have to
211 translate it ourself. Since we should avoid short translation
212 terms we artifically increase the length. */
213 enabled
= data
.dbs
[i
].enabled
? _(" yes") : _(" no");
214 if (check_file
[0] == '\0')
215 check_file
= data
.dbs
[i
].check_file
? _(" yes") : _(" no");
218 /* If nothing happened so far report a 0% hit rate. */
221 printf (_("\n%s cache:\n\n"
222 "%15s cache is enabled\n"
223 "%15Zu suggested size\n"
224 "%15lu seconds time to live for positive entries\n"
225 "%15lu seconds time to live for negative entries\n"
226 "%15lu cache hits on positive entries\n"
227 "%15lu cache hits on negative entries\n"
228 "%15lu cache misses on positive entries\n"
229 "%15lu cache misses on negative entries\n"
230 "%15lu%% cache hit rate\n"
231 "%15lu current number of cached values\n"
232 "%15lu maximum number of cached values\n"
233 "%15lu maximum chain length searched\n"
234 "%15lu number of delays on rdlock\n"
235 "%15lu number of delays on wrlock\n"
236 "%15s check /etc/%s for changes\n"),
239 data
.dbs
[i
].postimeout
, data
.dbs
[i
].negtimeout
,
240 data
.dbs
[i
].poshit
, data
.dbs
[i
].neghit
,
241 data
.dbs
[i
].posmiss
, data
.dbs
[i
].negmiss
,
243 data
.dbs
[i
].nentries
, data
.dbs
[i
].maxnentries
,
244 data
.dbs
[i
].maxnsearched
,
245 data
.dbs
[i
].rdlockdelayed
,
246 data
.dbs
[i
].wrlockdelayed
, check_file
, dbnames
[i
]);