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>
27 /* #if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) */
29 # define NFS_HAVE_READ 1
31 # define NFS_HAVE_READ 0
36 see http://www.missioncriticallinux.com/orph/NFS-Statistics
39 rpc_stat.netcnt Not used; always zero.
40 rpc_stat.netudpcnt Not used; always zero.
41 rpc_stat.nettcpcnt Not used; always zero.
42 rpc_stat.nettcpconn Not used; always zero.
45 rpc_stat.rpccnt The number of RPC calls.
46 rpc_stat.rpcretrans The number of retransmitted RPC calls.
47 rpc_stat.rpcauthrefresh The number of credential refreshes.
52 Procedure NFS Version NFS Version 3
53 Number Procedures Procedures
79 static data_source_t procedure_dsrc[1] =
81 {"value", DS_TYPE_COUNTER, 0, 4294967295.0}
84 static data_set_t procedure_ds =
86 "nfs_procedure", 1, procedure_dsrc
90 static const char *nfs2_procedures_names[] =
112 static int nfs2_procedures_names_num = 18;
114 static const char *nfs3_procedures_names[] =
140 static int nfs3_procedures_names_num = 22;
142 #if HAVE_LIBKSTAT && 0
143 extern kstat_ctl_t *kc;
144 static kstat_t *nfs2_ksp_client;
145 static kstat_t *nfs2_ksp_server;
146 static kstat_t *nfs3_ksp_client;
147 static kstat_t *nfs3_ksp_server;
148 static kstat_t *nfs4_ksp_client;
149 static kstat_t *nfs4_ksp_server;
152 /* Possibly TODO: NFSv4 statistics */
155 static int nfs_init (void)
157 #if HAVE_LIBKSTAT && 0
160 nfs2_ksp_client = NULL;
161 nfs2_ksp_server = NULL;
162 nfs3_ksp_client = NULL;
163 nfs3_ksp_server = NULL;
164 nfs4_ksp_client = NULL;
165 nfs4_ksp_server = NULL;
170 for (ksp_chain = kc->kc_chain; ksp_chain != NULL;
171 ksp_chain = ksp_chain->ks_next)
173 if (strncmp (ksp_chain->ks_module, "nfs", 3) != 0)
175 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v2", 13) == 0)
176 nfs2_ksp_server = ksp_chain;
177 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v3", 13) == 0)
178 nfs3_ksp_server = ksp_chain;
179 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v4", 13) == 0)
180 nfs4_ksp_server = ksp_chain;
181 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v2", 12) == 0)
182 nfs2_ksp_client = ksp_chain;
183 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v3", 12) == 0)
184 nfs3_ksp_client = ksp_chain;
185 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v4", 12) == 0)
186 nfs4_ksp_client = ksp_chain;
195 static void nfs_procedures_submit (const char *plugin_instance,
196 unsigned long long *val, const char **names, int len)
199 value_list_t vl = VALUE_LIST_INIT;
204 vl.time = time (NULL);
205 strcpy (vl.host, hostname_g);
206 strcpy (vl.plugin, "nfs");
207 strncpy (vl.plugin_instance, plugin_instance,
208 sizeof (vl.plugin_instance));
210 for (i = 0; i < len; i++)
212 values[0].counter = val[i];
213 strncpy (vl.type_instance, names[i],
214 sizeof (vl.type_instance));
215 DEBUG ("%s-%s/nfs_procedure-%s = %llu",
216 vl.plugin, vl.plugin_instance,
217 vl.type_instance, val[i]);
218 plugin_dispatch_values ("nfs_procedure", &vl);
220 } /* void nfs_procedures_submit */
223 static void nfs_read_stats_file (FILE *fh, char *inst)
225 char buffer[BUFSIZE];
227 char plugin_instance[DATA_MAX_NAME_LEN];
235 while (fgets (buffer, BUFSIZE, fh) != NULL)
237 numfields = strsplit (buffer, fields, 48);
239 if (((numfields - 2) != nfs2_procedures_names_num)
241 != nfs3_procedures_names_num))
244 if (strcmp (fields[0], "proc2") == 0)
247 unsigned long long *values;
249 if ((numfields - 2) != nfs2_procedures_names_num)
251 WARNING ("nfs plugin: Wrong "
252 "number of fields (= %i) "
253 "for NFSv2 statistics.",
258 snprintf (plugin_instance, sizeof (plugin_instance),
260 plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
262 values = (unsigned long long *) malloc (nfs2_procedures_names_num * sizeof (unsigned long long));
266 ERROR ("nfs plugin: malloc "
268 sstrerror (errno, errbuf, sizeof (errbuf)));
272 for (i = 0; i < nfs2_procedures_names_num; i++)
273 values[i] = atoll (fields[i + 2]);
275 nfs_procedures_submit (plugin_instance, values,
276 nfs2_procedures_names,
277 nfs2_procedures_names_num);
281 else if (strncmp (fields[0], "proc3", 5) == 0)
284 unsigned long long *values;
286 if ((numfields - 2) != nfs3_procedures_names_num)
288 WARNING ("nfs plugin: Wrong "
289 "number of fields (= %i) "
290 "for NFSv3 statistics.",
295 snprintf (plugin_instance, sizeof (plugin_instance),
297 plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
299 values = (unsigned long long *) malloc (nfs3_procedures_names_num * sizeof (unsigned long long));
303 ERROR ("nfs plugin: malloc "
305 sstrerror (errno, errbuf, sizeof (errbuf)));
309 for (i = 0; i < nfs3_procedures_names_num; i++)
310 values[i] = atoll (fields[i + 2]);
312 nfs_procedures_submit (plugin_instance, values,
313 nfs3_procedures_names,
314 nfs3_procedures_names_num);
318 } /* while (fgets (buffer, BUFSIZE, fh) != NULL) */
319 } /* void nfs_read_stats_file */
320 #endif /* defined(KERNEL_LINUX) */
323 #if HAVE_LIBKSTAT && 0
324 static void nfs2_read_kstat (kstat_t *ksp, char *inst)
326 unsigned long long values[18];
328 values[0] = get_kstat_value (ksp, "null");
329 values[1] = get_kstat_value (ksp, "getattr");
330 values[2] = get_kstat_value (ksp, "setattr");
331 values[3] = get_kstat_value (ksp, "root");
332 values[4] = get_kstat_value (ksp, "lookup");
333 values[5] = get_kstat_value (ksp, "readlink");
334 values[6] = get_kstat_value (ksp, "read");
335 values[7] = get_kstat_value (ksp, "wrcache");
336 values[8] = get_kstat_value (ksp, "write");
337 values[9] = get_kstat_value (ksp, "create");
338 values[10] = get_kstat_value (ksp, "remove");
339 values[11] = get_kstat_value (ksp, "rename");
340 values[12] = get_kstat_value (ksp, "link");
341 values[13] = get_kstat_value (ksp, "symlink");
342 values[14] = get_kstat_value (ksp, "mkdir");
343 values[15] = get_kstat_value (ksp, "rmdir");
344 values[16] = get_kstat_value (ksp, "readdir");
345 values[17] = get_kstat_value (ksp, "statfs");
347 nfs2_procedures_submit (values, inst);
351 static int nfs_read (void)
356 if ((fh = fopen ("/proc/net/rpc/nfs", "r")) != NULL)
358 nfs_read_stats_file (fh, "client");
362 if ((fh = fopen ("/proc/net/rpc/nfsd", "r")) != NULL)
364 nfs_read_stats_file (fh, "server");
368 /* #endif defined(KERNEL_LINUX) */
370 #elif HAVE_LIBKSTAT && 0
371 if (nfs2_ksp_client != NULL)
372 nfs2_read_kstat (nfs2_ksp_client, "client");
373 if (nfs2_ksp_server != NULL)
374 nfs2_read_kstat (nfs2_ksp_server, "server");
375 #endif /* defined(HAVE_LIBKSTAT) */
379 #endif /* NFS_HAVE_READ */
381 void module_register (modreg_e load)
383 if (load & MR_DATASETS)
384 plugin_register_data_set (&procedure_ds);
388 plugin_register_read ("nfs", nfs_read);
390 } /* void module_register */