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 if (fgets(buf, sizeof(buf)-1, lf->fp) == NULL)
53 fprintf(stderr, "Error: failed to read from input file stream.\n");
54 gettimeofday(&lf->last_read, NULL);
56 for (j = 0; j < lf->num_fields; j++) {
57 unsigned long f = strtoul(ptr, &ptr, 16);
59 lf->fields[j].values[i] = f;
61 lf->fields[j].values[i] += f;
67 static int time_after(struct timeval *last,
71 if (now->tv_sec > last->tv_sec + tout->tv_sec)
74 if (now->tv_sec == last->tv_sec + tout->tv_sec) {
75 if (now->tv_usec > last->tv_usec + tout->tv_usec)
82 int lnstat_update(struct lnstat_file *lnstat_files)
84 struct lnstat_file *lf;
85 char buf[FGETS_BUF_SIZE];
88 gettimeofday(&tv, NULL);
90 for (lf = lnstat_files; lf; lf = lf->next) {
91 if (time_after(&lf->last_read, &lf->interval, &tv)) {
93 struct lnstat_field *lfi;
98 if (fgets(buf, sizeof(buf)-1, lf->fp) == NULL)
99 fprintf(stderr, "Error: failed to read from input file stream.\n");
103 for (i = 0, lfi = &lf->fields[i];
104 i < lf->num_fields; i++, lfi = &lf->fields[i]) {
106 lfi->result = lfi->values[1];
108 lfi->result = (lfi->values[1]-lfi->values[0])
109 / lf->interval.tv_sec;
113 if (fgets(buf, sizeof(buf)-1, lf->fp) == NULL)
114 fprintf(stderr, "Error: failed to read from input file stream.\n");
122 /* scan first template line and fill in per-field data structures */
123 static int __lnstat_scan_fields(struct lnstat_file *lf, char *buf)
128 tok = strtok(buf, " \t\n");
129 for (i = 0; i < LNSTAT_MAX_FIELDS_PER_LINE; i++) {
130 lf->fields[i].file = lf;
131 strncpy(lf->fields[i].name, tok, LNSTAT_MAX_FIELD_NAME_LEN);
132 /* has to be null-terminate since we initialize to zero
133 * and field size is NAME_LEN + 1 */
134 tok = strtok(NULL, " \t\n");
136 lf->num_fields = i+1;
143 static int lnstat_scan_fields(struct lnstat_file *lf)
145 char buf[FGETS_BUF_SIZE];
148 if (fgets(buf, sizeof(buf)-1, lf->fp) == NULL)
149 fprintf(stderr, "Error: failed to read from input file stream.\n");
151 return __lnstat_scan_fields(lf, buf);
154 /* fake function emulating lnstat_scan_fields() for old kernels */
155 static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf)
157 char buf[FGETS_BUF_SIZE];
159 strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf)-1);
161 return __lnstat_scan_fields(lf, buf);
164 /* find out whether string 'name; is in given string array */
165 static int name_in_array(const int num, const char **arr, const char *name)
168 for (i = 0; i < num; i++) {
169 if (!strcmp(arr[i], name))
175 /* allocate lnstat_file and open given file */
176 static struct lnstat_file *alloc_and_open(const char *path, const char *file)
178 struct lnstat_file *lf;
181 lf = malloc(sizeof(*lf));
186 memset(lf, 0, sizeof(*lf));
188 /* de->d_name is guaranteed to be <= NAME_MAX */
189 strcpy(lf->basename, file);
190 strcpy(lf->path, path);
191 strcat(lf->path, "/");
192 strcat(lf->path, lf->basename);
194 /* initialize to default */
195 lf->interval.tv_sec = 1;
198 lf->fp = fopen(lf->path, "r");
208 /* lnstat_scan_dir - find and parse all available statistics files/fields */
209 struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files,
210 const char **req_files)
213 struct lnstat_file *lnstat_files = NULL;
217 path = PROC_NET_STAT;
221 struct lnstat_file *lf;
222 /* Old kernel, before /proc/net/stat was introduced */
223 fprintf(stderr, "Your kernel doesn't have lnstat support. ");
225 /* we only support rtstat, not multiple files */
226 if (num_req_files >= 2) {
231 /* we really only accept rt_cache */
232 if (num_req_files && !name_in_array(num_req_files,
233 req_files, "rt_cache")) {
238 fprintf(stderr, "Fallback to old rtstat-only operation\n");
240 lf = alloc_and_open("/proc/net", "rt_cache_stat");
244 strncpy(lf->basename, "rt_cache", sizeof(lf->basename));
246 /* FIXME: support for old files */
247 if (lnstat_scan_compat_rtstat_fields(lf) < 0)
250 lf->next = lnstat_files;
255 while ((de = readdir(dir))) {
256 struct lnstat_file *lf;
258 if (de->d_type != DT_REG)
261 if (num_req_files && !name_in_array(num_req_files,
262 req_files, de->d_name))
265 lf = alloc_and_open(path, de->d_name);
269 /* fill in field structure */
270 if (lnstat_scan_fields(lf) < 0)
273 /* prepend to global list */
274 lf->next = lnstat_files;
282 int lnstat_dump(FILE *outfd, struct lnstat_file *lnstat_files)
284 struct lnstat_file *lf;
286 for (lf = lnstat_files; lf; lf = lf->next) {
289 fprintf(outfd, "%s:\n", lf->path);
291 for (i = 0; i < lf->num_fields; i++)
292 fprintf(outfd, "\t%2u: %s\n", i+1, lf->fields[i].name);
298 struct lnstat_field *lnstat_find_field(struct lnstat_file *lnstat_files,
301 struct lnstat_file *lf;
302 struct lnstat_field *ret = NULL;
303 const char *colon = strchr(name, ':');
308 file = strndup(name, colon-name);
315 for (lf = lnstat_files; lf; lf = lf->next) {
318 if (file && strcmp(file, lf->basename))
321 for (i = 0; i < lf->num_fields; i++) {
322 if (!strcmp(field, lf->fields[i].name)) {
323 ret = &lf->fields[i];