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.2 by pajs, Tue Feb 18 23:23:36 2003 UTC vs.
Revision 1.26 by pajs, Sun Jan 25 20:13:57 2004 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * i-scream central monitoring system
3 < * http://www.i-scream.org.uk
4 < * Copyright (C) 2000-2002 i-scream
3 > * http://www.i-scream.org
4 > * Copyright (C) 2000-2004 i-scream
5   *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU General Public License
8 < * as published by the Free Software Foundation; either version 2
9 < * of the License, or (at your option) any later version.
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 program is distributed in the hope that it will be useful,
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
14 < * GNU General Public License for more details.
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 General Public License
17 < * along with this program; if not, write to the Free Software
18 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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$
22   */
23  
24   #ifdef HAVE_CONFIG_H
25   #include "config.h"
26   #endif
27  
25 #include <stdio.h>
26 #include "statgrab.h"
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>
31 #include <string.h>
35   #endif
36 + #ifdef LINUX
37 + #include <stdio.h>
38 + #include <sys/types.h>
39 + #include <regex.h>
40 + #include "tools.h"
41 + #endif
42 + #ifdef ALLBSD
43 + #include <sys/types.h>
44 + #include <sys/socket.h>
45 + #include <ifaddrs.h>
46 + #include <net/if.h>
47 + #include <net/if_media.h>
48 + #include <sys/ioctl.h>
49 + #endif
50  
51 < #define START_VAL 1
51 > static network_stat_t *network_stats=NULL;
52 > static int interfaces=0;
53  
54 < network_stat_t *network_stat_init(int num_iface, int *wmark, network_stat_t *net_stats){
37 <        int x;
38 <        network_stat_t *net_stats_ptr;
54 > void network_stat_init(int start, int end, network_stat_t *net_stats){
55  
56 <        if(*wmark==-1){
57 <                printf("new malloc\n");
58 <                if((net_stats=malloc(START_VAL * sizeof(network_stat_t)))==NULL){
56 >        for(net_stats+=start; start<end; start++){
57 >                net_stats->interface_name=NULL;
58 >                net_stats->tx=0;
59 >                net_stats->rx=0;
60 >                net_stats++;
61 >        }
62 > }
63 >
64 > network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){
65 >
66 >        if(net_stats==NULL){
67 >
68 >                if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){
69                          return NULL;
70                  }
71 <                *wmark=START_VAL;
72 <                net_stats_ptr=net_stats;
73 <                for(x=0;x<(*wmark);x++){
48 <                        net_stats_ptr->interface_name=NULL;
49 <                        net_stats_ptr++;
50 <                }
71 >                network_stat_init(0, needed_entries, net_stats);
72 >                *cur_entries=needed_entries;
73 >
74                  return net_stats;
75          }
76 <        if(num_iface>(*wmark-1)){
77 <                if((net_stats=realloc(net_stats, (*wmark)*2 * sizeof(network_stat_t)))==NULL){
76 >
77 >
78 >        if(*cur_entries<needed_entries){
79 >                net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries));
80 >                if(net_stats==NULL){
81                          return NULL;
82                  }
83 <
84 <                *wmark=(*wmark)*2;
59 <                net_stats_ptr=net_stats+num_iface;
60 <                for(x=num_iface;x<(*wmark);x++){
61 <                        net_stats_ptr->interface_name=NULL;
62 <                        net_stats_ptr++;
63 <                }
64 <                return net_stats;
83 >                network_stat_init(*cur_entries, needed_entries, net_stats);
84 >                *cur_entries=needed_entries;
85          }
86  
87          return net_stats;
88   }
89  
90 +
91   network_stat_t *get_network_stats(int *entries){
92 +
93 +        static int sizeof_network_stats=0;      
94 +        network_stat_t *network_stat_ptr;
95 +
96 + #ifdef SOLARIS
97          kstat_ctl_t *kc;
98          kstat_t *ksp;
99          kstat_named_t *knp;
100 + #endif
101 +
102 + #ifdef LINUX
103 +        FILE *f;
104 +        /* Horrible big enough, but it should be easily big enough */
105 +        char line[8096];
106 +        regex_t regex;
107 +        regmatch_t line_match[4];
108 + #endif
109 + #ifdef ALLBSD
110 +        struct ifaddrs *net, *net_ptr;
111 +        struct if_data *net_data;
112 + #endif
113 +
114 + #ifdef ALLBSD
115 +        if(getifaddrs(&net) != 0){
116 +                return NULL;
117 +        }
118 +
119 +        interfaces=0;
120          
121 <        int interfaces=0;      
122 <        network_stat_t *network_stat_ptr;
123 <        static network_stat_t *network_stats=NULL;
124 <        static int watermark=-1;
121 >        for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){
122 >                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
123 >                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
124 >                if(network_stats==NULL){
125 >                        return NULL;
126 >                }
127 >                network_stat_ptr=network_stats+interfaces;
128 >                
129 >                if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name);
130 >                network_stat_ptr->interface_name=strdup(net_ptr->ifa_name);
131 >                if(network_stat_ptr->interface_name==NULL) return NULL;
132 >                net_data=(struct if_data *)net_ptr->ifa_data;
133 >                network_stat_ptr->rx=net_data->ifi_ibytes;
134 >                network_stat_ptr->tx=net_data->ifi_obytes;                      
135 >                network_stat_ptr->systime=time(NULL);
136 >                interfaces++;
137 >        }
138 >        freeifaddrs(net);      
139 > #endif
140  
141 + #ifdef SOLARIS
142          if ((kc = kstat_open()) == NULL) {
143                  return NULL;
144          }
145  
146 +        interfaces=0;
147 +
148          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
149                  if (!strcmp(ksp->ks_class, "net")) {
150                          kstat_read(kc, ksp, NULL);
151  
152 <                        if((knp=kstat_data_lookup(ksp, "rbytes64"))==NULL){
152 > #ifdef SOL7
153 > #define RLOOKUP "rbytes"
154 > #define WLOOKUP "obytes"
155 > #define VALTYPE value.ui32
156 > #else
157 > #define RLOOKUP "rbytes64"
158 > #define WLOOKUP "obytes64"
159 > #define VALTYPE value.ui64
160 > #endif
161 >
162 >                        if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){
163                                  /* Not a network interface, so skip to the next entry */
164                                  continue;
165                          }
166 <                        network_stats=network_stat_init(interfaces, &watermark, network_stats);
166 >
167 >                        network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
168                          if(network_stats==NULL){
169                                  return NULL;
170                          }
171                          network_stat_ptr=network_stats+interfaces;
172 <                        network_stat_ptr->rx=knp->value.ui64;
172 >                        network_stat_ptr->rx=knp->VALTYPE;
173  
174 <                        if((knp=kstat_data_lookup(ksp, "obytes64"))==NULL){
174 >                        if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){
175                                  /* Not a network interface, so skip to the next entry */
176                                  continue;
177                          }
178 <                        network_stat_ptr->tx=knp->value.ui64;
178 >                        network_stat_ptr->tx=knp->VALTYPE;
179                          if(network_stat_ptr->interface_name!=NULL){
180                                  free(network_stat_ptr->interface_name);
181                          }
182                          network_stat_ptr->interface_name=strdup(ksp->ks_name);
183 +
184 +                        network_stat_ptr->systime=time(NULL);
185                          interfaces++;
186                  }
187          }
188                  
189          kstat_close(kc);        
190 + #endif
191 + #ifdef LINUX
192 +        f=fopen("/proc/net/dev", "r");
193 +        if(f==NULL){
194 +                return NULL;
195 +        }
196 +        /* read the 2 lines.. Its the title, so we dont care :) */
197 +        fgets(line, sizeof(line), f);
198 +        fgets(line, sizeof(line), f);
199  
200 +
201 +        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){
202 +                return NULL;
203 +        }
204 +
205 +        interfaces=0;
206 +
207 +        while((fgets(line, sizeof(line), f)) != NULL){
208 +                if((regexec(&regex, line, 4, line_match, 0))!=0){
209 +                        continue;
210 +                }
211 +                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
212 +                if(network_stats==NULL){
213 +                        return NULL;
214 +                }
215 +                network_stat_ptr=network_stats+interfaces;
216 +
217 +                if(network_stat_ptr->interface_name!=NULL){
218 +                        free(network_stat_ptr->interface_name);
219 +                }
220 +
221 +                network_stat_ptr->interface_name=get_string_match(line, &line_match[1]);
222 +                network_stat_ptr->rx=get_ll_match(line, &line_match[2]);
223 +                network_stat_ptr->tx=get_ll_match(line, &line_match[3]);
224 +                network_stat_ptr->systime=time(NULL);
225 +
226 +                interfaces++;
227 +        }
228 +        fclose(f);
229 +        regfree(&regex);
230 +
231 + #endif
232 +
233 + #ifdef CYGWIN
234 +        return NULL;
235 + #endif
236 +
237          *entries=interfaces;
238  
239          return network_stats;  
240 + }
241  
242 + long long transfer_diff(long long new, long long old){
243 + #if defined(SOL7) || defined(LINUX) || defined(FREEBSD)
244 + #define MAXVAL 4294967296LL
245 + #else
246 + #define MAXVAL 18446744073709551616LL
247 + #endif
248 +        long long result;
249 +        if(new>=old){
250 +                result = (new-old);
251 +        }else{
252 +                result = (MAXVAL+(new-old));
253 +        }
254 +
255 +        return result;
256 +
257 + }
258 +
259 + network_stat_t *get_network_stats_diff(int *entries) {
260 +        static network_stat_t *diff = NULL;
261 +        static int diff_count = 0;
262 +        network_stat_t *src, *dest;
263 +        int i, j, new_count;
264 +
265 +        if (network_stats == NULL) {
266 +                /* No previous stats, so we can't calculate a difference. */
267 +                return get_network_stats(entries);
268 +        }
269 +
270 +        /* Resize the results array to match the previous stats. */
271 +        diff = network_stat_malloc(interfaces, &diff_count, diff);
272 +        if (diff == NULL) {
273 +                return NULL;
274 +        }
275 +
276 +        /* Copy the previous stats into the result. */
277 +        for (i = 0; i < diff_count; i++) {
278 +                src = &network_stats[i];
279 +                dest = &diff[i];
280 +
281 +                if (dest->interface_name != NULL) {
282 +                        free(dest->interface_name);
283 +                }
284 +                dest->interface_name = strdup(src->interface_name);
285 +                dest->rx = src->rx;
286 +                dest->tx = src->tx;
287 +                dest->systime = src->systime;
288 +        }
289 +
290 +        /* Get a new set of stats. */
291 +        if (get_network_stats(&new_count) == NULL) {
292 +                return NULL;
293 +        }
294 +
295 +        /* For each previous stat... */
296 +        for (i = 0; i < diff_count; i++) {
297 +                dest = &diff[i];
298 +
299 +                /* ... find the corresponding new stat ... */
300 +                for (j = 0; j < new_count; j++) {
301 +                        /* Try the new stat in the same position first,
302 +                           since that's most likely to be it. */
303 +                        src = &network_stats[(i + j) % new_count];
304 +                        if (strcmp(src->interface_name, dest->interface_name) == 0) {
305 +                                break;
306 +                        }
307 +                }
308 +                if (j == new_count) {
309 +                        /* No match found. */
310 +                        continue;
311 +                }
312 +
313 +                /* ... and subtract the previous stat from it to get the
314 +                   difference. */
315 +                dest->rx = transfer_diff(src->rx, dest->rx);
316 +                dest->tx = transfer_diff(src->tx, dest->tx);
317 +                dest->systime = src->systime - dest->systime;
318 +        }
319 +
320 +        *entries = diff_count;
321 +        return diff;
322 + }
323 + /* NETWORK INTERFACE STATS */
324 +
325 + void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
326 +
327 +        for(net_stats+=start; start<end; start++){
328 +                net_stats->interface_name=NULL;
329 +                net_stats->speed=0;
330 +                net_stats->dup=NO_DUPLEX;
331 +                net_stats++;
332 +        }
333 + }
334 +
335 + network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
336 +
337 +        if(net_stats==NULL){
338 +
339 +                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
340 +                        return NULL;
341 +                }
342 +                network_iface_stat_init(0, needed_entries, net_stats);
343 +                *cur_entries=needed_entries;
344 +
345 +                return net_stats;
346 +        }
347 +
348 +
349 +        if(*cur_entries<needed_entries){
350 +                net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
351 +                if(net_stats==NULL){
352 +                        return NULL;
353 +                }
354 +                network_iface_stat_init(*cur_entries, needed_entries, net_stats);
355 +                *cur_entries=needed_entries;
356 +        }
357 +
358 +        return net_stats;
359 + }
360 +
361 + network_iface_stat_t *get_network_iface_stats(int *entries){
362 +        static network_iface_stat_t *network_iface_stats;
363 +        network_iface_stat_t *network_iface_stat_ptr;
364 +        static int sizeof_network_iface_stats=0;        
365 +        static int ifaces;
366 +
367 + #ifdef SOLARIS
368 +        kstat_ctl_t *kc;
369 +        kstat_t *ksp;
370 +        kstat_named_t *knp;
371 + #endif
372 + #ifdef ALLBSD
373 +        struct ifaddrs *net, *net_ptr;
374 +        struct ifmediareq ifmed;
375 +        int s;
376 +        int x;
377 + #endif
378 +
379 +        ifaces = 0;
380 + #ifdef ALLBSD
381 +        if(getifaddrs(&net) != 0){
382 +                return NULL;
383 +        }
384 +
385 +        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL;
386 +
387 +        for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
388 +                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
389 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
390 +                if(network_iface_stats==NULL){
391 +                        return NULL;
392 +                }
393 +                network_iface_stat_ptr = network_iface_stats + ifaces;
394 +
395 +                memset(&ifmed, 0, sizeof(struct ifmediareq));
396 +                strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
397 +                if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
398 +                        continue;
399 +                }
400 +
401 +                /* We may need to change this if we start doing wireless devices too */
402 +                if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
403 +                        /* Not a ETHER device */
404 +                        continue;
405 +                }
406 +
407 +                if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
408 +                network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
409 +                if(network_iface_stat_ptr->interface_name == NULL) return NULL;
410 +
411 +                /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
412 +                x = ifmed.ifm_active & 0x0f;    
413 +                switch(x){
414 +                        /* 10 Mbit connections. Speedy :) */
415 +                        case(IFM_10_T):
416 +                        case(IFM_10_2):
417 +                        case(IFM_10_5):
418 +                        case(IFM_10_STP):
419 +                        case(IFM_10_FL):
420 +                                network_iface_stat_ptr->speed = 10;
421 +                                break;
422 +                        /* 100 Mbit conneections */
423 +                        case(IFM_100_TX):
424 +                        case(IFM_100_FX):
425 +                        case(IFM_100_T4):
426 +                        case(IFM_100_VG):
427 +                        case(IFM_100_T2):
428 +                                network_iface_stat_ptr->speed = 100;
429 +                                break;
430 +                        /* 1000 Mbit connections */
431 +                        case(IFM_1000_SX):
432 +                        case(IFM_1000_LX):
433 +                        case(IFM_1000_CX):
434 +                        case(IFM_1000_TX):
435 +                                network_iface_stat_ptr->speed = 1000;
436 +                                break;
437 +                        /* We don't know what it is */
438 +                        default:
439 +                                network_iface_stat_ptr->speed = 0;
440 +                                break;
441 +                }
442 +
443 +                if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
444 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
445 +                }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
446 +                        network_iface_stat_ptr->dup = HALF_DUPLEX;
447 +                }else{
448 +                        network_iface_stat_ptr->dup = NO_DUPLEX;
449 +                }
450 +                ifaces++;
451 +        }      
452 +        freeifaddrs(net);
453 + #endif
454 +
455 + #ifdef SOLARIS
456 +        if ((kc = kstat_open()) == NULL) {
457 +                return NULL;
458 +        }
459 +
460 +        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
461 +                if (!strcmp(ksp->ks_class, "net")) {
462 +                        kstat_read(kc, ksp, NULL);
463 +                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
464 +                                /* Not a network interface, so skip to the next entry */
465 +                                continue;
466 +                        }
467 +                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
468 +                        if(network_iface_stats==NULL){
469 +                                return NULL;
470 +                        }
471 +                        network_iface_stat_ptr = network_iface_stats + ifaces;
472 +                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
473 +
474 +                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
475 +                                /* Not a network interface, so skip to the next entry */
476 +                                continue;
477 +                        }
478 +
479 +                        if(knp->value.ui64 == 0){
480 +                                network_iface_stat_ptr->dup = FULL_DUPLEX;
481 +                        }else{
482 +                                network_iface_stat_ptr->dup = HALF_DUPLEX;
483 +                        }
484 +
485 +                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
486 +                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
487 +                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
488 +                        ifaces++;
489 +                }
490 +        }
491 +        kstat_close(kc);
492 +        
493 + #endif  
494 +        *entries = ifaces;
495 +        return network_iface_stats;
496   }
497  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines