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>
28 # error "No applicable input method."
33 see http://www.missioncriticallinux.com/orph/NFS-Statistics
36 rpc_stat.netcnt Not used; always zero.
37 rpc_stat.netudpcnt Not used; always zero.
38 rpc_stat.nettcpcnt Not used; always zero.
39 rpc_stat.nettcpconn Not used; always zero.
42 rpc_stat.rpccnt The number of RPC calls.
43 rpc_stat.rpcretrans The number of retransmitted RPC calls.
44 rpc_stat.rpcauthrefresh The number of credential refreshes.
49 Procedure NFS Version NFS Version 3
50 Number Procedures Procedures
76 static const char *nfs2_procedures_names[] =
98 static int nfs2_procedures_names_num = 18;
100 static const char *nfs3_procedures_names[] =
126 static int nfs3_procedures_names_num = 22;
128 #if HAVE_LIBKSTAT && 0
129 extern kstat_ctl_t *kc;
130 static kstat_t *nfs2_ksp_client;
131 static kstat_t *nfs2_ksp_server;
132 static kstat_t *nfs3_ksp_client;
133 static kstat_t *nfs3_ksp_server;
134 static kstat_t *nfs4_ksp_client;
135 static kstat_t *nfs4_ksp_server;
138 /* Possibly TODO: NFSv4 statistics */
141 static int nfs_init (void)
143 #if HAVE_LIBKSTAT && 0
146 nfs2_ksp_client = NULL;
147 nfs2_ksp_server = NULL;
148 nfs3_ksp_client = NULL;
149 nfs3_ksp_server = NULL;
150 nfs4_ksp_client = NULL;
151 nfs4_ksp_server = NULL;
156 for (ksp_chain = kc->kc_chain; ksp_chain != NULL;
157 ksp_chain = ksp_chain->ks_next)
159 if (strncmp (ksp_chain->ks_module, "nfs", 3) != 0)
161 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v2", 13) == 0)
162 nfs2_ksp_server = ksp_chain;
163 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v3", 13) == 0)
164 nfs3_ksp_server = ksp_chain;
165 else if (strncmp (ksp_chain->ks_name, "rfsproccnt_v4", 13) == 0)
166 nfs4_ksp_server = ksp_chain;
167 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v2", 12) == 0)
168 nfs2_ksp_client = ksp_chain;
169 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v3", 12) == 0)
170 nfs3_ksp_client = ksp_chain;
171 else if (strncmp (ksp_chain->ks_name, "rfsreqcnt_v4", 12) == 0)
172 nfs4_ksp_client = ksp_chain;
181 static void nfs_procedures_submit (const char *plugin_instance,
182 unsigned long long *val, const char **names, int len)
185 value_list_t vl = VALUE_LIST_INIT;
190 vl.time = time (NULL);
191 strcpy (vl.host, hostname_g);
192 strcpy (vl.plugin, "nfs");
193 strncpy (vl.plugin_instance, plugin_instance,
194 sizeof (vl.plugin_instance));
196 for (i = 0; i < len; i++)
198 values[0].counter = val[i];
199 strncpy (vl.type_instance, names[i],
200 sizeof (vl.type_instance));
201 DEBUG ("%s-%s/nfs_procedure-%s = %llu",
202 vl.plugin, vl.plugin_instance,
203 vl.type_instance, val[i]);
204 plugin_dispatch_values ("nfs_procedure", &vl);
206 } /* void nfs_procedures_submit */
208 static void nfs_read_stats_file (FILE *fh, char *inst)
210 char buffer[BUFSIZE];
212 char plugin_instance[DATA_MAX_NAME_LEN];
220 while (fgets (buffer, BUFSIZE, fh) != NULL)
222 numfields = strsplit (buffer, fields, 48);
224 if (((numfields - 2) != nfs2_procedures_names_num)
226 != nfs3_procedures_names_num))
229 if (strcmp (fields[0], "proc2") == 0)
232 unsigned long long *values;
234 if ((numfields - 2) != nfs2_procedures_names_num)
236 WARNING ("nfs plugin: Wrong "
237 "number of fields (= %i) "
238 "for NFSv2 statistics.",
243 snprintf (plugin_instance, sizeof (plugin_instance),
245 plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
247 values = (unsigned long long *) malloc (nfs2_procedures_names_num * sizeof (unsigned long long));
251 ERROR ("nfs plugin: malloc "
253 sstrerror (errno, errbuf, sizeof (errbuf)));
257 for (i = 0; i < nfs2_procedures_names_num; i++)
258 values[i] = atoll (fields[i + 2]);
260 nfs_procedures_submit (plugin_instance, values,
261 nfs2_procedures_names,
262 nfs2_procedures_names_num);
266 else if (strncmp (fields[0], "proc3", 5) == 0)
269 unsigned long long *values;
271 if ((numfields - 2) != nfs3_procedures_names_num)
273 WARNING ("nfs plugin: Wrong "
274 "number of fields (= %i) "
275 "for NFSv3 statistics.",
280 snprintf (plugin_instance, sizeof (plugin_instance),
282 plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
284 values = (unsigned long long *) malloc (nfs3_procedures_names_num * sizeof (unsigned long long));
288 ERROR ("nfs plugin: malloc "
290 sstrerror (errno, errbuf, sizeof (errbuf)));
294 for (i = 0; i < nfs3_procedures_names_num; i++)
295 values[i] = atoll (fields[i + 2]);
297 nfs_procedures_submit (plugin_instance, values,
298 nfs3_procedures_names,
299 nfs3_procedures_names_num);
303 } /* while (fgets (buffer, BUFSIZE, fh) != NULL) */
304 } /* void nfs_read_stats_file */
307 #if HAVE_LIBKSTAT && 0
308 static void nfs2_read_kstat (kstat_t *ksp, char *inst)
310 unsigned long long values[18];
312 values[0] = get_kstat_value (ksp, "null");
313 values[1] = get_kstat_value (ksp, "getattr");
314 values[2] = get_kstat_value (ksp, "setattr");
315 values[3] = get_kstat_value (ksp, "root");
316 values[4] = get_kstat_value (ksp, "lookup");
317 values[5] = get_kstat_value (ksp, "readlink");
318 values[6] = get_kstat_value (ksp, "read");
319 values[7] = get_kstat_value (ksp, "wrcache");
320 values[8] = get_kstat_value (ksp, "write");
321 values[9] = get_kstat_value (ksp, "create");
322 values[10] = get_kstat_value (ksp, "remove");
323 values[11] = get_kstat_value (ksp, "rename");
324 values[12] = get_kstat_value (ksp, "link");
325 values[13] = get_kstat_value (ksp, "symlink");
326 values[14] = get_kstat_value (ksp, "mkdir");
327 values[15] = get_kstat_value (ksp, "rmdir");
328 values[16] = get_kstat_value (ksp, "readdir");
329 values[17] = get_kstat_value (ksp, "statfs");
331 nfs2_procedures_submit (values, inst);
335 static int nfs_read (void)
339 if ((fh = fopen ("/proc/net/rpc/nfs", "r")) != NULL)
341 nfs_read_stats_file (fh, "client");
345 if ((fh = fopen ("/proc/net/rpc/nfsd", "r")) != NULL)
347 nfs_read_stats_file (fh, "server");
351 #if HAVE_LIBKSTAT && 0
352 if (nfs2_ksp_client != NULL)
353 nfs2_read_kstat (nfs2_ksp_client, "client");
354 if (nfs2_ksp_server != NULL)
355 nfs2_read_kstat (nfs2_ksp_server, "server");
356 #endif /* defined(HAVE_LIBKSTAT) */
361 void module_register (void)
363 plugin_register_read ("nfs", nfs_read);
364 } /* void module_register */