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.63 by ats, Mon Apr 5 18:10:11 2004 UTC vs.
Revision 1.68 by ats, Thu Apr 8 13:44:44 2004 UTC

# Line 1 | Line 1
1   /*
2 < * i-scream central monitoring system
2 > * i-scream libstatgrab
3   * http://www.i-scream.org
4   * Copyright (C) 2000-2004 i-scream
5   *
# 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(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(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  
# Line 313 | Line 317 | static long long transfer_diff(long long new, long lon
317  
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);
320 >                              network_stat_init, network_stat_destroy);
321          sg_network_io_stats *src = NULL, *dest;
322          int i, j, diff_count, new_count;
323  
# Line 334 | Line 338 | sg_network_io_stats *sg_get_network_io_stats_diff(int
338                  dest = &diff[i];
339  
340                  if (sg_update_string(&dest->interface_name,
341 <                                     src->interface_name) < 0) {
341 >                                     src->interface_name) < 0) {
342                          return NULL;
343                  }
344                  dest->rx = src->rx;
# Line 386 | Line 390 | sg_network_io_stats *sg_get_network_io_stats_diff(int
390          return diff;
391   }
392  
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) {
# Line 400 | Line 411 | static void network_iface_stat_destroy(sg_network_ifac
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);
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;
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;
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];
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 <                return NULL;
441 <        }
439 >        if(getifaddrs(&net) != 0){
440 >                sg_set_error(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;
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;
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));
# Line 452 | Line 464 | sg_network_iface_stats *sg_get_network_iface_stats(int
464                  }
465  
466                  if (sg_update_string(&network_iface_stat_ptr->interface_name,
467 <                                     net_ptr->ifa_name) < 0) {
467 >                                     net_ptr->ifa_name) < 0) {
468                          return NULL;
469                  }
470  
# Line 528 | Line 540 | sg_network_iface_stats *sg_get_network_iface_stats(int
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(SG_ERROR_SOCKET, NULL);
549                  return NULL;
550          }
551  
# Line 554 | Line 568 | sg_network_iface_stats *sg_get_network_iface_stats(int
568                          ifaces++;
569  
570                          if (sg_update_string(&network_iface_stat_ptr->interface_name,
571 <                                             ksp->ks_name) < 0) {
571 >                                             ksp->ks_name) < 0) {
572                                  return NULL;
573                          }
574  
# Line 589 | Line 603 | sg_network_iface_stats *sg_get_network_iface_stats(int
603   #endif  
604   #ifdef LINUX
605          f = fopen("/proc/net/dev", "r");
606 <        if(f == NULL){
607 <                return NULL;
608 <        }
606 >        if(f == NULL){
607 >                sg_set_error(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(SG_ERROR_SOCKET, NULL);
614                  return NULL;
615          }
616  
617          /* Ignore first 2 lines.. Just headings */
618 <        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
619 <        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
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;
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 <                }
634 >                ptr = strchr(line, ':');
635 >                if (ptr == NULL) continue;
636 >                *ptr='\0';
637 >                name = line;
638 >                while(isspace(*(name))){
639 >                        name++;
640 >                }
641  
642 <                memset(&ifr, 0, sizeof ifr);
643 <                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
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;
# Line 631 | Line 653 | sg_network_iface_stats *sg_get_network_iface_stats(int
653                  network_iface_stat_ptr = network_iface_stats + ifaces;
654                  
655                  if (sg_update_string(&network_iface_stat_ptr->interface_name,
656 <                                     name) < 0) {
656 >                                     name) < 0) {
657                          return NULL;
658                  }
659                  if ((ifr.ifr_flags & IFF_UP) != 0) {
# Line 640 | Line 662 | sg_network_iface_stats *sg_get_network_iface_stats(int
662                          network_iface_stat_ptr->up = 0;
663                  }
664  
665 <                memset(&ethcmd, 0, sizeof ethcmd);
666 <                ethcmd.cmd = ETHTOOL_GSET;
667 <                ifr.ifr_data = (caddr_t) &ethcmd;
665 >                memset(&ethcmd, 0, sizeof ethcmd);
666 >                ethcmd.cmd = ETHTOOL_GSET;
667 >                ifr.ifr_data = (caddr_t) &ethcmd;
668  
669 <                err = ioctl(sock, SIOCETHTOOL, &ifr);
670 <                if (err == 0) {
669 >                err = ioctl(sock, SIOCETHTOOL, &ifr);
670 >                if (err == 0) {
671                          network_iface_stat_ptr->speed = ethcmd.speed;
672  
673                          switch (ethcmd.duplex) {
# Line 671 | Line 693 | sg_network_iface_stats *sg_get_network_iface_stats(int
693   #endif
694          *entries = ifaces;
695          return network_iface_stats;
696 + }
697 +
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  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines