Debian Codegraph @

src/nfs.c

raw
1 /**
2 * collectd - src/nfs.c
3 * Copyright (C) 2005,2006 Jason Pepas
4 * Copyright (C) 2012,2013 Florian Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Jason Pepas <cell at ices.utexas.edu>
21 * Florian octo Forster <octo at collectd.org>
22 * Cosmin Ioiart <cioiart at gmail.com>
23 **/
24
25 #include "collectd.h"
26
27 #include "plugin.h"
28 #include "utils/common/common.h"
29
30 #if KERNEL_NETBSD
31 // clang-format off
32 /*
33 * Explicit order is required or it will not compile, see GitHub issue #3333
34 */
35 #include <sys/param.h>
36 #include <sys/mount.h>
37 #include <sys/sysctl.h>
38 #include <nfs/rpcv2.h>
39 #include <nfs/nfsproto.h>
40 #include <nfs/nfs.h>
41 // clang-format on
42 #endif
43
44 #if HAVE_KSTAT_H
45 #include <kstat.h>
46 #endif
47
48 static const char *config_keys[] = {"ReportV2", "ReportV3", "ReportV4"};
49 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
50 static bool report_v2 = true;
51 static bool report_v3 = true;
52 static bool report_v4 = true;
53
54 /*
55 see /proc/net/rpc/nfs
56 see http://www.missioncriticallinux.com/orph/NFS-Statistics
57
58 net x x x x
59 rpc_stat.netcnt Not used; always zero.
60 rpc_stat.netudpcnt Not used; always zero.
61 rpc_stat.nettcpcnt Not used; always zero.
62 rpc_stat.nettcpconn Not used; always zero.
63
64 rpc x x x
65 rpc_stat.rpccnt The number of RPC calls.
66 rpc_stat.rpcretrans The number of retransmitted RPC calls.
67 rpc_stat.rpcauthrefresh The number of credential refreshes.
68
69 proc2 x x x...
70 proc3 x x x...
71
72 Procedure NFS Version NFS Version 3
73 Number Procedures Procedures
74
75 0 null null
76 1 getattr getattr
77 2 setattr setattr
78 3 root lookup
79 4 lookup access
80 5 readlink readlink
81 6 read read
82 7 wrcache write
83 8 write create
84 9 create mkdir
85 10 remove symlink
86 11 rename mknod
87 12 link remove
88 13 symlink rmdir
89 14 mkdir rename
90 15 rmdir link
91 16 readdir readdir
92 17 fsstat readdirplus
93 18 fsstat
94 19 fsinfo
95 20 pathconf
96 21 commit
97 */
98
99 #if KERNEL_LINUX || HAVE_LIBKSTAT
100 static const char *nfs2_procedures_names[] = {
101 "null", "getattr", "setattr", "root", "lookup", "readlink",
102 "read", "wrcache", "write", "create", "remove", "rename",
103 "link", "symlink", "mkdir", "rmdir", "readdir", "fsstat"};
104 static size_t nfs2_procedures_names_num =
105 STATIC_ARRAY_SIZE(nfs2_procedures_names);
106 #endif
107
108 static const char *nfs3_procedures_names[] = {
109 "null", "getattr", "setattr", "lookup", "access", "readlink",
110 "read", "write", "create", "mkdir", "symlink", "mknod",
111 "remove", "rmdir", "rename", "link", "readdir", "readdirplus",
112 "fsstat", "fsinfo", "pathconf", "commit"};
113 static size_t nfs3_procedures_names_num =
114 STATIC_ARRAY_SIZE(nfs3_procedures_names);
115
116 #if HAVE_LIBKSTAT
117 static const char *nfs4_procedures_names[] = {"null",
118 "compound",
119 "reserved",
120 "access",
121 "close",
122 "commit",
123 "create",
124 "delegpurge",
125 "delegreturn",
126 "getattr",
127 "getfh",
128 "link",
129 "lock",
130 "lockt",
131 "locku",
132 "lookup",
133 "lookupp",
134 "nverify",
135 "open",
136 "openattr",
137 "open_confirm",
138 "open_downgrade",
139 "putfh",
140 "putpubfh",
141 "putrootfh",
142 "read",
143 "readdir",
144 "readlink",
145 "remove",
146 "rename",
147 "renew",
148 "restorefh",
149 "savefh",
150 "secinfo",
151 "setattr",
152 "setclientid",
153 "setclientid_confirm",
154 "verify",
155 "write"};
156 static size_t nfs4_procedures_names_num =
157 STATIC_ARRAY_SIZE(nfs4_procedures_names);
158 #endif
159
160 #if KERNEL_LINUX
161 static const char *nfs4_server40_procedures_names[] = {"null",
162 "compound",
163 "reserved",
164 "access",
165 "close",
166 "commit",
167 "create",
168 "delegpurge",
169 "delegreturn",
170 "getattr",
171 "getfh",
172 "link",
173 "lock",
174 "lockt",
175 "locku",
176 "lookup",
177 "lookupp",
178 "nverify",
179 "open",
180 "openattr",
181 "open_confirm",
182 "open_downgrade",
183 "putfh",
184 "putpubfh",
185 "putrootfh",
186 "read",
187 "readdir",
188 "readlink",
189 "remove",
190 "rename",
191 "renew",
192 "restorefh",
193 "savefh",
194 "secinfo",
195 "setattr",
196 "setclientid",
197 "setcltid_confirm",
198 "verify",
199 "write",
200 "release_lockowner"};
201
202 static size_t nfs4_server40_procedures_names_num =
203 STATIC_ARRAY_SIZE(nfs4_server40_procedures_names);
204
205 static const char *nfs4_server4x_procedures_names[] = {
206 /* NFS 4.1 */
207 "backchannel_ctl", "bind_conn_to_session", "exchange_id", "create_session",
208 "destroy_session", "free_stateid", "get_dir_delegation", "getdeviceinfo",
209 "getdevicelist", "layoutcommit", "layoutget", "layoutreturn",
210 "secinfo_no_name", "sequence", "set_ssv", "test_stateid", "want_delegation",
211 "destroy_clientid", "reclaim_complete",
212 /* NFS 4.2 */
213 "allocate", /* 3.18 */
214 "copy", /* 3.18 */
215 "copy_notify", /* 3.18 */
216 "deallocate", /* 3.18 */
217 "ioadvise", /* 3.18 */
218 "layouterror", /* 3.18 */
219 "layoutstats", /* 3.18 */
220 "offloadcancel", /* 3.18 */
221 "offloadstatus", /* 3.18 */
222 "readplus", /* 3.18 */
223 "seek", /* 3.18 */
224 "write_same", /* 3.18 */
225 "clone" /* 4.5 */
226 };
227
228 #define NFS4_SERVER40_NUM_PROC \
229 (STATIC_ARRAY_SIZE(nfs4_server40_procedures_names))
230
231 #define NFS4_SERVER4X_NUM_PROC \
232 (STATIC_ARRAY_SIZE(nfs4_server40_procedures_names) + \
233 STATIC_ARRAY_SIZE(nfs4_server4x_procedures_names))
234
235 #define NFS4_SERVER_MAX_PROC (NFS4_SERVER4X_NUM_PROC)
236
237 static const char *nfs4_client40_procedures_names[] = {
238 "null",
239 "read",
240 "write",
241 "commit",
242 "open",
243 "open_confirm",
244 "open_noattr",
245 "open_downgrade",
246 "close",
247 "setattr",
248 "fsinfo",
249 "renew",
250 "setclientid",
251 "setclientid_confirm",
252 "lock",
253 "lockt",
254 "locku",
255 "access",
256 "getattr",
257 "lookup",
258 "lookupp",
259 "remove",
260 "rename",
261 "link",
262 "symlink",
263 "create",
264 "pathconf",
265 "statfs",
266 "readlink",
267 "readdir",
268 "server_caps",
269 "delegreturn",
270 "getacl",
271 "setacl",
272 "fs_locations", /* |35| 2.6.18 */
273 "release_lockowner", /* |42| 2.6.36 */
274 "secinfo", /* |46| 2.6.39 */
275 "fsid_present" /* |54| 3.13 */
276 };
277
278 static const char *nfs4_client4x_procedures_names[] = {
279 /* NFS 4.1 */
280 "exchange_id", /* |40| 2.6.30 */
281 "create_session", /* |40| 2.6.30 */
282 "destroy_session", /* |40| 2.6.30 */
283 "sequence", /* |40| 2.6.30 */
284 "get_lease_time", /* |40| 2.6.30 */
285 "reclaim_complete", /* |41| 2.6.33 */
286 "layoutget", /* |44| 2.6.37 */
287 "getdeviceinfo", /* |44| 2.6.37 */
288 "layoutcommit", /* |46| 2.6.39 */
289 "layoutreturn", /* |47| 3.0 */
290 "secinfo_no_name", /* |51| 3.1 */
291 "test_stateid", /* |51| 3.1 */
292 "free_stateid", /* |51| 3.1 */
293 "getdevicelist", /* |51| 3.1 */
294 "bind_conn_to_session", /* |53| 3.5 */
295 "destroy_clientid", /* |53| 3.5 */
296 /* NFS 4.2 */
297 "seek", /* |55| 3.18 */
298 "allocate", /* |57| 3.19 */
299 "deallocate", /* |57| 3.19 */
300 "layoutstats", /* |58| 4.2 */
301 "clone", /* |59| 4.4 */
302 "copy" /* |60| 4.7 */
303 };
304
305 #define NFS4_CLIENT40_NUM_PROC \
306 (STATIC_ARRAY_SIZE(nfs4_client40_procedures_names))
307
308 #define NFS4_CLIENT4X_NUM_PROC \
309 (STATIC_ARRAY_SIZE(nfs4_client40_procedures_names) + \
310 STATIC_ARRAY_SIZE(nfs4_client4x_procedures_names))
311
312 #define NFS4_CLIENT_MAX_PROC (NFS4_CLIENT4X_NUM_PROC)
313
314 #endif
315
316 #if HAVE_LIBKSTAT
317 extern kstat_ctl_t *kc;
318 static kstat_t *nfs2_ksp_client;
319 static kstat_t *nfs2_ksp_server;
320 static kstat_t *nfs3_ksp_client;
321 static kstat_t *nfs3_ksp_server;
322 static kstat_t *nfs4_ksp_client;
323 static kstat_t *nfs4_ksp_server;
324 #endif
325
326 static int nfs_config(const char *key, const char *value) {
327 if (strcasecmp(key, "ReportV2") == 0)
328 report_v2 = IS_TRUE(value);
329 else if (strcasecmp(key, "ReportV3") == 0)
330 report_v3 = IS_TRUE(value);
331 else if (strcasecmp(key, "ReportV4") == 0)
332 report_v4 = IS_TRUE(value);
333 else
334 return -1;
335
336 return 0;
337 }
338
339 #if KERNEL_LINUX || KERNEL_NETBSD
340 static int nfs_init(void) { return 0; }
341 /* #endif KERNEL_LINUX */
342
343 #elif HAVE_LIBKSTAT
344 static int nfs_init(void) {
345 nfs2_ksp_client = NULL;
346 nfs2_ksp_server = NULL;
347 nfs3_ksp_client = NULL;
348 nfs3_ksp_server = NULL;
349 nfs4_ksp_client = NULL;
350 nfs4_ksp_server = NULL;
351
352 if (kc == NULL)
353 return -1;
354
355 for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
356 ksp_chain = ksp_chain->ks_next) {
357 if (strncmp(ksp_chain->ks_module, "nfs", 3) != 0)
358 continue;
359 else if (strncmp(ksp_chain->ks_name, "rfsproccnt_v2", 13) == 0)
360 nfs2_ksp_server = ksp_chain;
361 else if (strncmp(ksp_chain->ks_name, "rfsproccnt_v3", 13) == 0)
362 nfs3_ksp_server = ksp_chain;
363 else if (strncmp(ksp_chain->ks_name, "rfsproccnt_v4", 13) == 0)
364 nfs4_ksp_server = ksp_chain;
365 else if (strncmp(ksp_chain->ks_name, "rfsreqcnt_v2", 12) == 0)
366 nfs2_ksp_client = ksp_chain;
367 else if (strncmp(ksp_chain->ks_name, "rfsreqcnt_v3", 12) == 0)
368 nfs3_ksp_client = ksp_chain;
369 else if (strncmp(ksp_chain->ks_name, "rfsreqcnt_v4", 12) == 0)
370 nfs4_ksp_client = ksp_chain;
371 }
372
373 return 0;
374 } /* int nfs_init */
375 #endif
376
377 static void nfs_procedures_submit(const char *plugin_instance,
378 const char **type_instances, value_t *values,
379 size_t values_num) {
380 value_list_t vl = VALUE_LIST_INIT;
381
382 vl.values_len = 1;
383 sstrncpy(vl.plugin, "nfs", sizeof(vl.plugin));
384 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
385 sstrncpy(vl.type, "nfs_procedure", sizeof(vl.type));
386
387 for (size_t i = 0; i < values_num; i++) {
388 vl.values = values + i;
389 sstrncpy(vl.type_instance, type_instances[i], sizeof(vl.type_instance));
390 plugin_dispatch_values(&vl);
391 }
392 } /* void nfs_procedures_submit */
393
394 #if KERNEL_LINUX
395 static void nfs_submit_fields(int nfs_version, const char *instance,
396 char **fields, size_t fields_num,
397 const char **proc_names) {
398 char plugin_instance[DATA_MAX_NAME_LEN];
399 value_t values[fields_num];
400
401 snprintf(plugin_instance, sizeof(plugin_instance), "v%i%s", nfs_version,
402 instance);
403
404 for (size_t i = 0; i < fields_num; i++)
405 (void)parse_value(fields[i], &values[i], DS_TYPE_DERIVE);
406
407 nfs_procedures_submit(plugin_instance, proc_names, values, fields_num);
408 }
409
410 static int nfs_submit_fields_safe(int nfs_version, const char *instance,
411 char **fields, size_t fields_num,
412 const char **proc_names,
413 size_t proc_names_num) {
414 if (fields_num != proc_names_num) {
415 WARNING("nfs plugin: Wrong number of fields for "
416 "NFSv%i %s statistics. Expected %" PRIsz ", got %" PRIsz ".",
417 nfs_version, instance, proc_names_num, fields_num);
418 return EINVAL;
419 }
420
421 nfs_submit_fields(nfs_version, instance, fields, fields_num, proc_names);
422
423 return 0;
424 }
425
426 static int nfs_submit_nfs4_server(const char *instance, char **fields,
427 size_t fields_num) {
428 static int suppress_warning;
429 size_t proc4x_names_num;
430
431 switch (fields_num) {
432 case NFS4_SERVER40_NUM_PROC:
433 case NFS4_SERVER40_NUM_PROC + 19: /* NFS 4.1 */
434 case NFS4_SERVER40_NUM_PROC + 31: /* NFS 4.2 */
435 case NFS4_SERVER40_NUM_PROC + 32: /* NFS 4.2 */
436 break;
437 default:
438 if (!suppress_warning) {
439 WARNING("nfs plugin: Unexpected number of fields for "
440 "NFSv4 %s statistics: %" PRIsz ". ",
441 instance, fields_num);
442 }
443
444 if (fields_num > NFS4_SERVER_MAX_PROC) {
445 fields_num = NFS4_SERVER_MAX_PROC;
446 suppress_warning = 1;
447 } else {
448 return EINVAL;
449 }
450 }
451
452 nfs_submit_fields(4, instance, fields, nfs4_server40_procedures_names_num,
453 nfs4_server40_procedures_names);
454
455 if (fields_num > nfs4_server40_procedures_names_num) {
456 proc4x_names_num = fields_num - nfs4_server40_procedures_names_num;
457 fields += nfs4_server40_procedures_names_num;
458
459 nfs_submit_fields(4, instance, fields, proc4x_names_num,
460 nfs4_server4x_procedures_names);
461 }
462
463 return 0;
464 }
465
466 static int nfs_submit_nfs4_client(const char *instance, char **fields,
467 size_t fields_num) {
468 size_t proc40_names_num, proc4x_names_num;
469
470 static int suppress_warning;
471
472 switch (fields_num) {
473 case 34:
474 case 35:
475 case 36:
476 case 37:
477 case 38:
478 /* 4.0-only configuration */
479 proc40_names_num = fields_num;
480 break;
481 case 40:
482 case 41:
483 proc40_names_num = 35;
484 break;
485 case 42:
486 case 44:
487 proc40_names_num = 36;
488 break;
489 case 46:
490 case 47:
491 case 51:
492 case 53:
493 proc40_names_num = 37;
494 break;
495 case 54:
496 case 55:
497 case 57:
498 case 58:
499 case 59:
500 case 60:
501 proc40_names_num = 38;
502 break;
503 default:
504 if (!suppress_warning) {
505 WARNING("nfs plugin: Unexpected number of fields for NFSv4 %s "
506 "statistics: %" PRIsz ". ",
507 instance, fields_num);
508 }
509
510 if (fields_num > 34) {
511 /* safe fallback to basic nfs40 procedures */
512 fields_num = 34;
513 proc40_names_num = 34;
514
515 suppress_warning = 1;
516 } else {
517 return EINVAL;
518 }
519 }
520
521 nfs_submit_fields(4, instance, fields, proc40_names_num,
522 nfs4_client40_procedures_names);
523
524 if (fields_num > proc40_names_num) {
525 proc4x_names_num = fields_num - proc40_names_num;
526 fields += proc40_names_num;
527
528 nfs_submit_fields(4, instance, fields, proc4x_names_num,
529 nfs4_client4x_procedures_names);
530 }
531
532 return 0;
533 }
534
535 static void nfs_read_linux(FILE *fh, const char *inst) {
536 char buffer[1024];
537
538 // The stats line is prefixed with type and number of fields, thus plus 2
539 char *fields[MAX(NFS4_SERVER_MAX_PROC, NFS4_CLIENT_MAX_PROC) + 2];
540 int fields_num = 0;
541
542 if (fh == NULL)
543 return;
544
545 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
546 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
547
548 if (fields_num < 3)
549 continue;
550
551 if (strcmp(fields[0], "proc2") == 0 && report_v2) {
552 nfs_submit_fields_safe(/* version = */ 2, inst, fields + 2,
553 (size_t)(fields_num - 2), nfs2_procedures_names,
554 nfs2_procedures_names_num);
555 } else if (strncmp(fields[0], "proc3", 5) == 0 && report_v3) {
556 nfs_submit_fields_safe(/* version = */ 3, inst, fields + 2,
557 (size_t)(fields_num - 2), nfs3_procedures_names,
558 nfs3_procedures_names_num);
559 } else if (strcmp(fields[0], "proc4ops") == 0 && report_v4) {
560 if (inst[0] == 's')
561 nfs_submit_nfs4_server(inst, fields + 2, (size_t)(fields_num - 2));
562 } else if (strcmp(fields[0], "proc4") == 0 && report_v4) {
563 if (inst[0] == 'c')
564 nfs_submit_nfs4_client(inst, fields + 2, (size_t)(fields_num - 2));
565 }
566 } /* while (fgets) */
567 } /* void nfs_read_linux */
568 #endif /* KERNEL_LINUX */
569
570 #if HAVE_LIBKSTAT
571 static int nfs_read_kstat(kstat_t *ksp, int nfs_version, const char *inst,
572 char const **proc_names, size_t proc_names_num) {
573 char plugin_instance[DATA_MAX_NAME_LEN];
574 value_t values[proc_names_num];
575
576 if (ksp == NULL)
577 return EINVAL;
578
579 snprintf(plugin_instance, sizeof(plugin_instance), "v%i%s", nfs_version,
580 inst);
581
582 kstat_read(kc, ksp, NULL);
583 for (size_t i = 0; i < proc_names_num; i++) {
584 /* The name passed to kstat_data_lookup() doesn't have the
585 * "const" modifier, so we need to copy the name here. */
586 char name[32];
587 sstrncpy(name, proc_names[i], sizeof(name));
588
589 values[i].counter = (derive_t)get_kstat_value(ksp, name);
590 }
591
592 nfs_procedures_submit(plugin_instance, proc_names, values, proc_names_num);
593 return 0;
594 }
595 #endif
596
597 #if KERNEL_LINUX
598 static int nfs_read(void) {
599 FILE *fh;
600
601 if ((fh = fopen("/proc/net/rpc/nfs", "r")) != NULL) {
602 nfs_read_linux(fh, "client");
603 fclose(fh);
604 }
605
606 if ((fh = fopen("/proc/net/rpc/nfsd", "r")) != NULL) {
607 nfs_read_linux(fh, "server");
608 fclose(fh);
609 }
610
611 return 0;
612 }
613 /* #endif KERNEL_LINUX */
614
615 #elif KERNEL_NETBSD
616 static int nfs_read(void) {
617 struct nfsstats ns;
618 size_t size = sizeof(ns);
619 int mib[] = {CTL_VFS, 2, NFS_NFSSTATS};
620 value_t values[nfs3_procedures_names_num];
621 int i;
622
623 /* NetBSD reports v2 statistics mapped to v3 and doen't yet support v4 */
624 if (report_v2) {
625 WARNING("nfs plugin: NFSv2 statistics have been requested "
626 "but they are mapped to NFSv3 statistics in the kernel on NetBSD.");
627 return 0;
628 }
629
630 if (report_v4) {
631 WARNING("nfs plugin: NFSv4 statistics have been requested "
632 "but they are not yet supported on NetBSD.");
633 return 0;
634 }
635
636 if (sysctl(mib, STATIC_ARRAY_SIZE(mib), &ns, &size, NULL, 0) != 0)
637 return 1;
638
639 for (i = 0; i < nfs3_procedures_names_num; i++)
640 values[i].counter = (derive_t)ns.rpccnt[i];
641 nfs_procedures_submit("v3client", nfs3_procedures_names, values,
642 nfs3_procedures_names_num);
643
644 for (i = 0; i < nfs3_procedures_names_num; i++)
645 values[i].counter = (derive_t)ns.srvrpccnt[i];
646 nfs_procedures_submit("v3server", nfs3_procedures_names, values,
647 nfs3_procedures_names_num);
648
649 return 0;
650 }
651 /* #endif KERNEL_NETBSD */
652
653 #elif HAVE_LIBKSTAT
654 static int nfs_read(void) {
655 if (report_v2) {
656 nfs_read_kstat(nfs2_ksp_client, /* version = */ 2, "client",
657 nfs2_procedures_names, nfs2_procedures_names_num);
658 nfs_read_kstat(nfs2_ksp_server, /* version = */ 2, "server",
659 nfs2_procedures_names, nfs2_procedures_names_num);
660 }
661 if (report_v3) {
662 nfs_read_kstat(nfs3_ksp_client, /* version = */ 3, "client",
663 nfs3_procedures_names, nfs3_procedures_names_num);
664 nfs_read_kstat(nfs3_ksp_server, /* version = */ 3, "server",
665 nfs3_procedures_names, nfs3_procedures_names_num);
666 }
667 if (report_v4) {
668 nfs_read_kstat(nfs4_ksp_client, /* version = */ 4, "client",
669 nfs4_procedures_names, nfs4_procedures_names_num);
670 nfs_read_kstat(nfs4_ksp_server, /* version = */ 4, "server",
671 nfs4_procedures_names, nfs4_procedures_names_num);
672 }
673
674 return 0;
675 }
676 #endif /* HAVE_LIBKSTAT */
677
678 void module_register(void) {
679 plugin_register_config("nfs", nfs_config, config_keys, config_keys_num);
680 plugin_register_init("nfs", nfs_init);
681 plugin_register_read("nfs", nfs_read);
682 } /* void module_register */