1 |
< |
/* |
2 |
< |
* i-scream central monitoring system |
1 |
> |
/* |
2 |
> |
* i-scream libstatgrab |
3 |
|
* http://www.i-scream.org |
4 |
< |
* Copyright (C) 2000-2003 i-scream |
4 |
> |
* Copyright (C) 2000-2004 i-scream |
5 |
|
* |
6 |
< |
* This program is free software; you can redistribute it and/or |
7 |
< |
* modify it under the terms of the GNU General Public License |
8 |
< |
* as published by the Free Software Foundation; either version 2 |
9 |
< |
* of the License, or (at your option) any later version. |
6 |
> |
* This library is free software; you can redistribute it and/or |
7 |
> |
* modify it under the terms of the GNU Lesser General Public |
8 |
> |
* License as published by the Free Software Foundation; either |
9 |
> |
* version 2.1 of the License, or (at your option) any later version. |
10 |
|
* |
11 |
< |
* This program is distributed in the hope that it will be useful, |
11 |
> |
* This library is distributed in the hope that it will be useful, |
12 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
< |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
< |
* GNU General Public License for more details. |
13 |
> |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
> |
* Lesser General Public License for more details. |
15 |
|
* |
16 |
< |
* You should have received a copy of the GNU General Public License |
17 |
< |
* along with this program; if not, write to the Free Software |
18 |
< |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
16 |
> |
* You should have received a copy of the GNU Lesser General Public |
17 |
> |
* License along with this library; if not, write to the Free Software |
18 |
> |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
19 |
> |
* 02111-1307 USA |
20 |
> |
* |
21 |
> |
* $Id$ |
22 |
|
*/ |
23 |
|
|
24 |
|
#ifdef HAVE_CONFIG_H |
28 |
|
#include <stdlib.h> |
29 |
|
#include <string.h> |
30 |
|
#include "statgrab.h" |
31 |
+ |
#include "vector.h" |
32 |
+ |
#include "tools.h" |
33 |
|
#include <time.h> |
34 |
|
#ifdef SOLARIS |
35 |
|
#include <kstat.h> |
36 |
|
#include <sys/sysinfo.h> |
37 |
+ |
#include <sys/types.h> |
38 |
+ |
#include <sys/socket.h> |
39 |
+ |
#include <sys/ioctl.h> |
40 |
+ |
#include <net/if.h> |
41 |
+ |
#include <netinet/in.h> |
42 |
+ |
#include <sys/sockio.h> |
43 |
+ |
#include <unistd.h> |
44 |
|
#endif |
45 |
|
#ifdef LINUX |
46 |
|
#include <stdio.h> |
47 |
|
#include <sys/types.h> |
48 |
< |
#include <regex.h> |
49 |
< |
#include "tools.h" |
48 |
> |
#include <sys/ioctl.h> |
49 |
> |
#include <sys/socket.h> |
50 |
> |
#include <net/if.h> |
51 |
> |
#include <ctype.h> |
52 |
> |
/* Stuff which could be defined by defining KERNEL, but |
53 |
> |
* that would be a bad idea, so we'll just declare it here |
54 |
> |
*/ |
55 |
> |
typedef __uint8_t u8; |
56 |
> |
typedef __uint16_t u16; |
57 |
> |
typedef __uint32_t u32; |
58 |
> |
typedef __uint64_t u64; |
59 |
> |
#include <linux/ethtool.h> |
60 |
> |
#include <linux/sockios.h> |
61 |
> |
#include <unistd.h> |
62 |
|
#endif |
63 |
< |
#ifdef FREEBSD |
63 |
> |
#ifdef ALLBSD |
64 |
|
#include <sys/types.h> |
65 |
|
#include <sys/socket.h> |
66 |
|
#include <ifaddrs.h> |
67 |
|
#include <net/if.h> |
68 |
+ |
#include <net/if_media.h> |
69 |
+ |
#include <sys/ioctl.h> |
70 |
+ |
#include <unistd.h> |
71 |
|
#endif |
72 |
|
|
73 |
< |
static network_stat_t *network_stats=NULL; |
74 |
< |
static int interfaces=0; |
75 |
< |
|
76 |
< |
void network_stat_init(int start, int end, network_stat_t *net_stats){ |
77 |
< |
|
78 |
< |
for(net_stats+=start; start<end; start++){ |
79 |
< |
net_stats->interface_name=NULL; |
80 |
< |
net_stats->tx=0; |
81 |
< |
net_stats->rx=0; |
55 |
< |
net_stats++; |
56 |
< |
} |
73 |
> |
static void network_stat_init(sg_network_io_stats *s) { |
74 |
> |
s->interface_name = NULL; |
75 |
> |
s->tx = 0; |
76 |
> |
s->rx = 0; |
77 |
> |
s->ipackets = 0; |
78 |
> |
s->opackets = 0; |
79 |
> |
s->ierrors = 0; |
80 |
> |
s->oerrors = 0; |
81 |
> |
s->collisions = 0; |
82 |
|
} |
83 |
|
|
84 |
< |
network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){ |
85 |
< |
|
61 |
< |
if(net_stats==NULL){ |
62 |
< |
|
63 |
< |
if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){ |
64 |
< |
return NULL; |
65 |
< |
} |
66 |
< |
network_stat_init(0, needed_entries, net_stats); |
67 |
< |
*cur_entries=needed_entries; |
68 |
< |
|
69 |
< |
return net_stats; |
70 |
< |
} |
71 |
< |
|
72 |
< |
|
73 |
< |
if(*cur_entries<needed_entries){ |
74 |
< |
net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries)); |
75 |
< |
if(net_stats==NULL){ |
76 |
< |
return NULL; |
77 |
< |
} |
78 |
< |
network_stat_init(*cur_entries, needed_entries, net_stats); |
79 |
< |
*cur_entries=needed_entries; |
80 |
< |
} |
81 |
< |
|
82 |
< |
return net_stats; |
84 |
> |
static void network_stat_destroy(sg_network_io_stats *s) { |
85 |
> |
free(s->interface_name); |
86 |
|
} |
87 |
|
|
88 |
+ |
VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5, |
89 |
+ |
network_stat_init, network_stat_destroy); |
90 |
|
|
91 |
< |
network_stat_t *get_network_stats(int *entries){ |
91 |
> |
sg_network_io_stats *sg_get_network_io_stats(int *entries){ |
92 |
> |
int interfaces; |
93 |
> |
sg_network_io_stats *network_stat_ptr; |
94 |
|
|
88 |
– |
static int sizeof_network_stats=0; |
89 |
– |
network_stat_t *network_stat_ptr; |
90 |
– |
|
95 |
|
#ifdef SOLARIS |
96 |
< |
kstat_ctl_t *kc; |
97 |
< |
kstat_t *ksp; |
96 |
> |
kstat_ctl_t *kc; |
97 |
> |
kstat_t *ksp; |
98 |
|
kstat_named_t *knp; |
99 |
|
#endif |
100 |
|
|
103 |
|
/* Horrible big enough, but it should be easily big enough */ |
104 |
|
char line[8096]; |
105 |
|
regex_t regex; |
106 |
< |
regmatch_t line_match[4]; |
106 |
> |
regmatch_t line_match[9]; |
107 |
|
#endif |
108 |
< |
#ifdef FREEBSD |
108 |
> |
#ifdef ALLBSD |
109 |
|
struct ifaddrs *net, *net_ptr; |
110 |
|
struct if_data *net_data; |
111 |
|
#endif |
112 |
|
|
113 |
< |
#ifdef FREEBSD |
113 |
> |
#ifdef ALLBSD |
114 |
|
if(getifaddrs(&net) != 0){ |
115 |
+ |
sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL); |
116 |
|
return NULL; |
117 |
|
} |
118 |
|
|
120 |
|
|
121 |
|
for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){ |
122 |
|
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
123 |
< |
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
124 |
< |
if(network_stats==NULL){ |
123 |
> |
|
124 |
> |
if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) { |
125 |
|
return NULL; |
126 |
|
} |
127 |
|
network_stat_ptr=network_stats+interfaces; |
128 |
|
|
129 |
< |
if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name); |
130 |
< |
network_stat_ptr->interface_name=strdup(net_ptr->ifa_name); |
131 |
< |
if(network_stat_ptr->interface_name==NULL) return NULL; |
129 |
> |
if (sg_update_string(&network_stat_ptr->interface_name, |
130 |
> |
net_ptr->ifa_name) < 0) { |
131 |
> |
return NULL; |
132 |
> |
} |
133 |
|
net_data=(struct if_data *)net_ptr->ifa_data; |
134 |
|
network_stat_ptr->rx=net_data->ifi_ibytes; |
135 |
< |
network_stat_ptr->tx=net_data->ifi_obytes; |
135 |
> |
network_stat_ptr->tx=net_data->ifi_obytes; |
136 |
> |
network_stat_ptr->ipackets=net_data->ifi_ipackets; |
137 |
> |
network_stat_ptr->opackets=net_data->ifi_opackets; |
138 |
> |
network_stat_ptr->ierrors=net_data->ifi_ierrors; |
139 |
> |
network_stat_ptr->oerrors=net_data->ifi_oerrors; |
140 |
> |
network_stat_ptr->collisions=net_data->ifi_collisions; |
141 |
|
network_stat_ptr->systime=time(NULL); |
142 |
|
interfaces++; |
143 |
|
} |
145 |
|
#endif |
146 |
|
|
147 |
|
#ifdef SOLARIS |
148 |
< |
if ((kc = kstat_open()) == NULL) { |
149 |
< |
return NULL; |
150 |
< |
} |
148 |
> |
if ((kc = kstat_open()) == NULL) { |
149 |
> |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
150 |
> |
return NULL; |
151 |
> |
} |
152 |
|
|
153 |
|
interfaces=0; |
154 |
|
|
155 |
< |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
156 |
< |
if (!strcmp(ksp->ks_class, "net")) { |
157 |
< |
kstat_read(kc, ksp, NULL); |
155 |
> |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
156 |
> |
if (!strcmp(ksp->ks_class, "net")) { |
157 |
> |
kstat_read(kc, ksp, NULL); |
158 |
|
|
159 |
|
#ifdef SOL7 |
160 |
< |
#define RLOOKUP "rbytes" |
161 |
< |
#define WLOOKUP "obytes" |
160 |
> |
#define LRX "rbytes" |
161 |
> |
#define LTX "obytes" |
162 |
> |
#define LIPACKETS "ipackets" |
163 |
> |
#define LOPACKETS "opackets" |
164 |
|
#define VALTYPE value.ui32 |
165 |
|
#else |
166 |
< |
#define RLOOKUP "rbytes64" |
167 |
< |
#define WLOOKUP "obytes64" |
166 |
> |
#define LRX "rbytes64" |
167 |
> |
#define LTX "obytes64" |
168 |
> |
#define LIPACKETS "ipackets64" |
169 |
> |
#define LOPACKETS "opackets64" |
170 |
|
#define VALTYPE value.ui64 |
171 |
|
#endif |
172 |
|
|
173 |
< |
if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){ |
174 |
< |
/* Not a network interface, so skip to the next entry */ |
173 |
> |
/* Read rx */ |
174 |
> |
if((knp=kstat_data_lookup(ksp, LRX))==NULL){ |
175 |
> |
/* This is a network interface, but it doesn't |
176 |
> |
* have the rbytes/obytes values; for instance, |
177 |
> |
* the loopback devices have this behaviour |
178 |
> |
* (although they do track packets in/out). */ |
179 |
> |
/* FIXME: Show packet counts when byte counts |
180 |
> |
* not available. */ |
181 |
|
continue; |
182 |
|
} |
183 |
|
|
184 |
< |
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
185 |
< |
if(network_stats==NULL){ |
184 |
> |
/* Create new network_stats */ |
185 |
> |
if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) { |
186 |
|
return NULL; |
187 |
|
} |
188 |
|
network_stat_ptr=network_stats+interfaces; |
189 |
+ |
|
190 |
+ |
/* Finish reading rx */ |
191 |
|
network_stat_ptr->rx=knp->VALTYPE; |
192 |
|
|
193 |
< |
if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){ |
194 |
< |
/* Not a network interface, so skip to the next entry */ |
193 |
> |
/* Read tx */ |
194 |
> |
if((knp=kstat_data_lookup(ksp, LTX))==NULL){ |
195 |
|
continue; |
196 |
|
} |
197 |
|
network_stat_ptr->tx=knp->VALTYPE; |
198 |
< |
if(network_stat_ptr->interface_name!=NULL){ |
199 |
< |
free(network_stat_ptr->interface_name); |
198 |
> |
|
199 |
> |
/* Read ipackets */ |
200 |
> |
if((knp=kstat_data_lookup(ksp, LIPACKETS))==NULL){ |
201 |
> |
continue; |
202 |
|
} |
203 |
< |
network_stat_ptr->interface_name=strdup(ksp->ks_name); |
203 |
> |
network_stat_ptr->ipackets=knp->VALTYPE; |
204 |
|
|
205 |
+ |
/* Read opackets */ |
206 |
+ |
if((knp=kstat_data_lookup(ksp, LOPACKETS))==NULL){ |
207 |
+ |
continue; |
208 |
+ |
} |
209 |
+ |
network_stat_ptr->opackets=knp->VALTYPE; |
210 |
+ |
|
211 |
+ |
/* Read ierrors */ |
212 |
+ |
if((knp=kstat_data_lookup(ksp, "ierrors"))==NULL){ |
213 |
+ |
continue; |
214 |
+ |
} |
215 |
+ |
network_stat_ptr->ierrors=knp->value.ui32; |
216 |
+ |
|
217 |
+ |
/* Read oerrors */ |
218 |
+ |
if((knp=kstat_data_lookup(ksp, "oerrors"))==NULL){ |
219 |
+ |
continue; |
220 |
+ |
} |
221 |
+ |
network_stat_ptr->oerrors=knp->value.ui32; |
222 |
+ |
|
223 |
+ |
/* Read collisions */ |
224 |
+ |
if((knp=kstat_data_lookup(ksp, "collisions"))==NULL){ |
225 |
+ |
continue; |
226 |
+ |
} |
227 |
+ |
network_stat_ptr->collisions=knp->value.ui32; |
228 |
+ |
|
229 |
+ |
/* Read interface name */ |
230 |
+ |
if (sg_update_string(&network_stat_ptr->interface_name, |
231 |
+ |
ksp->ks_name) < 0) { |
232 |
+ |
return NULL; |
233 |
+ |
} |
234 |
+ |
|
235 |
+ |
/* Store systime */ |
236 |
|
network_stat_ptr->systime=time(NULL); |
237 |
+ |
|
238 |
|
interfaces++; |
239 |
|
} |
240 |
|
} |
244 |
|
#ifdef LINUX |
245 |
|
f=fopen("/proc/net/dev", "r"); |
246 |
|
if(f==NULL){ |
247 |
+ |
sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev"); |
248 |
|
return NULL; |
249 |
|
} |
250 |
|
/* read the 2 lines.. Its the title, so we dont care :) */ |
252 |
|
fgets(line, sizeof(line), f); |
253 |
|
|
254 |
|
|
255 |
< |
if((regcomp(®ex, "^[[:space:]]*([^:]+):[[:space:]]*([[:digit:]]+)[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+([[:digit:]]+)", REG_EXTENDED))!=0){ |
255 |
> |
if((regcomp(®ex, "^ *([^:]+): *([0-9]+) +([0-9]+) +([0-9]+) +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +([0-9]+) +([0-9]+) +([0-9]+) +[0-9]+ +[0-9]+ +([0-9]+)", REG_EXTENDED))!=0){ |
256 |
> |
sg_set_error(SG_ERROR_PARSE, NULL); |
257 |
|
return NULL; |
258 |
|
} |
259 |
|
|
260 |
|
interfaces=0; |
261 |
|
|
262 |
|
while((fgets(line, sizeof(line), f)) != NULL){ |
263 |
< |
if((regexec(®ex, line, 4, line_match, 0))!=0){ |
263 |
> |
if((regexec(®ex, line, 9, line_match, 0))!=0){ |
264 |
|
continue; |
265 |
|
} |
266 |
< |
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
267 |
< |
if(network_stats==NULL){ |
268 |
< |
return NULL; |
266 |
> |
|
267 |
> |
if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) { |
268 |
> |
return NULL; |
269 |
|
} |
270 |
< |
network_stat_ptr=network_stats+interfaces; |
270 |
> |
network_stat_ptr=network_stats+interfaces; |
271 |
|
|
272 |
|
if(network_stat_ptr->interface_name!=NULL){ |
273 |
|
free(network_stat_ptr->interface_name); |
274 |
|
} |
275 |
|
|
276 |
< |
network_stat_ptr->interface_name=get_string_match(line, &line_match[1]); |
277 |
< |
network_stat_ptr->rx=get_ll_match(line, &line_match[2]); |
278 |
< |
network_stat_ptr->tx=get_ll_match(line, &line_match[3]); |
276 |
> |
network_stat_ptr->interface_name=sg_get_string_match(line, &line_match[1]); |
277 |
> |
network_stat_ptr->rx=sg_get_ll_match(line, &line_match[2]); |
278 |
> |
network_stat_ptr->tx=sg_get_ll_match(line, &line_match[5]); |
279 |
> |
network_stat_ptr->ipackets=sg_get_ll_match(line, &line_match[3]); |
280 |
> |
network_stat_ptr->opackets=sg_get_ll_match(line, &line_match[6]); |
281 |
> |
network_stat_ptr->ierrors=sg_get_ll_match(line, &line_match[4]); |
282 |
> |
network_stat_ptr->oerrors=sg_get_ll_match(line, &line_match[7]); |
283 |
> |
network_stat_ptr->collisions=sg_get_ll_match(line, &line_match[8]); |
284 |
|
network_stat_ptr->systime=time(NULL); |
285 |
|
|
286 |
|
interfaces++; |
289 |
|
regfree(®ex); |
290 |
|
|
291 |
|
#endif |
292 |
+ |
|
293 |
+ |
#ifdef CYGWIN |
294 |
+ |
sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin"); |
295 |
+ |
return NULL; |
296 |
+ |
#endif |
297 |
+ |
|
298 |
|
*entries=interfaces; |
299 |
|
|
300 |
|
return network_stats; |
301 |
|
} |
302 |
|
|
303 |
< |
long long transfer_diff(long long new, long long old){ |
304 |
< |
#ifdef SOL7 |
305 |
< |
#define MAXVAL 4294967296 |
303 |
> |
static long long transfer_diff(long long new, long long old){ |
304 |
> |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD) |
305 |
> |
/* 32-bit quantities, so we must explicitly deal with wraparound. */ |
306 |
> |
#define MAXVAL 0x100000000LL |
307 |
> |
if (new >= old) { |
308 |
> |
return new - old; |
309 |
> |
} else { |
310 |
> |
return MAXVAL + new - old; |
311 |
> |
} |
312 |
|
#else |
313 |
< |
#define MAXVAL 18446744073709551616 |
313 |
> |
/* 64-bit quantities, so plain subtraction works. */ |
314 |
> |
return new - old; |
315 |
|
#endif |
316 |
< |
long long result; |
239 |
< |
if(new>=old){ |
240 |
< |
result = (new-old); |
241 |
< |
}else{ |
242 |
< |
result = (MAXVAL+(new-old)); |
243 |
< |
} |
316 |
> |
} |
317 |
|
|
318 |
< |
return result; |
318 |
> |
sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) { |
319 |
> |
VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1, |
320 |
> |
network_stat_init, network_stat_destroy); |
321 |
> |
sg_network_io_stats *src = NULL, *dest; |
322 |
> |
int i, j, diff_count, new_count; |
323 |
|
|
324 |
< |
} |
324 |
> |
if (network_stats == NULL) { |
325 |
> |
/* No previous stats, so we can't calculate a difference. */ |
326 |
> |
return sg_get_network_io_stats(entries); |
327 |
> |
} |
328 |
|
|
329 |
< |
network_stat_t *get_network_stats_diff(int *entries){ |
330 |
< |
static network_stat_t *network_stats_diff=NULL; |
331 |
< |
static int sizeof_net_stats_diff=0; |
332 |
< |
network_stat_t *network_stats_ptr, *network_stats_diff_ptr; |
333 |
< |
int ifaces, x, y; |
329 |
> |
/* Resize the results array to match the previous stats. */ |
330 |
> |
diff_count = VECTOR_SIZE(network_stats); |
331 |
> |
if (VECTOR_RESIZE(diff, diff_count) < 0) { |
332 |
> |
return NULL; |
333 |
> |
} |
334 |
|
|
335 |
< |
if(network_stats==NULL){ |
336 |
< |
network_stats_ptr=get_network_stats(&ifaces); |
337 |
< |
*entries=ifaces; |
338 |
< |
return network_stats_ptr; |
335 |
> |
/* Copy the previous stats into the result. */ |
336 |
> |
for (i = 0; i < diff_count; i++) { |
337 |
> |
src = &network_stats[i]; |
338 |
> |
dest = &diff[i]; |
339 |
> |
|
340 |
> |
if (sg_update_string(&dest->interface_name, |
341 |
> |
src->interface_name) < 0) { |
342 |
> |
return NULL; |
343 |
> |
} |
344 |
> |
dest->rx = src->rx; |
345 |
> |
dest->tx = src->tx; |
346 |
> |
dest->ipackets = src->ipackets; |
347 |
> |
dest->opackets = src->opackets; |
348 |
> |
dest->ierrors = src->ierrors; |
349 |
> |
dest->oerrors = src->oerrors; |
350 |
> |
dest->collisions = src->collisions; |
351 |
> |
dest->systime = src->systime; |
352 |
|
} |
353 |
|
|
354 |
< |
network_stats_diff=network_stat_malloc(interfaces, &sizeof_net_stats_diff, network_stats_diff); |
355 |
< |
if(network_stats_diff==NULL){ |
354 |
> |
/* Get a new set of stats. */ |
355 |
> |
if (sg_get_network_io_stats(&new_count) == NULL) { |
356 |
|
return NULL; |
357 |
|
} |
358 |
|
|
359 |
< |
network_stats_ptr=network_stats; |
360 |
< |
network_stats_diff_ptr=network_stats_diff; |
359 |
> |
/* For each previous stat... */ |
360 |
> |
for (i = 0; i < diff_count; i++) { |
361 |
> |
dest = &diff[i]; |
362 |
|
|
363 |
< |
for(ifaces=0;ifaces<interfaces;ifaces++){ |
364 |
< |
if(network_stats_diff_ptr->interface_name!=NULL){ |
365 |
< |
free(network_stats_diff_ptr->interface_name); |
363 |
> |
/* ... find the corresponding new stat ... */ |
364 |
> |
for (j = 0; j < new_count; j++) { |
365 |
> |
/* Try the new stat in the same position first, |
366 |
> |
since that's most likely to be it. */ |
367 |
> |
src = &network_stats[(i + j) % new_count]; |
368 |
> |
if (strcmp(src->interface_name, dest->interface_name) == 0) { |
369 |
> |
break; |
370 |
> |
} |
371 |
|
} |
372 |
< |
network_stats_diff_ptr->interface_name=strdup(network_stats_ptr->interface_name); |
373 |
< |
network_stats_diff_ptr->tx=network_stats_ptr->tx; |
374 |
< |
network_stats_diff_ptr->rx=network_stats_ptr->rx; |
375 |
< |
network_stats_diff_ptr->systime=network_stats->systime; |
372 |
> |
if (j == new_count) { |
373 |
> |
/* No match found. */ |
374 |
> |
continue; |
375 |
> |
} |
376 |
|
|
377 |
< |
network_stats_ptr++; |
378 |
< |
network_stats_diff_ptr++; |
377 |
> |
/* ... and subtract the previous stat from it to get the |
378 |
> |
difference. */ |
379 |
> |
dest->rx = transfer_diff(src->rx, dest->rx); |
380 |
> |
dest->tx = transfer_diff(src->tx, dest->tx); |
381 |
> |
dest->ipackets = transfer_diff(src->ipackets, dest->ipackets); |
382 |
> |
dest->opackets = transfer_diff(src->opackets, dest->opackets); |
383 |
> |
dest->ierrors = transfer_diff(src->ierrors, dest->ierrors); |
384 |
> |
dest->oerrors = transfer_diff(src->oerrors, dest->oerrors); |
385 |
> |
dest->collisions = transfer_diff(src->collisions, dest->collisions); |
386 |
> |
dest->systime = src->systime - dest->systime; |
387 |
|
} |
281 |
– |
network_stats_ptr=get_network_stats(&ifaces); |
282 |
– |
network_stats_diff_ptr=network_stats_diff; |
388 |
|
|
389 |
< |
for(x=0;x<sizeof_net_stats_diff;x++){ |
389 |
> |
*entries = diff_count; |
390 |
> |
return diff; |
391 |
> |
} |
392 |
|
|
393 |
< |
if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){ |
394 |
< |
network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx); |
395 |
< |
network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx); |
396 |
< |
network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime; |
393 |
> |
int sg_network_io_compare_name(const void *va, const void *vb) { |
394 |
> |
const sg_network_io_stats *a = (const sg_network_io_stats *)va; |
395 |
> |
const sg_network_io_stats *b = (const sg_network_io_stats *)vb; |
396 |
> |
|
397 |
> |
return strcmp(a->interface_name, b->interface_name); |
398 |
> |
} |
399 |
> |
|
400 |
> |
/* NETWORK INTERFACE STATS */ |
401 |
> |
|
402 |
> |
static void network_iface_stat_init(sg_network_iface_stats *s) { |
403 |
> |
s->interface_name = NULL; |
404 |
> |
s->speed = 0; |
405 |
> |
s->dup = SG_IFACE_DUPLEX_UNKNOWN; |
406 |
> |
} |
407 |
> |
|
408 |
> |
static void network_iface_stat_destroy(sg_network_iface_stats *s) { |
409 |
> |
free(s->interface_name); |
410 |
> |
} |
411 |
> |
|
412 |
> |
sg_network_iface_stats *sg_get_network_iface_stats(int *entries){ |
413 |
> |
VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5, |
414 |
> |
network_iface_stat_init, network_iface_stat_destroy); |
415 |
> |
sg_network_iface_stats *network_iface_stat_ptr; |
416 |
> |
int ifaces = 0; |
417 |
> |
|
418 |
> |
#ifdef SOLARIS |
419 |
> |
kstat_ctl_t *kc; |
420 |
> |
kstat_t *ksp; |
421 |
> |
kstat_named_t *knp; |
422 |
> |
int sock; |
423 |
> |
#endif |
424 |
> |
#ifdef ALLBSD |
425 |
> |
struct ifaddrs *net, *net_ptr; |
426 |
> |
struct ifmediareq ifmed; |
427 |
> |
struct ifreq ifr; |
428 |
> |
int sock; |
429 |
> |
int x; |
430 |
> |
#endif |
431 |
> |
#ifdef LINUX |
432 |
> |
FILE *f; |
433 |
> |
/* Horrible big enough, but it should be easily big enough */ |
434 |
> |
char line[8096]; |
435 |
> |
int sock; |
436 |
> |
#endif |
437 |
> |
|
438 |
> |
#ifdef ALLBSD |
439 |
> |
if(getifaddrs(&net) != 0){ |
440 |
> |
sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL); |
441 |
> |
return NULL; |
442 |
> |
} |
443 |
> |
|
444 |
> |
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL; |
445 |
> |
|
446 |
> |
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
447 |
> |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
448 |
> |
|
449 |
> |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
450 |
> |
return NULL; |
451 |
> |
} |
452 |
> |
network_iface_stat_ptr = network_iface_stats + ifaces; |
453 |
> |
|
454 |
> |
memset(&ifr, 0, sizeof(ifr)); |
455 |
> |
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
456 |
> |
|
457 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){ |
458 |
> |
continue; |
459 |
> |
} |
460 |
> |
if((ifr.ifr_flags & IFF_UP) != 0){ |
461 |
> |
network_iface_stat_ptr->up = 1; |
462 |
|
}else{ |
463 |
< |
|
464 |
< |
network_stats_ptr=network_stats; |
465 |
< |
for(y=0;y<ifaces;y++){ |
466 |
< |
if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){ |
467 |
< |
network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx); |
468 |
< |
network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx); |
469 |
< |
network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime; |
463 |
> |
network_iface_stat_ptr->up = 0; |
464 |
> |
} |
465 |
> |
|
466 |
> |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
467 |
> |
net_ptr->ifa_name) < 0) { |
468 |
> |
return NULL; |
469 |
> |
} |
470 |
> |
|
471 |
> |
network_iface_stat_ptr->speed = 0; |
472 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
473 |
> |
ifaces++; |
474 |
> |
|
475 |
> |
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
476 |
> |
sg_strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
477 |
> |
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
478 |
> |
/* Not all interfaces support the media ioctls. */ |
479 |
> |
continue; |
480 |
> |
} |
481 |
> |
|
482 |
> |
/* We may need to change this if we start doing wireless devices too */ |
483 |
> |
if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){ |
484 |
> |
/* Not a ETHER device */ |
485 |
> |
continue; |
486 |
> |
} |
487 |
> |
|
488 |
> |
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
489 |
> |
x = ifmed.ifm_active & 0x0f; |
490 |
> |
switch(x){ |
491 |
> |
/* 10 Mbit connections. Speedy :) */ |
492 |
> |
case(IFM_10_T): |
493 |
> |
case(IFM_10_2): |
494 |
> |
case(IFM_10_5): |
495 |
> |
case(IFM_10_STP): |
496 |
> |
case(IFM_10_FL): |
497 |
> |
network_iface_stat_ptr->speed = 10; |
498 |
> |
break; |
499 |
> |
/* 100 Mbit conneections */ |
500 |
> |
case(IFM_100_TX): |
501 |
> |
case(IFM_100_FX): |
502 |
> |
case(IFM_100_T4): |
503 |
> |
case(IFM_100_VG): |
504 |
> |
case(IFM_100_T2): |
505 |
> |
network_iface_stat_ptr->speed = 100; |
506 |
> |
break; |
507 |
> |
/* 1000 Mbit connections */ |
508 |
> |
case(IFM_1000_SX): |
509 |
> |
case(IFM_1000_LX): |
510 |
> |
case(IFM_1000_CX): |
511 |
> |
#if defined(IFM_1000_TX) && !defined(OPENBSD) |
512 |
> |
case(IFM_1000_TX): /* FreeBSD 4 and others (but NOT OpenBSD)? */ |
513 |
> |
#endif |
514 |
> |
#ifdef IFM_1000_FX |
515 |
> |
case(IFM_1000_FX): /* FreeBSD 4 */ |
516 |
> |
#endif |
517 |
> |
#ifdef IFM_1000_T |
518 |
> |
case(IFM_1000_T): /* FreeBSD 5 */ |
519 |
> |
#endif |
520 |
> |
network_iface_stat_ptr->speed = 1000; |
521 |
> |
break; |
522 |
> |
/* We don't know what it is */ |
523 |
> |
default: |
524 |
> |
network_iface_stat_ptr->speed = 0; |
525 |
> |
break; |
526 |
> |
} |
527 |
> |
|
528 |
> |
if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){ |
529 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL; |
530 |
> |
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
531 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF; |
532 |
> |
}else{ |
533 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
534 |
> |
} |
535 |
> |
|
536 |
> |
} |
537 |
> |
freeifaddrs(net); |
538 |
> |
close(sock); |
539 |
> |
#endif |
540 |
> |
|
541 |
> |
#ifdef SOLARIS |
542 |
> |
if ((kc = kstat_open()) == NULL) { |
543 |
> |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
544 |
> |
return NULL; |
545 |
> |
} |
546 |
> |
|
547 |
> |
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
548 |
> |
sg_set_error_with_errno(SG_ERROR_SOCKET, NULL); |
549 |
> |
return NULL; |
550 |
> |
} |
551 |
> |
|
552 |
> |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
553 |
> |
if (!strcmp(ksp->ks_class, "net")) { |
554 |
> |
struct ifreq ifr; |
555 |
> |
|
556 |
> |
kstat_read(kc, ksp, NULL); |
557 |
> |
|
558 |
> |
strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name); |
559 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
560 |
> |
/* Not a network interface. */ |
561 |
> |
continue; |
562 |
> |
} |
563 |
> |
|
564 |
> |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
565 |
> |
return NULL; |
566 |
> |
} |
567 |
> |
network_iface_stat_ptr = network_iface_stats + ifaces; |
568 |
> |
ifaces++; |
569 |
> |
|
570 |
> |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
571 |
> |
ksp->ks_name) < 0) { |
572 |
> |
return NULL; |
573 |
> |
} |
574 |
> |
|
575 |
> |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
576 |
> |
network_iface_stat_ptr->up = 1; |
577 |
> |
} else { |
578 |
> |
network_iface_stat_ptr->up = 1; |
579 |
> |
} |
580 |
> |
|
581 |
> |
if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) { |
582 |
> |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000); |
583 |
> |
} else { |
584 |
> |
network_iface_stat_ptr->speed = 0; |
585 |
> |
} |
586 |
> |
|
587 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
588 |
> |
if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) { |
589 |
> |
switch (knp->value.ui32) { |
590 |
> |
case 1: |
591 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF; |
592 |
|
break; |
593 |
+ |
case 2: |
594 |
+ |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL; |
595 |
+ |
break; |
596 |
|
} |
597 |
+ |
} |
598 |
+ |
} |
599 |
+ |
} |
600 |
|
|
601 |
< |
network_stats_ptr++; |
602 |
< |
} |
601 |
> |
close(sock); |
602 |
> |
kstat_close(kc); |
603 |
> |
#endif |
604 |
> |
#ifdef LINUX |
605 |
> |
f = fopen("/proc/net/dev", "r"); |
606 |
> |
if(f == NULL){ |
607 |
> |
sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev"); |
608 |
> |
return NULL; |
609 |
> |
} |
610 |
> |
|
611 |
> |
/* Setup stuff so we can do the ioctl to get the info */ |
612 |
> |
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ |
613 |
> |
sg_set_error_with_errno(SG_ERROR_SOCKET, NULL); |
614 |
> |
return NULL; |
615 |
> |
} |
616 |
> |
|
617 |
> |
/* Ignore first 2 lines.. Just headings */ |
618 |
> |
if((fgets(line, sizeof(line), f)) == NULL) { |
619 |
> |
sg_set_error(SG_ERROR_PARSE, NULL); |
620 |
> |
return NULL; |
621 |
> |
} |
622 |
> |
if((fgets(line, sizeof(line), f)) == NULL) { |
623 |
> |
sg_set_error(SG_ERROR_PARSE, NULL); |
624 |
> |
return NULL; |
625 |
> |
} |
626 |
> |
|
627 |
> |
while((fgets(line, sizeof(line), f)) != NULL){ |
628 |
> |
char *name, *ptr; |
629 |
> |
struct ifreq ifr; |
630 |
> |
struct ethtool_cmd ethcmd; |
631 |
> |
int err; |
632 |
> |
|
633 |
> |
/* Get the interface name */ |
634 |
> |
ptr = strchr(line, ':'); |
635 |
> |
if (ptr == NULL) continue; |
636 |
> |
*ptr='\0'; |
637 |
> |
name = line; |
638 |
> |
while(isspace(*(name))){ |
639 |
> |
name++; |
640 |
|
} |
641 |
|
|
642 |
< |
network_stats_ptr++; |
643 |
< |
network_stats_diff_ptr++; |
642 |
> |
memset(&ifr, 0, sizeof ifr); |
643 |
> |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
644 |
> |
|
645 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
646 |
> |
continue; |
647 |
> |
} |
648 |
> |
|
649 |
> |
/* We have a good interface to add */ |
650 |
> |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
651 |
> |
return NULL; |
652 |
> |
} |
653 |
> |
network_iface_stat_ptr = network_iface_stats + ifaces; |
654 |
> |
|
655 |
> |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
656 |
> |
name) < 0) { |
657 |
> |
return NULL; |
658 |
> |
} |
659 |
> |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
660 |
> |
network_iface_stat_ptr->up = 1; |
661 |
> |
} else { |
662 |
> |
network_iface_stat_ptr->up = 0; |
663 |
> |
} |
664 |
> |
|
665 |
> |
memset(ðcmd, 0, sizeof ethcmd); |
666 |
> |
ethcmd.cmd = ETHTOOL_GSET; |
667 |
> |
ifr.ifr_data = (caddr_t) ðcmd; |
668 |
> |
|
669 |
> |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
670 |
> |
if (err == 0) { |
671 |
> |
network_iface_stat_ptr->speed = ethcmd.speed; |
672 |
> |
|
673 |
> |
switch (ethcmd.duplex) { |
674 |
> |
case 0x00: |
675 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL; |
676 |
> |
break; |
677 |
> |
case 0x01: |
678 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF; |
679 |
> |
break; |
680 |
> |
default: |
681 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
682 |
> |
} |
683 |
> |
} else { |
684 |
> |
/* Not all interfaces support the ethtool ioctl. */ |
685 |
> |
network_iface_stat_ptr->speed = 0; |
686 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
687 |
> |
} |
688 |
> |
|
689 |
> |
ifaces++; |
690 |
|
} |
691 |
+ |
close(sock); |
692 |
+ |
fclose(f); |
693 |
+ |
#endif |
694 |
+ |
*entries = ifaces; |
695 |
+ |
return network_iface_stats; |
696 |
+ |
} |
697 |
|
|
698 |
< |
*entries=sizeof_net_stats_diff; |
699 |
< |
return network_stats_diff; |
700 |
< |
} |
698 |
> |
int sg_network_iface_compare_name(const void *va, const void *vb) { |
699 |
> |
const sg_network_iface_stats *a = (const sg_network_iface_stats *)va; |
700 |
> |
const sg_network_iface_stats *b = (const sg_network_iface_stats *)vb; |
701 |
> |
|
702 |
> |
return strcmp(a->interface_name, b->interface_name); |
703 |
> |
} |
704 |
|
|