Annotation of embedaddon/bmon/src/in_sysctl.c, revision 1.1.1.1
1.1 misho 1: /*
2: * in_sysctl.c sysctl (BSD)
3: *
4: * Copyright (c) 2001-2004 Thomas Graf <tgraf@suug.ch>
5: *
6: * Permission is hereby granted, free of charge, to any person obtaining a
7: * copy of this software and associated documentation files (the "Software"),
8: * to deal in the Software without restriction, including without limitation
9: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10: * and/or sell copies of the Software, and to permit persons to whom the
11: * Software is furnished to do so, subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included
14: * in all copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22: * DEALINGS IN THE SOFTWARE.
23: */
24:
25: #include <bmon/bmon.h>
26: #include <bmon/input.h>
27: #include <bmon/item.h>
28: #include <bmon/conf.h>
29: #include <bmon/node.h>
30: #include <bmon/utils.h>
31:
32: #if defined SYS_BSD
33: #include <sys/socket.h>
34: #include <net/if.h>
35: #include <sys/param.h>
36: #include <sys/sysctl.h>
37: #include <net/if_dl.h>
38: #include <net/route.h>
39:
40: static int c_debug;
41:
42: static void sysctl_read(void)
43: {
44: int mib[] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
45: size_t n;
46: char *buf, *next, *lim;
47:
48: if (sysctl(mib, 6, NULL, &n, NULL, 0) < 0)
49: quit("sysctl() failed");
50:
51: if (c_debug)
52: fprintf(stderr, "sysctl 1-pass n=%d\n", (int) n);
53:
54: buf = xcalloc(1, n);
55:
56: if (sysctl(mib, 6, buf, &n, NULL, 0) < 0)
57: quit("sysctl() failed");
58:
59: if (c_debug)
60: fprintf(stderr, "sysctl 2-pass n=%d\n", (int) n);
61:
62: lim = buf + n;
63: next = buf;
64:
65: while (next < lim) {
66: struct if_msghdr *ifm, *nextifm;
67: struct sockaddr_dl *sdl;
68: item_t *it;
69:
70: ifm = (struct if_msghdr *) next;
71: if (ifm->ifm_type != RTM_IFINFO)
72: break;
73:
74: next += ifm->ifm_msglen;
75: while (next < lim) {
76: nextifm = (struct if_msghdr *) next;
77: if (nextifm->ifm_type != RTM_NEWADDR)
78: break;
79: next += nextifm->ifm_msglen;
80: }
81:
82: sdl = (struct sockaddr_dl *) (ifm + 1);
83:
84: if (sdl->sdl_family != AF_LINK)
85: continue;
86:
87: if (get_show_only_running() && !(ifm->ifm_flags & IFF_UP))
88: continue;
89:
90: if (c_debug)
91: fprintf(stderr, "Processing %s\n", sdl->sdl_data);
92:
93: it = lookup_item(get_local_node(), sdl->sdl_data, 0, 0);
94: if (it == NULL)
95: continue;
96:
97: it->i_major_attr = BYTES;
98: it->i_minor_attr = PACKETS;
99:
100: update_attr(it, PACKETS,
101: ifm->ifm_data.ifi_ipackets,
102: ifm->ifm_data.ifi_opackets,
103: RX_PROVIDED | TX_PROVIDED);
104:
105: update_attr(it, BYTES,
106: ifm->ifm_data.ifi_ibytes,
107: ifm->ifm_data.ifi_obytes,
108: RX_PROVIDED | TX_PROVIDED);
109:
110: update_attr(it, ERRORS,
111: ifm->ifm_data.ifi_ierrors,
112: ifm->ifm_data.ifi_oerrors,
113: RX_PROVIDED | TX_PROVIDED);
114:
115: update_attr(it, COLLISIONS,
116: 0, ifm->ifm_data.ifi_collisions,
117: TX_PROVIDED);
118:
119: update_attr(it, MULTICAST,
120: ifm->ifm_data.ifi_imcasts,
121: 0,
122: RX_PROVIDED);
123:
124: update_attr(it, DROP,
125: 0,
126: ifm->ifm_data.ifi_iqdrops,
127: TX_PROVIDED);
128:
129: notify_update(it, NULL);
130: increase_lifetime(it, 1);
131: }
132:
133: xfree(buf);
134: }
135:
136: static void print_help(void)
137: {
138: printf(
139: "sysctl - sysctl statistic collector for BSD and Darwin\n" \
140: "\n" \
141: " BSD and Darwin statistic collector using sysctl()\n" \
142: " Author: Thomas Graf <tgraf@suug.ch>\n" \
143: "\n");
144: }
145:
146: static void sysctl_set_opts(tv_t *attrs)
147: {
148: while (attrs) {
149: if (!strcasecmp(attrs->type, "debug"))
150: c_debug = 1;
151: else if (!strcasecmp(attrs->type, "help")) {
152: print_help();
153: exit(0);
154: }
155: attrs = attrs->next;
156: }
157: }
158:
159: static int sysctl_probe(void)
160: {
161: size_t n;
162: int mib[] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
163: if (sysctl(mib, 6, NULL, &n, NULL, 0) < 0)
164: return 0;
165: return 1;
166: }
167:
168: static struct input_module kstat_ops = {
169: .im_name = "sysctl",
170: .im_read = sysctl_read,
171: .im_set_opts = sysctl_set_opts,
172: .im_probe = sysctl_probe,
173: };
174:
175: static void __init sysctl_init(void)
176: {
177: register_input_module(&kstat_ops);
178: }
179:
180: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>