]> git.proxmox.com Git - mirror_iproute2.git/blame - misc/lnstat_util.c
add const to find_field
[mirror_iproute2.git] / misc / lnstat_util.c
CommitLineData
4677a549
SH
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. */
39static int scan_lines(struct lnstat_file *lf, int i)
40{
41 int j, num_lines = 0;
42
43 for (j = 0; j < lf->num_fields; j++)
44 lf->fields[j].values[i] = 0;
45
46 while(!feof(lf->fp)) {
47 char buf[FGETS_BUF_SIZE];
48 char *ptr = buf;
49
50 num_lines++;
51
52 fgets(buf, sizeof(buf)-1, lf->fp);
53 gettimeofday(&lf->last_read, NULL);
54
55 for (j = 0; j < lf->num_fields; j++)
56 lf->fields[j].values[i] = strtoul(ptr, &ptr, 16);
57 }
58 return num_lines;
59}
60
61static int time_after(struct timeval *last,
62 struct timeval *tout,
63 struct timeval *now)
64{
65 if (now->tv_sec > last->tv_sec + tout->tv_sec)
66 return 1;
67
68 if (now->tv_sec == last->tv_sec + tout->tv_sec) {
69 if (now->tv_usec > last->tv_usec + tout->tv_usec)
70 return 1;
71 }
72
73 return 0;
74}
75
76int lnstat_update(struct lnstat_file *lnstat_files)
77{
78 struct lnstat_file *lf;
79 char buf[FGETS_BUF_SIZE];
80 struct timeval tv;
81
82 gettimeofday(&tv, NULL);
83
84 for (lf = lnstat_files; lf; lf = lf->next) {
85 if (time_after(&lf->last_read, &lf->interval, &tv)) {
86 int i;
87 struct lnstat_field *lfi;
88
89 rewind(lf->fp);
90 if (!lf->compat) {
91 /* skip first line */
92 fgets(buf, sizeof(buf)-1, lf->fp);
93 }
94 scan_lines(lf, 1);
95
96 for (i = 0, lfi = &lf->fields[i];
97 i < lf->num_fields; i++, lfi = &lf->fields[i]) {
98 if (i == 0)
99 lfi->result = lfi->values[1];
100 else
101 lfi->result = (lfi->values[1]-lfi->values[0])
102 / lf->interval.tv_sec;
103 }
104
105 rewind(lf->fp);
106 fgets(buf, sizeof(buf)-1, lf->fp);
107 scan_lines(lf, 0);
108 }
109 }
110
111 return 0;
112}
113
114/* scan first template line and fill in per-field data structures */
115static int __lnstat_scan_fields(struct lnstat_file *lf, char *buf)
116{
117 char *tok;
118 int i;
119
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");
127 if (!tok) {
128 lf->num_fields = i+1;
129 return 0;
130 }
131 }
132 return 0;
133}
134
135static int lnstat_scan_fields(struct lnstat_file *lf)
136{
137 char buf[FGETS_BUF_SIZE];
138
139 rewind(lf->fp);
140 fgets(buf, sizeof(buf)-1, lf->fp);
141
142 return __lnstat_scan_fields(lf, buf);
143}
144
145/* fake function emulating lnstat_scan_fields() for old kernels */
146static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf)
147{
148 char buf[FGETS_BUF_SIZE];
149
150 strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf)-1);
151
152 return __lnstat_scan_fields(lf, buf);
153}
154
155/* find out whether string 'name; is in given string array */
156static int name_in_array(const int num, const char **arr, const char *name)
157{
158 int i;
159 for (i = 0; i < num; i++) {
160 if (!strcmp(arr[i], name))
161 return 1;
162 }
163 return 0;
164}
165
166/* allocate lnstat_file and open given file */
167static struct lnstat_file *alloc_and_open(const char *path, const char *file)
168{
169 struct lnstat_file *lf;
170
171 /* allocate */
172 lf = malloc(sizeof(*lf));
173 if (!lf)
174 return NULL;
175
176 /* initialize */
177 memset(lf, 0, sizeof(*lf));
178
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);
184
185 /* initialize to default */
186 lf->interval.tv_sec = 1;
187
188 /* open */
189 lf->fp = fopen(lf->path, "r");
190 if (!lf->fp) {
191 free(lf);
192 return NULL;
193 }
194
195 return lf;
196}
197
198
199/* lnstat_scan_dir - find and parse all available statistics files/fields */
200struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files,
201 const char **req_files)
202{
203 DIR *dir;
204 struct lnstat_file *lnstat_files = NULL;
205 struct dirent *de;
206
207 if (!path)
208 path = PROC_NET_STAT;
209
210 dir = opendir(path);
211 if (!dir) {
212 struct lnstat_file *lf;
213 /* Old kernel, before /proc/net/stat was introduced */
214 fprintf(stderr, "Your kernel doesn't have lnstat support. ");
215
216 /* we only support rtstat, not multiple files */
217 if (num_req_files >= 2) {
218 fputc('\n', stderr);
219 return NULL;
220 }
221
222 /* we really only accept rt_cache */
223 if (num_req_files && !name_in_array(num_req_files,
224 req_files, "rt_cache")) {
225 fputc('\n', stderr);
226 return NULL;
227 }
228
229 fprintf(stderr, "Fallback to old rtstat-only operation\n");
230
231 lf = alloc_and_open("/proc/net", "rt_cache_stat");
232 if (!lf)
233 return NULL;
234 lf->compat = 1;
235 strncpy(lf->basename, "rt_cache", sizeof(lf->basename));
236
237 /* FIXME: support for old files */
238 if (lnstat_scan_compat_rtstat_fields(lf) < 0)
239 return NULL;
240
241 lf->next = lnstat_files;
242 lnstat_files = lf;
243 return lnstat_files;
244 }
245
246 while ((de = readdir(dir))) {
247 struct lnstat_file *lf;
248
249 if (de->d_type != DT_REG)
250 continue;
251
252 if (num_req_files && !name_in_array(num_req_files,
253 req_files, de->d_name))
254 continue;
255
256 lf = alloc_and_open(path, de->d_name);
257 if (!lf)
258 return NULL;
259
260 /* fill in field structure */
261 if (lnstat_scan_fields(lf) < 0)
262 return NULL;
263
264 /* prepend to global list */
265 lf->next = lnstat_files;
266 lnstat_files = lf;
267 }
268 closedir(dir);
269
270 return lnstat_files;
271}
272
273int lnstat_dump(FILE *outfd, struct lnstat_file *lnstat_files)
274{
275 struct lnstat_file *lf;
276
277 for (lf = lnstat_files; lf; lf = lf->next) {
278 int i;
279
280 fprintf(outfd, "%s:\n", lf->path);
281
282 for (i = 0; i < lf->num_fields; i++)
283 fprintf(outfd, "\t%2u: %s\n", i+1, lf->fields[i].name);
284
285 }
286 return 0;
287}
288
289struct lnstat_field *lnstat_find_field(struct lnstat_file *lnstat_files,
290 const char *name)
291{
292 struct lnstat_file *lf;
293 struct lnstat_field *ret = NULL;
294 const char *colon = strchr(name, ':');
295 char *file;
296 const char *field;
297
298 if (colon) {
299 file = strndup(name, colon-name);
300 field = colon+1;
301 } else {
302 file = NULL;
303 field = name;
304 }
305
306 for (lf = lnstat_files; lf; lf = lf->next) {
307 int i;
308
309 if (file && strcmp(file, lf->basename))
310 continue;
311
312 for (i = 0; i < lf->num_fields; i++) {
313 if (!strcmp(field, lf->fields[i].name)) {
314 ret = &lf->fields[i];
315 goto out;
316 }
317 }
318 }
319out:
320 if (file)
321 free(file);
322
323 return ret;
324}