1 /* Copyright (c) 1998-2013 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, see
17 <http://www.gnu.org/licenses/>. */
26 #include <sys/socket.h>
34 # include <selinux/selinux.h>
35 # include <selinux/avc.h>
36 #endif /* HAVE_SELINUX */
39 /* We use this to make sure the receiver is the same. */
40 static const char compilation[21] = __DATE__ " " __TIME__;
42 /* Statistic data for one database. */
51 unsigned long int postimeout;
52 unsigned long int negtimeout;
65 uintmax_t rdlockdelayed;
66 uintmax_t wrlockdelayed;
71 /* Record for transmitting statistics. */
74 char version[sizeof (compilation)];
77 unsigned long int client_queued;
81 time_t restart_interval;
82 unsigned int reload_count;
84 struct dbstat dbs[lastdb];
86 struct avc_cache_stats cstats;
87 #endif /* HAVE_SELINUX */
92 send_stats (int fd, struct database_dyn dbs[lastdb])
97 memcpy (data.version, compilation, sizeof (compilation));
98 data.debug_level = debug_level;
99 data.runtime = time (NULL) - start_time;
100 data.client_queued = client_queued;
101 data.nthreads = nthreads;
102 data.max_nthreads = max_nthreads;
103 data.paranoia = paranoia;
104 data.restart_interval = restart_interval;
105 data.reload_count = reload_count;
108 for (cnt = 0; cnt < lastdb; ++cnt)
110 memset (&data.dbs[cnt], 0, sizeof (data.dbs[cnt]));
111 data.dbs[cnt].enabled = dbs[cnt].enabled;
112 data.dbs[cnt].check_file = dbs[cnt].check_file;
113 data.dbs[cnt].shared = dbs[cnt].shared;
114 data.dbs[cnt].persistent = dbs[cnt].persistent;
115 data.dbs[cnt].postimeout = dbs[cnt].postimeout;
116 data.dbs[cnt].negtimeout = dbs[cnt].negtimeout;
117 if (dbs[cnt].head != NULL)
119 data.dbs[cnt].module = dbs[cnt].head->module;
120 data.dbs[cnt].poshit = dbs[cnt].head->poshit;
121 data.dbs[cnt].neghit = dbs[cnt].head->neghit;
122 data.dbs[cnt].posmiss = dbs[cnt].head->posmiss;
123 data.dbs[cnt].negmiss = dbs[cnt].head->negmiss;
124 data.dbs[cnt].nentries = dbs[cnt].head->nentries;
125 data.dbs[cnt].maxnentries = dbs[cnt].head->maxnentries;
126 data.dbs[cnt].datasize = dbs[cnt].head->data_size;
127 data.dbs[cnt].dataused = dbs[cnt].head->first_free;
128 data.dbs[cnt].maxnsearched = dbs[cnt].head->maxnsearched;
129 data.dbs[cnt].rdlockdelayed = dbs[cnt].head->rdlockdelayed;
130 data.dbs[cnt].wrlockdelayed = dbs[cnt].head->wrlockdelayed;
131 data.dbs[cnt].addfailed = dbs[cnt].head->addfailed;
136 nscd_avc_cache_stats (&data.cstats);
138 if (TEMP_FAILURE_RETRY (send (fd, &data, sizeof (data), MSG_NOSIGNAL))
142 dbg_log (_("cannot write statistics: %s"),
143 strerror_r (errno, buf, sizeof (buf)));
149 receive_print_stats (void)
151 struct statdata data;
156 uid_t uid = getuid ();
157 const char *yesstr = _("yes");
158 const char *nostr = _("no");
160 /* Find out whether there is another user but root allowed to
161 request statistics. */
164 /* User specified? */
165 if(stat_user == NULL || stat_uid != uid)
167 if (stat_user != NULL)
168 error (EXIT_FAILURE, 0,
169 _("Only root or %s is allowed to use this option!"),
172 error (EXIT_FAILURE, 0,
173 _("Only root is allowed to use this option!"));
177 /* Open a socket to the running nscd. */
178 fd = nscd_open_socket ();
180 error (EXIT_FAILURE, 0, _("nscd not running!\n"));
182 /* Send the request. */
183 req.version = NSCD_VERSION;
186 nbytes = TEMP_FAILURE_RETRY (send (fd, &req, sizeof (request_header),
188 if (nbytes != sizeof (request_header))
192 error (EXIT_FAILURE, err, _("write incomplete"));
195 /* Read as much data as we expect. */
196 if (TEMP_FAILURE_RETRY (read (fd, &data, sizeof (data))) != sizeof (data)
197 || (memcmp (data.version, compilation, sizeof (compilation)) != 0
198 /* Yes, this is an assignment! */
199 && (errno = EINVAL)))
201 /* Not the right version. */
204 error (EXIT_FAILURE, err, _("cannot read statistics data"));
207 printf (_("nscd configuration:\n\n%15d server debug level\n"),
210 /* We know that we can simply subtract time_t values. */
211 unsigned long int diff = data.runtime;
212 unsigned int ndays = 0;
213 unsigned int nhours = 0;
214 unsigned int nmins = 0;
215 if (diff > 24 * 60 * 60)
217 ndays = diff / (24 * 60 * 60);
218 diff %= 24 * 60 * 60;
222 nhours = diff / (60 * 60);
231 printf (_("%3ud %2uh %2um %2lus server runtime\n"),
232 ndays, nhours, nmins, diff);
233 else if (nhours != 0)
234 printf (_(" %2uh %2um %2lus server runtime\n"), nhours, nmins, diff);
236 printf (_(" %2um %2lus server runtime\n"), nmins, diff);
238 printf (_(" %2lus server runtime\n"), diff);
240 printf (_("%15d current number of threads\n"
241 "%15d maximum number of threads\n"
242 "%15lu number of times clients had to wait\n"
243 "%15s paranoia mode enabled\n"
244 "%15lu restart internal\n"
245 "%15u reload count\n"),
246 data.nthreads, data.max_nthreads, data.client_queued,
247 data.paranoia ? yesstr : nostr,
248 (unsigned long int) data.restart_interval, data.reload_count);
250 for (i = 0; i < lastdb; ++i)
252 unsigned long int hit = data.dbs[i].poshit + data.dbs[i].neghit;
253 unsigned long int all = hit + data.dbs[i].posmiss + data.dbs[i].negmiss;
254 const char *enabled = data.dbs[i].enabled ? yesstr : nostr;
255 const char *check_file = data.dbs[i].check_file ? yesstr : nostr;
256 const char *shared = data.dbs[i].shared ? yesstr : nostr;
257 const char *persistent = data.dbs[i].persistent ? yesstr : nostr;
259 if (enabled[0] == '\0')
260 /* The locale does not provide this information so we have to
261 translate it ourself. Since we should avoid short translation
262 terms we artifically increase the length. */
263 enabled = data.dbs[i].enabled ? yesstr : nostr;
264 if (check_file[0] == '\0')
265 check_file = data.dbs[i].check_file ? yesstr : nostr;
266 if (shared[0] == '\0')
267 shared = data.dbs[i].shared ? yesstr : nostr;
268 if (persistent[0] == '\0')
269 persistent = data.dbs[i].persistent ? yesstr : nostr;
272 /* If nothing happened so far report a 0% hit rate. */
275 printf (_("\n%s cache:\n\n"
276 "%15s cache is enabled\n"
277 "%15s cache is persistent\n"
278 "%15s cache is shared\n"
279 "%15zu suggested size\n"
280 "%15zu total data pool size\n"
281 "%15zu used data pool size\n"
282 "%15lu seconds time to live for positive entries\n"
283 "%15lu seconds time to live for negative entries\n"
284 "%15" PRIuMAX " cache hits on positive entries\n"
285 "%15" PRIuMAX " cache hits on negative entries\n"
286 "%15" PRIuMAX " cache misses on positive entries\n"
287 "%15" PRIuMAX " cache misses on negative entries\n"
288 "%15lu%% cache hit rate\n"
289 "%15zu current number of cached values\n"
290 "%15zu maximum number of cached values\n"
291 "%15zu maximum chain length searched\n"
292 "%15" PRIuMAX " number of delays on rdlock\n"
293 "%15" PRIuMAX " number of delays on wrlock\n"
294 "%15" PRIuMAX " memory allocations failed\n"
295 "%15s check /etc/%s for changes\n"),
296 dbnames[i], enabled, persistent, shared,
298 data.dbs[i].datasize, data.dbs[i].dataused,
299 data.dbs[i].postimeout, data.dbs[i].negtimeout,
300 data.dbs[i].poshit, data.dbs[i].neghit,
301 data.dbs[i].posmiss, data.dbs[i].negmiss,
303 data.dbs[i].nentries, data.dbs[i].maxnentries,
304 data.dbs[i].maxnsearched,
305 data.dbs[i].rdlockdelayed,
306 data.dbs[i].wrlockdelayed,
307 data.dbs[i].addfailed, check_file, dbnames[i]);
311 nscd_avc_print_stats (&data.cstats);