ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
Revision: 1.26
Committed: Sun Jan 25 20:13:57 2004 UTC (20 years, 3 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.25: +91 -7 lines
Log Message:
Added code for freebsd to do the network interface speeds. I've put it
under the label ALLBSD however, as i suspect it will work on all the
BSD's of intrest.

File Contents

# User Rev Content
1 tdb 1.22 /*
2 pajs 1.1 * i-scream central monitoring system
3 tdb 1.15 * http://www.i-scream.org
4 tdb 1.22 * Copyright (C) 2000-2004 i-scream
5 pajs 1.1 *
6 tdb 1.22 * 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 pajs 1.1 *
11 tdb 1.22 * This library is distributed in the hope that it will be useful,
12 pajs 1.1 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 tdb 1.22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * Lesser General Public License for more details.
15 pajs 1.1 *
16 tdb 1.22 * 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 tdb 1.23 *
21 pajs 1.26 * $Id: network_stats.c,v 1.25 2004/01/23 23:23:33 pajs Exp $
22 pajs 1.1 */
23    
24     #ifdef HAVE_CONFIG_H
25     #include "config.h"
26     #endif
27    
28 tdb 1.4 #include <stdlib.h>
29 pajs 1.6 #include <string.h>
30 pajs 1.1 #include "statgrab.h"
31 tdb 1.11 #include <time.h>
32 pajs 1.1 #ifdef SOLARIS
33     #include <kstat.h>
34     #include <sys/sysinfo.h>
35 pajs 1.6 #endif
36     #ifdef LINUX
37     #include <stdio.h>
38     #include <sys/types.h>
39     #include <regex.h>
40     #include "tools.h"
41 pajs 1.1 #endif
42 ats 1.18 #ifdef ALLBSD
43 pajs 1.14 #include <sys/types.h>
44     #include <sys/socket.h>
45     #include <ifaddrs.h>
46     #include <net/if.h>
47 pajs 1.26 #include <net/if_media.h>
48     #include <sys/ioctl.h>
49 pajs 1.14 #endif
50 pajs 1.1
51 pajs 1.3 static network_stat_t *network_stats=NULL;
52     static int interfaces=0;
53 pajs 1.1
54 pajs 1.3 void network_stat_init(int start, int end, network_stat_t *net_stats){
55    
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 pajs 1.1 return NULL;
70     }
71 pajs 1.3 network_stat_init(0, needed_entries, net_stats);
72     *cur_entries=needed_entries;
73    
74 pajs 1.1 return net_stats;
75     }
76 pajs 1.3
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 pajs 1.1 return NULL;
82     }
83 pajs 1.3 network_stat_init(*cur_entries, needed_entries, net_stats);
84     *cur_entries=needed_entries;
85 pajs 1.1 }
86    
87     return net_stats;
88     }
89    
90 pajs 1.3
91 pajs 1.1 network_stat_t *get_network_stats(int *entries){
92 pajs 1.6
93     static int sizeof_network_stats=0;
94     network_stat_t *network_stat_ptr;
95    
96     #ifdef SOLARIS
97 pajs 1.1 kstat_ctl_t *kc;
98     kstat_t *ksp;
99     kstat_named_t *knp;
100 pajs 1.6 #endif
101 pajs 1.3
102 pajs 1.6 #ifdef LINUX
103     FILE *f;
104 pajs 1.12 /* Horrible big enough, but it should be easily big enough */
105 pajs 1.6 char line[8096];
106     regex_t regex;
107     regmatch_t line_match[4];
108 pajs 1.14 #endif
109 ats 1.18 #ifdef ALLBSD
110 pajs 1.14 struct ifaddrs *net, *net_ptr;
111     struct if_data *net_data;
112     #endif
113    
114 ats 1.18 #ifdef ALLBSD
115 pajs 1.14 if(getifaddrs(&net) != 0){
116     return NULL;
117     }
118    
119     interfaces=0;
120    
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 pajs 1.6 #endif
140 pajs 1.1
141 pajs 1.6 #ifdef SOLARIS
142 pajs 1.1 if ((kc = kstat_open()) == NULL) {
143     return NULL;
144     }
145    
146 pajs 1.6 interfaces=0;
147 pajs 1.3
148 pajs 1.1 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 pajs 1.7 #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 pajs 1.1 /* Not a network interface, so skip to the next entry */
164     continue;
165     }
166 pajs 1.3
167     network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
168 pajs 1.1 if(network_stats==NULL){
169     return NULL;
170     }
171     network_stat_ptr=network_stats+interfaces;
172 pajs 1.7 network_stat_ptr->rx=knp->VALTYPE;
173 pajs 1.1
174 pajs 1.7 if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){
175 pajs 1.1 /* Not a network interface, so skip to the next entry */
176     continue;
177     }
178 pajs 1.7 network_stat_ptr->tx=knp->VALTYPE;
179 pajs 1.1 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 pajs 1.3
184     network_stat_ptr->systime=time(NULL);
185 pajs 1.1 interfaces++;
186     }
187     }
188 pajs 1.2
189     kstat_close(kc);
190 pajs 1.6 #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 pajs 1.1
207 pajs 1.6 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 pajs 1.12 fclose(f);
229 pajs 1.13 regfree(&regex);
230 pajs 1.6
231     #endif
232 ats 1.20
233     #ifdef CYGWIN
234     return NULL;
235     #endif
236    
237 pajs 1.1 *entries=interfaces;
238    
239     return network_stats;
240 pajs 1.3 }
241    
242 pajs 1.8 long long transfer_diff(long long new, long long old){
243 pajs 1.17 #if defined(SOL7) || defined(LINUX) || defined(FREEBSD)
244 ats 1.19 #define MAXVAL 4294967296LL
245 pajs 1.8 #else
246 ats 1.19 #define MAXVAL 18446744073709551616LL
247 pajs 1.8 #endif
248     long long result;
249 pajs 1.10 if(new>=old){
250 pajs 1.8 result = (new-old);
251     }else{
252     result = (MAXVAL+(new-old));
253     }
254    
255     return result;
256    
257     }
258    
259 ats 1.24 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 pajs 1.3 }
269    
270 ats 1.24 /* Resize the results array to match the previous stats. */
271     diff = network_stat_malloc(interfaces, &diff_count, diff);
272     if (diff == NULL) {
273 pajs 1.3 return NULL;
274     }
275    
276 ats 1.24 /* 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 pajs 1.3
281 ats 1.24 if (dest->interface_name != NULL) {
282     free(dest->interface_name);
283 pajs 1.3 }
284 ats 1.24 dest->interface_name = strdup(src->interface_name);
285     dest->rx = src->rx;
286     dest->tx = src->tx;
287     dest->systime = src->systime;
288     }
289 pajs 1.3
290 ats 1.24 /* Get a new set of stats. */
291     if (get_network_stats(&new_count) == NULL) {
292 ats 1.21 return NULL;
293     }
294 pajs 1.3
295 ats 1.24 /* 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 pajs 1.3 }
312    
313 ats 1.24 /* ... 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 pajs 1.3 }
319    
320 ats 1.24 *entries = diff_count;
321     return diff;
322     }
323 pajs 1.25 /* 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 pajs 1.26 static int ifaces;
366 pajs 1.25
367     #ifdef SOLARIS
368     kstat_ctl_t *kc;
369     kstat_t *ksp;
370     kstat_named_t *knp;
371     #endif
372 pajs 1.26 #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 pajs 1.25
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 pajs 1.26 network_iface_stat_ptr = network_iface_stats + ifaces;
472 pajs 1.25 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 pajs 1.26 if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
486 pajs 1.25 network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
487 pajs 1.26 if(network_iface_stat_ptr->interface_name == NULL) return NULL;
488     ifaces++;
489 pajs 1.25 }
490     }
491     kstat_close(kc);
492    
493     #endif
494 pajs 1.26 *entries = ifaces;
495 pajs 1.25 return network_iface_stats;
496     }
497