ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/network_stats.c (file contents):
Revision 1.17 by pajs, Tue Aug 26 22:34:49 2003 UTC vs.
Revision 1.26 by pajs, Sun Jan 25 20:13:57 2004 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * i-scream central monitoring system
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
# Line 36 | Line 39
39   #include <regex.h>
40   #include "tools.h"
41   #endif
42 < #ifdef FREEBSD
42 > #ifdef ALLBSD
43   #include <sys/types.h>
44   #include <sys/socket.h>
45   #include <ifaddrs.h>
46   #include <net/if.h>
47 + #include <net/if_media.h>
48 + #include <sys/ioctl.h>
49   #endif
50  
51   static network_stat_t *network_stats=NULL;
# Line 101 | Line 106 | network_stat_t *get_network_stats(int *entries){
106          regex_t regex;
107          regmatch_t line_match[4];
108   #endif
109 < #ifdef FREEBSD
109 > #ifdef ALLBSD
110          struct ifaddrs *net, *net_ptr;
111          struct if_data *net_data;
112   #endif
113  
114 < #ifdef FREEBSD
114 > #ifdef ALLBSD
115          if(getifaddrs(&net) != 0){
116                  return NULL;
117          }
# Line 224 | Line 229 | network_stat_t *get_network_stats(int *entries){
229          regfree(&regex);
230  
231   #endif
232 +
233 + #ifdef CYGWIN
234 +        return NULL;
235 + #endif
236 +
237          *entries=interfaces;
238  
239          return network_stats;  
# Line 231 | Line 241 | network_stat_t *get_network_stats(int *entries){
241  
242   long long transfer_diff(long long new, long long old){
243   #if defined(SOL7) || defined(LINUX) || defined(FREEBSD)
244 < #define MAXVAL 4294967296
244 > #define MAXVAL 4294967296LL
245   #else
246 < #define MAXVAL 18446744073709551616
246 > #define MAXVAL 18446744073709551616LL
247   #endif
248          long long result;
249          if(new>=old){
# Line 246 | Line 256 | long long transfer_diff(long long new, long long old){
256  
257   }
258  
259 < network_stat_t *get_network_stats_diff(int *entries){
260 <        static network_stat_t *network_stats_diff=NULL;
261 <        static int sizeof_net_stats_diff=0;
262 <        network_stat_t *network_stats_ptr, *network_stats_diff_ptr;
263 <        int ifaces, x, y;
259 > network_stat_t *get_network_stats_diff(int *entries) {
260 >        static network_stat_t *diff = NULL;
261 >        static int diff_count = 0;
262 >        network_stat_t *src, *dest;
263 >        int i, j, new_count;
264  
265 <        if(network_stats==NULL){
266 <                network_stats_ptr=get_network_stats(&ifaces);
267 <                *entries=ifaces;
258 <                return network_stats_ptr;
265 >        if (network_stats == NULL) {
266 >                /* No previous stats, so we can't calculate a difference. */
267 >                return get_network_stats(entries);
268          }
269  
270 <        network_stats_diff=network_stat_malloc(interfaces, &sizeof_net_stats_diff, network_stats_diff);
271 <        if(network_stats_diff==NULL){
270 >        /* Resize the results array to match the previous stats. */
271 >        diff = network_stat_malloc(interfaces, &diff_count, diff);
272 >        if (diff == NULL) {
273                  return NULL;
274          }
275  
276 <        network_stats_ptr=network_stats;
277 <        network_stats_diff_ptr=network_stats_diff;
276 >        /* Copy the previous stats into the result. */
277 >        for (i = 0; i < diff_count; i++) {
278 >                src = &network_stats[i];
279 >                dest = &diff[i];
280  
281 <        for(ifaces=0;ifaces<interfaces;ifaces++){
282 <                if(network_stats_diff_ptr->interface_name!=NULL){
271 <                        free(network_stats_diff_ptr->interface_name);
281 >                if (dest->interface_name != NULL) {
282 >                        free(dest->interface_name);
283                  }
284 <                network_stats_diff_ptr->interface_name=strdup(network_stats_ptr->interface_name);
285 <                network_stats_diff_ptr->tx=network_stats_ptr->tx;
286 <                network_stats_diff_ptr->rx=network_stats_ptr->rx;
287 <                network_stats_diff_ptr->systime=network_stats->systime;
284 >                dest->interface_name = strdup(src->interface_name);
285 >                dest->rx = src->rx;
286 >                dest->tx = src->tx;
287 >                dest->systime = src->systime;
288 >        }
289  
290 <                network_stats_ptr++;
291 <                network_stats_diff_ptr++;
290 >        /* Get a new set of stats. */
291 >        if (get_network_stats(&new_count) == NULL) {
292 >                return NULL;
293          }
281        network_stats_ptr=get_network_stats(&ifaces);          
282        network_stats_diff_ptr=network_stats_diff;
294  
295 <        for(x=0;x<sizeof_net_stats_diff;x++){
295 >        /* For each previous stat... */
296 >        for (i = 0; i < diff_count; i++) {
297 >                dest = &diff[i];
298  
299 <                if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){
300 <                        network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx);
301 <                        network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx);
302 <                        network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime;
303 <                }else{
304 <                        
305 <                        network_stats_ptr=network_stats;
306 <                        for(y=0;y<ifaces;y++){
307 <                                if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){
308 <                                        network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx);
309 <                                        network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx);  
310 <                                        network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime;
311 <                                        break;
299 <                                }
299 >                /* ... find the corresponding new stat ... */
300 >                for (j = 0; j < new_count; j++) {
301 >                        /* Try the new stat in the same position first,
302 >                           since that's most likely to be it. */
303 >                        src = &network_stats[(i + j) % new_count];
304 >                        if (strcmp(src->interface_name, dest->interface_name) == 0) {
305 >                                break;
306 >                        }
307 >                }
308 >                if (j == new_count) {
309 >                        /* No match found. */
310 >                        continue;
311 >                }
312  
313 <                                network_stats_ptr++;
314 <                        }      
313 >                /* ... and subtract the previous stat from it to get the
314 >                   difference. */
315 >                dest->rx = transfer_diff(src->rx, dest->rx);
316 >                dest->tx = transfer_diff(src->tx, dest->tx);
317 >                dest->systime = src->systime - dest->systime;
318 >        }
319 >
320 >        *entries = diff_count;
321 >        return diff;
322 > }
323 > /* NETWORK INTERFACE STATS */
324 >
325 > void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
326 >
327 >        for(net_stats+=start; start<end; start++){
328 >                net_stats->interface_name=NULL;
329 >                net_stats->speed=0;
330 >                net_stats->dup=NO_DUPLEX;
331 >                net_stats++;
332 >        }
333 > }
334 >
335 > network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
336 >
337 >        if(net_stats==NULL){
338 >
339 >                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
340 >                        return NULL;
341                  }
342 +                network_iface_stat_init(0, needed_entries, net_stats);
343 +                *cur_entries=needed_entries;
344  
345 <                network_stats_ptr++;
306 <                network_stats_diff_ptr++;
345 >                return net_stats;
346          }
347  
348 <        *entries=sizeof_net_stats_diff;
349 <        return network_stats_diff;
350 < }      
348 >
349 >        if(*cur_entries<needed_entries){
350 >                net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
351 >                if(net_stats==NULL){
352 >                        return NULL;
353 >                }
354 >                network_iface_stat_init(*cur_entries, needed_entries, net_stats);
355 >                *cur_entries=needed_entries;
356 >        }
357 >
358 >        return net_stats;
359 > }
360 >
361 > network_iface_stat_t *get_network_iface_stats(int *entries){
362 >        static network_iface_stat_t *network_iface_stats;
363 >        network_iface_stat_t *network_iface_stat_ptr;
364 >        static int sizeof_network_iface_stats=0;        
365 >        static int ifaces;
366 >
367 > #ifdef SOLARIS
368 >        kstat_ctl_t *kc;
369 >        kstat_t *ksp;
370 >        kstat_named_t *knp;
371 > #endif
372 > #ifdef ALLBSD
373 >        struct ifaddrs *net, *net_ptr;
374 >        struct ifmediareq ifmed;
375 >        int s;
376 >        int x;
377 > #endif
378 >
379 >        ifaces = 0;
380 > #ifdef ALLBSD
381 >        if(getifaddrs(&net) != 0){
382 >                return NULL;
383 >        }
384 >
385 >        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL;
386 >
387 >        for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
388 >                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
389 >                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
390 >                if(network_iface_stats==NULL){
391 >                        return NULL;
392 >                }
393 >                network_iface_stat_ptr = network_iface_stats + ifaces;
394 >
395 >                memset(&ifmed, 0, sizeof(struct ifmediareq));
396 >                strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
397 >                if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
398 >                        continue;
399 >                }
400 >
401 >                /* We may need to change this if we start doing wireless devices too */
402 >                if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
403 >                        /* Not a ETHER device */
404 >                        continue;
405 >                }
406 >
407 >                if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
408 >                network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
409 >                if(network_iface_stat_ptr->interface_name == NULL) return NULL;
410 >
411 >                /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
412 >                x = ifmed.ifm_active & 0x0f;    
413 >                switch(x){
414 >                        /* 10 Mbit connections. Speedy :) */
415 >                        case(IFM_10_T):
416 >                        case(IFM_10_2):
417 >                        case(IFM_10_5):
418 >                        case(IFM_10_STP):
419 >                        case(IFM_10_FL):
420 >                                network_iface_stat_ptr->speed = 10;
421 >                                break;
422 >                        /* 100 Mbit conneections */
423 >                        case(IFM_100_TX):
424 >                        case(IFM_100_FX):
425 >                        case(IFM_100_T4):
426 >                        case(IFM_100_VG):
427 >                        case(IFM_100_T2):
428 >                                network_iface_stat_ptr->speed = 100;
429 >                                break;
430 >                        /* 1000 Mbit connections */
431 >                        case(IFM_1000_SX):
432 >                        case(IFM_1000_LX):
433 >                        case(IFM_1000_CX):
434 >                        case(IFM_1000_TX):
435 >                                network_iface_stat_ptr->speed = 1000;
436 >                                break;
437 >                        /* We don't know what it is */
438 >                        default:
439 >                                network_iface_stat_ptr->speed = 0;
440 >                                break;
441 >                }
442 >
443 >                if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
444 >                        network_iface_stat_ptr->dup = FULL_DUPLEX;
445 >                }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
446 >                        network_iface_stat_ptr->dup = HALF_DUPLEX;
447 >                }else{
448 >                        network_iface_stat_ptr->dup = NO_DUPLEX;
449 >                }
450 >                ifaces++;
451 >        }      
452 >        freeifaddrs(net);
453 > #endif
454 >
455 > #ifdef SOLARIS
456 >        if ((kc = kstat_open()) == NULL) {
457 >                return NULL;
458 >        }
459 >
460 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
461 >                if (!strcmp(ksp->ks_class, "net")) {
462 >                        kstat_read(kc, ksp, NULL);
463 >                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
464 >                                /* Not a network interface, so skip to the next entry */
465 >                                continue;
466 >                        }
467 >                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
468 >                        if(network_iface_stats==NULL){
469 >                                return NULL;
470 >                        }
471 >                        network_iface_stat_ptr = network_iface_stats + ifaces;
472 >                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
473 >
474 >                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
475 >                                /* Not a network interface, so skip to the next entry */
476 >                                continue;
477 >                        }
478 >
479 >                        if(knp->value.ui64 == 0){
480 >                                network_iface_stat_ptr->dup = FULL_DUPLEX;
481 >                        }else{
482 >                                network_iface_stat_ptr->dup = HALF_DUPLEX;
483 >                        }
484 >
485 >                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
486 >                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
487 >                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
488 >                        ifaces++;
489 >                }
490 >        }
491 >        kstat_close(kc);
492 >        
493 > #endif  
494 >        *entries = ifaces;
495 >        return network_iface_stats;
496 > }
497  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines