ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
Revision: 1.22
Committed: Fri Jan 16 15:54:54 2004 UTC (20 years, 4 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.21: +13 -12 lines
Log Message:
Alter the licensing of libstatgrab. The library part is now under the
LGPL, whilst the tools/examples are under the GPL. Both licenses are
included in the distribution (and are both now in CVS). Also made a
minor alteration to the webpage where it said everything was licensed
under the GPL.

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 pajs 1.1 */
21    
22     #ifdef HAVE_CONFIG_H
23     #include "config.h"
24     #endif
25    
26 tdb 1.4 #include <stdlib.h>
27 pajs 1.6 #include <string.h>
28 pajs 1.1 #include "statgrab.h"
29 tdb 1.11 #include <time.h>
30 pajs 1.1 #ifdef SOLARIS
31     #include <kstat.h>
32     #include <sys/sysinfo.h>
33 pajs 1.6 #endif
34     #ifdef LINUX
35     #include <stdio.h>
36     #include <sys/types.h>
37     #include <regex.h>
38     #include "tools.h"
39 pajs 1.1 #endif
40 ats 1.18 #ifdef ALLBSD
41 pajs 1.14 #include <sys/types.h>
42     #include <sys/socket.h>
43     #include <ifaddrs.h>
44     #include <net/if.h>
45     #endif
46 pajs 1.1
47 pajs 1.3 static network_stat_t *network_stats=NULL;
48     static int interfaces=0;
49 pajs 1.1
50 pajs 1.3 void network_stat_init(int start, int end, network_stat_t *net_stats){
51    
52     for(net_stats+=start; start<end; start++){
53     net_stats->interface_name=NULL;
54     net_stats->tx=0;
55     net_stats->rx=0;
56     net_stats++;
57     }
58     }
59    
60     network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){
61    
62     if(net_stats==NULL){
63    
64     if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){
65 pajs 1.1 return NULL;
66     }
67 pajs 1.3 network_stat_init(0, needed_entries, net_stats);
68     *cur_entries=needed_entries;
69    
70 pajs 1.1 return net_stats;
71     }
72 pajs 1.3
73    
74     if(*cur_entries<needed_entries){
75     net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries));
76     if(net_stats==NULL){
77 pajs 1.1 return NULL;
78     }
79 pajs 1.3 network_stat_init(*cur_entries, needed_entries, net_stats);
80     *cur_entries=needed_entries;
81 pajs 1.1 }
82    
83     return net_stats;
84     }
85    
86 pajs 1.3
87 pajs 1.1 network_stat_t *get_network_stats(int *entries){
88 pajs 1.6
89     static int sizeof_network_stats=0;
90     network_stat_t *network_stat_ptr;
91    
92     #ifdef SOLARIS
93 pajs 1.1 kstat_ctl_t *kc;
94     kstat_t *ksp;
95     kstat_named_t *knp;
96 pajs 1.6 #endif
97 pajs 1.3
98 pajs 1.6 #ifdef LINUX
99     FILE *f;
100 pajs 1.12 /* Horrible big enough, but it should be easily big enough */
101 pajs 1.6 char line[8096];
102     regex_t regex;
103     regmatch_t line_match[4];
104 pajs 1.14 #endif
105 ats 1.18 #ifdef ALLBSD
106 pajs 1.14 struct ifaddrs *net, *net_ptr;
107     struct if_data *net_data;
108     #endif
109    
110 ats 1.18 #ifdef ALLBSD
111 pajs 1.14 if(getifaddrs(&net) != 0){
112     return NULL;
113     }
114    
115     interfaces=0;
116    
117     for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){
118     if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
119     network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
120     if(network_stats==NULL){
121     return NULL;
122     }
123     network_stat_ptr=network_stats+interfaces;
124    
125     if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name);
126     network_stat_ptr->interface_name=strdup(net_ptr->ifa_name);
127     if(network_stat_ptr->interface_name==NULL) return NULL;
128     net_data=(struct if_data *)net_ptr->ifa_data;
129     network_stat_ptr->rx=net_data->ifi_ibytes;
130     network_stat_ptr->tx=net_data->ifi_obytes;
131     network_stat_ptr->systime=time(NULL);
132     interfaces++;
133     }
134     freeifaddrs(net);
135 pajs 1.6 #endif
136 pajs 1.1
137 pajs 1.6 #ifdef SOLARIS
138 pajs 1.1 if ((kc = kstat_open()) == NULL) {
139     return NULL;
140     }
141    
142 pajs 1.6 interfaces=0;
143 pajs 1.3
144 pajs 1.1 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
145     if (!strcmp(ksp->ks_class, "net")) {
146     kstat_read(kc, ksp, NULL);
147    
148 pajs 1.7 #ifdef SOL7
149     #define RLOOKUP "rbytes"
150     #define WLOOKUP "obytes"
151     #define VALTYPE value.ui32
152     #else
153     #define RLOOKUP "rbytes64"
154     #define WLOOKUP "obytes64"
155     #define VALTYPE value.ui64
156     #endif
157    
158     if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){
159 pajs 1.1 /* Not a network interface, so skip to the next entry */
160     continue;
161     }
162 pajs 1.3
163     network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
164 pajs 1.1 if(network_stats==NULL){
165     return NULL;
166     }
167     network_stat_ptr=network_stats+interfaces;
168 pajs 1.7 network_stat_ptr->rx=knp->VALTYPE;
169 pajs 1.1
170 pajs 1.7 if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){
171 pajs 1.1 /* Not a network interface, so skip to the next entry */
172     continue;
173     }
174 pajs 1.7 network_stat_ptr->tx=knp->VALTYPE;
175 pajs 1.1 if(network_stat_ptr->interface_name!=NULL){
176     free(network_stat_ptr->interface_name);
177     }
178     network_stat_ptr->interface_name=strdup(ksp->ks_name);
179 pajs 1.3
180     network_stat_ptr->systime=time(NULL);
181 pajs 1.1 interfaces++;
182     }
183     }
184 pajs 1.2
185     kstat_close(kc);
186 pajs 1.6 #endif
187     #ifdef LINUX
188     f=fopen("/proc/net/dev", "r");
189     if(f==NULL){
190     return NULL;
191     }
192     /* read the 2 lines.. Its the title, so we dont care :) */
193     fgets(line, sizeof(line), f);
194     fgets(line, sizeof(line), f);
195    
196    
197     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){
198     return NULL;
199     }
200    
201     interfaces=0;
202 pajs 1.1
203 pajs 1.6 while((fgets(line, sizeof(line), f)) != NULL){
204     if((regexec(&regex, line, 4, line_match, 0))!=0){
205     continue;
206     }
207     network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
208     if(network_stats==NULL){
209     return NULL;
210     }
211     network_stat_ptr=network_stats+interfaces;
212    
213     if(network_stat_ptr->interface_name!=NULL){
214     free(network_stat_ptr->interface_name);
215     }
216    
217     network_stat_ptr->interface_name=get_string_match(line, &line_match[1]);
218     network_stat_ptr->rx=get_ll_match(line, &line_match[2]);
219     network_stat_ptr->tx=get_ll_match(line, &line_match[3]);
220     network_stat_ptr->systime=time(NULL);
221    
222     interfaces++;
223     }
224 pajs 1.12 fclose(f);
225 pajs 1.13 regfree(&regex);
226 pajs 1.6
227     #endif
228 ats 1.20
229     #ifdef CYGWIN
230     return NULL;
231     #endif
232    
233 pajs 1.1 *entries=interfaces;
234    
235     return network_stats;
236 pajs 1.3 }
237    
238 pajs 1.8 long long transfer_diff(long long new, long long old){
239 pajs 1.17 #if defined(SOL7) || defined(LINUX) || defined(FREEBSD)
240 ats 1.19 #define MAXVAL 4294967296LL
241 pajs 1.8 #else
242 ats 1.19 #define MAXVAL 18446744073709551616LL
243 pajs 1.8 #endif
244     long long result;
245 pajs 1.10 if(new>=old){
246 pajs 1.8 result = (new-old);
247     }else{
248     result = (MAXVAL+(new-old));
249     }
250    
251     return result;
252    
253     }
254    
255 pajs 1.3 network_stat_t *get_network_stats_diff(int *entries){
256     static network_stat_t *network_stats_diff=NULL;
257     static int sizeof_net_stats_diff=0;
258     network_stat_t *network_stats_ptr, *network_stats_diff_ptr;
259     int ifaces, x, y;
260    
261     if(network_stats==NULL){
262     network_stats_ptr=get_network_stats(&ifaces);
263     *entries=ifaces;
264     return network_stats_ptr;
265     }
266    
267     network_stats_diff=network_stat_malloc(interfaces, &sizeof_net_stats_diff, network_stats_diff);
268     if(network_stats_diff==NULL){
269     return NULL;
270     }
271    
272     network_stats_ptr=network_stats;
273     network_stats_diff_ptr=network_stats_diff;
274    
275     for(ifaces=0;ifaces<interfaces;ifaces++){
276     if(network_stats_diff_ptr->interface_name!=NULL){
277     free(network_stats_diff_ptr->interface_name);
278     }
279     network_stats_diff_ptr->interface_name=strdup(network_stats_ptr->interface_name);
280     network_stats_diff_ptr->tx=network_stats_ptr->tx;
281     network_stats_diff_ptr->rx=network_stats_ptr->rx;
282     network_stats_diff_ptr->systime=network_stats->systime;
283    
284     network_stats_ptr++;
285     network_stats_diff_ptr++;
286     }
287     network_stats_ptr=get_network_stats(&ifaces);
288 ats 1.21 if (network_stats_ptr == NULL) {
289     return NULL;
290     }
291 pajs 1.3 network_stats_diff_ptr=network_stats_diff;
292    
293     for(x=0;x<sizeof_net_stats_diff;x++){
294    
295     if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){
296 pajs 1.9 network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx);
297     network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx);
298 pajs 1.3 network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime;
299     }else{
300    
301     network_stats_ptr=network_stats;
302     for(y=0;y<ifaces;y++){
303     if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){
304 pajs 1.9 network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx);
305     network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx);
306 pajs 1.3 network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime;
307     break;
308     }
309    
310     network_stats_ptr++;
311     }
312     }
313    
314     network_stats_ptr++;
315     network_stats_diff_ptr++;
316     }
317    
318 pajs 1.5 *entries=sizeof_net_stats_diff;
319 pajs 1.3 return network_stats_diff;
320     }
321 pajs 1.1