Back to index

glibc  2.9
nscd_stat.c
Go to the documentation of this file.
00001 /* Copyright (c) 1998, 2003, 2004, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <error.h>
00022 #include <inttypes.h>
00023 #include <langinfo.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <sys/socket.h>
00028 #include <unistd.h>
00029 #include <libintl.h>
00030 
00031 #include "nscd.h"
00032 #include "dbg_log.h"
00033 #include "selinux.h"
00034 #ifdef HAVE_SELINUX
00035 # include <selinux/selinux.h>
00036 # include <selinux/avc.h>
00037 #endif /* HAVE_SELINUX */
00038 
00039 
00040 /* We use this to make sure the receiver is the same.  */
00041 static const char compilation[21] = __DATE__ " " __TIME__;
00042 
00043 /* Statistic data for one database.  */
00044 struct dbstat
00045 {
00046   int enabled;
00047   int check_file;
00048   int shared;
00049   int persistent;
00050   size_t module;
00051 
00052   unsigned long int postimeout;
00053   unsigned long int negtimeout;
00054 
00055   size_t nentries;
00056   size_t maxnentries;
00057   size_t maxnsearched;
00058   size_t datasize;
00059   size_t dataused;
00060 
00061   uintmax_t poshit;
00062   uintmax_t neghit;
00063   uintmax_t posmiss;
00064   uintmax_t negmiss;
00065 
00066   uintmax_t rdlockdelayed;
00067   uintmax_t wrlockdelayed;
00068 
00069   uintmax_t addfailed;
00070 };
00071 
00072 /* Record for transmitting statistics.  */
00073 struct statdata
00074 {
00075   char version[sizeof (compilation)];
00076   int debug_level;
00077   time_t runtime;
00078   unsigned long int client_queued;
00079   int nthreads;
00080   int max_nthreads;
00081   int paranoia;
00082   time_t restart_interval;
00083   int ndbs;
00084   struct dbstat dbs[lastdb];
00085 #ifdef HAVE_SELINUX
00086   struct avc_cache_stats cstats;
00087 #endif /* HAVE_SELINUX */
00088 };
00089 
00090 
00091 void
00092 send_stats (int fd, struct database_dyn dbs[lastdb])
00093 {
00094   struct statdata data;
00095   int cnt;
00096 
00097   memcpy (data.version, compilation, sizeof (compilation));
00098   data.debug_level = debug_level;
00099   data.runtime = time (NULL) - start_time;
00100   data.client_queued = client_queued;
00101   data.nthreads = nthreads;
00102   data.max_nthreads = max_nthreads;
00103   data.paranoia = paranoia;
00104   data.restart_interval = restart_interval;
00105   data.ndbs = lastdb;
00106 
00107   for (cnt = 0; cnt < lastdb; ++cnt)
00108     {
00109       memset (&data.dbs[cnt], 0, sizeof (data.dbs[cnt]));
00110       data.dbs[cnt].enabled = dbs[cnt].enabled;
00111       data.dbs[cnt].check_file = dbs[cnt].check_file;
00112       data.dbs[cnt].shared = dbs[cnt].shared;
00113       data.dbs[cnt].persistent = dbs[cnt].persistent;
00114       data.dbs[cnt].postimeout = dbs[cnt].postimeout;
00115       data.dbs[cnt].negtimeout = dbs[cnt].negtimeout;
00116       if (dbs[cnt].head != NULL)
00117        {
00118          data.dbs[cnt].module = dbs[cnt].head->module;
00119          data.dbs[cnt].poshit = dbs[cnt].head->poshit;
00120          data.dbs[cnt].neghit = dbs[cnt].head->neghit;
00121          data.dbs[cnt].posmiss = dbs[cnt].head->posmiss;
00122          data.dbs[cnt].negmiss = dbs[cnt].head->negmiss;
00123          data.dbs[cnt].nentries = dbs[cnt].head->nentries;
00124          data.dbs[cnt].maxnentries = dbs[cnt].head->maxnentries;
00125          data.dbs[cnt].datasize = dbs[cnt].head->data_size;
00126          data.dbs[cnt].dataused = dbs[cnt].head->first_free;
00127          data.dbs[cnt].maxnsearched = dbs[cnt].head->maxnsearched;
00128          data.dbs[cnt].rdlockdelayed = dbs[cnt].head->rdlockdelayed;
00129          data.dbs[cnt].wrlockdelayed = dbs[cnt].head->wrlockdelayed;
00130          data.dbs[cnt].addfailed = dbs[cnt].head->addfailed;
00131        }
00132     }
00133 
00134   if (selinux_enabled)
00135     nscd_avc_cache_stats (&data.cstats);
00136 
00137   if (TEMP_FAILURE_RETRY (send (fd, &data, sizeof (data), MSG_NOSIGNAL))
00138       != sizeof (data))
00139     {
00140       char buf[256];
00141       dbg_log (_("cannot write statistics: %s"),
00142               strerror_r (errno, buf, sizeof (buf)));
00143     }
00144 }
00145 
00146 
00147 int
00148 receive_print_stats (void)
00149 {
00150   struct statdata data;
00151   request_header req;
00152   ssize_t nbytes;
00153   int fd;
00154   int i;
00155   uid_t uid = getuid ();
00156   const char *yesstr = _("yes");
00157   const char *nostr = _("no");
00158 
00159   /* Find out whether there is another user but root allowed to
00160      request statistics.  */
00161   if (uid != 0)
00162     {
00163       /* User specified?  */
00164       if(stat_user == NULL || stat_uid != uid)
00165        {
00166          if (stat_user != NULL)
00167            error (EXIT_FAILURE, 0,
00168                  _("Only root or %s is allowed to use this option!"),
00169                  stat_user);
00170          else
00171            error (EXIT_FAILURE, 0,
00172                  _("Only root is allowed to use this option!"));
00173        }
00174     }
00175 
00176   /* Open a socket to the running nscd.  */
00177   fd = nscd_open_socket ();
00178   if (fd == -1)
00179     error (EXIT_FAILURE, 0, _("nscd not running!\n"));
00180 
00181   /* Send the request.  */
00182   req.version = NSCD_VERSION;
00183   req.type = GETSTAT;
00184   req.key_len = 0;
00185   nbytes = TEMP_FAILURE_RETRY (send (fd, &req, sizeof (request_header),
00186                                  MSG_NOSIGNAL));
00187   if (nbytes != sizeof (request_header))
00188     {
00189       int err = errno;
00190       close (fd);
00191       error (EXIT_FAILURE, err, _("write incomplete"));
00192     }
00193 
00194   /* Read as much data as we expect.  */
00195   if (TEMP_FAILURE_RETRY (read (fd, &data, sizeof (data))) != sizeof (data)
00196       || (memcmp (data.version, compilation, sizeof (compilation)) != 0
00197          /* Yes, this is an assignment!  */
00198          && (errno = EINVAL)))
00199     {
00200       /* Not the right version.  */
00201       int err = errno;
00202       close (fd);
00203       error (EXIT_FAILURE, err, _("cannot read statistics data"));
00204     }
00205 
00206   printf (_("nscd configuration:\n\n%15d  server debug level\n"),
00207          data.debug_level);
00208 
00209   /* We know that we can simply subtract time_t values.  */
00210   unsigned long int diff = data.runtime;
00211   unsigned int ndays = 0;
00212   unsigned int nhours = 0;
00213   unsigned int nmins = 0;
00214   if (diff > 24 * 60 * 60)
00215     {
00216       ndays = diff / (24 * 60 * 60);
00217       diff %= 24 * 60 * 60;
00218     }
00219   if (diff > 60 * 60)
00220     {
00221       nhours = diff / (60 * 60);
00222       diff %= 60 * 60;
00223     }
00224   if (diff > 60)
00225     {
00226       nmins = diff / 60;
00227       diff %= 60;
00228     }
00229   if (ndays != 0)
00230     printf (_("%3ud %2uh %2um %2lus  server runtime\n"),
00231            ndays, nhours, nmins, diff);
00232   else if (nhours != 0)
00233     printf (_("    %2uh %2um %2lus  server runtime\n"), nhours, nmins, diff);
00234   else if (nmins != 0)
00235     printf (_("        %2um %2lus  server runtime\n"), nmins, diff);
00236   else
00237     printf (_("            %2lus  server runtime\n"), diff);
00238 
00239   printf (_("%15d  current number of threads\n"
00240            "%15d  maximum number of threads\n"
00241            "%15lu  number of times clients had to wait\n"
00242            "%15s  paranoia mode enabled\n"
00243            "%15lu  restart internal\n"),
00244          data.nthreads, data.max_nthreads, data.client_queued,
00245          data.paranoia ? yesstr : nostr,
00246          (unsigned long int) data.restart_interval);
00247 
00248   for (i = 0; i < lastdb; ++i)
00249     {
00250       unsigned long int hit = data.dbs[i].poshit + data.dbs[i].neghit;
00251       unsigned long int all = hit + data.dbs[i].posmiss + data.dbs[i].negmiss;
00252       const char *enabled = data.dbs[i].enabled ? yesstr : nostr;
00253       const char *check_file = data.dbs[i].check_file ? yesstr : nostr;
00254       const char *shared = data.dbs[i].shared ? yesstr : nostr;
00255       const char *persistent = data.dbs[i].persistent ? yesstr : nostr;
00256 
00257       if (enabled[0] == '\0')
00258        /* The locale does not provide this information so we have to
00259           translate it ourself.  Since we should avoid short translation
00260           terms we artifically increase the length.  */
00261        enabled = data.dbs[i].enabled ? yesstr : nostr;
00262       if (check_file[0] == '\0')
00263        check_file = data.dbs[i].check_file ? yesstr : nostr;
00264       if (shared[0] == '\0')
00265        shared = data.dbs[i].shared ? yesstr : nostr;
00266       if (persistent[0] == '\0')
00267        persistent = data.dbs[i].persistent ? yesstr : nostr;
00268 
00269       if (all == 0)
00270        /* If nothing happened so far report a 0% hit rate.  */
00271        all = 1;
00272 
00273       printf (_("\n%s cache:\n\n"
00274               "%15s  cache is enabled\n"
00275               "%15s  cache is persistent\n"
00276               "%15s  cache is shared\n"
00277               "%15zu  suggested size\n"
00278               "%15zu  total data pool size\n"
00279               "%15zu  used data pool size\n"
00280               "%15lu  seconds time to live for positive entries\n"
00281               "%15lu  seconds time to live for negative entries\n"
00282               "%15" PRIuMAX "  cache hits on positive entries\n"
00283               "%15" PRIuMAX "  cache hits on negative entries\n"
00284               "%15" PRIuMAX "  cache misses on positive entries\n"
00285               "%15" PRIuMAX "  cache misses on negative entries\n"
00286               "%15lu%% cache hit rate\n"
00287               "%15zu  current number of cached values\n"
00288               "%15zu  maximum number of cached values\n"
00289               "%15zu  maximum chain length searched\n"
00290               "%15" PRIuMAX "  number of delays on rdlock\n"
00291               "%15" PRIuMAX "  number of delays on wrlock\n"
00292               "%15" PRIuMAX "  memory allocations failed\n"
00293               "%15s  check /etc/%s for changes\n"),
00294              dbnames[i], enabled, persistent, shared,
00295              data.dbs[i].module,
00296              data.dbs[i].datasize, data.dbs[i].dataused,
00297              data.dbs[i].postimeout, data.dbs[i].negtimeout,
00298              data.dbs[i].poshit, data.dbs[i].neghit,
00299              data.dbs[i].posmiss, data.dbs[i].negmiss,
00300              (100 * hit) / all,
00301              data.dbs[i].nentries, data.dbs[i].maxnentries,
00302              data.dbs[i].maxnsearched,
00303              data.dbs[i].rdlockdelayed,
00304              data.dbs[i].wrlockdelayed,
00305              data.dbs[i].addfailed, check_file, dbnames[i]);
00306     }
00307 
00308   if (selinux_enabled)
00309     nscd_avc_print_stats (&data.cstats);
00310 
00311   close (fd);
00312 
00313   exit (0);
00314 }