1 /*- 2 * Copyright (c) 2009 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 */ 29 30 #include <sys/param.h> 31 32 #include <err.h> 33 #include <signal.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <strings.h> 37 #include <unistd.h> 38 39 #include "npestats.h" 40 41 static struct { 42 const char *tag; 43 const char *fmt; 44 } tags[] = { 45 { "default", 46 "align,fcs,macrx,overrun,learn,large,stp,badsrc,underflow,collision1,collisionM,deferred,late,excessive,mactx,carrier,toobig" 47 }, 48 }; 49 50 static const char * 51 getfmt(const char *tag) 52 { 53 int i; 54 for (i = 0; i < nitems(tags); i++) 55 if (strcasecmp(tags[i].tag, tag) == 0) 56 return tags[i].fmt; 57 return tag; 58 } 59 60 static int signalled; 61 62 static void 63 catchalarm(int signo __unused) 64 { 65 signalled = 1; 66 } 67 68 int 69 main(int argc, char *argv[]) 70 { 71 struct npestatfoo *wf; 72 const char *ifname; 73 int c, banner = 1; 74 75 ifname = getenv("NPE"); 76 if (ifname == NULL) 77 ifname = "npe0"; 78 wf = npestats_new(ifname, getfmt("default")); 79 while ((c = getopt(argc, argv, "bi:lo:z")) != -1) { 80 switch (c) { 81 case 'b': 82 banner = 0; 83 break; 84 case 'i': 85 wf->setifname(wf, optarg); 86 break; 87 case 'l': 88 wf->print_fields(wf, stdout); 89 return 0; 90 case 'o': 91 wf->setfmt(wf, getfmt(optarg)); 92 break; 93 default: 94 errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [interval]\n", argv[0]); 95 /*NOTREACHED*/ 96 } 97 } 98 argc -= optind; 99 argv += optind; 100 101 if (argc > 0) { 102 u_long interval = strtoul(argv[0], NULL, 0); 103 int line, omask; 104 105 if (interval < 1) 106 interval = 1; 107 signal(SIGALRM, catchalarm); 108 signalled = 0; 109 alarm(interval); 110 banner: 111 if (banner) 112 wf->print_header(wf, stdout); 113 line = 0; 114 loop: 115 if (line != 0) { 116 wf->collect_cur(wf); 117 wf->print_current(wf, stdout); 118 wf->update_tot(wf); 119 } else { 120 wf->collect_tot(wf); 121 wf->print_total(wf, stdout); 122 } 123 fflush(stdout); 124 omask = sigblock(sigmask(SIGALRM)); 125 if (!signalled) 126 sigpause(0); 127 sigsetmask(omask); 128 signalled = 0; 129 alarm(interval); 130 line++; 131 if (line == 21) /* XXX tty line count */ 132 goto banner; 133 else 134 goto loop; 135 /*NOTREACHED*/ 136 } else { 137 wf->collect_tot(wf); 138 wf->print_verbose(wf, stdout); 139 } 140 return 0; 141 } 142