ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
Revision: 1.46
Committed: Sat Mar 6 19:04:29 2004 UTC (20 years, 2 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.45: +7 -2 lines
Log Message:
Add getting errors, packets, and collisions for network interfaces on BSD.

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org
4 * Copyright (C) 2000-2004 i-scream
5 *
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 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 GNU
14 * Lesser General Public License for more details.
15 *
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: network_stats.c,v 1.45 2004/02/16 14:55:32 tdb Exp $
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include "statgrab.h"
31 #include <time.h>
32 #ifdef SOLARIS
33 #include <kstat.h>
34 #include <sys/sysinfo.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
38 #include <net/if.h>
39 #include <netinet/in.h>
40 #include <sys/sockio.h>
41 #endif
42 #ifdef LINUX
43 #include <stdio.h>
44 #include <sys/types.h>
45 #include <regex.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <net/if.h>
49 #include <ctype.h>
50 #include "tools.h"
51 /* Stuff which could be defined by defining KERNEL, but
52 * that would be a bad idea, so we'll just declare it here
53 */
54 typedef __uint8_t u8;
55 typedef __uint16_t u16;
56 typedef __uint32_t u32;
57 typedef __uint64_t u64;
58 #include <linux/ethtool.h>
59 #include <linux/sockios.h>
60 #include <unistd.h>
61 #endif
62 #ifdef ALLBSD
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 #include <ifaddrs.h>
66 #include <net/if.h>
67 #include <net/if_media.h>
68 #include <sys/ioctl.h>
69 #endif
70
71 static network_stat_t *network_stats=NULL;
72 static int interfaces=0;
73
74 void network_stat_init(int start, int end, network_stat_t *net_stats){
75
76 for(net_stats+=start; start<end; start++){
77 net_stats->interface_name=NULL;
78 net_stats->tx=0;
79 net_stats->rx=0;
80 net_stats++;
81 }
82 }
83
84 network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){
85
86 if(net_stats==NULL){
87
88 if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){
89 return NULL;
90 }
91 network_stat_init(0, needed_entries, net_stats);
92 *cur_entries=needed_entries;
93
94 return net_stats;
95 }
96
97
98 if(*cur_entries<needed_entries){
99 net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries));
100 if(net_stats==NULL){
101 return NULL;
102 }
103 network_stat_init(*cur_entries, needed_entries, net_stats);
104 *cur_entries=needed_entries;
105 }
106
107 return net_stats;
108 }
109
110
111 network_stat_t *get_network_stats(int *entries){
112
113 static int sizeof_network_stats=0;
114 network_stat_t *network_stat_ptr;
115
116 #ifdef SOLARIS
117 kstat_ctl_t *kc;
118 kstat_t *ksp;
119 kstat_named_t *knp;
120 #endif
121
122 #ifdef LINUX
123 FILE *f;
124 /* Horrible big enough, but it should be easily big enough */
125 char line[8096];
126 regex_t regex;
127 regmatch_t line_match[4];
128 #endif
129 #ifdef ALLBSD
130 struct ifaddrs *net, *net_ptr;
131 struct if_data *net_data;
132 #endif
133
134 #ifdef ALLBSD
135 if(getifaddrs(&net) != 0){
136 return NULL;
137 }
138
139 interfaces=0;
140
141 for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){
142 if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
143 network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
144 if(network_stats==NULL){
145 return NULL;
146 }
147 network_stat_ptr=network_stats+interfaces;
148
149 if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name);
150 network_stat_ptr->interface_name=strdup(net_ptr->ifa_name);
151 if(network_stat_ptr->interface_name==NULL) return NULL;
152 net_data=(struct if_data *)net_ptr->ifa_data;
153 network_stat_ptr->rx=net_data->ifi_ibytes;
154 network_stat_ptr->tx=net_data->ifi_obytes;
155 network_stat_ptr->ipackets=net_data->ifi_ipackets;
156 network_stat_ptr->opackets=net_data->ifi_opackets;
157 network_stat_ptr->ierrors=net_data->ifi_ierrors;
158 network_stat_ptr->oerrors=net_data->ifi_oerrors;
159 network_stat_ptr->collisions=net_data->ifi_collisions;
160 network_stat_ptr->systime=time(NULL);
161 interfaces++;
162 }
163 freeifaddrs(net);
164 #endif
165
166 #ifdef SOLARIS
167 if ((kc = kstat_open()) == NULL) {
168 return NULL;
169 }
170
171 interfaces=0;
172
173 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
174 if (!strcmp(ksp->ks_class, "net")) {
175 kstat_read(kc, ksp, NULL);
176
177 #ifdef SOL7
178 #define RLOOKUP "rbytes"
179 #define WLOOKUP "obytes"
180 #define VALTYPE value.ui32
181 #else
182 #define RLOOKUP "rbytes64"
183 #define WLOOKUP "obytes64"
184 #define VALTYPE value.ui64
185 #endif
186
187 if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){
188 /* This is a network interface, but it doesn't
189 * have the rbytes/obytes values; for instance,
190 * the loopback devices have this behaviour
191 * (although they do track packets in/out). */
192 continue;
193 }
194
195 network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
196 if(network_stats==NULL){
197 return NULL;
198 }
199 network_stat_ptr=network_stats+interfaces;
200 network_stat_ptr->rx=knp->VALTYPE;
201
202 if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){
203 continue;
204 }
205 network_stat_ptr->tx=knp->VALTYPE;
206 if(network_stat_ptr->interface_name!=NULL){
207 free(network_stat_ptr->interface_name);
208 }
209 network_stat_ptr->interface_name=strdup(ksp->ks_name);
210
211 network_stat_ptr->systime=time(NULL);
212 interfaces++;
213 }
214 }
215
216 kstat_close(kc);
217 #endif
218 #ifdef LINUX
219 f=fopen("/proc/net/dev", "r");
220 if(f==NULL){
221 return NULL;
222 }
223 /* read the 2 lines.. Its the title, so we dont care :) */
224 fgets(line, sizeof(line), f);
225 fgets(line, sizeof(line), f);
226
227
228 if((regcomp(&regex, "^[[:space:]]*([^:]+):[[:space:]]*([[:digit:]]+)[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+([[:digit:]]+)", REG_EXTENDED))!=0){
229 return NULL;
230 }
231
232 interfaces=0;
233
234 while((fgets(line, sizeof(line), f)) != NULL){
235 if((regexec(&regex, line, 4, line_match, 0))!=0){
236 continue;
237 }
238 network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
239 if(network_stats==NULL){
240 return NULL;
241 }
242 network_stat_ptr=network_stats+interfaces;
243
244 if(network_stat_ptr->interface_name!=NULL){
245 free(network_stat_ptr->interface_name);
246 }
247
248 network_stat_ptr->interface_name=get_string_match(line, &line_match[1]);
249 network_stat_ptr->rx=get_ll_match(line, &line_match[2]);
250 network_stat_ptr->tx=get_ll_match(line, &line_match[3]);
251 network_stat_ptr->systime=time(NULL);
252
253 interfaces++;
254 }
255 fclose(f);
256 regfree(&regex);
257
258 #endif
259
260 #ifdef CYGWIN
261 return NULL;
262 #endif
263
264 *entries=interfaces;
265
266 return network_stats;
267 }
268
269 long long transfer_diff(long long new, long long old){
270 #if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD)
271 #define MAXVAL 4294967296LL
272 #else
273 #define MAXVAL 18446744073709551616LL
274 #endif
275 long long result;
276 if(new>=old){
277 result = (new-old);
278 }else{
279 result = (MAXVAL+(new-old));
280 }
281
282 return result;
283
284 }
285
286 network_stat_t *get_network_stats_diff(int *entries) {
287 static network_stat_t *diff = NULL;
288 static int diff_count = 0;
289 network_stat_t *src, *dest;
290 int i, j, new_count;
291
292 if (network_stats == NULL) {
293 /* No previous stats, so we can't calculate a difference. */
294 return get_network_stats(entries);
295 }
296
297 /* Resize the results array to match the previous stats. */
298 diff = network_stat_malloc(interfaces, &diff_count, diff);
299 if (diff == NULL) {
300 return NULL;
301 }
302
303 /* Copy the previous stats into the result. */
304 for (i = 0; i < diff_count; i++) {
305 src = &network_stats[i];
306 dest = &diff[i];
307
308 if (dest->interface_name != NULL) {
309 free(dest->interface_name);
310 }
311 dest->interface_name = strdup(src->interface_name);
312 dest->rx = src->rx;
313 dest->tx = src->tx;
314 dest->systime = src->systime;
315 }
316
317 /* Get a new set of stats. */
318 if (get_network_stats(&new_count) == NULL) {
319 return NULL;
320 }
321
322 /* For each previous stat... */
323 for (i = 0; i < diff_count; i++) {
324 dest = &diff[i];
325
326 /* ... find the corresponding new stat ... */
327 for (j = 0; j < new_count; j++) {
328 /* Try the new stat in the same position first,
329 since that's most likely to be it. */
330 src = &network_stats[(i + j) % new_count];
331 if (strcmp(src->interface_name, dest->interface_name) == 0) {
332 break;
333 }
334 }
335 if (j == new_count) {
336 /* No match found. */
337 continue;
338 }
339
340 /* ... and subtract the previous stat from it to get the
341 difference. */
342 dest->rx = transfer_diff(src->rx, dest->rx);
343 dest->tx = transfer_diff(src->tx, dest->tx);
344 dest->systime = src->systime - dest->systime;
345 }
346
347 *entries = diff_count;
348 return diff;
349 }
350 /* NETWORK INTERFACE STATS */
351
352 void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
353
354 for(net_stats+=start; start<end; start++){
355 net_stats->interface_name=NULL;
356 net_stats->speed=0;
357 net_stats->dup=UNKNOWN_DUPLEX;
358 net_stats++;
359 }
360 }
361
362 network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
363
364 if(net_stats==NULL){
365
366 if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
367 return NULL;
368 }
369 network_iface_stat_init(0, needed_entries, net_stats);
370 *cur_entries=needed_entries;
371
372 return net_stats;
373 }
374
375
376 if(*cur_entries<needed_entries){
377 net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
378 if(net_stats==NULL){
379 return NULL;
380 }
381 network_iface_stat_init(*cur_entries, needed_entries, net_stats);
382 *cur_entries=needed_entries;
383 }
384
385 return net_stats;
386 }
387
388 network_iface_stat_t *get_network_iface_stats(int *entries){
389 static network_iface_stat_t *network_iface_stats;
390 network_iface_stat_t *network_iface_stat_ptr;
391 static int sizeof_network_iface_stats=0;
392 int ifaces = 0;
393
394 #ifdef SOLARIS
395 kstat_ctl_t *kc;
396 kstat_t *ksp;
397 kstat_named_t *knp;
398 int sock;
399 #endif
400 #ifdef ALLBSD
401 struct ifaddrs *net, *net_ptr;
402 struct ifmediareq ifmed;
403 struct ifreq ifr;
404 int sock;
405 int x;
406 #endif
407 #ifdef LINUX
408 FILE *f;
409 /* Horrible big enough, but it should be easily big enough */
410 char line[8096];
411 int sock;
412 #endif
413
414 #ifdef ALLBSD
415 if(getifaddrs(&net) != 0){
416 return NULL;
417 }
418
419 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
420
421 for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
422 if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
423 network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
424 if(network_iface_stats==NULL){
425 return NULL;
426 }
427 network_iface_stat_ptr = network_iface_stats + ifaces;
428
429 memset(&ifr, 0, sizeof(ifr));
430 strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
431
432 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
433 continue;
434 }
435 if((ifr.ifr_flags & IFF_UP) != 0){
436 network_iface_stat_ptr->up = 1;
437 }else{
438 network_iface_stat_ptr->up = 0;
439 }
440
441 if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
442 network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
443 if (network_iface_stat_ptr->interface_name == NULL) return NULL;
444
445 network_iface_stat_ptr->speed = 0;
446 network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
447 ifaces++;
448
449 memset(&ifmed, 0, sizeof(struct ifmediareq));
450 strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
451 if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
452 /* Not all interfaces support the media ioctls. */
453 continue;
454 }
455
456 /* We may need to change this if we start doing wireless devices too */
457 if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
458 /* Not a ETHER device */
459 continue;
460 }
461
462 /* Only intrested in the first 4 bits) - Assuming only ETHER devices */
463 x = ifmed.ifm_active & 0x0f;
464 switch(x){
465 /* 10 Mbit connections. Speedy :) */
466 case(IFM_10_T):
467 case(IFM_10_2):
468 case(IFM_10_5):
469 case(IFM_10_STP):
470 case(IFM_10_FL):
471 network_iface_stat_ptr->speed = 10;
472 break;
473 /* 100 Mbit conneections */
474 case(IFM_100_TX):
475 case(IFM_100_FX):
476 case(IFM_100_T4):
477 case(IFM_100_VG):
478 case(IFM_100_T2):
479 network_iface_stat_ptr->speed = 100;
480 break;
481 /* 1000 Mbit connections */
482 case(IFM_1000_SX):
483 case(IFM_1000_LX):
484 case(IFM_1000_CX):
485 #ifdef IFM_1000_TX
486 case(IFM_1000_TX): /* FreeBSD 4 and others? */
487 #endif
488 #ifdef IFM_1000_FX
489 case(IFM_1000_FX): /* FreeBSD 4 */
490 #endif
491 #ifdef IFM_1000_T
492 case(IFM_1000_T): /* FreeBSD 5 */
493 #endif
494 network_iface_stat_ptr->speed = 1000;
495 break;
496 /* We don't know what it is */
497 default:
498 network_iface_stat_ptr->speed = 0;
499 break;
500 }
501
502 if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
503 network_iface_stat_ptr->dup = FULL_DUPLEX;
504 }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
505 network_iface_stat_ptr->dup = HALF_DUPLEX;
506 }else{
507 network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
508 }
509
510 }
511 freeifaddrs(net);
512 close(sock);
513 #endif
514
515 #ifdef SOLARIS
516 if ((kc = kstat_open()) == NULL) {
517 return NULL;
518 }
519
520 if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
521 return NULL;
522 }
523
524 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
525 if (!strcmp(ksp->ks_class, "net")) {
526 struct ifreq ifr;
527
528 kstat_read(kc, ksp, NULL);
529
530 strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name);
531 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
532 /* Not a network interface. */
533 continue;
534 }
535
536 network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats);
537 if (network_iface_stats == NULL) {
538 return NULL;
539 }
540 network_iface_stat_ptr = network_iface_stats + ifaces;
541 ifaces++;
542
543 if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
544 network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
545 if (network_iface_stat_ptr->interface_name == NULL) return NULL;
546
547 if ((ifr.ifr_flags & IFF_UP) != 0) {
548 network_iface_stat_ptr->up = 1;
549 } else {
550 network_iface_stat_ptr->up = 1;
551 }
552
553 if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) {
554 network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000);
555 } else {
556 network_iface_stat_ptr->speed = 0;
557 }
558
559 network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
560 if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) {
561 switch (knp->value.ui32) {
562 case 1:
563 network_iface_stat_ptr->dup = HALF_DUPLEX;
564 break;
565 case 2:
566 network_iface_stat_ptr->dup = FULL_DUPLEX;
567 break;
568 }
569 }
570 }
571 }
572
573 close(sock);
574 kstat_close(kc);
575 #endif
576 #ifdef LINUX
577 f = fopen("/proc/net/dev", "r");
578 if(f == NULL){
579 return NULL;
580 }
581
582 /* Setup stuff so we can do the ioctl to get the info */
583 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
584 return NULL;
585 }
586
587 /* Ignore first 2 lines.. Just headings */
588 if((fgets(line, sizeof(line), f)) == NULL) return NULL;
589 if((fgets(line, sizeof(line), f)) == NULL) return NULL;
590
591 while((fgets(line, sizeof(line), f)) != NULL){
592 char *name, *ptr;
593 struct ifreq ifr;
594 struct ethtool_cmd ethcmd;
595 int err;
596
597 /* Get the interface name */
598 ptr = strchr(line, ':');
599 if (ptr == NULL) continue;
600 *ptr='\0';
601 name = line;
602 while(isspace(*(name))){
603 name++;
604 }
605
606 memset(&ifr, 0, sizeof ifr);
607 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
608
609 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
610 continue;
611 }
612
613 /* We have a good interface to add */
614 network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
615 if(network_iface_stats==NULL){
616 return NULL;
617 }
618 network_iface_stat_ptr = network_iface_stats + ifaces;
619 network_iface_stat_ptr->interface_name = strdup(name);
620 if ((ifr.ifr_flags & IFF_UP) != 0) {
621 network_iface_stat_ptr->up = 1;
622 } else {
623 network_iface_stat_ptr->up = 0;
624 }
625
626 memset(&ethcmd, 0, sizeof ethcmd);
627 ethcmd.cmd = ETHTOOL_GSET;
628 ifr.ifr_data = (caddr_t) &ethcmd;
629
630 err = ioctl(sock, SIOCETHTOOL, &ifr);
631 if (err == 0) {
632 network_iface_stat_ptr->speed = ethcmd.speed;
633
634 switch (ethcmd.duplex) {
635 case 0x00:
636 network_iface_stat_ptr->dup = FULL_DUPLEX;
637 break;
638 case 0x01:
639 network_iface_stat_ptr->dup = HALF_DUPLEX;
640 break;
641 default:
642 network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
643 }
644 } else {
645 /* Not all interfaces support the ethtool ioctl. */
646 network_iface_stat_ptr->speed = 0;
647 network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
648 }
649
650 ifaces++;
651 }
652 close(sock);
653 fclose(f);
654 #endif
655 *entries = ifaces;
656 return network_iface_stats;
657 }
658