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.12 by pajs, Mon Mar 31 11:30:54 2003 UTC vs.
Revision 1.31 by ats, Fri Feb 13 15:12:26 2004 UTC

# Line 1 | Line 1
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
# Line 34 | Line 37
37   #include <stdio.h>
38   #include <sys/types.h>
39   #include <regex.h>
40 + #include <sys/ioctl.h>
41 + #include <sys/socket.h>
42 + #include <net/if.h>
43 + #include <ctype.h>
44   #include "tools.h"
45 + /* Stuff which could be defined by defining KERNEL, but
46 + * that would be a bad idea, so we'll just declare it here
47 + */
48 + typedef __uint8_t u8;
49 + typedef __uint16_t u16;
50 + typedef __uint32_t u32;
51 + #include <linux/ethtool.h>
52 + #include <linux/sockios.h>
53   #endif
54 + #ifdef ALLBSD
55 + #include <sys/types.h>
56 + #include <sys/socket.h>
57 + #include <ifaddrs.h>
58 + #include <net/if.h>
59 + #include <net/if_media.h>
60 + #include <sys/ioctl.h>
61 + #endif
62  
63   static network_stat_t *network_stats=NULL;
64   static int interfaces=0;
# Line 95 | Line 118 | network_stat_t *get_network_stats(int *entries){
118          regex_t regex;
119          regmatch_t line_match[4];
120   #endif
121 + #ifdef ALLBSD
122 +        struct ifaddrs *net, *net_ptr;
123 +        struct if_data *net_data;
124 + #endif
125  
126 + #ifdef ALLBSD
127 +        if(getifaddrs(&net) != 0){
128 +                return NULL;
129 +        }
130 +
131 +        interfaces=0;
132 +        
133 +        for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){
134 +                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
135 +                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
136 +                if(network_stats==NULL){
137 +                        return NULL;
138 +                }
139 +                network_stat_ptr=network_stats+interfaces;
140 +                
141 +                if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name);
142 +                network_stat_ptr->interface_name=strdup(net_ptr->ifa_name);
143 +                if(network_stat_ptr->interface_name==NULL) return NULL;
144 +                net_data=(struct if_data *)net_ptr->ifa_data;
145 +                network_stat_ptr->rx=net_data->ifi_ibytes;
146 +                network_stat_ptr->tx=net_data->ifi_obytes;                      
147 +                network_stat_ptr->systime=time(NULL);
148 +                interfaces++;
149 +        }
150 +        freeifaddrs(net);      
151 + #endif
152 +
153   #ifdef SOLARIS
154          if ((kc = kstat_open()) == NULL) {
155                  return NULL;
# Line 184 | Line 238 | network_stat_t *get_network_stats(int *entries){
238                  interfaces++;
239          }
240          fclose(f);
241 +        regfree(&regex);
242  
243   #endif
244 +
245 + #ifdef CYGWIN
246 +        return NULL;
247 + #endif
248 +
249          *entries=interfaces;
250  
251          return network_stats;  
252   }
253  
254   long long transfer_diff(long long new, long long old){
255 < #ifdef SOL7
256 < #define MAXVAL 4294967296
255 > #if defined(SOL7) || defined(LINUX) || defined(FREEBSD)
256 > #define MAXVAL 4294967296LL
257   #else
258 < #define MAXVAL 18446744073709551616
258 > #define MAXVAL 18446744073709551616LL
259   #endif
260          long long result;
261          if(new>=old){
# Line 208 | Line 268 | long long transfer_diff(long long new, long long old){
268  
269   }
270  
271 < network_stat_t *get_network_stats_diff(int *entries){
272 <        static network_stat_t *network_stats_diff=NULL;
273 <        static int sizeof_net_stats_diff=0;
274 <        network_stat_t *network_stats_ptr, *network_stats_diff_ptr;
275 <        int ifaces, x, y;
271 > network_stat_t *get_network_stats_diff(int *entries) {
272 >        static network_stat_t *diff = NULL;
273 >        static int diff_count = 0;
274 >        network_stat_t *src, *dest;
275 >        int i, j, new_count;
276  
277 <        if(network_stats==NULL){
278 <                network_stats_ptr=get_network_stats(&ifaces);
279 <                *entries=ifaces;
220 <                return network_stats_ptr;
277 >        if (network_stats == NULL) {
278 >                /* No previous stats, so we can't calculate a difference. */
279 >                return get_network_stats(entries);
280          }
281  
282 <        network_stats_diff=network_stat_malloc(interfaces, &sizeof_net_stats_diff, network_stats_diff);
283 <        if(network_stats_diff==NULL){
282 >        /* Resize the results array to match the previous stats. */
283 >        diff = network_stat_malloc(interfaces, &diff_count, diff);
284 >        if (diff == NULL) {
285                  return NULL;
286          }
287  
288 <        network_stats_ptr=network_stats;
289 <        network_stats_diff_ptr=network_stats_diff;
288 >        /* Copy the previous stats into the result. */
289 >        for (i = 0; i < diff_count; i++) {
290 >                src = &network_stats[i];
291 >                dest = &diff[i];
292  
293 <        for(ifaces=0;ifaces<interfaces;ifaces++){
294 <                if(network_stats_diff_ptr->interface_name!=NULL){
233 <                        free(network_stats_diff_ptr->interface_name);
293 >                if (dest->interface_name != NULL) {
294 >                        free(dest->interface_name);
295                  }
296 <                network_stats_diff_ptr->interface_name=strdup(network_stats_ptr->interface_name);
297 <                network_stats_diff_ptr->tx=network_stats_ptr->tx;
298 <                network_stats_diff_ptr->rx=network_stats_ptr->rx;
299 <                network_stats_diff_ptr->systime=network_stats->systime;
296 >                dest->interface_name = strdup(src->interface_name);
297 >                dest->rx = src->rx;
298 >                dest->tx = src->tx;
299 >                dest->systime = src->systime;
300 >        }
301  
302 <                network_stats_ptr++;
303 <                network_stats_diff_ptr++;
302 >        /* Get a new set of stats. */
303 >        if (get_network_stats(&new_count) == NULL) {
304 >                return NULL;
305          }
243        network_stats_ptr=get_network_stats(&ifaces);          
244        network_stats_diff_ptr=network_stats_diff;
306  
307 <        for(x=0;x<sizeof_net_stats_diff;x++){
307 >        /* For each previous stat... */
308 >        for (i = 0; i < diff_count; i++) {
309 >                dest = &diff[i];
310  
311 <                if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){
312 <                        network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx);
313 <                        network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx);
314 <                        network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime;
311 >                /* ... find the corresponding new stat ... */
312 >                for (j = 0; j < new_count; j++) {
313 >                        /* Try the new stat in the same position first,
314 >                           since that's most likely to be it. */
315 >                        src = &network_stats[(i + j) % new_count];
316 >                        if (strcmp(src->interface_name, dest->interface_name) == 0) {
317 >                                break;
318 >                        }
319 >                }
320 >                if (j == new_count) {
321 >                        /* No match found. */
322 >                        continue;
323 >                }
324 >
325 >                /* ... and subtract the previous stat from it to get the
326 >                   difference. */
327 >                dest->rx = transfer_diff(src->rx, dest->rx);
328 >                dest->tx = transfer_diff(src->tx, dest->tx);
329 >                dest->systime = src->systime - dest->systime;
330 >        }
331 >
332 >        *entries = diff_count;
333 >        return diff;
334 > }
335 > /* NETWORK INTERFACE STATS */
336 >
337 > void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
338 >
339 >        for(net_stats+=start; start<end; start++){
340 >                net_stats->interface_name=NULL;
341 >                net_stats->speed=0;
342 >                net_stats->dup=UNKNOWN_DUPLEX;
343 >                net_stats++;
344 >        }
345 > }
346 >
347 > network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
348 >
349 >        if(net_stats==NULL){
350 >
351 >                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
352 >                        return NULL;
353 >                }
354 >                network_iface_stat_init(0, needed_entries, net_stats);
355 >                *cur_entries=needed_entries;
356 >
357 >                return net_stats;
358 >        }
359 >
360 >
361 >        if(*cur_entries<needed_entries){
362 >                net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
363 >                if(net_stats==NULL){
364 >                        return NULL;
365 >                }
366 >                network_iface_stat_init(*cur_entries, needed_entries, net_stats);
367 >                *cur_entries=needed_entries;
368 >        }
369 >
370 >        return net_stats;
371 > }
372 >
373 > network_iface_stat_t *get_network_iface_stats(int *entries){
374 >        static network_iface_stat_t *network_iface_stats;
375 >        network_iface_stat_t *network_iface_stat_ptr;
376 >        static int sizeof_network_iface_stats=0;        
377 >        static int ifaces;
378 >
379 > #ifdef SOLARIS
380 >        kstat_ctl_t *kc;
381 >        kstat_t *ksp;
382 >        kstat_named_t *knp;
383 > #endif
384 > #ifdef ALLBSD
385 >        struct ifaddrs *net, *net_ptr;
386 >        struct ifmediareq ifmed;
387 >        int s;
388 >        int x;
389 > #endif
390 > #ifdef LINUX
391 >        FILE *f;
392 >        /* Horrible big enough, but it should be easily big enough */
393 >        char line[8096];
394 >        void *eth_tool_cmd_buf;
395 >        int buf_size;
396 >        int sock;
397 > #endif
398 >        ifaces = 0;
399 > #ifdef ALLBSD
400 >        if(getifaddrs(&net) != 0){
401 >                return NULL;
402 >        }
403 >
404 >        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL;
405 >
406 >        for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
407 >                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
408 >                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
409 >                if(network_iface_stats==NULL){
410 >                        return NULL;
411 >                }
412 >                network_iface_stat_ptr = network_iface_stats + ifaces;
413 >
414 >                memset(&ifmed, 0, sizeof(struct ifmediareq));
415 >                strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
416 >                if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
417 >                        continue;
418 >                }
419 >
420 >                /* We may need to change this if we start doing wireless devices too */
421 >                if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
422 >                        /* Not a ETHER device */
423 >                        continue;
424 >                }
425 >
426 >                if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
427 >                network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
428 >                if(network_iface_stat_ptr->interface_name == NULL) return NULL;
429 >
430 >                /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
431 >                x = ifmed.ifm_active & 0x0f;    
432 >                switch(x){
433 >                        /* 10 Mbit connections. Speedy :) */
434 >                        case(IFM_10_T):
435 >                        case(IFM_10_2):
436 >                        case(IFM_10_5):
437 >                        case(IFM_10_STP):
438 >                        case(IFM_10_FL):
439 >                                network_iface_stat_ptr->speed = 10;
440 >                                break;
441 >                        /* 100 Mbit conneections */
442 >                        case(IFM_100_TX):
443 >                        case(IFM_100_FX):
444 >                        case(IFM_100_T4):
445 >                        case(IFM_100_VG):
446 >                        case(IFM_100_T2):
447 >                                network_iface_stat_ptr->speed = 100;
448 >                                break;
449 >                        /* 1000 Mbit connections */
450 >                        case(IFM_1000_SX):
451 >                        case(IFM_1000_LX):
452 >                        case(IFM_1000_CX):
453 > #if defined(FREEBSD) && !defined(FREEBSD5)
454 >                        case(IFM_1000_TX):
455 >                        case(IFM_1000_FX):
456 > #else
457 >                        case(IFM_1000_T):
458 > #endif
459 >                                network_iface_stat_ptr->speed = 1000;
460 >                                break;
461 >                        /* We don't know what it is */
462 >                        default:
463 >                                network_iface_stat_ptr->speed = 0;
464 >                                break;
465 >                }
466 >
467 >                if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
468 >                        network_iface_stat_ptr->dup = FULL_DUPLEX;
469 >                }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
470 >                        network_iface_stat_ptr->dup = HALF_DUPLEX;
471                  }else{
472 <                        
473 <                        network_stats_ptr=network_stats;
474 <                        for(y=0;y<ifaces;y++){
475 <                                if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){
476 <                                        network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx);
477 <                                        network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx);  
478 <                                        network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime;
260 <                                        break;
261 <                                }
472 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
473 >                }
474 >                ifaces++;
475 >        }      
476 >        freeifaddrs(net);
477 >        close(s);
478 > #endif
479  
480 <                                network_stats_ptr++;
481 <                        }      
480 > #ifdef SOLARIS
481 >        if ((kc = kstat_open()) == NULL) {
482 >                return NULL;
483 >        }
484 >
485 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
486 >                if (!strcmp(ksp->ks_class, "net")) {
487 >                        kstat_read(kc, ksp, NULL);
488 >                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
489 >                                /* Not a network interface, so skip to the next entry */
490 >                                continue;
491 >                        }
492 >                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
493 >                        if(network_iface_stats==NULL){
494 >                                return NULL;
495 >                        }
496 >                        network_iface_stat_ptr = network_iface_stats + ifaces;
497 >                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
498 >
499 >                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
500 >                                /* Not a network interface, so skip to the next entry */
501 >                                continue;
502 >                        }
503 >
504 >                        if(knp->value.ui64 == 0){
505 >                                network_iface_stat_ptr->dup = FULL_DUPLEX;
506 >                        }else{
507 >                                network_iface_stat_ptr->dup = HALF_DUPLEX;
508 >                        }
509 >
510 >                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
511 >                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
512 >                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
513 >                        ifaces++;
514                  }
515 +        }
516 +        kstat_close(kc);
517 +        
518 + #endif  
519 + #ifdef LINUX
520  
521 <                network_stats_ptr++;
522 <                network_stats_diff_ptr++;
521 >        f = fopen("/proc/net/dev", "r");
522 >        if(f == NULL){
523 >                return NULL;
524 >        }
525 >
526 >        /* Setup stuff so we can do the ioctl to get the info */
527 >        if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
528 >                return NULL;
529          }
530  
531 <        *entries=sizeof_net_stats_diff;
532 <        return network_stats_diff;
533 < }      
531 >        buf_size = sizeof(struct ethtool_cmd);
532 >        eth_tool_cmd_buf = malloc(buf_size);
533 >        if(eth_tool_cmd_buf == NULL) return NULL;
534 >
535 >        /* Ignore first 2 lines.. Just headings */
536 >        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
537 >        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
538 >
539 >        while((fgets(line, sizeof(line), f)) != NULL){
540 >                char *name, *ptr;
541 >                struct ifreq ifr;
542 >                struct ethtool_cmd *ethcmd;
543 >                int err;
544 >
545 >                /* Get the interface name */
546 >                ptr = strchr(line, ':');
547 >                if (ptr == NULL) continue;
548 >                *ptr='\0';
549 >                name = line;
550 >                while(isspace(*(name))){
551 >                        name++;
552 >                }
553 >
554 >                memset(&ifr, 0, sizeof(ifr));
555 >                memset(eth_tool_cmd_buf, 0, buf_size);
556 >                ifr.ifr_data = (caddr_t) eth_tool_cmd_buf;
557 >                strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
558 >
559 >                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
560 >                ethcmd->cmd = ETHTOOL_GSET;
561 >
562 >                err = ioctl(sock, SIOCETHTOOL, &ifr);
563 >                if(err < 0){
564 >                        /* This could fail if the interface doesn't support the command. Carry
565 >                         * on to the next :)
566 >                         */
567 >                        continue;
568 >                }
569 >
570 >                /* We have a good interface to add */
571 >                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
572 >                if(network_iface_stats==NULL){
573 >                        return NULL;
574 >                }
575 >                network_iface_stat_ptr = network_iface_stats + ifaces;
576 >                network_iface_stat_ptr->interface_name = strdup(name);
577 >                network_iface_stat_ptr->speed = ethcmd->speed;
578 >                network_iface_stat_ptr->dup = UNKNOWN_DUPEX;
579 >                if(ethcmd->duplex == 0x00){
580 >                        network_iface_stat_ptr->dup = FULL_DUPLEX;
581 >                }
582 >                if(ethcmd->duplex == 0x01){
583 >                        network_iface_stat_ptr->dup = HALF_DUPLEX;
584 >                }
585 >                ifaces++;
586 >        }
587 >        close(sock);
588 >        free(eth_tool_cmd_buf);
589 > #endif
590 >        *entries = ifaces;
591 >        return network_iface_stats;
592 > }
593  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines