Line 0
Link Here
|
|
|
1 |
--- ./plugins/net/net.c.orig 2010-03-07 00:14:04.000000000 -0800 |
2 |
+++ ./plugins/net/net.c 2010-11-17 11:06:07.000000000 -0800 |
3 |
@@ -19,10 +19,23 @@ |
4 |
* |
5 |
*/ |
6 |
/*A little bug fixed by Mykola <mykola@2ka.mipt.ru>:) */ |
7 |
+/* FreeBSD code borrowed from patches to the lxpanel port */ |
8 |
|
9 |
|
10 |
|
11 |
#include "../chart/chart.h" |
12 |
+ |
13 |
+#ifdef __FreeBSD__ |
14 |
+#include <sys/types.h> |
15 |
+#include <sys/param.h> |
16 |
+#include <sys/socket.h> |
17 |
+#include <sys/ioctl.h> |
18 |
+#include <ifaddrs.h> |
19 |
+#include <net/if.h> |
20 |
+#include <net/if_media.h> |
21 |
+#include <net/if_var.h> |
22 |
+#endif |
23 |
+ |
24 |
#include <stdlib.h> |
25 |
#include <string.h> |
26 |
|
27 |
@@ -102,6 +115,171 @@ |
28 |
RET(TRUE); |
29 |
|
30 |
} |
31 |
+#elif defined __FreeBSD__ |
32 |
+static inline gboolean |
33 |
+parse_stats(char *buf, |
34 |
+ int prx_idx, |
35 |
+ int ptx_idx, |
36 |
+ gulong * in_packets, |
37 |
+ gulong * out_packets, |
38 |
+ int brx_idx, |
39 |
+ int btx_idx, |
40 |
+ gulong * in_bytes, |
41 |
+ gulong * out_bytes) |
42 |
+{ |
43 |
+ char *p; |
44 |
+ int i; |
45 |
+ |
46 |
+ p = strtok(buf, " \t\n"); |
47 |
+ for (i = 0; p; i++, p = strtok(NULL, " \t\n")) { |
48 |
+ if (i == prx_idx) |
49 |
+ *in_packets = g_ascii_strtoull(p, NULL, 10); |
50 |
+ if (i == ptx_idx) |
51 |
+ *out_packets = g_ascii_strtoull(p, NULL, 10); |
52 |
+ if (i == brx_idx) |
53 |
+ *in_bytes = g_ascii_strtoull(p, NULL, 10); |
54 |
+ if (i == btx_idx) |
55 |
+ *out_bytes = g_ascii_strtoull(p, NULL, 10); |
56 |
+ } |
57 |
+ |
58 |
+ if (i <= prx_idx || i <= ptx_idx || i <= brx_idx || i <= btx_idx) |
59 |
+ return FALSE; |
60 |
+ |
61 |
+ return TRUE; |
62 |
+} |
63 |
+ |
64 |
+static inline void |
65 |
+parse_header(char *buf, |
66 |
+ int *prx_idx, |
67 |
+ int *ptx_idx, |
68 |
+ int *brx_idx, |
69 |
+ int *btx_idx) |
70 |
+{ |
71 |
+ char *p; |
72 |
+ int i; |
73 |
+ |
74 |
+ *prx_idx = *ptx_idx = -1; |
75 |
+ *brx_idx = *btx_idx = -1; |
76 |
+ |
77 |
+ p = strtok(buf, " \n\t"); |
78 |
+ for (i = 0; p; i++, p = strtok(NULL, " \t\n")) { |
79 |
+ if (!strcmp(p, "Ipkts")) { |
80 |
+ *prx_idx = i; |
81 |
+ } else if (!strcmp(p, "Ibytes")) { |
82 |
+ *brx_idx = i; |
83 |
+ } else if (!strcmp(p, "Opkts")) { |
84 |
+ *ptx_idx = i; |
85 |
+ } else if (!strcmp(p, "Obytes")) { |
86 |
+ *btx_idx = i; |
87 |
+ } |
88 |
+ } |
89 |
+} |
90 |
+static int |
91 |
+net_get_load(net_priv * c) |
92 |
+{ |
93 |
+ struct net_stat net, net_diff; |
94 |
+ float total[2]; |
95 |
+ GError *error; |
96 |
+ char *command_line; |
97 |
+ char **argv; |
98 |
+ char *error_message = NULL; |
99 |
+ int pipe_out; |
100 |
+ gulong in_packets = -1; |
101 |
+ gulong out_packets = -1; |
102 |
+ gulong in_bytes = -1; |
103 |
+ gulong out_bytes = -1; |
104 |
+ char tooltip[256]; |
105 |
+ |
106 |
+ ENTER; |
107 |
+ error = NULL; |
108 |
+ command_line = g_strdup_printf("/usr/bin/netstat -n -I %s -b -f inet", c->iface); |
109 |
+ DBG(command_line); |
110 |
+ if (!g_shell_parse_argv(command_line, NULL, &argv, &error)) { |
111 |
+ error_message = g_strdup_printf("Could not parse command line '%s': %s", |
112 |
+ command_line, |
113 |
+ error->message); |
114 |
+ DBG(error_message); |
115 |
+ g_error_free(error); |
116 |
+ g_free(command_line); |
117 |
+ RET(0); |
118 |
+ } |
119 |
+ g_free(command_line); |
120 |
+ |
121 |
+ error = NULL; |
122 |
+ if (g_spawn_async_with_pipes(NULL, |
123 |
+ argv, |
124 |
+ NULL, |
125 |
+ 0, |
126 |
+ NULL, |
127 |
+ NULL, |
128 |
+ NULL, |
129 |
+ NULL, |
130 |
+ &pipe_out, |
131 |
+ NULL, |
132 |
+ &error)) { |
133 |
+ GIOChannel *channel; |
134 |
+ char *buf; |
135 |
+ int prx_idx , ptx_idx; |
136 |
+ int brx_idx , btx_idx; |
137 |
+ |
138 |
+ channel = g_io_channel_unix_new(pipe_out); |
139 |
+ |
140 |
+ g_io_channel_read_line(channel, &buf, NULL, NULL, NULL); |
141 |
+ parse_header(buf, &prx_idx, &ptx_idx, &brx_idx, &btx_idx); |
142 |
+ g_free(buf); |
143 |
+ |
144 |
+ if (prx_idx == -1 || ptx_idx == -1 || |
145 |
+ brx_idx == -1 || btx_idx == -1) { |
146 |
+ error_message = g_strdup("Could not parse 'netstat' output. Unknown format"); |
147 |
+ DBG(error_message); |
148 |
+ goto error_shutdown; |
149 |
+ } |
150 |
+ g_io_channel_read_line(channel, &buf, NULL, NULL, NULL); |
151 |
+ |
152 |
+ if (!parse_stats(buf, |
153 |
+ prx_idx, ptx_idx, &in_packets, &out_packets, |
154 |
+ brx_idx, btx_idx, &in_bytes, &out_bytes)) { |
155 |
+ error_message = g_strdup_printf("Could not parse interface statistics from '%s'. " |
156 |
+ "prx_idx = %d; ptx_idx = %d; brx_idx = %d; btx_idx = %d;", |
157 |
+ buf, prx_idx, ptx_idx, brx_idx, btx_idx); |
158 |
+ DBG(error_message); |
159 |
+ goto error_shutdown; |
160 |
+ } else if (in_packets == -1 || out_packets == -1 || in_bytes == -1 || out_bytes == -1) { |
161 |
+ error_message = g_strdup_printf("Could not obtain information on interface '%s' from netstat", |
162 |
+ c->iface); |
163 |
+ DBG(error_message); |
164 |
+ goto error_shutdown; |
165 |
+ } |
166 |
+ net.tx = out_bytes; |
167 |
+ net.rx = in_bytes; |
168 |
+ |
169 |
+ net_diff.tx = ((net.tx - c->net_prev.tx) >> 10) / CHECK_PERIOD; |
170 |
+ net_diff.rx = ((net.rx - c->net_prev.rx) >> 10) / CHECK_PERIOD; |
171 |
+ |
172 |
+ c->net_prev = net; |
173 |
+ total[0] = (float)(net_diff.tx) / c->max; |
174 |
+ total[1] = (float)(net_diff.rx) / c->max; |
175 |
+ DBG("%f %ul %ul\n", total, net_diff.tx, net_diff.rx); |
176 |
+ k->add_tick(&c->chart, total); |
177 |
+ g_snprintf(tooltip, sizeof(tooltip), "<b>%s:</b>\nD %lu Kbs, U %lu Kbs", |
178 |
+ c->iface, net_diff.rx, net_diff.tx); |
179 |
+ gtk_widget_set_tooltip_markup(((plugin_instance *) c)->pwid, tooltip); |
180 |
+ |
181 |
+error_shutdown: |
182 |
+ g_free(buf); |
183 |
+ g_io_channel_unref(channel); |
184 |
+ close(pipe_out); |
185 |
+ } else { |
186 |
+ error_message = g_strdup_printf("Error running /usr/bin/netstat for '%s': %s", |
187 |
+ c->iface, error->message); |
188 |
+ g_error_free(error); |
189 |
+ } |
190 |
+ |
191 |
+ g_strfreev(argv); |
192 |
+ |
193 |
+ RET(TRUE); |
194 |
+ |
195 |
+} |
196 |
#else |
197 |
|
198 |
static int |