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
80 static const char *nfs2_procedures_names[] =
102 static int nfs2_procedures_names_num = 18;
104 static const char *nfs3_procedures_names[] =
130 static int nfs3_procedures_names_num = 22;
132 #if HAVE_LIBKSTAT && 0
133 extern kstat_ctl_t *kc;
134 static kstat_t *nfs2_ksp_client;
135 static kstat_t *nfs2_ksp_server;
136 static kstat_t *nfs3_ksp_client;
137 static kstat_t *nfs3_ksp_server;
138 static kstat_t *nfs4_ksp_client;
139 static kstat_t *nfs4_ksp_server;
142 /* Possibly TODO: NFSv4 statistics */
145 static int nfs_init (void)
147 #if HAVE_LIBKSTAT && 0
150 nfs2_ksp_client = NULL;
151 nfs2_ksp_server = NULL;
152 nfs3_ksp_client = NULL;
153 nfs3_ksp_server = NULL;
154 nfs4_ksp_client = NULL;
155 nfs4_ksp_server = NULL;
160 for (ksp_chain = kc->kc_chain; ksp_chain != NULL;
161 ksp_chain = ksp_chain->ks_next)
163 if (strncmp (ksp_chain->ks_module, "nfs", 3) != 0)
165 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v2", 13) == 0)
166 nfs2_ksp_server = ksp_chain;
167 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v3", 13) == 0)
168 nfs3_ksp_server = ksp_chain;
169 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v4", 13) == 0)
170 nfs4_ksp_server = ksp_chain;
171 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v2", 12) == 0)
172 nfs2_ksp_client = ksp_chain;
173 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v3", 12) == 0)
174 nfs3_ksp_client = ksp_chain;
175 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v4", 12) == 0)
176 nfs4_ksp_client = ksp_chain;
185 static void nfs_procedures_submit (const char *plugin_instance,
186 unsigned long long *val, const char **names, int len)
189 value_list_t vl = VALUE_LIST_INIT;
194 vl.time = time (NULL);
195 strcpy (vl.host, hostname_g);
196 strcpy (vl.plugin, "nfs");
197 strncpy (vl.plugin_instance, plugin_instance,
198 sizeof (vl.plugin_instance));
200 for (i = 0; i < len; i++)
202 values[0].counter = val[i];
203 strncpy (vl.type_instance, names[i],
204 sizeof (vl.type_instance));
205 DEBUG ("%s-%s/nfs_procedure-%s = %llu",
206 vl.plugin, vl.plugin_instance,
207 vl.type_instance, val[i]);
208 plugin_dispatch_values ("nfs_procedure", &vl);
210 } /* void nfs_procedures_submit */
213 static void nfs_read_stats_file (FILE *fh, char *inst)
215 char buffer[BUFSIZE];
217 char plugin_instance[DATA_MAX_NAME_LEN];
225 while (fgets (buffer, BUFSIZE, fh) != NULL)
227 numfields = strsplit (buffer, fields, 48);
229 if (((numfields - 2) != nfs2_procedures_names_num)
231 != nfs3_procedures_names_num))
234 if (strcmp (fields[0], "proc2") == 0)
237 unsigned long long *values;
239 if ((numfields - 2) != nfs2_procedures_names_num)
241 WARNING ("nfs plugin: Wrong "
242 "number of fields (= %i) "
243 "for NFSv2 statistics.",
248 snprintf (plugin_instance, sizeof (plugin_instance),
250 plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
252 values = (unsigned long long *) malloc (nfs2_procedures_names_num * sizeof (unsigned long long));
256 ERROR ("nfs plugin: malloc "
258 sstrerror (errno, errbuf, sizeof (errbuf)));
262 for (i = 0; i < nfs2_procedures_names_num; i++)
263 values[i] = atoll (fields[i + 2]);
265 nfs_procedures_submit (plugin_instance, values,
266 nfs2_procedures_names,
267 nfs2_procedures_names_num);
271 else if (strncmp (fields[0], "proc3", 5) == 0)
274 unsigned long long *values;
276 if ((numfields - 2) != nfs3_procedures_names_num)
278 WARNING ("nfs plugin: Wrong "
279 "number of fields (= %i) "
280 "for NFSv3 statistics.",
285 snprintf (plugin_instance, sizeof (plugin_instance),
287 plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
289 values = (unsigned long long *) malloc (nfs3_procedures_names_num * sizeof (unsigned long long));
293 ERROR ("nfs plugin: malloc "
295 sstrerror (errno, errbuf, sizeof (errbuf)));
299 for (i = 0; i < nfs3_procedures_names_num; i++)
300 values[i] = atoll (fields[i + 2]);
302 nfs_procedures_submit (plugin_instance, values,
303 nfs3_procedures_names,
304 nfs3_procedures_names_num);
308 } /* while (fgets (buffer, BUFSIZE, fh) != NULL) */
309 } /* void nfs_read_stats_file */
310 #endif /* defined(KERNEL_LINUX) */
313 #if HAVE_LIBKSTAT && 0
314 static void nfs2_read_kstat (kstat_t *ksp, char *inst)
316 unsigned long long values[18];
318 values[0] = get_kstat_value (ksp, "null");
319 values[1] = get_kstat_value (ksp, "getattr");
320 values[2] = get_kstat_value (ksp, "setattr");
321 values[3] = get_kstat_value (ksp, "root");
322 values[4] = get_kstat_value (ksp, "lookup");
323 values[5] = get_kstat_value (ksp, "readlink");
324 values[6] = get_kstat_value (ksp, "read");
325 values[7] = get_kstat_value (ksp, "wrcache");
326 values[8] = get_kstat_value (ksp, "write");
327 values[9] = get_kstat_value (ksp, "create");
328 values[10] = get_kstat_value (ksp, "remove");
329 values[11] = get_kstat_value (ksp, "rename");
330 values[12] = get_kstat_value (ksp, "link");
331 values[13] = get_kstat_value (ksp, "symlink");
332 values[14] = get_kstat_value (ksp, "mkdir");
333 values[15] = get_kstat_value (ksp, "rmdir");
334 values[16] = get_kstat_value (ksp, "readdir");
335 values[17] = get_kstat_value (ksp, "statfs");
337 nfs2_procedures_submit (values, inst);
341 static int nfs_read (void)
346 if ((fh = fopen ("/proc/net/rpc/nfs", "r")) != NULL)
348 nfs_read_stats_file (fh, "client");
352 if ((fh = fopen ("/proc/net/rpc/nfsd", "r")) != NULL)
354 nfs_read_stats_file (fh, "server");
358 /* #endif defined(KERNEL_LINUX) */
360 #elif HAVE_LIBKSTAT && 0
361 if (nfs2_ksp_client != NULL)
362 nfs2_read_kstat (nfs2_ksp_client, "client");
363 if (nfs2_ksp_server != NULL)
364 nfs2_read_kstat (nfs2_ksp_server, "server");
365 #endif /* defined(HAVE_LIBKSTAT) */
369 #endif /* NFS_HAVE_READ */
371 void module_register (void)
374 plugin_register_read ("nfs", nfs_read);
376 } /* void module_register */