]> git.proxmox.com Git - mirror_iproute2.git/blob - misc/lnstat_util.c
433e992976eac630c1858f7c1040aaa8c7cee633
[mirror_iproute2.git] / misc / lnstat_util.c
1 /* lnstat.c: Unified linux network statistics
2 *
3 * Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>
4 *
5 * Development of this code was funded by Astaro AG, http://www.astaro.com/
6 *
7 * Based on original concept and ideas from predecessor rtstat.c:
8 *
9 * Copyright 2001 by Robert Olsson <robert.olsson@its.uu.se>
10 * Uppsala University, Sweden
11 *
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.
16 *
17 */
18
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <dirent.h>
24 #include <limits.h>
25 #include <time.h>
26
27 #include <sys/time.h>
28 #include <sys/types.h>
29
30 #include "lnstat.h"
31
32 /* size of temp buffer used to read lines from procfiles */
33 #define FGETS_BUF_SIZE 1024
34
35
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"
37
38 /* Read (and summarize for SMP) the different stats vars. */
39 static int scan_lines(struct lnstat_file *lf, int i)
40 {
41 char buf[FGETS_BUF_SIZE];
42 int j, num_lines = 0;
43
44 for (j = 0; j < lf->num_fields; j++)
45 lf->fields[j].values[i] = 0;
46
47 rewind(lf->fp);
48 /* skip first line */
49 if (!lf->compat && !fgets(buf, sizeof(buf)-1, lf->fp))
50 return -1;
51
52 while(!feof(lf->fp) && fgets(buf, sizeof(buf)-1, lf->fp)) {
53 char *ptr = buf;
54
55 num_lines++;
56
57 gettimeofday(&lf->last_read, NULL);
58
59 for (j = 0; j < lf->num_fields; j++) {
60 unsigned long f = strtoul(ptr, &ptr, 16);
61 if (j == 0)
62 lf->fields[j].values[i] = f;
63 else
64 lf->fields[j].values[i] += f;
65 }
66 }
67 return num_lines;
68 }
69
70 static int time_after(struct timeval *last,
71 struct timeval *tout,
72 struct timeval *now)
73 {
74 if (now->tv_sec > last->tv_sec + tout->tv_sec)
75 return 1;
76
77 if (now->tv_sec == last->tv_sec + tout->tv_sec) {
78 if (now->tv_usec > last->tv_usec + tout->tv_usec)
79 return 1;
80 }
81
82 return 0;
83 }
84
85 int lnstat_update(struct lnstat_file *lnstat_files)
86 {
87 struct lnstat_file *lf;
88 struct timeval tv;
89
90 gettimeofday(&tv, NULL);
91
92 for (lf = lnstat_files; lf; lf = lf->next) {
93 if (time_after(&lf->last_read, &lf->interval, &tv)) {
94 int i;
95 struct lnstat_field *lfi;
96
97 scan_lines(lf, 1);
98
99 for (i = 0, lfi = &lf->fields[i];
100 i < lf->num_fields; i++, lfi = &lf->fields[i]) {
101 if (i == 0)
102 lfi->result = lfi->values[1];
103 else
104 lfi->result = (lfi->values[1]-lfi->values[0])
105 / lf->interval.tv_sec;
106 }
107
108 scan_lines(lf, 0);
109 }
110 }
111
112 return 0;
113 }
114
115 /* scan first template line and fill in per-field data structures */
116 static int __lnstat_scan_fields(struct lnstat_file *lf, char *buf)
117 {
118 char *tok;
119 int i;
120
121 tok = strtok(buf, " \t\n");
122 for (i = 0; i < LNSTAT_MAX_FIELDS_PER_LINE; i++) {
123 lf->fields[i].file = lf;
124 strncpy(lf->fields[i].name, tok, LNSTAT_MAX_FIELD_NAME_LEN);
125 /* has to be null-terminate since we initialize to zero
126 * and field size is NAME_LEN + 1 */
127 tok = strtok(NULL, " \t\n");
128 if (!tok) {
129 lf->num_fields = i+1;
130 return 0;
131 }
132 }
133 return 0;
134 }
135
136 static int lnstat_scan_fields(struct lnstat_file *lf)
137 {
138 char buf[FGETS_BUF_SIZE];
139
140 rewind(lf->fp);
141 fgets(buf, sizeof(buf)-1, lf->fp);
142
143 return __lnstat_scan_fields(lf, buf);
144 }
145
146 /* fake function emulating lnstat_scan_fields() for old kernels */
147 static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf)
148 {
149 char buf[FGETS_BUF_SIZE];
150
151 strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf)-1);
152
153 return __lnstat_scan_fields(lf, buf);
154 }
155
156 /* find out whether string 'name; is in given string array */
157 static int name_in_array(const int num, const char **arr, const char *name)
158 {
159 int i;
160 for (i = 0; i < num; i++) {
161 if (!strcmp(arr[i], name))
162 return 1;
163 }
164 return 0;
165 }
166
167 /* allocate lnstat_file and open given file */
168 static struct lnstat_file *alloc_and_open(const char *path, const char *file)
169 {
170 struct lnstat_file *lf;
171
172 /* allocate */
173 lf = malloc(sizeof(*lf));
174 if (!lf)
175 return NULL;
176
177 /* initialize */
178 memset(lf, 0, sizeof(*lf));
179
180 /* de->d_name is guaranteed to be <= NAME_MAX */
181 strcpy(lf->basename, file);
182 strcpy(lf->path, path);
183 strcat(lf->path, "/");
184 strcat(lf->path, lf->basename);
185
186 /* initialize to default */
187 lf->interval.tv_sec = 1;
188
189 /* open */
190 lf->fp = fopen(lf->path, "r");
191 if (!lf->fp) {
192 free(lf);
193 return NULL;
194 }
195
196 return lf;
197 }
198
199
200 /* lnstat_scan_dir - find and parse all available statistics files/fields */
201 struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files,
202 const char **req_files)
203 {
204 DIR *dir;
205 struct lnstat_file *lnstat_files = NULL;
206 struct dirent *de;
207
208 if (!path)
209 path = PROC_NET_STAT;
210
211 dir = opendir(path);
212 if (!dir) {
213 struct lnstat_file *lf;
214 /* Old kernel, before /proc/net/stat was introduced */
215 fprintf(stderr, "Your kernel doesn't have lnstat support. ");
216
217 /* we only support rtstat, not multiple files */
218 if (num_req_files >= 2) {
219 fputc('\n', stderr);
220 return NULL;
221 }
222
223 /* we really only accept rt_cache */
224 if (num_req_files && !name_in_array(num_req_files,
225 req_files, "rt_cache")) {
226 fputc('\n', stderr);
227 return NULL;
228 }
229
230 fprintf(stderr, "Fallback to old rtstat-only operation\n");
231
232 lf = alloc_and_open("/proc/net", "rt_cache_stat");
233 if (!lf)
234 return NULL;
235 lf->compat = 1;
236 strncpy(lf->basename, "rt_cache", sizeof(lf->basename));
237
238 /* FIXME: support for old files */
239 if (lnstat_scan_compat_rtstat_fields(lf) < 0)
240 return NULL;
241
242 lf->next = lnstat_files;
243 lnstat_files = lf;
244 return lnstat_files;
245 }
246
247 while ((de = readdir(dir))) {
248 struct lnstat_file *lf;
249
250 if (de->d_type != DT_REG)
251 continue;
252
253 if (num_req_files && !name_in_array(num_req_files,
254 req_files, de->d_name))
255 continue;
256
257 lf = alloc_and_open(path, de->d_name);
258 if (!lf)
259 return NULL;
260
261 /* fill in field structure */
262 if (lnstat_scan_fields(lf) < 0)
263 return NULL;
264
265 /* prepend to global list */
266 lf->next = lnstat_files;
267 lnstat_files = lf;
268 }
269 closedir(dir);
270
271 return lnstat_files;
272 }
273
274 int lnstat_dump(FILE *outfd, struct lnstat_file *lnstat_files)
275 {
276 struct lnstat_file *lf;
277
278 for (lf = lnstat_files; lf; lf = lf->next) {
279 int i;
280
281 fprintf(outfd, "%s:\n", lf->path);
282
283 for (i = 0; i < lf->num_fields; i++)
284 fprintf(outfd, "\t%2u: %s\n", i+1, lf->fields[i].name);
285
286 }
287 return 0;
288 }
289
290 struct lnstat_field *lnstat_find_field(struct lnstat_file *lnstat_files,
291 const char *name)
292 {
293 struct lnstat_file *lf;
294 struct lnstat_field *ret = NULL;
295 const char *colon = strchr(name, ':');
296 char *file;
297 const char *field;
298
299 if (colon) {
300 file = strndup(name, colon-name);
301 field = colon+1;
302 } else {
303 file = NULL;
304 field = name;
305 }
306
307 for (lf = lnstat_files; lf; lf = lf->next) {
308 int i;
309
310 if (file && strcmp(file, lf->basename))
311 continue;
312
313 for (i = 0; i < lf->num_fields; i++) {
314 if (!strcmp(field, lf->fields[i].name)) {
315 ret = &lf->fields[i];
316 goto out;
317 }
318 }
319 }
320 out:
321 free(file);
322
323 return ret;
324 }