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-2003 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 |
25 |
|
#include <stdlib.h> |
26 |
|
#include <string.h> |
27 |
|
#include "statgrab.h" |
28 |
< |
#include "time.h" |
28 |
> |
#include <time.h> |
29 |
|
#ifdef SOLARIS |
30 |
|
#include <kstat.h> |
31 |
|
#include <sys/sysinfo.h> |
36 |
|
#include <regex.h> |
37 |
|
#include "tools.h" |
38 |
|
#endif |
39 |
+ |
#ifdef FREEBSD |
40 |
+ |
#include <sys/types.h> |
41 |
+ |
#include <sys/socket.h> |
42 |
+ |
#include <ifaddrs.h> |
43 |
+ |
#include <net/if.h> |
44 |
+ |
#endif |
45 |
|
|
46 |
|
static network_stat_t *network_stats=NULL; |
47 |
|
static int interfaces=0; |
96 |
|
|
97 |
|
#ifdef LINUX |
98 |
|
FILE *f; |
99 |
< |
/* Horrible big enough, but it should be quite easily */ |
99 |
> |
/* Horrible big enough, but it should be easily big enough */ |
100 |
|
char line[8096]; |
101 |
|
regex_t regex; |
102 |
|
regmatch_t line_match[4]; |
103 |
|
#endif |
104 |
+ |
#ifdef FREEBSD |
105 |
+ |
struct ifaddrs *net, *net_ptr; |
106 |
+ |
struct if_data *net_data; |
107 |
+ |
#endif |
108 |
|
|
109 |
+ |
#ifdef FREEBSD |
110 |
+ |
if(getifaddrs(&net) != 0){ |
111 |
+ |
return NULL; |
112 |
+ |
} |
113 |
+ |
|
114 |
+ |
interfaces=0; |
115 |
+ |
|
116 |
+ |
for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){ |
117 |
+ |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
118 |
+ |
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
119 |
+ |
if(network_stats==NULL){ |
120 |
+ |
return NULL; |
121 |
+ |
} |
122 |
+ |
network_stat_ptr=network_stats+interfaces; |
123 |
+ |
|
124 |
+ |
if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name); |
125 |
+ |
network_stat_ptr->interface_name=strdup(net_ptr->ifa_name); |
126 |
+ |
if(network_stat_ptr->interface_name==NULL) return NULL; |
127 |
+ |
net_data=(struct if_data *)net_ptr->ifa_data; |
128 |
+ |
network_stat_ptr->rx=net_data->ifi_ibytes; |
129 |
+ |
network_stat_ptr->tx=net_data->ifi_obytes; |
130 |
+ |
network_stat_ptr->systime=time(NULL); |
131 |
+ |
interfaces++; |
132 |
+ |
} |
133 |
+ |
freeifaddrs(net); |
134 |
+ |
#endif |
135 |
+ |
|
136 |
|
#ifdef SOLARIS |
137 |
|
if ((kc = kstat_open()) == NULL) { |
138 |
|
return NULL; |
144 |
|
if (!strcmp(ksp->ks_class, "net")) { |
145 |
|
kstat_read(kc, ksp, NULL); |
146 |
|
|
147 |
< |
if((knp=kstat_data_lookup(ksp, "rbytes64"))==NULL){ |
147 |
> |
#ifdef SOL7 |
148 |
> |
#define RLOOKUP "rbytes" |
149 |
> |
#define WLOOKUP "obytes" |
150 |
> |
#define VALTYPE value.ui32 |
151 |
> |
#else |
152 |
> |
#define RLOOKUP "rbytes64" |
153 |
> |
#define WLOOKUP "obytes64" |
154 |
> |
#define VALTYPE value.ui64 |
155 |
> |
#endif |
156 |
> |
|
157 |
> |
if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){ |
158 |
|
/* Not a network interface, so skip to the next entry */ |
159 |
|
continue; |
160 |
|
} |
164 |
|
return NULL; |
165 |
|
} |
166 |
|
network_stat_ptr=network_stats+interfaces; |
167 |
< |
network_stat_ptr->rx=knp->value.ui64; |
167 |
> |
network_stat_ptr->rx=knp->VALTYPE; |
168 |
|
|
169 |
< |
if((knp=kstat_data_lookup(ksp, "obytes64"))==NULL){ |
169 |
> |
if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){ |
170 |
|
/* Not a network interface, so skip to the next entry */ |
171 |
|
continue; |
172 |
|
} |
173 |
< |
network_stat_ptr->tx=knp->value.ui64; |
173 |
> |
network_stat_ptr->tx=knp->VALTYPE; |
174 |
|
if(network_stat_ptr->interface_name!=NULL){ |
175 |
|
free(network_stat_ptr->interface_name); |
176 |
|
} |
220 |
|
|
221 |
|
interfaces++; |
222 |
|
} |
223 |
+ |
fclose(f); |
224 |
+ |
regfree(®ex); |
225 |
|
|
226 |
|
#endif |
227 |
|
*entries=interfaces; |
229 |
|
return network_stats; |
230 |
|
} |
231 |
|
|
232 |
+ |
long long transfer_diff(long long new, long long old){ |
233 |
+ |
#ifdef SOL7 |
234 |
+ |
#define MAXVAL 4294967296 |
235 |
+ |
#else |
236 |
+ |
#define MAXVAL 18446744073709551616 |
237 |
+ |
#endif |
238 |
+ |
long long result; |
239 |
+ |
if(new>=old){ |
240 |
+ |
result = (new-old); |
241 |
+ |
}else{ |
242 |
+ |
result = (MAXVAL+(new-old)); |
243 |
+ |
} |
244 |
+ |
|
245 |
+ |
return result; |
246 |
+ |
|
247 |
+ |
} |
248 |
+ |
|
249 |
|
network_stat_t *get_network_stats_diff(int *entries){ |
250 |
|
static network_stat_t *network_stats_diff=NULL; |
251 |
|
static int sizeof_net_stats_diff=0; |
284 |
|
for(x=0;x<sizeof_net_stats_diff;x++){ |
285 |
|
|
286 |
|
if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){ |
287 |
< |
network_stats_diff_ptr->tx = network_stats_ptr->tx - network_stats_diff_ptr->tx; |
288 |
< |
network_stats_diff_ptr->rx = network_stats_ptr->rx - network_stats_diff_ptr->rx; |
287 |
> |
network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx); |
288 |
> |
network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx); |
289 |
|
network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime; |
290 |
|
}else{ |
291 |
|
|
292 |
|
network_stats_ptr=network_stats; |
293 |
|
for(y=0;y<ifaces;y++){ |
294 |
|
if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){ |
295 |
< |
network_stats_diff_ptr->tx = network_stats_ptr->tx - network_stats_diff_ptr->tx; |
296 |
< |
network_stats_diff_ptr->rx = network_stats_ptr->rx - network_stats_diff_ptr->rx; |
295 |
> |
network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx); |
296 |
> |
network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx); |
297 |
|
network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime; |
298 |
|
break; |
299 |
|
} |