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.81 by tdb, Wed Jun 9 14:44:28 2010 UTC vs.
Revision 1.82 by tdb, Sun Oct 3 18:35:57 2010 UTC

# Line 70 | Line 70 | typedef __uint64_t u64;
70   #include <sys/ioctl.h>
71   #include <unistd.h>
72   #endif
73 + #ifdef AIX
74 + #include <sys/types.h>
75 + #include <sys/socket.h>
76 + #include <net/if.h>
77 + #include <errno.h>
78 + #include <strings.h>
79 + #include <sys/ioctl.h>
80 + #include <unistd.h>
81 + #include <libperfstat.h>
82 + #endif
83 + #ifdef HPUX
84 + #include <fcntl.h>
85 + #include <sys/types.h>
86 + #include <sys/socket.h>
87 + #include <net/if.h>
88 + #include <errno.h>
89 + #include <strings.h>
90 + #include <sys/ioctl.h>
91 + #include <unistd.h>
92 + #include <sys/un.h>
93 + #include <sys/stropts.h>
94 + #include <sys/dlpi.h>
95 + #include <sys/dlpi_ext.h>
96 + #include <sys/mib.h>
97 + #define HP_UX11 /* Currently no HP-UX below 11 is supported, fix here if this changes */
98 + #endif
99   #ifdef WIN32
100   #include <windows.h>
101   #include <Iphlpapi.h>
# Line 128 | Line 154 | static PMIB_IFTABLE win32_get_devices()
154  
155   sg_network_io_stats *sg_get_network_io_stats(int *entries){
156          int interfaces;
157 + #ifndef AIX
158          sg_network_io_stats *network_stat_ptr;
159 + #endif
160 + #ifdef HPUX
161 + /*
162 + * talk to Data Link Provider Interface aka /dev/dlpi
163 + * see: http://docs.hp.com/hpux/onlinedocs/B2355-90139/B2355-90139.html
164 + */
165 + #define BUF_SIZE 40960
166  
167 +        u_long *ctrl_area = malloc(BUF_SIZE);
168 +        u_long *data_area = malloc(BUF_SIZE);
169 +        u_long *ppa_area = malloc(BUF_SIZE);
170 +        struct strbuf ctrl_buf = {BUF_SIZE, 0, (char*) ctrl_area};
171 +        struct strbuf data_buf = {BUF_SIZE, 0, (char*) data_area};
172 +
173 +        dl_hp_ppa_info_t *ppa_info;
174 +        dl_hp_ppa_req_t *ppa_req;
175 +        dl_hp_ppa_ack_t *ppa_ack;
176 +        char name_buf[24];
177 +        int fd = -1, ppa_count, count, flags;
178 + #endif
179 +
180   #ifdef SOLARIS
181          kstat_ctl_t *kc;
182          kstat_t *ksp;
# Line 156 | Line 203 | sg_network_io_stats *sg_get_network_io_stats(int *entr
203           * numbers and terminating slash */
204          char buf[5];
205   #endif
206 + #ifdef AIX
207 +        int i;
208 +        perfstat_netinterface_t *statp;
209 +        perfstat_id_t first;
210 + #endif
211  
212 + #ifdef HPUX
213 +        interfaces=0;
214 +
215 +        if ((fd = open("/dev/dlpi", O_RDWR)) < 0) {
216 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/dev/dlpi");
217 +                return NULL;
218 +        }
219 +
220 +        ppa_req = (dl_hp_ppa_req_t *) ctrl_area;
221 +        ppa_ack = (dl_hp_ppa_ack_t *) ctrl_area;
222 +
223 +        ppa_req->dl_primitive = DL_HP_PPA_REQ;
224 +
225 +        ctrl_buf.len = sizeof(dl_hp_ppa_req_t);
226 +        if(putmsg(fd, &ctrl_buf, 0, 0) < 0) {
227 +                sg_set_error_with_errno(SG_ERROR_PUTMSG, "DL_HP_PPA_REQ");
228 +                return NULL;
229 +        }
230 +
231 +        flags = 0;
232 +        ctrl_area[0] = 0;
233 +
234 +        if (getmsg(fd, &ctrl_buf, &data_buf, &flags) < 0) {
235 +                sg_set_error_with_errno(SG_ERROR_GETMSG, "DL_HP_PPA_REQ");
236 +                return NULL;
237 +        }
238 +
239 +        if (ppa_ack->dl_length == 0) {
240 +                sg_set_error_with_errno(SG_ERROR_PARSE, "DL_HP_PPA_REQ");
241 +                return NULL;
242 +        }
243 +
244 +        // save all the PPA information
245 +        memcpy ((u_char*) ppa_area, (u_char*) ctrl_area + ppa_ack->dl_offset, ppa_ack->dl_length);
246 +        ppa_count = ppa_ack->dl_count;
247 +
248 +        for (count = 0, ppa_info = (dl_hp_ppa_info_t*) ppa_area;
249 +             count < ppa_count;
250 +             count++) {
251 +                dl_get_statistics_req_t *get_statistics_req = (dl_get_statistics_req_t*) ctrl_area;
252 +                dl_get_statistics_ack_t *get_statistics_ack = (dl_get_statistics_ack_t*) ctrl_area;
253 +
254 +                dl_attach_req_t *attach_req;
255 +                dl_detach_req_t *detach_req;
256 +                mib_ifEntry *mib_ptr;
257 + #ifdef HP_UX11
258 +                mib_Dot3StatsEntry *mib_Dot3_ptr;
259 + #endif
260 +
261 +                attach_req = (dl_attach_req_t*) ctrl_area;
262 +                attach_req->dl_primitive = DL_ATTACH_REQ;
263 +                attach_req->dl_ppa = ppa_info[count].dl_ppa;
264 +                ctrl_buf.len = sizeof(dl_attach_req_t);
265 +                if (putmsg(fd, &ctrl_buf, 0, 0) < 0) {
266 +                        sg_set_error_with_errno(SG_ERROR_PUTMSG, "DL_ATTACH_REQ");
267 +                        return NULL;
268 +                }
269 +
270 +                ctrl_area[0] = 0;
271 +                if (getmsg(fd, &ctrl_buf, &data_buf, &flags) < 0) {
272 +                        sg_set_error_with_errno(SG_ERROR_GETMSG, "DL_ATTACH_REQ");
273 +                        return NULL;
274 +                }
275 +
276 +                get_statistics_req->dl_primitive = DL_GET_STATISTICS_REQ;
277 +                ctrl_buf.len = sizeof(dl_get_statistics_req_t);
278 +
279 +                if (putmsg(fd, &ctrl_buf, NULL, 0) < 0) {
280 +                        sg_set_error_with_errno(SG_ERROR_PUTMSG, "DL_GET_STATISTICS_REQ");
281 +                        return NULL;
282 +                }
283 +
284 +                flags = 0;
285 +                ctrl_area[0] = 0;
286 +
287 +                if (getmsg(fd, &ctrl_buf, NULL, &flags) < 0) {
288 +                        sg_set_error_with_errno(SG_ERROR_GETMSG, "DL_GET_STATISTICS_REQ");
289 +                        return NULL;
290 +                }
291 +                if (get_statistics_ack->dl_primitive != DL_GET_STATISTICS_ACK) {
292 +                        sg_set_error_with_errno(SG_ERROR_PARSE, "DL_GET_STATISTICS_ACK");
293 +                        return NULL;
294 +                }
295 +
296 +                mib_ptr = (mib_ifEntry*) ((u_char*) ctrl_area + get_statistics_ack->dl_stat_offset);
297 +
298 +                if (0 == (mib_ptr->ifOper & 1)) {
299 +                        continue;
300 +                }
301 +
302 +                if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
303 +                        return NULL;
304 +                }
305 +                network_stat_ptr=network_stats+interfaces;
306 +
307 +                snprintf( name_buf, sizeof(name_buf), "lan%d", ppa_info[count].dl_ppa );
308 +
309 +                if (sg_update_string(&network_stat_ptr->interface_name, name_buf ) < 0 ) {
310 +                        return NULL;
311 +                }
312 +                network_stat_ptr->rx = mib_ptr->ifInOctets;
313 +                network_stat_ptr->tx = mib_ptr->ifOutOctets;
314 +                network_stat_ptr->ipackets = mib_ptr->ifInUcastPkts + mib_ptr->ifInNUcastPkts;
315 +                network_stat_ptr->opackets = mib_ptr->ifOutUcastPkts + mib_ptr->ifOutNUcastPkts;
316 +                network_stat_ptr->ierrors = mib_ptr->ifInErrors;
317 +                network_stat_ptr->oerrors = mib_ptr->ifOutErrors;
318 + #ifdef HP_UX11
319 +                mib_Dot3_ptr = (mib_Dot3StatsEntry *) (void *) ((int) mib_ptr + sizeof (mib_ifEntry));
320 +                network_stat_ptr->collisions = mib_Dot3_ptr->dot3StatsSingleCollisionFrames
321 +                                             + mib_Dot3_ptr->dot3StatsMultipleCollisionFrames;
322 + #else
323 +                network_stat_ptr->collisions = 0; /* currently unknown */
324 + #endif
325 +                network_stat_ptr->systime = time(NULL);
326 +                interfaces++;
327 +
328 +                detach_req = (dl_detach_req_t*) ctrl_area;
329 +                detach_req->dl_primitive = DL_DETACH_REQ;
330 +                ctrl_buf.len = sizeof(dl_detach_req_t);
331 +                if (putmsg(fd, &ctrl_buf, 0, 0) < 0) {
332 +                        sg_set_error_with_errno(SG_ERROR_PUTMSG, "DL_DETACH_REQ");
333 +                        return NULL;
334 +                }
335 +
336 +                ctrl_area[0] = 0;
337 +                flags = 0;
338 +                if (getmsg(fd, &ctrl_buf, &data_buf, &flags) < 0) {
339 +                        sg_set_error_with_errno(SG_ERROR_GETMSG, "DL_DETACH_REQ");
340 +                        return NULL;
341 +                }
342 +        }
343 +
344 +        close(fd);
345 +
346 +        free(ctrl_area);
347 +        free(data_area);
348 +        free(ppa_area);
349 +
350 + #endif
351 +
352   #ifdef ALLBSD
353 <        if(getifaddrs(&net) != 0){
353 >        if (getifaddrs(&net) != 0){
354                  sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
355                  return NULL;
356          }
# Line 166 | Line 358 | sg_network_io_stats *sg_get_network_io_stats(int *entr
358          interfaces=0;
359          
360          for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){
361 <                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
361 >                if (net_ptr->ifa_addr->sa_family != AF_LINK) continue;
362  
363                  if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
364                          return NULL;
# Line 190 | Line 382 | sg_network_io_stats *sg_get_network_io_stats(int *entr
382          }
383          freeifaddrs(net);      
384   #endif
385 + #ifdef AIX
386 +        /* check how many perfstat_netinterface_t structures are available */
387 +        interfaces = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0);
388  
389 +        /* allocate enough memory for all the structures */
390 +        statp = calloc(interfaces, sizeof(perfstat_netinterface_t));
391 +
392 +        /* set name to first interface */
393 +        strcpy(first.name, FIRST_NETINTERFACE);
394 +
395 +        /* ask to get all the structures available in one call */
396 +        /* return code is number of structures returned */
397 +        interfaces = perfstat_netinterface(&first, statp, sizeof(perfstat_netinterface_t), interfaces);
398 +        if (-1 == interfaces) {
399 +                sg_set_error(SG_ERROR_SYSCTLBYNAME, "perfstat_netinterface");
400 +                free(statp);
401 +                return NULL;
402 +        }
403 +
404 +        if (VECTOR_RESIZE(network_stats, interfaces) < 0) {
405 +                sg_set_error(SG_ERROR_MALLOC, NULL);
406 +                free(statp);
407 +                return NULL;
408 +        }
409 +
410 +        /* print statistics for each of the interfaces */
411 +        for (i = 0; i < interfaces; i++) {
412 +                if (sg_update_string(&network_stats[i].interface_name, statp[i].name) < 0) {
413 +                        free(statp);
414 +                        return NULL;
415 +                }
416 +                network_stats[i].tx = statp[i].obytes;
417 +                network_stats[i].rx = statp[i].ibytes;
418 +                network_stats[i].opackets = statp[i].opackets;
419 +                network_stats[i].ipackets = statp[i].ipackets;
420 +                network_stats[i].oerrors = statp[i].oerrors;
421 +                network_stats[i].ierrors = statp[i].ierrors;
422 +                network_stats[i].collisions = statp[i].collisions;
423 +                network_stats[i].systime = time(NULL);
424 +        }
425 +
426 +        free(statp);
427 + #endif
428   #ifdef SOLARIS
429          if ((kc = kstat_open()) == NULL) {
430                  sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
# Line 344 | Line 578 | sg_network_io_stats *sg_get_network_io_stats(int *entr
578          sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
579          return NULL;
580   #endif
347 #ifdef HPUX
348        sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
349        return NULL;
350 #endif
581  
582   #ifdef WIN32
583          interfaces = 0;
# Line 538 | Line 768 | sg_network_iface_stats *sg_get_network_iface_stats(int
768          int sock;
769   #endif
770   #ifdef ALLBSD
541        struct ifaddrs *net, *net_ptr;
771          struct ifmediareq ifmed;
772 +        struct ifaddrs *net, *net_ptr;
773          struct ifreq ifr;
774          int sock;
775          int x;
# Line 556 | Line 786 | sg_network_iface_stats *sg_get_network_iface_stats(int
786          int i,j,no;
787          char buf[5];
788   #endif
789 + #ifdef AIX
790 +        int fd, n;
791 +        size_t pagesize;
792 +        struct ifreq *ifr, *lifr, iff;
793 +        struct ifconf ifc;
794 + #endif
795 + #ifdef HPUX
796 +        int fd;
797 +        struct ifreq *ifr, *lifr, iff;
798 +        struct ifconf ifc;
799 + #endif
800  
801   #ifdef ALLBSD
802          if(getifaddrs(&net) != 0){
# Line 654 | Line 895 | sg_network_iface_stats *sg_get_network_iface_stats(int
895                  }else{
896                          network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
897                  }
657
898          }      
899          freeifaddrs(net);
900          close(sock);
901   #endif
902  
903 + #ifdef AIX
904 + /*
905 + * Fix up variable length of struct ifr
906 + */
907 + #ifndef _SIZEOF_ADDR_IFREQ
908 + #define _SIZEOF_ADDR_IFREQ(ifr) \
909 +        (((ifr).ifr_addr.sa_len > sizeof(struct sockaddr)) \
910 +        ? sizeof(struct ifreq) - sizeof(struct sockaddr) + (ifr).ifr_addr.sa_len \
911 +        : sizeof(struct ifreq))
912 + #endif
913 +
914 +        if ((pagesize = sysconf(_SC_PAGESIZE)) == ((size_t)-1)) {
915 +                sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE");
916 +                return NULL;
917 +        }
918 +
919 +        if ((fd = socket (PF_INET, SOCK_DGRAM, 0)) == -1) {
920 +                return NULL;
921 +        }
922 +
923 +        n = 2;
924 +        ifr = (struct ifreq *)malloc( n*pagesize );
925 +        if( NULL == ifr ) {
926 +                sg_set_error_with_errno(SG_ERROR_MALLOC, NULL);
927 +                close (fd);
928 +                return NULL;
929 +        }
930 +        bzero(&ifc, sizeof(ifc));
931 +        ifc.ifc_req = ifr;
932 +        ifc.ifc_len = n * pagesize;
933 +        while( ( ioctl( fd, SIOCGIFCONF, &ifc ) == -1 ) ||
934 +               ( ((size_t)ifc.ifc_len) >= ( (n-1) * pagesize)) )
935 +        {
936 +                n *= 2;
937 +                ifr = (struct ifreq *)realloc( ifr, n*pagesize );
938 +                if( NULL == ifr ) {
939 +                        free( ifc.ifc_req );
940 +                        close (fd);
941 +                        sg_set_error_with_errno(SG_ERROR_MALLOC, NULL);
942 +                        return NULL;
943 +                }
944 +                ifc.ifc_req = ifr;
945 +                ifc.ifc_len = n * pagesize;
946 +        }
947 +
948 +        lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
949 +        for ( ifr = ifc.ifc_req;
950 +              ifr < lifr;
951 +              ifr = (struct ifreq *)&(((char *)ifr)[_SIZEOF_ADDR_IFREQ(*ifr)]) )
952 +        {
953 +                struct sockaddr *sa = &ifr->ifr_addr;
954 +
955 +                if(sa->sa_family != AF_LINK)
956 +                        continue;
957 +
958 +                if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
959 +                        free( ifc.ifc_req );
960 +                        close (fd);
961 +                        return NULL;
962 +                }
963 +                network_iface_stat_ptr = network_iface_stats + ifaces;
964 +
965 +                memset(&iff, 0, sizeof(iff));
966 +                strncpy(iff.ifr_name, ifr->ifr_name, sizeof(iff.ifr_name));
967 +
968 +                if (ioctl(fd, SIOCGIFFLAGS, &iff) < 0) {
969 +                        continue;
970 +                }
971 +
972 +                if ((iff.ifr_flags & IFF_UP) != 0) {
973 +                        network_iface_stat_ptr->up = 1;
974 +                } else {
975 +                        network_iface_stat_ptr->up = 0;
976 +                }
977 +                if (sg_update_string(&network_iface_stat_ptr->interface_name,
978 +                                     ifr->ifr_name) < 0) {
979 +                        close (fd);
980 +                        free( ifc.ifc_req );
981 +                        return NULL;
982 +                }
983 +
984 +                network_iface_stat_ptr->speed = 0;
985 +                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
986 +                ifaces++;
987 +        }
988 +
989 +        close (fd);
990 +        free( ifc.ifc_req );
991 + #endif
992 +
993 + #ifdef HPUX
994 + /*
995 + * Fix up variable length of struct ifr
996 + */
997 + #ifndef _SA_LEN
998 + #define _SA_LEN(sa) \
999 +   ((sa).sa_family == AF_UNIX ? sizeof(struct sockaddr_un) : \
1000 +   (sa).sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : \
1001 +   sizeof(struct sockaddr))
1002 + #endif
1003 +
1004 + #ifndef _SIZEOF_ADDR_IFREQ
1005 + #define _SIZEOF_ADDR_IFREQ(ifr) \
1006 +        ((_SA_LEN((ifr).ifr_addr) > sizeof(struct sockaddr)) \
1007 +        ? sizeof(struct ifreq) - sizeof(struct sockaddr) + _SA_LEN((ifr).ifr_addr) \
1008 +        : sizeof(struct ifreq))
1009 + #endif
1010 +        if ((fd = socket (PF_INET, SOCK_DGRAM, 0)) == -1) {
1011 +                return NULL;
1012 +        }
1013 +
1014 +        bzero(&ifc, sizeof(ifc));
1015 +        if (ioctl (fd, SIOCGIFNUM, &ifc) != -1) {
1016 +                ifr = calloc(sizeof (*ifr), ifc.ifc_len);
1017 +                if( NULL == ifr ) {
1018 +                        close (fd);
1019 +                        sg_set_error_with_errno(SG_ERROR_MALLOC, NULL);
1020 +                        return NULL;
1021 +                }
1022 +                ifc.ifc_req = ifr;
1023 +                ifc.ifc_len *= sizeof (*ifr);
1024 +                if (ioctl (fd, SIOCGIFCONF, &ifc) == -1) {
1025 +                        free (ifr);
1026 +                        close (fd);
1027 +                        sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
1028 +                        return NULL;
1029 +                }
1030 +        } else {
1031 +                close (fd);
1032 +                sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
1033 +                return NULL;
1034 +        }
1035 +
1036 +        lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
1037 +        for ( ifr = ifc.ifc_req;
1038 +              ifr < lifr;
1039 +              ifr = (struct ifreq *)(((char *)ifr) + _SIZEOF_ADDR_IFREQ(*ifr)) )
1040 +        {
1041 +                if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
1042 +                        free( ifc.ifc_req );
1043 +                        close (fd);
1044 +                        return NULL;
1045 +                }
1046 +                network_iface_stat_ptr = network_iface_stats + ifaces;
1047 +
1048 +                bzero(&iff, sizeof(iff));
1049 +                strncpy(iff.ifr_name, ifr->ifr_name, sizeof(iff.ifr_name));
1050 +
1051 +                if (ioctl(fd, SIOCGIFFLAGS, &iff) < 0) {
1052 +                        continue;
1053 +                }
1054 +
1055 +                if ((iff.ifr_flags & IFF_UP) != 0) {
1056 +                        network_iface_stat_ptr->up = 1;
1057 +                } else {
1058 +                        network_iface_stat_ptr->up = 0;
1059 +                }
1060 +                if (sg_update_string(&network_iface_stat_ptr->interface_name,
1061 +                                     ifr->ifr_name) < 0) {
1062 +                        close (fd);
1063 +                        free( ifc.ifc_req );
1064 +                        return NULL;
1065 +                }
1066 +
1067 +                /**
1068 +                 * for physical devices we now could ask /dev/dlpi for speed and type,
1069 +                 * but for monitoring we don't care
1070 +                 */
1071 +                network_iface_stat_ptr->speed = 0;
1072 +                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
1073 +                ifaces++;
1074 +        }
1075 +
1076 +        close (fd);
1077 +        free( ifc.ifc_req );
1078 + #endif
1079 +
1080   #ifdef SOLARIS
1081          if ((kc = kstat_open()) == NULL) {
1082                  sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
# Line 830 | Line 1247 | sg_network_iface_stats *sg_get_network_iface_stats(int
1247   #endif
1248   #ifdef CYGWIN
1249          sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
833        return NULL;
834 #endif
835 #ifdef HPUX
836        sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
1250          return NULL;
1251   #endif
1252   #ifdef WIN32

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines