1 |
< |
/* |
1 |
> |
/* |
2 |
|
* i-scream central monitoring system |
3 |
< |
* http://www.i-scream.org.uk |
4 |
< |
* Copyright (C) 2000-2002 i-scream |
3 |
> |
* http://www.i-scream.org |
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 |
39 |
|
#include <regex.h> |
40 |
|
#include "tools.h" |
41 |
|
#endif |
42 |
+ |
#ifdef ALLBSD |
43 |
+ |
#include <sys/types.h> |
44 |
+ |
#include <sys/socket.h> |
45 |
+ |
#include <ifaddrs.h> |
46 |
+ |
#include <net/if.h> |
47 |
+ |
#endif |
48 |
|
|
49 |
|
static network_stat_t *network_stats=NULL; |
50 |
|
static int interfaces=0; |
104 |
|
regex_t regex; |
105 |
|
regmatch_t line_match[4]; |
106 |
|
#endif |
107 |
+ |
#ifdef ALLBSD |
108 |
+ |
struct ifaddrs *net, *net_ptr; |
109 |
+ |
struct if_data *net_data; |
110 |
+ |
#endif |
111 |
|
|
112 |
+ |
#ifdef ALLBSD |
113 |
+ |
if(getifaddrs(&net) != 0){ |
114 |
+ |
return NULL; |
115 |
+ |
} |
116 |
+ |
|
117 |
+ |
interfaces=0; |
118 |
+ |
|
119 |
+ |
for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){ |
120 |
+ |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
121 |
+ |
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
122 |
+ |
if(network_stats==NULL){ |
123 |
+ |
return NULL; |
124 |
+ |
} |
125 |
+ |
network_stat_ptr=network_stats+interfaces; |
126 |
+ |
|
127 |
+ |
if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name); |
128 |
+ |
network_stat_ptr->interface_name=strdup(net_ptr->ifa_name); |
129 |
+ |
if(network_stat_ptr->interface_name==NULL) return NULL; |
130 |
+ |
net_data=(struct if_data *)net_ptr->ifa_data; |
131 |
+ |
network_stat_ptr->rx=net_data->ifi_ibytes; |
132 |
+ |
network_stat_ptr->tx=net_data->ifi_obytes; |
133 |
+ |
network_stat_ptr->systime=time(NULL); |
134 |
+ |
interfaces++; |
135 |
+ |
} |
136 |
+ |
freeifaddrs(net); |
137 |
+ |
#endif |
138 |
+ |
|
139 |
|
#ifdef SOLARIS |
140 |
|
if ((kc = kstat_open()) == NULL) { |
141 |
|
return NULL; |
227 |
|
regfree(®ex); |
228 |
|
|
229 |
|
#endif |
230 |
+ |
|
231 |
+ |
#ifdef CYGWIN |
232 |
+ |
return NULL; |
233 |
+ |
#endif |
234 |
+ |
|
235 |
|
*entries=interfaces; |
236 |
|
|
237 |
|
return network_stats; |
238 |
|
} |
239 |
|
|
240 |
|
long long transfer_diff(long long new, long long old){ |
241 |
< |
#ifdef SOL7 |
242 |
< |
#define MAXVAL 4294967296 |
241 |
> |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) |
242 |
> |
#define MAXVAL 4294967296LL |
243 |
|
#else |
244 |
< |
#define MAXVAL 18446744073709551616 |
244 |
> |
#define MAXVAL 18446744073709551616LL |
245 |
|
#endif |
246 |
|
long long result; |
247 |
|
if(new>=old){ |
254 |
|
|
255 |
|
} |
256 |
|
|
257 |
< |
network_stat_t *get_network_stats_diff(int *entries){ |
258 |
< |
static network_stat_t *network_stats_diff=NULL; |
259 |
< |
static int sizeof_net_stats_diff=0; |
260 |
< |
network_stat_t *network_stats_ptr, *network_stats_diff_ptr; |
261 |
< |
int ifaces, x, y; |
257 |
> |
network_stat_t *get_network_stats_diff(int *entries) { |
258 |
> |
static network_stat_t *diff = NULL; |
259 |
> |
static int diff_count = 0; |
260 |
> |
network_stat_t *src, *dest; |
261 |
> |
int i, j, new_count; |
262 |
|
|
263 |
< |
if(network_stats==NULL){ |
264 |
< |
network_stats_ptr=get_network_stats(&ifaces); |
265 |
< |
*entries=ifaces; |
221 |
< |
return network_stats_ptr; |
263 |
> |
if (network_stats == NULL) { |
264 |
> |
/* No previous stats, so we can't calculate a difference. */ |
265 |
> |
return get_network_stats(entries); |
266 |
|
} |
267 |
|
|
268 |
< |
network_stats_diff=network_stat_malloc(interfaces, &sizeof_net_stats_diff, network_stats_diff); |
269 |
< |
if(network_stats_diff==NULL){ |
268 |
> |
/* Resize the results array to match the previous stats. */ |
269 |
> |
diff = network_stat_malloc(interfaces, &diff_count, diff); |
270 |
> |
if (diff == NULL) { |
271 |
|
return NULL; |
272 |
|
} |
273 |
|
|
274 |
< |
network_stats_ptr=network_stats; |
275 |
< |
network_stats_diff_ptr=network_stats_diff; |
274 |
> |
/* Copy the previous stats into the result. */ |
275 |
> |
for (i = 0; i < diff_count; i++) { |
276 |
> |
src = &network_stats[i]; |
277 |
> |
dest = &diff[i]; |
278 |
|
|
279 |
< |
for(ifaces=0;ifaces<interfaces;ifaces++){ |
280 |
< |
if(network_stats_diff_ptr->interface_name!=NULL){ |
234 |
< |
free(network_stats_diff_ptr->interface_name); |
279 |
> |
if (dest->interface_name != NULL) { |
280 |
> |
free(dest->interface_name); |
281 |
|
} |
282 |
< |
network_stats_diff_ptr->interface_name=strdup(network_stats_ptr->interface_name); |
283 |
< |
network_stats_diff_ptr->tx=network_stats_ptr->tx; |
284 |
< |
network_stats_diff_ptr->rx=network_stats_ptr->rx; |
285 |
< |
network_stats_diff_ptr->systime=network_stats->systime; |
282 |
> |
dest->interface_name = strdup(src->interface_name); |
283 |
> |
dest->rx = src->rx; |
284 |
> |
dest->tx = src->tx; |
285 |
> |
dest->systime = src->systime; |
286 |
> |
} |
287 |
|
|
288 |
< |
network_stats_ptr++; |
289 |
< |
network_stats_diff_ptr++; |
288 |
> |
/* Get a new set of stats. */ |
289 |
> |
if (get_network_stats(&new_count) == NULL) { |
290 |
> |
return NULL; |
291 |
|
} |
244 |
– |
network_stats_ptr=get_network_stats(&ifaces); |
245 |
– |
network_stats_diff_ptr=network_stats_diff; |
292 |
|
|
293 |
< |
for(x=0;x<sizeof_net_stats_diff;x++){ |
293 |
> |
/* For each previous stat... */ |
294 |
> |
for (i = 0; i < diff_count; i++) { |
295 |
> |
dest = &diff[i]; |
296 |
|
|
297 |
< |
if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){ |
298 |
< |
network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx); |
299 |
< |
network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx); |
300 |
< |
network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime; |
301 |
< |
}else{ |
302 |
< |
|
303 |
< |
network_stats_ptr=network_stats; |
304 |
< |
for(y=0;y<ifaces;y++){ |
305 |
< |
if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){ |
306 |
< |
network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx); |
307 |
< |
network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx); |
308 |
< |
network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime; |
309 |
< |
break; |
262 |
< |
} |
297 |
> |
/* ... find the corresponding new stat ... */ |
298 |
> |
for (j = 0; j < new_count; j++) { |
299 |
> |
/* Try the new stat in the same position first, |
300 |
> |
since that's most likely to be it. */ |
301 |
> |
src = &network_stats[(i + j) % new_count]; |
302 |
> |
if (strcmp(src->interface_name, dest->interface_name) == 0) { |
303 |
> |
break; |
304 |
> |
} |
305 |
> |
} |
306 |
> |
if (j == new_count) { |
307 |
> |
/* No match found. */ |
308 |
> |
continue; |
309 |
> |
} |
310 |
|
|
311 |
< |
network_stats_ptr++; |
312 |
< |
} |
311 |
> |
/* ... and subtract the previous stat from it to get the |
312 |
> |
difference. */ |
313 |
> |
dest->rx = transfer_diff(src->rx, dest->rx); |
314 |
> |
dest->tx = transfer_diff(src->tx, dest->tx); |
315 |
> |
dest->systime = src->systime - dest->systime; |
316 |
> |
} |
317 |
> |
|
318 |
> |
*entries = diff_count; |
319 |
> |
return diff; |
320 |
> |
} |
321 |
> |
/* NETWORK INTERFACE STATS */ |
322 |
> |
|
323 |
> |
void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){ |
324 |
> |
|
325 |
> |
for(net_stats+=start; start<end; start++){ |
326 |
> |
net_stats->interface_name=NULL; |
327 |
> |
net_stats->speed=0; |
328 |
> |
net_stats->dup=NO_DUPLEX; |
329 |
> |
net_stats++; |
330 |
> |
} |
331 |
> |
} |
332 |
> |
|
333 |
> |
network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){ |
334 |
> |
|
335 |
> |
if(net_stats==NULL){ |
336 |
> |
|
337 |
> |
if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){ |
338 |
> |
return NULL; |
339 |
|
} |
340 |
+ |
network_iface_stat_init(0, needed_entries, net_stats); |
341 |
+ |
*cur_entries=needed_entries; |
342 |
|
|
343 |
< |
network_stats_ptr++; |
269 |
< |
network_stats_diff_ptr++; |
343 |
> |
return net_stats; |
344 |
|
} |
345 |
|
|
346 |
< |
*entries=sizeof_net_stats_diff; |
347 |
< |
return network_stats_diff; |
348 |
< |
} |
346 |
> |
|
347 |
> |
if(*cur_entries<needed_entries){ |
348 |
> |
net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries)); |
349 |
> |
if(net_stats==NULL){ |
350 |
> |
return NULL; |
351 |
> |
} |
352 |
> |
network_iface_stat_init(*cur_entries, needed_entries, net_stats); |
353 |
> |
*cur_entries=needed_entries; |
354 |
> |
} |
355 |
> |
|
356 |
> |
return net_stats; |
357 |
> |
} |
358 |
> |
|
359 |
> |
network_iface_stat_t *get_network_iface_stats(int *entries){ |
360 |
> |
static network_iface_stat_t *network_iface_stats; |
361 |
> |
network_iface_stat_t *network_iface_stat_ptr; |
362 |
> |
static int sizeof_network_iface_stats=0; |
363 |
> |
int ifaces; |
364 |
> |
|
365 |
> |
#ifdef SOLARIS |
366 |
> |
kstat_ctl_t *kc; |
367 |
> |
kstat_t *ksp; |
368 |
> |
kstat_named_t *knp; |
369 |
> |
#endif |
370 |
> |
|
371 |
> |
#ifdef SOLARIS |
372 |
> |
if ((kc = kstat_open()) == NULL) { |
373 |
> |
return NULL; |
374 |
> |
} |
375 |
> |
|
376 |
> |
ifaces=0; |
377 |
> |
|
378 |
> |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
379 |
> |
if (!strcmp(ksp->ks_class, "net")) { |
380 |
> |
kstat_read(kc, ksp, NULL); |
381 |
> |
if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){ |
382 |
> |
/* Not a network interface, so skip to the next entry */ |
383 |
> |
continue; |
384 |
> |
} |
385 |
> |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
386 |
> |
if(network_iface_stats==NULL){ |
387 |
> |
return NULL; |
388 |
> |
} |
389 |
> |
network_iface_stat_ptr = network_iface_stats + interfaces; |
390 |
> |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000); |
391 |
> |
|
392 |
> |
if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){ |
393 |
> |
/* Not a network interface, so skip to the next entry */ |
394 |
> |
continue; |
395 |
> |
} |
396 |
> |
|
397 |
> |
if(knp->value.ui64 == 0){ |
398 |
> |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
399 |
> |
}else{ |
400 |
> |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
401 |
> |
} |
402 |
> |
|
403 |
> |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
404 |
> |
interfaces++; |
405 |
> |
} |
406 |
> |
} |
407 |
> |
kstat_close(kc); |
408 |
> |
|
409 |
> |
#endif |
410 |
> |
*entries = interfaces; |
411 |
> |
return network_iface_stats; |
412 |
> |
} |
413 |
|
|