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.66 by tdb, Wed Apr 7 21:08:40 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 87 | Line 87 | static void network_stat_destroy(sg_network_io_stats *
87   }
88  
89   VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5,
90 <                      network_stat_init, network_stat_destroy);
90 >                      network_stat_init, network_stat_destroy);
91  
92   sg_network_io_stats *sg_get_network_io_stats(int *entries){
93          int interfaces;
94          sg_network_io_stats *network_stat_ptr;
95  
96   #ifdef SOLARIS
97 <        kstat_ctl_t *kc;
98 <        kstat_t *ksp;
97 >        kstat_ctl_t *kc;
98 >        kstat_t *ksp;
99          kstat_named_t *knp;
100   #endif
101  
# Line 113 | Line 113 | sg_network_io_stats *sg_get_network_io_stats(int *entr
113  
114   #ifdef ALLBSD
115          if(getifaddrs(&net) != 0){
116 +                sg_set_error(SG_ERROR_GETIFADDRS, NULL);
117                  return NULL;
118          }
119  
# Line 127 | Line 128 | sg_network_io_stats *sg_get_network_io_stats(int *entr
128                  network_stat_ptr=network_stats+interfaces;
129                  
130                  if (sg_update_string(&network_stat_ptr->interface_name,
131 <                                     net_ptr->ifa_name) < 0) {
131 >                                     net_ptr->ifa_name) < 0) {
132                          return NULL;
133                  }
134                  net_data=(struct if_data *)net_ptr->ifa_data;
# Line 145 | Line 146 | sg_network_io_stats *sg_get_network_io_stats(int *entr
146   #endif
147  
148   #ifdef SOLARIS
149 <        if ((kc = kstat_open()) == NULL) {
150 <                return NULL;
151 <        }
149 >        if ((kc = kstat_open()) == NULL) {
150 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
151 >                return NULL;
152 >        }
153  
154          interfaces=0;
155  
156 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
157 <                if (!strcmp(ksp->ks_class, "net")) {
158 <                        kstat_read(kc, ksp, NULL);
156 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
157 >                if (!strcmp(ksp->ks_class, "net")) {
158 >                        kstat_read(kc, ksp, NULL);
159  
160   #ifdef SOL7
161   #define LRX "rbytes"
# Line 227 | Line 229 | sg_network_io_stats *sg_get_network_io_stats(int *entr
229  
230                          /* Read interface name */
231                          if (sg_update_string(&network_stat_ptr->interface_name,
232 <                                             ksp->ks_name) < 0) {
232 >                                             ksp->ks_name) < 0) {
233                                  return NULL;
234                          }
235  
# Line 243 | Line 245 | sg_network_io_stats *sg_get_network_io_stats(int *entr
245   #ifdef LINUX
246          f=fopen("/proc/net/dev", "r");
247          if(f==NULL){
248 +                sg_set_error(SG_ERROR_OPEN, "/proc/net/dev");
249                  return NULL;
250          }
251          /* read the 2 lines.. Its the title, so we dont care :) */
# Line 251 | Line 254 | sg_network_io_stats *sg_get_network_io_stats(int *entr
254  
255  
256          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){
257 +                sg_set_error(SG_ERROR_PARSE, NULL);
258                  return NULL;
259          }
260  
# Line 264 | Line 268 | sg_network_io_stats *sg_get_network_io_stats(int *entr
268                  if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
269                          return NULL;
270                  }
271 <                network_stat_ptr=network_stats+interfaces;
271 >                network_stat_ptr=network_stats+interfaces;
272  
273                  if(network_stat_ptr->interface_name!=NULL){
274                          free(network_stat_ptr->interface_name);
# Line 288 | Line 292 | sg_network_io_stats *sg_get_network_io_stats(int *entr
292   #endif
293  
294   #ifdef CYGWIN
295 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
296          return NULL;
297   #endif
298  
# Line 313 | Line 318 | static long long transfer_diff(long long new, long lon
318  
319   sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) {
320          VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1,
321 <                              network_stat_init, network_stat_destroy);
321 >                              network_stat_init, network_stat_destroy);
322          sg_network_io_stats *src = NULL, *dest;
323          int i, j, diff_count, new_count;
324  
# Line 334 | Line 339 | sg_network_io_stats *sg_get_network_io_stats_diff(int
339                  dest = &diff[i];
340  
341                  if (sg_update_string(&dest->interface_name,
342 <                                     src->interface_name) < 0) {
342 >                                     src->interface_name) < 0) {
343                          return NULL;
344                  }
345                  dest->rx = src->rx;
# Line 400 | Line 405 | static void network_iface_stat_destroy(sg_network_ifac
405  
406   sg_network_iface_stats *sg_get_network_iface_stats(int *entries){
407          VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5,
408 <                              network_iface_stat_init, network_iface_stat_destroy);
408 >                              network_iface_stat_init, network_iface_stat_destroy);
409          sg_network_iface_stats *network_iface_stat_ptr;
410          int ifaces = 0;
411  
412   #ifdef SOLARIS
413 <        kstat_ctl_t *kc;
414 <        kstat_t *ksp;
413 >        kstat_ctl_t *kc;
414 >        kstat_t *ksp;
415          kstat_named_t *knp;
416          int sock;
417   #endif
418   #ifdef ALLBSD
419 <        struct ifaddrs *net, *net_ptr;
419 >        struct ifaddrs *net, *net_ptr;
420          struct ifmediareq ifmed;
421          struct ifreq ifr;
422          int sock;
423          int x;
424   #endif
425   #ifdef LINUX
426 <        FILE *f;
427 <        /* Horrible big enough, but it should be easily big enough */
428 <        char line[8096];
426 >        FILE *f;
427 >        /* Horrible big enough, but it should be easily big enough */
428 >        char line[8096];
429          int sock;
430   #endif
431  
432   #ifdef ALLBSD
433 <        if(getifaddrs(&net) != 0){
434 <                return NULL;
435 <        }
433 >        if(getifaddrs(&net) != 0){
434 >                sg_set_error(SG_ERROR_GETIFADDRS, NULL);
435 >                return NULL;
436 >        }
437  
438          if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
439  
440          for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
441 <                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
441 >                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
442  
443                  if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
444 <                        return NULL;
445 <                }
446 <                network_iface_stat_ptr = network_iface_stats + ifaces;
444 >                        return NULL;
445 >                }
446 >                network_iface_stat_ptr = network_iface_stats + ifaces;
447  
448                  memset(&ifr, 0, sizeof(ifr));
449                  strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
# Line 452 | Line 458 | sg_network_iface_stats *sg_get_network_iface_stats(int
458                  }
459  
460                  if (sg_update_string(&network_iface_stat_ptr->interface_name,
461 <                                     net_ptr->ifa_name) < 0) {
461 >                                     net_ptr->ifa_name) < 0) {
462                          return NULL;
463                  }
464  
# Line 528 | Line 534 | sg_network_iface_stats *sg_get_network_iface_stats(int
534  
535   #ifdef SOLARIS
536          if ((kc = kstat_open()) == NULL) {
537 +                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
538                  return NULL;
539          }
540  
541          if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
542 +                sg_set_error(SG_ERROR_SOCKET, NULL);
543                  return NULL;
544          }
545  
# Line 554 | Line 562 | sg_network_iface_stats *sg_get_network_iface_stats(int
562                          ifaces++;
563  
564                          if (sg_update_string(&network_iface_stat_ptr->interface_name,
565 <                                             ksp->ks_name) < 0) {
565 >                                             ksp->ks_name) < 0) {
566                                  return NULL;
567                          }
568  
# Line 589 | Line 597 | sg_network_iface_stats *sg_get_network_iface_stats(int
597   #endif  
598   #ifdef LINUX
599          f = fopen("/proc/net/dev", "r");
600 <        if(f == NULL){
601 <                return NULL;
602 <        }
600 >        if(f == NULL){
601 >                sg_set_error(SG_ERROR_OPEN, "/proc/net/dev");
602 >                return NULL;
603 >        }
604  
605          /* Setup stuff so we can do the ioctl to get the info */
606          if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
607 +                sg_set_error(SG_ERROR_SOCKET, NULL);
608                  return NULL;
609          }
610  
611          /* Ignore first 2 lines.. Just headings */
612 <        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
613 <        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
612 >        if((fgets(line, sizeof(line), f)) == NULL) {
613 >                sg_set_error(SG_ERROR_PARSE, NULL);
614 >                return NULL;
615 >        }
616 >        if((fgets(line, sizeof(line), f)) == NULL) {
617 >                sg_set_error(SG_ERROR_PARSE, NULL);
618 >                return NULL;
619 >        }
620  
621 <        while((fgets(line, sizeof(line), f)) != NULL){
622 <                char *name, *ptr;
623 <                struct ifreq ifr;
624 <                struct ethtool_cmd ethcmd;
625 <                int err;
621 >        while((fgets(line, sizeof(line), f)) != NULL){
622 >                char *name, *ptr;
623 >                struct ifreq ifr;
624 >                struct ethtool_cmd ethcmd;
625 >                int err;
626  
627                  /* Get the interface name */
628 <                ptr = strchr(line, ':');
629 <                if (ptr == NULL) continue;
630 <                *ptr='\0';
631 <                name = line;
632 <                while(isspace(*(name))){
633 <                        name++;
634 <                }
628 >                ptr = strchr(line, ':');
629 >                if (ptr == NULL) continue;
630 >                *ptr='\0';
631 >                name = line;
632 >                while(isspace(*(name))){
633 >                        name++;
634 >                }
635  
636 <                memset(&ifr, 0, sizeof ifr);
637 <                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
636 >                memset(&ifr, 0, sizeof ifr);
637 >                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
638  
639                  if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
640                          continue;
# Line 631 | Line 647 | sg_network_iface_stats *sg_get_network_iface_stats(int
647                  network_iface_stat_ptr = network_iface_stats + ifaces;
648                  
649                  if (sg_update_string(&network_iface_stat_ptr->interface_name,
650 <                                     name) < 0) {
650 >                                     name) < 0) {
651                          return NULL;
652                  }
653                  if ((ifr.ifr_flags & IFF_UP) != 0) {
# Line 640 | Line 656 | sg_network_iface_stats *sg_get_network_iface_stats(int
656                          network_iface_stat_ptr->up = 0;
657                  }
658  
659 <                memset(&ethcmd, 0, sizeof ethcmd);
660 <                ethcmd.cmd = ETHTOOL_GSET;
661 <                ifr.ifr_data = (caddr_t) &ethcmd;
659 >                memset(&ethcmd, 0, sizeof ethcmd);
660 >                ethcmd.cmd = ETHTOOL_GSET;
661 >                ifr.ifr_data = (caddr_t) &ethcmd;
662  
663 <                err = ioctl(sock, SIOCETHTOOL, &ifr);
664 <                if (err == 0) {
663 >                err = ioctl(sock, SIOCETHTOOL, &ifr);
664 >                if (err == 0) {
665                          network_iface_stat_ptr->speed = ethcmd.speed;
666  
667                          switch (ethcmd.duplex) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines