1 /*
2  * Copyright (c) 2001-2007 Willem Dijkstra
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  *
9  *    - Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *    - Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials provided
14  *      with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  */
30 
31 /*
32  * Get current interface statistics from kernel and return them in symon_buf as
33  *
34  * ipackets : opackets : ibytes : obytes : imcasts : omcasts : ierrors :
35  * oerrors : colls : drops
36  */
37 
38 #include <sys/types.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #include <net/if_types.h>
46 #include <netinet/in.h>
47 #include <netinet/in_var.h>
48 
49 #include <errno.h>
50 #include <limits.h>
51 #include <string.h>
52 
53 #include "error.h"
54 #include "symon.h"
55 
56 /* Globals for this module start with if_ */
57 static int if_s = -1;
58 
59 void
init_if(struct stream * st)60 init_if(struct stream *st)
61 {
62     if (if_s == -1) {
63         if ((if_s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
64             fatal("%s:%d: socket failed, %.200",
65                   __FILE__, __LINE__, strerror(errno));
66         }
67     }
68 
69     strncpy(st->parg.ifr.ifr_name, st->arg, IFNAMSIZ - 1);
70     st->parg.ifr.ifr_name[IFNAMSIZ - 1] = '\0';
71 
72     info("started module if(%.200s)", st->arg);
73 }
74 
75 void
gets_if()76 gets_if()
77 {
78     /* EMPTY */
79 }
80 
81 int
get_if(char * symon_buf,int maxlen,struct stream * st)82 get_if(char *symon_buf, int maxlen, struct stream *st)
83 {
84     struct if_data ifdata;
85 
86     st->parg.ifr.ifr_data = (caddr_t) &ifdata;
87 
88     if (ioctl(if_s, SIOCGIFDATA, &st->parg.ifr)) {
89         warning("if(%.200s) failed (ioctl error)", st->arg);
90         return 0;
91     }
92 
93     return snpack(symon_buf, maxlen, st->arg, MT_IF2,
94                   (u_int64_t) ifdata.ifi_ipackets,
95                   (u_int64_t) ifdata.ifi_opackets,
96                   (u_int64_t) ifdata.ifi_ibytes,
97                   (u_int64_t) ifdata.ifi_obytes,
98                   (u_int64_t) ifdata.ifi_imcasts,
99                   (u_int64_t) ifdata.ifi_omcasts,
100                   (u_int64_t) ifdata.ifi_ierrors,
101                   (u_int64_t) ifdata.ifi_oerrors,
102                   (u_int64_t) ifdata.ifi_collisions,
103                   (u_int64_t) ifdata.ifi_iqdrops);
104 }
105