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
12 * perkins@cps.msu.edu: Added compression statistics and alternate
15 * Brad Parker (brad@cayman.com) 6/92
17 * from the original "slstats" by Van Jaconson
19 * Copyright (c) 1989 Regents of the University of California.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms are permitted
23 * provided that the above copyright notice and this paragraph are
24 * duplicated in all such forms and that any documentation,
25 * advertising materials, and other materials related to such
26 * distribution and use acknowledge that the software was developed
27 * by the University of California, Berkeley. The name of the
28 * University may not be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
32 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
34 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
35 * - Initial distribution.
39 static char rcsid[] = "$Id: pppstats.c,v 1.9 1995/06/01 02:23:39 paulus Exp $";
48 #include <sys/param.h>
49 #include <sys/types.h>
50 #include <sys/ioctl.h>
52 #include <net/ppp_defs.h>
55 #include <sys/stropts.h>
56 #include <net/pppio.h> /* SVR4, Solaris 2, etc. */
59 #include <sys/socket.h>
63 #include <net/if_ppp.h> /* BSD, Linux, NeXT, etc. */
65 #else /* SunOS 4, AIX 4, OSF/1, etc. */
66 #define PPP_STATS 1 /* should be defined iff it is in ppp_if.c */
67 #include <sys/stream.h>
68 #include <net/ppp_str.h>
72 int vflag, rflag, cflag;
73 unsigned interval = 5;
75 int s; /* socket file descriptor */
76 int signalled; /* set if alarm goes off "early" */
78 extern char *malloc();
79 void catchalarm __P((int));
87 if (strcmp(argv[0], "-v") == 0) {
92 if (strcmp(argv[0], "-r") == 0) {
97 if (strcmp(argv[0], "-c") == 0) {
102 if (strcmp(argv[0], "-i") == 0 && argv[1] &&
103 isdigit(argv[1][0])) {
104 interval = atoi(argv[1]);
111 if (isdigit(argv[0][0])) {
112 unit = atoi(argv[0]);
122 if ((s = open("/dev/ppp", O_RDONLY)) < 0) {
123 perror("pppstats: Couldn't open /dev/ppp: ");
126 if (strioctl(s, PPPIO_ATTACH, &unit, sizeof(int), 0) < 0) {
127 fprintf(stderr, "pppstats: ppp%d is not available\n", unit);
131 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
132 perror("couldn't create IP socket");
142 fprintf(stderr, "Usage: pppstats [-v] [-r] [-c] [-i interval] [unit]\n");
146 #define V(offset) (line % 20? cur.offset - old.offset: cur.offset)
147 #define W(offset) (line % 20? ccs.offset - ocs.offset: ccs.offset)
149 #define CRATE(comp, inc, unc) ((unc) == 0? 0.0: \
150 1.0 - (double)((comp) + (inc)) / (unc))
153 * Print a running summary of interface statistics.
154 * Repeat display every interval seconds, showing statistics
155 * collected over that interval. Assumes that interval is non-zero.
156 * First line printed at top of screen is always cumulative.
160 register int line = 0;
161 sigset_t oldmask, mask;
162 struct ppp_stats cur, old;
163 struct ppp_comp_stats ccs, ocs;
165 memset(&old, 0, sizeof(old));
166 memset(&ocs, 0, sizeof(ocs));
171 get_ppp_cstats(&ccs);
173 (void)signal(SIGALRM, catchalarm);
175 (void)alarm(interval);
177 if ((line % 20) == 0) {
182 printf("%6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s",
183 "ubyte", "upack", "cbyte", "cpack", "ibyte", "ipack", "ratio");
184 printf(" | %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s",
185 "ubyte", "upack", "cbyte", "cpack", "ibyte", "ipack", "ratio");
189 printf("%6.6s %6.6s %6.6s %6.6s %6.6s",
190 "in", "pack", "comp", "uncomp", "err");
192 printf(" %6.6s %6.6s", "toss", "ip");
194 printf(" %6.6s %6.6s", "ratio", "ubyte");
195 printf(" | %6.6s %6.6s %6.6s %6.6s %6.6s",
196 "out", "pack", "comp", "uncomp", "ip");
198 printf(" %6.6s %6.6s", "search", "miss");
200 printf(" %6.6s %6.6s", "ratio", "ubyte");
203 memset(&old, 0, sizeof(old));
204 memset(&ocs, 0, sizeof(ocs));
208 printf("%6d %6d %6d %6d %6d %6d %6.2f",
215 W(d.ratio) == 0? 0.0: 1 - 1.0 / W(d.ratio) * 256.0);
217 printf(" | %6d %6d %6d %6d %6d %6d %6.2f",
224 W(d.ratio) == 0? 0.0: 1 - 1.0 / W(d.ratio) * 256.0);
229 printf("%6d %6d %6d %6d %6d",
231 V(p.ppp_ipackets), V(vj.vjs_compressedin),
232 V(vj.vjs_uncompressedin), V(vj.vjs_errorin));
234 printf(" %6d %6d", V(vj.vjs_tossed),
235 V(p.ppp_ipackets) - V(vj.vjs_compressedin) -
236 V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
239 CRATE(W(d.comp_bytes), W(d.unc_bytes), W(d.unc_bytes)),
241 printf(" | %6d %6d %6d %6d %6d", V(p.ppp_obytes),
242 V(p.ppp_opackets), V(vj.vjs_compressed),
243 V(vj.vjs_packets) - V(vj.vjs_compressed),
244 V(p.ppp_opackets) - V(vj.vjs_packets));
246 printf(" %6d %6d", V(vj.vjs_searches), V(vj.vjs_misses));
250 CRATE(W(d.comp_bytes), W(d.unc_bytes), W(d.unc_bytes)),
262 sigaddset(&mask, SIGALRM);
263 sigprocmask(SIG_BLOCK, &mask, &oldmask);
268 sigprocmask(SIG_SETMASK, &oldmask, NULL);
270 (void)alarm(interval);
277 * Called if an interval expires before sidewaysintpr has completed a loop.
278 * Sets a flag to not wait for the alarm.
288 struct ppp_stats *curp;
290 struct ifpppstatsreq req;
292 sprintf(req.ifr_name, "ppp%d", unit);
293 if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
295 fprintf(stderr, "pppstats: kernel support missing\n");
297 perror("ioctl(SIOCGPPPSTATS)");
304 struct ppp_comp_stats *csp;
306 struct ifpppcstatsreq creq;
308 sprintf(creq.ifr_name, "ppp%d", unit);
309 if (ioctl(s, SIOCGPPPCSTATS, &creq) < 0) {
310 if (errno == ENOTTY) {
311 fprintf(stderr, "pppstats: no kernel compression support\n");
316 perror("ioctl(SIOCGPPPCSTATS)");
325 struct ppp_stats *curp;
327 if (strioctl(s, PPPIO_GETSTAT, curp, 0, sizeof(*curp)) < 0) {
329 fprintf(stderr, "pppstats: kernel support missing\n");
331 perror("pppstats: Couldn't get statistics");
337 struct ppp_comp_stats *csp;
339 if (strioctl(s, PPPIO_GETCSTAT, csp, 0, sizeof(*csp)) < 0) {
340 if (errno == ENOTTY) {
341 fprintf(stderr, "pppstats: no kernel compression support\n");
346 perror("pppstats: Couldn't get compression statistics");
353 strioctl(fd, cmd, ptr, ilen, olen)
354 int fd, cmd, ilen, olen;
363 if (ioctl(fd, I_STR, &str) == -1)
365 if (str.ic_len != olen)
366 fprintf(stderr, "strioctl: expected %d bytes, got %d for cmd %x\n",
367 olen, str.ic_len, cmd);
370 #endif /* __svr4__ */