3 * Copyright (C) 2005,2006 Jason Pepas
4 * Copyright (C) 2012,2013 Florian Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Jason Pepas <cell at ices.utexas.edu>
21 * Florian octo Forster <octo at collectd.org>
22 * Cosmin Ioiart <cioiart at gmail.com>
35 see http://www.missioncriticallinux.com/orph/NFS-Statistics
38 rpc_stat.netcnt Not used; always zero.
39 rpc_stat.netudpcnt Not used; always zero.
40 rpc_stat.nettcpcnt Not used; always zero.
41 rpc_stat.nettcpconn Not used; always zero.
44 rpc_stat.rpccnt The number of RPC calls.
45 rpc_stat.rpcretrans The number of retransmitted RPC calls.
46 rpc_stat.rpcauthrefresh The number of credential refreshes.
51 Procedure NFS Version NFS Version 3
52 Number Procedures Procedures
78 static const char *nfs2_procedures_names[] =
99 static size_t nfs2_procedures_names_num = STATIC_ARRAY_SIZE (nfs2_procedures_names);
101 static const char *nfs3_procedures_names[] =
126 static size_t nfs3_procedures_names_num = STATIC_ARRAY_SIZE (nfs3_procedures_names);
129 static const char *nfs4_procedures_names[] =
167 "setclientid_confirm",
171 static size_t nfs4_procedures_names_num = STATIC_ARRAY_SIZE (nfs4_procedures_names);
175 static const char *nfs4_server40_procedures_names[] =
219 static size_t nfs4_server40_procedures_names_num = STATIC_ARRAY_SIZE (nfs4_server40_procedures_names);
221 static const char *nfs4_server41_procedures_names[] =
224 "bind_conn_to_session",
229 "get_dir_delegation",
244 static size_t nfs4_server41_procedures_names_num = STATIC_ARRAY_SIZE (nfs4_server41_procedures_names);
246 #define NFS4_SERVER40_NUM_PROC ( \
247 STATIC_ARRAY_SIZE (nfs4_server40_procedures_names) )
249 #define NFS4_SERVER41_NUM_PROC ( \
250 STATIC_ARRAY_SIZE (nfs4_server40_procedures_names) + \
251 STATIC_ARRAY_SIZE (nfs4_server41_procedures_names) )
253 #define NFS4_SERVER_MAX_PROC (NFS4_SERVER41_NUM_PROC)
255 static const char *nfs4_client40_procedures_names[] =
270 "setclientid_confirm",
291 "fs_locations", /* |35| 2.6.18 */
292 "release_lockowner", /* |42| 2.6.36 */
293 "secinfo", /* |46| 2.6.39 */
294 "fsid_present" /* |54| 3.13 */
297 static const char *nfs4_client41_procedures_names[] =
299 "exchange_id", /* |40| 2.6.30 */
300 "create_session", /* |40| 2.6.30 */
301 "destroy_session", /* |40| 2.6.30 */
302 "sequence", /* |40| 2.6.30 */
303 "get_lease_time", /* |40| 2.6.30 */
304 "reclaim_complete", /* |41| 2.6.33 */
305 "layoutget", /* |44| 2.6.37 */
306 "getdeviceinfo", /* |44| 2.6.37 */
307 "layoutcommit", /* |46| 2.6.39 */
308 "layoutreturn", /* |47| 3.0 */
309 "secinfo_no_name", /* |51| 3.1 */
310 "test_stateid", /* |51| 3.1 */
311 "free_stateid", /* |51| 3.1 */
312 "getdevicelist", /* |51| 3.1 */
313 "bind_conn_to_session", /* |53| 3.5 */
314 "destroy_clientid" /* |53| 3.5 */
317 #define NFS4_CLIENT40_NUM_PROC ( \
318 STATIC_ARRAY_SIZE (nfs4_client40_procedures_names) )
320 #define NFS4_CLIENT41_NUM_PROC ( \
321 STATIC_ARRAY_SIZE (nfs4_client40_procedures_names) + \
322 STATIC_ARRAY_SIZE (nfs4_client41_procedures_names) )
324 #define NFS4_CLIENT_MAX_PROC (NFS4_CLIENT41_NUM_PROC)
329 extern kstat_ctl_t *kc;
330 static kstat_t *nfs2_ksp_client;
331 static kstat_t *nfs2_ksp_server;
332 static kstat_t *nfs3_ksp_client;
333 static kstat_t *nfs3_ksp_server;
334 static kstat_t *nfs4_ksp_client;
335 static kstat_t *nfs4_ksp_server;
339 static int nfs_init (void)
343 /* #endif KERNEL_LINUX */
346 static int nfs_init (void)
348 kstat_t *ksp_chain = NULL;
350 nfs2_ksp_client = NULL;
351 nfs2_ksp_server = NULL;
352 nfs3_ksp_client = NULL;
353 nfs3_ksp_server = NULL;
354 nfs4_ksp_client = NULL;
355 nfs4_ksp_server = NULL;
360 for (ksp_chain = kc->kc_chain; ksp_chain != NULL;
361 ksp_chain = ksp_chain->ks_next)
363 if (strncmp (ksp_chain->ks_module, "nfs", 3) != 0)
365 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v2", 13) == 0)
366 nfs2_ksp_server = ksp_chain;
367 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v3", 13) == 0)
368 nfs3_ksp_server = ksp_chain;
369 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v4", 13) == 0)
370 nfs4_ksp_server = ksp_chain;
371 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v2", 12) == 0)
372 nfs2_ksp_client = ksp_chain;
373 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v3", 12) == 0)
374 nfs3_ksp_client = ksp_chain;
375 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v4", 12) == 0)
376 nfs4_ksp_client = ksp_chain;
383 static void nfs_procedures_submit (const char *plugin_instance,
384 const char **type_instances,
385 value_t *values, size_t values_num)
387 value_list_t vl = VALUE_LIST_INIT;
391 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
392 sstrncpy (vl.plugin, "nfs", sizeof (vl.plugin));
393 sstrncpy (vl.plugin_instance, plugin_instance,
394 sizeof (vl.plugin_instance));
395 sstrncpy (vl.type, "nfs_procedure", sizeof (vl.type));
397 for (i = 0; i < values_num; i++)
399 vl.values = values + i;
400 sstrncpy (vl.type_instance, type_instances[i],
401 sizeof (vl.type_instance));
402 plugin_dispatch_values (&vl);
404 } /* void nfs_procedures_submit */
407 static void nfs_submit_fields (int nfs_version, const char *instance,
408 char **fields, size_t fields_num, const char **proc_names)
410 char plugin_instance[DATA_MAX_NAME_LEN];
411 value_t values[fields_num];
414 ssnprintf (plugin_instance, sizeof (plugin_instance), "v%i%s",
415 nfs_version, instance);
417 for (i = 0; i < fields_num; i++)
418 (void) parse_value (fields[i], &values[i], DS_TYPE_DERIVE);
420 nfs_procedures_submit (plugin_instance, proc_names, values,
424 static int nfs_submit_fields_safe (int nfs_version, const char *instance,
425 char **fields, size_t fields_num,
426 const char **proc_names, size_t proc_names_num)
428 if (fields_num != proc_names_num)
430 WARNING ("nfs plugin: Wrong number of fields for "
431 "NFSv%i %s statistics. Expected %zu, got %zu.",
432 nfs_version, instance,
433 proc_names_num, fields_num);
437 nfs_submit_fields (nfs_version, instance, fields, fields_num,
443 static int nfs_submit_nfs4_server (const char *instance, char **fields,
446 static int suppress_warning = 0;
448 if (fields_num != NFS4_SERVER40_NUM_PROC &&
449 fields_num != NFS4_SERVER41_NUM_PROC)
451 if (!suppress_warning)
453 WARNING ("nfs plugin: Unexpected number of fields for "
454 "NFSv4 %s statistics: %zu. ",
455 instance, fields_num);
458 if (fields_num > NFS4_SERVER_MAX_PROC)
460 fields_num = NFS4_SERVER_MAX_PROC;
461 suppress_warning = 1;
469 nfs_submit_fields (4, instance, fields,
470 nfs4_server40_procedures_names_num,
471 nfs4_server40_procedures_names);
473 if (fields_num >= NFS4_SERVER41_NUM_PROC)
475 fields += nfs4_server40_procedures_names_num;
477 nfs_submit_fields (4, instance, fields,
478 nfs4_server41_procedures_names_num,
479 nfs4_server41_procedures_names);
485 static int nfs_submit_nfs4_client (const char *instance, char **fields,
488 size_t proc40_names_num, proc41_names_num;
490 static int suppress_warning = 0;
499 /* 4.0-only configuration */
500 proc40_names_num = fields_num;
504 proc40_names_num = 35;
508 proc40_names_num = 36;
513 proc40_names_num = 37;
516 proc40_names_num = 38;
519 if (!suppress_warning)
521 WARNING ("nfs plugin: Unexpected number of "
522 "fields for NFSv4 %s "
524 instance, fields_num);
529 /* safe fallback to basic nfs40 procedures */
531 proc40_names_num = 34;
533 suppress_warning = 1;
541 nfs_submit_fields (4, instance, fields, proc40_names_num,
542 nfs4_client40_procedures_names);
544 if (fields_num > proc40_names_num)
546 proc41_names_num = fields_num - proc40_names_num;
547 fields += proc40_names_num;
549 nfs_submit_fields (4, instance, fields,proc41_names_num,
550 nfs4_client41_procedures_names);
556 static void nfs_read_linux (FILE *fh, char *inst)
566 while (fgets (buffer, sizeof (buffer), fh) != NULL)
568 fields_num = strsplit (buffer,
569 fields, STATIC_ARRAY_SIZE (fields));
574 if (strcmp (fields[0], "proc2") == 0)
576 nfs_submit_fields_safe (/* version = */ 2, inst,
577 fields + 2, (size_t) (fields_num - 2),
578 nfs2_procedures_names,
579 nfs2_procedures_names_num);
581 else if (strncmp (fields[0], "proc3", 5) == 0)
583 nfs_submit_fields_safe (/* version = */ 3, inst,
584 fields + 2, (size_t) (fields_num - 2),
585 nfs3_procedures_names,
586 nfs3_procedures_names_num);
588 else if (strcmp (fields[0], "proc4ops") == 0)
591 nfs_submit_nfs4_server (inst, fields + 2,
592 (size_t) (fields_num - 2));
594 else if (strcmp (fields[0], "proc4") == 0)
597 nfs_submit_nfs4_client (inst, fields + 2,
598 (size_t) (fields_num - 2));
600 } /* while (fgets) */
601 } /* void nfs_read_linux */
602 #endif /* KERNEL_LINUX */
605 static int nfs_read_kstat (kstat_t *ksp, int nfs_version, char *inst,
606 char const **proc_names, size_t proc_names_num)
608 char plugin_instance[DATA_MAX_NAME_LEN];
609 value_t values[proc_names_num];
615 ssnprintf (plugin_instance, sizeof (plugin_instance), "v%i%s",
618 kstat_read(kc, ksp, NULL);
619 for (i = 0; i < proc_names_num; i++)
621 /* The name passed to kstat_data_lookup() doesn't have the
622 * "const" modifier, so we need to copy the name here. */
624 sstrncpy (name, proc_names[i], sizeof (name));
626 values[i].counter = (derive_t) get_kstat_value (ksp, name);
629 nfs_procedures_submit (plugin_instance, proc_names, values,
636 static int nfs_read (void)
640 if ((fh = fopen ("/proc/net/rpc/nfs", "r")) != NULL)
642 nfs_read_linux (fh, "client");
646 if ((fh = fopen ("/proc/net/rpc/nfsd", "r")) != NULL)
648 nfs_read_linux (fh, "server");
654 /* #endif KERNEL_LINUX */
657 static int nfs_read (void)
659 nfs_read_kstat (nfs2_ksp_client, /* version = */ 2, "client",
660 nfs2_procedures_names, nfs2_procedures_names_num);
661 nfs_read_kstat (nfs2_ksp_server, /* version = */ 2, "server",
662 nfs2_procedures_names, nfs2_procedures_names_num);
663 nfs_read_kstat (nfs3_ksp_client, /* version = */ 3, "client",
664 nfs3_procedures_names, nfs3_procedures_names_num);
665 nfs_read_kstat (nfs3_ksp_server, /* version = */ 3, "server",
666 nfs3_procedures_names, nfs3_procedures_names_num);
667 nfs_read_kstat (nfs4_ksp_client, /* version = */ 4, "client",
668 nfs4_procedures_names, nfs4_procedures_names_num);
669 nfs_read_kstat (nfs4_ksp_server, /* version = */ 4, "server",
670 nfs4_procedures_names, nfs4_procedures_names_num);
674 #endif /* HAVE_LIBKSTAT */
676 void module_register (void)
678 plugin_register_init ("nfs", nfs_init);
679 plugin_register_read ("nfs", nfs_read);
680 } /* void module_register */