i am sorry for that double change, didnt noticed, my cvs change mail seems
[oss-qm-packages.git] / statistics.c
bloba878df8ddaaf77a663c1c2d2559afa5126867c01
1 /*
2 * Copyright 1997,1999,2000 Andi Kleen. Subject to the GPL.
3 * $Id: statistics.c,v 1.14 2001/02/02 18:01:23 pb Exp $
4 * 19980630 - i18n - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
5 * 19981113 - i18n fixes - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6 * 19990101 - added net/netstat, -t, -u, -w supprt - Bernd Eckenfels
7 */
8 #include <ctype.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "config.h"
13 #include "intl.h"
15 /* #define WARN 1 */
17 #ifdef WARN
18 #define UFWARN(x) x
19 #else
20 #define UFWARN(x)
21 #endif
23 int print_static,f_raw,f_tcp,f_udp,f_unknown = 1;
25 enum State {
26 number = 0, opt_number, i_forward, i_inp_icmp, i_outp_icmp, i_rto_alg,
27 MaxState
30 #define normal number
32 struct entry {
33 char *title;
34 char *out;
35 enum State type;
38 struct statedesc {
39 int indent;
40 char *title;
41 };
43 struct statedesc states[] = {
44 [number] = { 4, NULL },
45 [opt_number] = { 4, NULL },
46 [i_forward] = { 4, NULL },
47 [i_inp_icmp] = { 8, N_("ICMP input histogram:") },
48 [i_outp_icmp] = { 8, N_("ICMP output histogram:") },
49 [MaxState] = {0},
50 };
52 static enum State state;
54 #define I_STATIC (1<<16) /* static configuration option. */
55 #define I_TITLE (1<<17)
57 /*
58 * XXX check against the snmp mib rfc.
60 * Don't mark the first field as translatable! It's a snmp MIB standard.
61 * - acme
63 struct entry Iptab[] =
65 {"Forwarding", N_("Forwarding is %s"), i_forward | I_STATIC},
66 {"DefaultTTL", N_("Default TTL is %u"), number | I_STATIC},
67 {"InReceives", N_("%u total packets received"), number},
68 {"InHdrErrors", N_("%u with invalid headers"), opt_number},
69 {"InAddrErrors", N_("%u with invalid addresses"), opt_number},
70 {"ForwDatagrams", N_("%u forwarded"), number},
71 {"InUnknownProtos", N_("%u with unknown protocol"), opt_number},
72 {"InDiscards", N_("%u incoming packets discarded"), number},
73 {"InDelivers", N_("%u incoming packets delivered"), number},
74 {"OutRequests", N_("%u requests sent out"), number}, /*? */
75 {"OutDiscards", N_("%u outgoing packets dropped"), opt_number},
76 {"OutNoRoutes", N_("%u dropped because of missing route"), opt_number},
77 {"ReasmTimeout", N_("%u fragments dropped after timeout"), opt_number},
78 {"ReasmReqds", N_("%u reassemblies required"), opt_number}, /* ? */
79 {"ReasmOKs", N_("%u packets reassembled ok"), opt_number},
80 {"ReasmFails", N_("%u packet reassembles failed"), opt_number},
81 {"FragOKs", N_("%u fragments received ok"), opt_number},
82 {"FragFails", N_("%u fragments failed"), opt_number},
83 {"FragCreates", N_("%u fragments created"), opt_number}
86 struct entry Icmptab[] =
88 {"InMsgs", N_("%u ICMP messages received"), number},
89 {"InErrors", N_("%u input ICMP message failed."), number},
90 {"InDestUnreachs", N_("destination unreachable: %u"), i_inp_icmp | I_TITLE},
91 {"InTimeExcds", N_("timeout in transit: %u"), i_inp_icmp | I_TITLE},
92 {"InParmProbs", N_("wrong parameters: %u"), i_inp_icmp | I_TITLE}, /*? */
93 {"InSrcQuenchs", N_("source quenches: %u"), i_inp_icmp | I_TITLE},
94 {"InRedirects", N_("redirects: %u"), i_inp_icmp | I_TITLE},
95 {"InEchos", N_("echo requests: %u"), i_inp_icmp | I_TITLE},
96 {"InEchoReps", N_("echo replies: %u"), i_inp_icmp | I_TITLE},
97 {"InTimestamps", N_("timestamp request: %u"), i_inp_icmp | I_TITLE},
98 {"InTimestampReps", N_("timestamp reply: %u"), i_inp_icmp | I_TITLE},
99 {"InAddrMasks", N_("address mask request: %u"), i_inp_icmp | I_TITLE}, /*? */
100 {"InAddrMaskReps", N_("address mask replies: %u"), i_inp_icmp | I_TITLE}, /*? */
101 {"OutMsgs", N_("%u ICMP messages sent"), number},
102 {"OutErrors", N_("%u ICMP messages failed"), number},
103 {"OutDestUnreachs", N_("destination unreachable: %u"), i_outp_icmp | I_TITLE},
104 {"OutTimeExcds", N_("time exceeded: %u"), i_outp_icmp | I_TITLE},
105 {"OutParmProbs", N_("wrong parameters: %u"), i_outp_icmp | I_TITLE}, /*? */
106 {"OutSrcQuenchs", N_("source quench: %u"), i_outp_icmp | I_TITLE},
107 {"OutRedirects", N_("redirect: %u"), i_outp_icmp | I_TITLE},
108 {"OutEchos", N_("echo request: %u"), i_outp_icmp | I_TITLE},
109 {"OutEchoReps", N_("echo replies: %u"), i_outp_icmp | I_TITLE},
110 {"OutTimestamps", N_("timestamp requests: %u"), i_outp_icmp | I_TITLE},
111 {"OutTimestampReps", N_("timestamp replies: %u"), i_outp_icmp | I_TITLE},
112 {"OutAddrMasks", N_("address mask requests: %u"), i_outp_icmp | I_TITLE},
113 {"OutAddrMaskReps", N_("address mask replies: %u"), i_outp_icmp | I_TITLE},
116 struct entry Tcptab[] =
118 {"RtoAlgorithm", N_("RTO algorithm is %s"), i_rto_alg | I_STATIC},
119 {"RtoMin", "", number},
120 {"RtoMax", "", number},
121 {"MaxConn", "", number},
122 {"ActiveOpens", N_("%u active connections openings"), number},
123 {"PassiveOpens", N_("%u passive connection openings"), number},
124 {"AttemptFails", N_("%u failed connection attempts"), number},
125 {"EstabResets", N_("%u connection resets received"), number},
126 {"CurrEstab", N_("%u connections established"), number},
127 {"InSegs", N_("%u segments received"), number},
128 {"OutSegs", N_("%u segments send out"), number},
129 {"RetransSegs", N_("%u segments retransmited"), number},
130 {"InErrs", N_("%u bad segments received."), number},
131 {"OutRsts", N_("%u resets sent"), number},
134 struct entry Udptab[] =
136 {"InDatagrams", N_("%u packets received"), number},
137 {"NoPorts", N_("%u packets to unknown port received."), number},
138 {"InErrors", N_("%u packet receive errors"), number},
139 {"OutDatagrams", N_("%u packets sent"), number},
142 struct entry Tcpexttab[] =
144 {"SyncookiesSent", N_("%u SYN cookies sent"), opt_number},
145 {"SyncookiesRecv", N_("%u SYN cookies received"), opt_number},
146 {"SyncookiesFailed", N_("%u invalid SYN cookies received"), opt_number},
148 { "EmbryonicRsts", N_("%u resets received for embryonic SYN_RECV sockets"),
149 opt_number },
150 { "PruneCalled", N_("%u packets pruned from receive queue because of socket"
151 " buffer overrun"), opt_number },
152 /* obsolete: 2.2.0 doesn't do that anymore */
153 { "RcvPruned", N_("%u packets pruned from receive queue"), opt_number },
154 { "OfoPruned", N_("%u packets dropped from out-of-order queue because of"
155 " socket buffer overrun"), opt_number },
156 { "OutOfWindowIcmps", N_("%u ICMP packets dropped because they were "
157 "out-of-window"), opt_number },
158 { "LockDroppedIcmps", N_("%u ICMP packets dropped because"
159 " socket was locked"), opt_number },
160 { "TW", N_("%u TCP sockets finished time wait in fast timer"), opt_number },
161 { "TWRecycled", N_("%u time wait sockets recycled by time stamp"), opt_number },
162 { "TWKilled", N_("%u TCP sockets finished time wait in slow timer"), opt_number },
163 { "PAWSPassive", N_("%u passive connections rejected because of"
164 " time stamp"), opt_number },
165 { "PAWSActive", N_("%u active connections rejected because of "
166 "time stamp"), opt_number },
167 { "PAWSEstab", N_("%u packets rejects in established connections because of"
168 " timestamp"), opt_number },
169 { "DelayedACKs", N_("%u delayed acks sent"), opt_number },
170 { "DelayedACKLocked", N_("%u delayed acks further delayed because of"
171 " locked socket"), opt_number },
172 { "DelayedACKLost", N_("Quick ack mode was activated %u times"), opt_number },
173 { "ListenOverflows", N_("%u times the listen queue of a socket overflowed"),
174 opt_number },
175 { "ListenDrops", N_("%u SYNs to LISTEN sockets ignored"), opt_number },
176 { "TCPPrequeued", N_("%u packets directly queued to recvmsg prequeue."),
177 opt_number },
178 { "TCPDirectCopyFromBacklog", N_("%u packets directly received"
179 " from backlog"), opt_number },
180 { "TCPDirectCopyFromPrequeue", N_("%u packets directly received"
181 " from prequeue"), opt_number },
182 { "TCPPrequeueDropped", N_("%u packets dropped from prequeue"), opt_number },
183 { "TCPHPHits", N_("%u packets header predicted"), number },
184 { "TCPHPHitsToUser", N_("%u packets header predicted and "
185 "directly queued to user"), opt_number },
186 { "SockMallocOOM", N_("Ran %u times out of system memory during "
187 "packet sending"), opt_number },
190 struct tabtab {
191 char *title;
192 struct entry *tab;
193 size_t size;
194 int *flag;
197 struct tabtab snmptabs[] =
199 {"Ip", Iptab, sizeof(Iptab), &f_raw},
200 {"Icmp", Icmptab, sizeof(Icmptab), &f_raw},
201 {"Tcp", Tcptab, sizeof(Tcptab), &f_tcp},
202 {"Udp", Udptab, sizeof(Udptab), &f_udp},
203 {"TcpExt", Tcpexttab, sizeof(Tcpexttab), &f_tcp},
204 {NULL}
207 /* XXX IGMP */
209 int cmpentries(const void *a, const void *b)
211 return strcmp(((struct entry *) a)->title, ((struct entry *) b)->title);
214 void printval(struct tabtab *tab, char *title, int val)
216 struct entry *ent = NULL, key;
217 int type;
218 char buf[512];
220 key.title = title;
221 if (tab->tab)
222 ent = bsearch(&key, tab->tab, tab->size / sizeof(struct entry),
223 sizeof(struct entry), cmpentries);
224 if (!ent) { /* try our best */
225 printf("%*s%s: %d\n", states[state].indent, "", title, val);
226 return;
228 type = ent->type;
229 if (type & I_STATIC) {
230 type &= ~I_STATIC;
231 if (!print_static)
232 return;
234 if (*ent->out == '\0')
235 return;
237 if (type & I_TITLE) {
238 type &= ~I_TITLE;
239 if (state != type)
240 printf("%*s%s\n", states[state].indent, "", _(states[type].title));
242 buf[0] = '\0';
243 switch (type) {
244 case opt_number:
245 if (val == 0)
246 break;
247 /*FALL THOUGH*/
248 case number:
249 snprintf(buf, sizeof(buf), _(ent->out), val);
250 break;
251 case i_forward:
252 type = normal;
253 snprintf(buf, sizeof(buf), _(ent->out), val == 2 ? _("enabled") : _("disabled"));
254 break;
255 case i_outp_icmp:
256 case i_inp_icmp:
257 if (val > 0)
258 snprintf(buf, sizeof(buf), _(ent->out), val);
259 break;
260 case i_rto_alg: /* XXXX */
261 break;
262 default:
263 abort();
265 if (buf[0])
266 printf("%*s%s\n", states[type].indent, "", buf);
268 state = type;
271 struct tabtab *newtable(struct tabtab *tabs, char *title)
273 struct tabtab *t;
274 static struct tabtab dummytab;
276 for (t = tabs; t->title; t++) {
277 if (!strcmp(title, t->title)) {
278 if (*(t->flag))
279 printf("%s:\n", _(title));
280 state = normal;
281 return t;
284 if (!f_unknown)
285 return NULL;
286 printf("%s:\n", _(title));
287 dummytab.title = title;
288 dummytab.flag = &f_unknown;
289 return &dummytab;
292 void process_fd(FILE *f)
294 char buf1[1024], buf2[1024];
295 char *sp, *np, *p;
296 while (fgets(buf1, sizeof buf1, f)) {
297 int endflag;
298 struct tabtab *tab;
300 if (!fgets(buf2, sizeof buf2, f))
301 break;
302 sp = strchr(buf1, ':');
303 np = strchr(buf2, ':');
304 if (!np || !sp)
305 goto formaterr;
306 *sp = '\0';
308 tab = newtable(snmptabs, buf1);
309 if (tab == NULL) {
310 printf("unknown %s\n", buf1);
311 continue;
313 np++;
314 sp++;
316 endflag = 0;
317 while (!endflag) {
318 sp += strspn(sp, " \t\n");
319 np += strspn(np, " \t\n");
320 /*if (*np == '\0') goto formaterr; */
322 p = sp+strcspn(sp, " \t\n");
323 if (*p == '\0')
324 endflag = 1;
325 *p = '\0';
327 if (*sp != '\0' && *(tab->flag))
328 printval(tab, sp, strtoul(np, &np, 10));
330 sp = p + 1;
333 return;
335 formaterr:
336 perror(_("error parsing /proc/net/snmp"));
337 return;
341 void parsesnmp(int flag_raw, int flag_tcp, int flag_udp)
343 FILE *f;
345 f_raw = flag_raw; f_tcp = flag_tcp; f_udp = flag_udp;
347 f = fopen("/proc/net/snmp", "r");
348 if (!f) {
349 perror(_("cannot open /proc/net/snmp"));
350 return;
352 process_fd(f);
354 if (ferror(f))
355 perror("/proc/net/snmp");
357 fclose(f);
359 f = fopen("/proc/net/netstat", "r");
361 if (f) {
362 process_fd(f);
364 if (ferror(f))
365 perror("/proc/net/netstat");
367 fclose(f);
369 return;
373 void inittab(void)
375 struct tabtab *t;
377 /* we sort at runtime because I'm lazy ;) */
378 for (t = snmptabs; t->title; t++)
379 qsort(t->tab, t->size / sizeof(struct entry),
380 sizeof(struct entry), cmpentries);