1 /* lnstat.c: Unified linux network statistics
3 * Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>
5 * Development of this code was funded by Astaro AG, http://www.astaro.com/
7 * Based on original concept and ideas from predecessor rtstat.c:
9 * Copyright 2001 by Robert Olsson <robert.olsson@its.uu.se>
10 * Uppsala University, Sweden
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
28 #include <sys/types.h>
32 /* size of temp buffer used to read lines from procfiles */
33 #define FGETS_BUF_SIZE 1024
36 #define RTSTAT_COMPAT_LINE "entries in_hit in_slow_tot in_no_route in_brd in_martian_dst in_martian_src out_hit out_slow_tot out_slow_mc gc_total gc_ignored gc_goal_miss gc_dst_overflow in_hlist_search out_hlist_search\n"
38 /* Read (and summarize for SMP) the different stats vars. */
39 static int scan_lines(struct lnstat_file *lf, int i)
43 for (j = 0; j < lf->num_fields; j++)
44 lf->fields[j].values[i] = 0;
46 while(!feof(lf->fp)) {
47 char buf[FGETS_BUF_SIZE];
52 fgets(buf, sizeof(buf)-1, lf->fp);
53 gettimeofday(&lf->last_read, NULL);
55 for (j = 0; j < lf->num_fields; j++)
56 lf->fields[j].values[i] = strtoul(ptr, &ptr, 16);
61 static int time_after(struct timeval *last,
65 if (now->tv_sec > last->tv_sec + tout->tv_sec)
68 if (now->tv_sec == last->tv_sec + tout->tv_sec) {
69 if (now->tv_usec > last->tv_usec + tout->tv_usec)
76 int lnstat_update(struct lnstat_file *lnstat_files)
78 struct lnstat_file *lf;
79 char buf[FGETS_BUF_SIZE];
82 gettimeofday(&tv, NULL);
84 for (lf = lnstat_files; lf; lf = lf->next) {
85 if (time_after(&lf->last_read, &lf->interval, &tv)) {
87 struct lnstat_field *lfi;
92 fgets(buf, sizeof(buf)-1, lf->fp);
96 for (i = 0, lfi = &lf->fields[i];
97 i < lf->num_fields; i++, lfi = &lf->fields[i]) {
99 lfi->result = lfi->values[1];
101 lfi->result = (lfi->values[1]-lfi->values[0])
102 / lf->interval.tv_sec;
106 fgets(buf, sizeof(buf)-1, lf->fp);
114 /* scan first template line and fill in per-field data structures */
115 static int __lnstat_scan_fields(struct lnstat_file *lf, char *buf)
120 tok = strtok(buf, " \t\n");
121 for (i = 0; i < LNSTAT_MAX_FIELDS_PER_LINE; i++) {
122 lf->fields[i].file = lf;
123 strncpy(lf->fields[i].name, tok, LNSTAT_MAX_FIELD_NAME_LEN);
124 /* has to be null-terminate since we initialize to zero
125 * and field size is NAME_LEN + 1 */
126 tok = strtok(NULL, " \t\n");
128 lf->num_fields = i+1;
135 static int lnstat_scan_fields(struct lnstat_file *lf)
137 char buf[FGETS_BUF_SIZE];
140 fgets(buf, sizeof(buf)-1, lf->fp);
142 return __lnstat_scan_fields(lf, buf);
145 /* fake function emulating lnstat_scan_fields() for old kernels */
146 static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf)
148 char buf[FGETS_BUF_SIZE];
150 strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf)-1);
152 return __lnstat_scan_fields(lf, buf);
155 /* find out whether string 'name; is in given string array */
156 static int name_in_array(const int num, const char **arr, const char *name)
159 for (i = 0; i < num; i++) {
160 if (!strcmp(arr[i], name))
166 /* allocate lnstat_file and open given file */
167 static struct lnstat_file *alloc_and_open(const char *path, const char *file)
169 struct lnstat_file *lf;
172 lf = malloc(sizeof(*lf));
177 memset(lf, 0, sizeof(*lf));
179 /* de->d_name is guaranteed to be <= NAME_MAX */
180 strcpy(lf->basename, file);
181 strcpy(lf->path, path);
182 strcat(lf->path, "/");
183 strcat(lf->path, lf->basename);
185 /* initialize to default */
186 lf->interval.tv_sec = 1;
189 lf->fp = fopen(lf->path, "r");
199 /* lnstat_scan_dir - find and parse all available statistics files/fields */
200 struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files,
201 const char **req_files)
204 struct lnstat_file *lnstat_files = NULL;
208 path = PROC_NET_STAT;
212 struct lnstat_file *lf;
213 /* Old kernel, before /proc/net/stat was introduced */
214 fprintf(stderr, "Your kernel doesn't have lnstat support. ");
216 /* we only support rtstat, not multiple files */
217 if (num_req_files >= 2) {
222 /* we really only accept rt_cache */
223 if (num_req_files && !name_in_array(num_req_files,
224 req_files, "rt_cache")) {
229 fprintf(stderr, "Fallback to old rtstat-only operation\n");
231 lf = alloc_and_open("/proc/net", "rt_cache_stat");
235 strncpy(lf->basename, "rt_cache", sizeof(lf->basename));
237 /* FIXME: support for old files */
238 if (lnstat_scan_compat_rtstat_fields(lf) < 0)
241 lf->next = lnstat_files;
246 while ((de = readdir(dir))) {
247 struct lnstat_file *lf;
249 if (de->d_type != DT_REG)
252 if (num_req_files && !name_in_array(num_req_files,
253 req_files, de->d_name))
256 lf = alloc_and_open(path, de->d_name);
260 /* fill in field structure */
261 if (lnstat_scan_fields(lf) < 0)
264 /* prepend to global list */
265 lf->next = lnstat_files;
273 int lnstat_dump(FILE *outfd, struct lnstat_file *lnstat_files)
275 struct lnstat_file *lf;
277 for (lf = lnstat_files; lf; lf = lf->next) {
280 fprintf(outfd, "%s:\n", lf->path);
282 for (i = 0; i < lf->num_fields; i++)
283 fprintf(outfd, "\t%2u: %s\n", i+1, lf->fields[i].name);
289 struct lnstat_field *lnstat_find_field(struct lnstat_file *lnstat_files,
292 struct lnstat_file *lf;
293 struct lnstat_field *ret = NULL;
294 const char *colon = strchr(name, ':');
299 file = strndup(name, colon-name);
306 for (lf = lnstat_files; lf; lf = lf->next) {
309 if (file && strcmp(file, lf->basename))
312 for (i = 0; i < lf->num_fields; i++) {
313 if (!strcmp(field, lf->fields[i].name)) {
314 ret = &lf->fields[i];