2 * print PPP statistics:
3 * pppstats [-i interval] [-v] [-r] [-c] [interface]
5 * -i <update interval in seconds>
6 * -v Verbose mode for default display
7 * -r Show compression ratio in default display
8 * -c Show Compression statistics instead of default display
9 * -a Do not show relative values. Show absolute values at all times.
13 * perkins@cps.msu.edu: Added compression statistics and alternate
16 * Brad Parker (brad@cayman.com) 6/92
18 * from the original "slstats" by Van Jaconson
20 * Copyright (c) 1989 Regents of the University of California.
21 * All rights reserved.
23 * Redistribution and use in source and binary forms are permitted
24 * provided that the above copyright notice and this paragraph are
25 * duplicated in all such forms and that any documentation,
26 * advertising materials, and other materials related to such
27 * distribution and use acknowledge that the software was developed
28 * by the University of California, Berkeley. The name of the
29 * University may not be used to endorse or promote products derived
30 * from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
33 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
35 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
36 * - Initial distribution.
40 static char rcsid[] = "$Id: pppstats.c,v 1.12 1995/12/11 05:18:59 paulus Exp $";
49 #include <sys/param.h>
50 #include <sys/types.h>
51 #include <sys/ioctl.h>
53 #include <net/ppp_defs.h>
56 #include <sys/stropts.h>
57 #include <net/pppio.h> /* SVR4, Solaris 2, etc. */
60 #include <sys/socket.h>
64 #include <net/if_ppp.h> /* BSD, Linux, NeXT, etc. */
66 #else /* SunOS 4, AIX 4, OSF/1, etc. */
67 #define PPP_STATS 1 /* should be defined iff it is in ppp_if.c */
68 #include <sys/stream.h>
69 #include <net/ppp_str.h>
73 int vflag, rflag, cflag, aflag;
74 unsigned interval = 5;
76 int s; /* socket file descriptor */
77 int signalled; /* set if alarm goes off "early" */
79 extern char *malloc();
80 void catchalarm __P((int));
88 if (strcmp(argv[0], "-a") == 0) {
93 if (strcmp(argv[0], "-v") == 0) {
98 if (strcmp(argv[0], "-r") == 0) {
103 if (strcmp(argv[0], "-c") == 0) {
108 if (strcmp(argv[0], "-i") == 0 && argv[1] &&
109 isdigit(argv[1][0])) {
110 interval = atoi(argv[1]);
117 if (isdigit(argv[0][0])) {
118 unit = atoi(argv[0]);
128 if ((s = open("/dev/ppp", O_RDONLY)) < 0) {
129 perror("pppstats: Couldn't open /dev/ppp: ");
132 if (strioctl(s, PPPIO_ATTACH, &unit, sizeof(int), 0) < 0) {
133 fprintf(stderr, "pppstats: ppp%d is not available\n", unit);
137 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
138 perror("couldn't create IP socket");
148 fprintf(stderr, "Usage: pppstats [-v] [-r] [-c] [-i interval] [unit]\n");
152 #define V(offset) (line % 20? cur.offset - old.offset: cur.offset)
153 #define W(offset) (line % 20? ccs.offset - ocs.offset: ccs.offset)
155 #define CRATE(comp, inc, unc) ((unc) == 0? 0.0: \
156 1.0 - (double)((comp) + (inc)) / (unc))
159 * Print a running summary of interface statistics.
160 * Repeat display every interval seconds, showing statistics
161 * collected over that interval. Assumes that interval is non-zero.
162 * First line printed at top of screen is always cumulative.
166 register int line = 0;
167 sigset_t oldmask, mask;
168 struct ppp_stats cur, old;
169 struct ppp_comp_stats ccs, ocs;
171 memset(&old, 0, sizeof(old));
172 memset(&ocs, 0, sizeof(ocs));
177 get_ppp_cstats(&ccs);
179 (void)signal(SIGALRM, catchalarm);
181 (void)alarm(interval);
183 if ((line % 20) == 0) {
188 printf("%6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s",
189 "ubyte", "upack", "cbyte", "cpack", "ibyte", "ipack", "ratio");
190 printf(" | %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s",
191 "ubyte", "upack", "cbyte", "cpack", "ibyte", "ipack", "ratio");
195 printf("%6.6s %6.6s %6.6s %6.6s %6.6s",
196 "in", "pack", "comp", "uncomp", "err");
198 printf(" %6.6s %6.6s", "toss", "ip");
200 printf(" %6.6s %6.6s", "ratio", "ubyte");
201 printf(" | %6.6s %6.6s %6.6s %6.6s %6.6s",
202 "out", "pack", "comp", "uncomp", "ip");
204 printf(" %6.6s %6.6s", "search", "miss");
206 printf(" %6.6s %6.6s", "ratio", "ubyte");
209 memset(&old, 0, sizeof(old));
210 memset(&ocs, 0, sizeof(ocs));
214 printf("%6d %6d %6d %6d %6d %6d %6.2f",
221 W(d.ratio) == 0? 0.0: 1 - 1.0 / W(d.ratio) * 256.0);
223 printf(" | %6d %6d %6d %6d %6d %6d %6.2f",
230 W(d.ratio) == 0? 0.0: 1 - 1.0 / W(d.ratio) * 256.0);
235 printf("%6d %6d %6d %6d %6d",
237 V(p.ppp_ipackets), V(vj.vjs_compressedin),
238 V(vj.vjs_uncompressedin), V(vj.vjs_errorin));
240 printf(" %6d %6d", V(vj.vjs_tossed),
241 V(p.ppp_ipackets) - V(vj.vjs_compressedin) -
242 V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
245 CRATE(W(d.comp_bytes), W(d.unc_bytes), W(d.unc_bytes)),
247 printf(" | %6d %6d %6d %6d %6d", V(p.ppp_obytes),
248 V(p.ppp_opackets), V(vj.vjs_compressed),
249 V(vj.vjs_packets) - V(vj.vjs_compressed),
250 V(p.ppp_opackets) - V(vj.vjs_packets));
252 printf(" %6d %6d", V(vj.vjs_searches), V(vj.vjs_misses));
256 CRATE(W(d.comp_bytes), W(d.unc_bytes), W(d.unc_bytes)),
268 sigaddset(&mask, SIGALRM);
269 sigprocmask(SIG_BLOCK, &mask, &oldmask);
274 sigprocmask(SIG_SETMASK, &oldmask, NULL);
276 (void)alarm(interval);
286 * Called if an interval expires before sidewaysintpr has completed a loop.
287 * Sets a flag to not wait for the alarm.
297 struct ppp_stats *curp;
299 struct ifpppstatsreq req;
301 memset (&req, 0, sizeof (req));
304 req.stats_ptr = (caddr_t) &req.stats;
306 #define ifr_name ifr__name
309 sprintf(req.ifr_name, "ppp%d", unit);
310 if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
312 fprintf(stderr, "pppstats: kernel support missing\n");
314 perror("ioctl(SIOCGPPPSTATS)");
321 struct ppp_comp_stats *csp;
323 struct ifpppcstatsreq creq;
325 memset (&creq, 0, sizeof (creq));
328 creq.stats_ptr = (caddr_t) &creq.stats;
330 #define ifr_name ifr__name
333 sprintf(creq.ifr_name, "ppp%d", unit);
334 if (ioctl(s, SIOCGPPPCSTATS, &creq) < 0) {
335 if (errno == ENOTTY) {
336 fprintf(stderr, "pppstats: no kernel compression support\n");
341 perror("ioctl(SIOCGPPPCSTATS)");
347 if (creq.stats.c.bytes_out == 0)
348 creq.stats.c.ratio = 0.0;
350 creq.stats.c.ratio = (double) creq.stats.c.in_count /
351 (double) creq.stats.c.bytes_out;
353 if (creq.stats.d.bytes_out == 0)
354 creq.stats.d.ratio = 0.0;
356 creq.stats.d.ratio = (double) creq.stats.d.in_count /
357 (double) creq.stats.d.bytes_out;
365 struct ppp_stats *curp;
367 if (strioctl(s, PPPIO_GETSTAT, curp, 0, sizeof(*curp)) < 0) {
369 fprintf(stderr, "pppstats: kernel support missing\n");
371 perror("pppstats: Couldn't get statistics");
377 struct ppp_comp_stats *csp;
379 if (strioctl(s, PPPIO_GETCSTAT, csp, 0, sizeof(*csp)) < 0) {
380 if (errno == ENOTTY) {
381 fprintf(stderr, "pppstats: no kernel compression support\n");
386 perror("pppstats: Couldn't get compression statistics");
393 strioctl(fd, cmd, ptr, ilen, olen)
394 int fd, cmd, ilen, olen;
403 if (ioctl(fd, I_STR, &str) == -1)
405 if (str.ic_len != olen)
406 fprintf(stderr, "strioctl: expected %d bytes, got %d for cmd %x\n",
407 olen, str.ic_len, cmd);
410 #endif /* __svr4__ */