3 * Copyright (C) 2005,2006 Jason Pepas
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Jason Pepas <cell at ices.utexas.edu>
20 * Florian octo Forster <octo at verplant.org>
26 #include "utils_debug.h"
28 #define MODULE_NAME "nfs"
30 /* #if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) */
32 # define NFS_HAVE_READ 1
34 # define NFS_HAVE_READ 0
39 see http://www.missioncriticallinux.com/orph/NFS-Statistics
42 rpc_stat.netcnt Not used; always zero.
43 rpc_stat.netudpcnt Not used; always zero.
44 rpc_stat.nettcpcnt Not used; always zero.
45 rpc_stat.nettcpconn Not used; always zero.
48 rpc_stat.rpccnt The number of RPC calls.
49 rpc_stat.rpcretrans The number of retransmitted RPC calls.
50 rpc_stat.rpcauthrefresh The number of credential refreshes.
55 Procedure NFS Version NFS Version 3
56 Number Procedures Procedures
82 static data_source_t procedure_dsrc[1] =
84 {"value", DS_TYPE_COUNTER, 0, 4294967295.0}
87 static data_set_t procedure_ds =
89 "nfs_procedure", 1, procedure_dsrc
92 static const char *nfs2_procedures_names[] =
114 static int nfs2_procedures_names_num = 18;
116 static const char *nfs3_procedures_names[] =
142 static int nfs3_procedures_names_num = 22;
144 #if HAVE_LIBKSTAT && 0
145 extern kstat_ctl_t *kc;
146 static kstat_t *nfs2_ksp_client;
147 static kstat_t *nfs2_ksp_server;
148 static kstat_t *nfs3_ksp_client;
149 static kstat_t *nfs3_ksp_server;
150 static kstat_t *nfs4_ksp_client;
151 static kstat_t *nfs4_ksp_server;
154 /* Possibly TODO: NFSv4 statistics */
157 static int nfs_init (void)
159 #if HAVE_LIBKSTAT && 0
162 nfs2_ksp_client = NULL;
163 nfs2_ksp_server = NULL;
164 nfs3_ksp_client = NULL;
165 nfs3_ksp_server = NULL;
166 nfs4_ksp_client = NULL;
167 nfs4_ksp_server = NULL;
172 for (ksp_chain = kc->kc_chain; ksp_chain != NULL;
173 ksp_chain = ksp_chain->ks_next)
175 if (strncmp (ksp_chain->ks_module, "nfs", 3) != 0)
177 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v2", 13) == 0)
178 nfs2_ksp_server = ksp_chain;
179 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v3", 13) == 0)
180 nfs3_ksp_server = ksp_chain;
181 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v4", 13) == 0)
182 nfs4_ksp_server = ksp_chain;
183 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v2", 12) == 0)
184 nfs2_ksp_client = ksp_chain;
185 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v3", 12) == 0)
186 nfs3_ksp_client = ksp_chain;
187 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v4", 12) == 0)
188 nfs4_ksp_client = ksp_chain;
198 static void nfs_procedures_submit (const char *plugin_instance,
199 unsigned long long *val, const char **names, int len)
202 value_list_t vl = VALUE_LIST_INIT;
207 vl.time = time (NULL);
208 strcpy (vl.host, hostname);
209 strcpy (vl.plugin, "nfs");
210 strncpy (vl.plugin_instance, plugin_instance,
211 sizeof (vl.plugin_instance));
213 for (i = 0; i < len; i++)
215 values[0].counter = val[i];
216 strncpy (vl.type_instance, names[i],
217 sizeof (vl.type_instance));
218 DBG ("%s-%s/nfs_procedure-%s = %llu",
219 vl.plugin, vl.plugin_instance,
220 vl.type_instance, val[i]);
221 plugin_dispatch_values ("nfs_procedure", &vl);
223 } /* void nfs_procedures_submit */
224 #endif /* NFS_HAVE_READ */
227 static void nfs_read_stats_file (FILE *fh, char *inst)
229 char buffer[BUFSIZE];
231 char plugin_instance[DATA_MAX_NAME_LEN];
239 while (fgets (buffer, BUFSIZE, fh) != NULL)
241 numfields = strsplit (buffer, fields, 48);
243 if (((numfields - 2) != nfs2_procedures_names_num)
245 != nfs3_procedures_names_num))
248 if (strcmp (fields[0], "proc2") == 0)
251 unsigned long long *values;
253 if ((numfields - 2) != nfs2_procedures_names_num)
255 syslog (LOG_WARNING, "nfs plugin: Wrong "
256 "number of fields (= %i) "
257 "for NFSv2 statistics.",
262 snprintf (plugin_instance, sizeof (plugin_instance),
264 plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
266 values = (unsigned long long *) malloc (nfs2_procedures_names_num * sizeof (unsigned long long));
269 syslog (LOG_ERR, "nfs plugin: malloc "
275 for (i = 0; i < nfs2_procedures_names_num; i++)
276 values[i] = atoll (fields[i + 2]);
278 nfs_procedures_submit (plugin_instance, values,
279 nfs2_procedures_names,
280 nfs2_procedures_names_num);
284 else if (strncmp (fields[0], "proc3", 5) == 0)
287 unsigned long long *values;
289 if ((numfields - 2) != nfs3_procedures_names_num)
291 syslog (LOG_WARNING, "nfs plugin: Wrong "
292 "number of fields (= %i) "
293 "for NFSv3 statistics.",
298 snprintf (plugin_instance, sizeof (plugin_instance),
300 plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
302 values = (unsigned long long *) malloc (nfs3_procedures_names_num * sizeof (unsigned long long));
305 syslog (LOG_ERR, "nfs plugin: malloc "
311 for (i = 0; i < nfs3_procedures_names_num; i++)
312 values[i] = atoll (fields[i + 2]);
314 nfs_procedures_submit (plugin_instance, values,
315 nfs3_procedures_names,
316 nfs3_procedures_names_num);
320 } /* while (fgets (buffer, BUFSIZE, fh) != NULL) */
321 } /* void nfs_read_stats_file */
322 #endif /* defined(KERNEL_LINUX) */
325 #if HAVE_LIBKSTAT && 0
326 static void nfs2_read_kstat (kstat_t *ksp, char *inst)
328 unsigned long long values[18];
330 values[0] = get_kstat_value (ksp, "null");
331 values[1] = get_kstat_value (ksp, "getattr");
332 values[2] = get_kstat_value (ksp, "setattr");
333 values[3] = get_kstat_value (ksp, "root");
334 values[4] = get_kstat_value (ksp, "lookup");
335 values[5] = get_kstat_value (ksp, "readlink");
336 values[6] = get_kstat_value (ksp, "read");
337 values[7] = get_kstat_value (ksp, "wrcache");
338 values[8] = get_kstat_value (ksp, "write");
339 values[9] = get_kstat_value (ksp, "create");
340 values[10] = get_kstat_value (ksp, "remove");
341 values[11] = get_kstat_value (ksp, "rename");
342 values[12] = get_kstat_value (ksp, "link");
343 values[13] = get_kstat_value (ksp, "symlink");
344 values[14] = get_kstat_value (ksp, "mkdir");
345 values[15] = get_kstat_value (ksp, "rmdir");
346 values[16] = get_kstat_value (ksp, "readdir");
347 values[17] = get_kstat_value (ksp, "statfs");
349 nfs2_procedures_submit (values, inst);
354 static int nfs_read (void)
359 if ((fh = fopen ("/proc/net/rpc/nfs", "r")) != NULL)
361 nfs_read_stats_file (fh, "client");
365 if ((fh = fopen ("/proc/net/rpc/nfsd", "r")) != NULL)
367 nfs_read_stats_file (fh, "server");
371 /* #endif defined(KERNEL_LINUX) */
373 #elif HAVE_LIBKSTAT && 0
374 if (nfs2_ksp_client != NULL)
375 nfs2_read_kstat (nfs2_ksp_client, "client");
376 if (nfs2_ksp_server != NULL)
377 nfs2_read_kstat (nfs2_ksp_server, "server");
378 #endif /* defined(HAVE_LIBKSTAT) */
382 #endif /* NFS_HAVE_READ */
384 void module_register (void)
386 plugin_register_data_set (&procedure_ds);
389 plugin_register_read ("nfs", nfs_read);