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.64 by tdb, Tue Apr 6 14:52:58 2004 UTC vs.
Revision 1.70 by ats, Sun Nov 7 12:30:54 2004 UTC

# Line 45 | Line 45
45   #ifdef LINUX
46   #include <stdio.h>
47   #include <sys/types.h>
48 #include <regex.h>
48   #include <sys/ioctl.h>
49   #include <sys/socket.h>
50   #include <net/if.h>
# Line 87 | Line 86 | static void network_stat_destroy(sg_network_io_stats *
86   }
87  
88   VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5,
89 <                      network_stat_init, network_stat_destroy);
89 >                      network_stat_init, network_stat_destroy);
90  
91   sg_network_io_stats *sg_get_network_io_stats(int *entries){
92          int interfaces;
93          sg_network_io_stats *network_stat_ptr;
94  
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  
# Line 113 | Line 112 | sg_network_io_stats *sg_get_network_io_stats(int *entr
112  
113   #ifdef ALLBSD
114          if(getifaddrs(&net) != 0){
115 +                sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
116                  return NULL;
117          }
118  
# Line 127 | Line 127 | sg_network_io_stats *sg_get_network_io_stats(int *entr
127                  network_stat_ptr=network_stats+interfaces;
128                  
129                  if (sg_update_string(&network_stat_ptr->interface_name,
130 <                                     net_ptr->ifa_name) < 0) {
130 >                                     net_ptr->ifa_name) < 0) {
131                          return NULL;
132                  }
133                  net_data=(struct if_data *)net_ptr->ifa_data;
# Line 145 | Line 145 | sg_network_io_stats *sg_get_network_io_stats(int *entr
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 LRX "rbytes"
# Line 227 | Line 228 | sg_network_io_stats *sg_get_network_io_stats(int *entr
228  
229                          /* Read interface name */
230                          if (sg_update_string(&network_stat_ptr->interface_name,
231 <                                             ksp->ks_name) < 0) {
231 >                                             ksp->ks_name) < 0) {
232                                  return NULL;
233                          }
234  
# Line 243 | Line 244 | sg_network_io_stats *sg_get_network_io_stats(int *entr
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 :) */
# Line 251 | Line 253 | sg_network_io_stats *sg_get_network_io_stats(int *entr
253  
254  
255          if((regcomp(&regex, "^ *([^:]+): *([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  
# Line 264 | Line 267 | sg_network_io_stats *sg_get_network_io_stats(int *entr
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);
# Line 288 | Line 291 | sg_network_io_stats *sg_get_network_io_stats(int *entr
291   #endif
292  
293   #ifdef CYGWIN
294 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
295          return NULL;
296   #endif
297 + #ifdef HPUX
298 +        sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
299 +        return NULL;
300 + #endif
301  
302          *entries=interfaces;
303  
# Line 313 | Line 321 | static long long transfer_diff(long long new, long lon
321  
322   sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) {
323          VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1,
324 <                              network_stat_init, network_stat_destroy);
324 >                              network_stat_init, network_stat_destroy);
325          sg_network_io_stats *src = NULL, *dest;
326          int i, j, diff_count, new_count;
327  
# Line 334 | Line 342 | sg_network_io_stats *sg_get_network_io_stats_diff(int
342                  dest = &diff[i];
343  
344                  if (sg_update_string(&dest->interface_name,
345 <                                     src->interface_name) < 0) {
345 >                                     src->interface_name) < 0) {
346                          return NULL;
347                  }
348                  dest->rx = src->rx;
# Line 386 | Line 394 | sg_network_io_stats *sg_get_network_io_stats_diff(int
394          return diff;
395   }
396  
397 + int sg_network_io_compare_name(const void *va, const void *vb) {
398 +        const sg_network_io_stats *a = (const sg_network_io_stats *)va;
399 +        const sg_network_io_stats *b = (const sg_network_io_stats *)vb;
400 +
401 +        return strcmp(a->interface_name, b->interface_name);
402 + }
403 +
404   /* NETWORK INTERFACE STATS */
405  
406   static void network_iface_stat_init(sg_network_iface_stats *s) {
# Line 400 | Line 415 | static void network_iface_stat_destroy(sg_network_ifac
415  
416   sg_network_iface_stats *sg_get_network_iface_stats(int *entries){
417          VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5,
418 <                              network_iface_stat_init, network_iface_stat_destroy);
418 >                              network_iface_stat_init, network_iface_stat_destroy);
419          sg_network_iface_stats *network_iface_stat_ptr;
420          int ifaces = 0;
421  
422   #ifdef SOLARIS
423 <        kstat_ctl_t *kc;
424 <        kstat_t *ksp;
423 >        kstat_ctl_t *kc;
424 >        kstat_t *ksp;
425          kstat_named_t *knp;
426          int sock;
427   #endif
428   #ifdef ALLBSD
429 <        struct ifaddrs *net, *net_ptr;
429 >        struct ifaddrs *net, *net_ptr;
430          struct ifmediareq ifmed;
431          struct ifreq ifr;
432          int sock;
433          int x;
434   #endif
435   #ifdef LINUX
436 <        FILE *f;
437 <        /* Horrible big enough, but it should be easily big enough */
438 <        char line[8096];
436 >        FILE *f;
437 >        /* Horrible big enough, but it should be easily big enough */
438 >        char line[8096];
439          int sock;
440   #endif
441  
442   #ifdef ALLBSD
443 <        if(getifaddrs(&net) != 0){
444 <                return NULL;
445 <        }
443 >        if(getifaddrs(&net) != 0){
444 >                sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
445 >                return NULL;
446 >        }
447  
448          if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
449  
450          for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
451 <                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
451 >                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
452  
453                  if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
454 <                        return NULL;
455 <                }
456 <                network_iface_stat_ptr = network_iface_stats + ifaces;
454 >                        return NULL;
455 >                }
456 >                network_iface_stat_ptr = network_iface_stats + ifaces;
457  
458                  memset(&ifr, 0, sizeof(ifr));
459                  strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
# Line 452 | Line 468 | sg_network_iface_stats *sg_get_network_iface_stats(int
468                  }
469  
470                  if (sg_update_string(&network_iface_stat_ptr->interface_name,
471 <                                     net_ptr->ifa_name) < 0) {
471 >                                     net_ptr->ifa_name) < 0) {
472                          return NULL;
473                  }
474  
# Line 528 | Line 544 | sg_network_iface_stats *sg_get_network_iface_stats(int
544  
545   #ifdef SOLARIS
546          if ((kc = kstat_open()) == NULL) {
547 +                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
548                  return NULL;
549          }
550  
551          if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
552 +                sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
553                  return NULL;
554          }
555  
# Line 554 | Line 572 | sg_network_iface_stats *sg_get_network_iface_stats(int
572                          ifaces++;
573  
574                          if (sg_update_string(&network_iface_stat_ptr->interface_name,
575 <                                             ksp->ks_name) < 0) {
575 >                                             ksp->ks_name) < 0) {
576                                  return NULL;
577                          }
578  
# Line 589 | Line 607 | sg_network_iface_stats *sg_get_network_iface_stats(int
607   #endif  
608   #ifdef LINUX
609          f = fopen("/proc/net/dev", "r");
610 <        if(f == NULL){
611 <                return NULL;
612 <        }
610 >        if(f == NULL){
611 >                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
612 >                return NULL;
613 >        }
614  
615          /* Setup stuff so we can do the ioctl to get the info */
616          if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
617 +                sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
618                  return NULL;
619          }
620  
621          /* Ignore first 2 lines.. Just headings */
622 <        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
623 <        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
622 >        if((fgets(line, sizeof(line), f)) == NULL) {
623 >                sg_set_error(SG_ERROR_PARSE, NULL);
624 >                return NULL;
625 >        }
626 >        if((fgets(line, sizeof(line), f)) == NULL) {
627 >                sg_set_error(SG_ERROR_PARSE, NULL);
628 >                return NULL;
629 >        }
630  
631 <        while((fgets(line, sizeof(line), f)) != NULL){
632 <                char *name, *ptr;
633 <                struct ifreq ifr;
634 <                struct ethtool_cmd ethcmd;
635 <                int err;
631 >        while((fgets(line, sizeof(line), f)) != NULL){
632 >                char *name, *ptr;
633 >                struct ifreq ifr;
634 >                struct ethtool_cmd ethcmd;
635 >                int err;
636  
637                  /* Get the interface name */
638 <                ptr = strchr(line, ':');
639 <                if (ptr == NULL) continue;
640 <                *ptr='\0';
641 <                name = line;
642 <                while(isspace(*(name))){
643 <                        name++;
644 <                }
638 >                ptr = strchr(line, ':');
639 >                if (ptr == NULL) continue;
640 >                *ptr='\0';
641 >                name = line;
642 >                while(isspace(*(name))){
643 >                        name++;
644 >                }
645  
646 <                memset(&ifr, 0, sizeof ifr);
647 <                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
646 >                memset(&ifr, 0, sizeof ifr);
647 >                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
648  
649                  if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
650                          continue;
# Line 631 | Line 657 | sg_network_iface_stats *sg_get_network_iface_stats(int
657                  network_iface_stat_ptr = network_iface_stats + ifaces;
658                  
659                  if (sg_update_string(&network_iface_stat_ptr->interface_name,
660 <                                     name) < 0) {
660 >                                     name) < 0) {
661                          return NULL;
662                  }
663                  if ((ifr.ifr_flags & IFF_UP) != 0) {
# Line 640 | Line 666 | sg_network_iface_stats *sg_get_network_iface_stats(int
666                          network_iface_stat_ptr->up = 0;
667                  }
668  
669 <                memset(&ethcmd, 0, sizeof ethcmd);
670 <                ethcmd.cmd = ETHTOOL_GSET;
671 <                ifr.ifr_data = (caddr_t) &ethcmd;
669 >                memset(&ethcmd, 0, sizeof ethcmd);
670 >                ethcmd.cmd = ETHTOOL_GSET;
671 >                ifr.ifr_data = (caddr_t) &ethcmd;
672  
673 <                err = ioctl(sock, SIOCETHTOOL, &ifr);
674 <                if (err == 0) {
673 >                err = ioctl(sock, SIOCETHTOOL, &ifr);
674 >                if (err == 0) {
675                          network_iface_stat_ptr->speed = ethcmd.speed;
676  
677                          switch (ethcmd.duplex) {
# Line 671 | Line 697 | sg_network_iface_stats *sg_get_network_iface_stats(int
697   #endif
698          *entries = ifaces;
699          return network_iface_stats;
700 + }
701 +
702 + int sg_network_iface_compare_name(const void *va, const void *vb) {
703 +        const sg_network_iface_stats *a = (const sg_network_iface_stats *)va;
704 +        const sg_network_iface_stats *b = (const sg_network_iface_stats *)vb;
705 +
706 +        return strcmp(a->interface_name, b->interface_name);
707   }
708  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines