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.3 by pajs, Wed Feb 19 17:13:24 2003 UTC vs.
Revision 1.11 by tdb, Sat Mar 29 18:51:13 2003 UTC

# Line 22 | Line 22
22   #include "config.h"
23   #endif
24  
25 #include <stdio.h>
26 #include "statgrab.h"
25   #include <stdlib.h>
26 + #include <string.h>
27 + #include "statgrab.h"
28 + #include <time.h>
29   #ifdef SOLARIS
30   #include <kstat.h>
31   #include <sys/sysinfo.h>
31 #include <string.h>
32   #endif
33 + #ifdef LINUX
34 + #include <stdio.h>
35 + #include <sys/types.h>
36 + #include <regex.h>
37 + #include "tools.h"
38 + #endif
39  
40   static network_stat_t *network_stats=NULL;
41   static int interfaces=0;
# Line 72 | Line 78 | network_stat_t *network_stat_malloc(int needed_entries
78  
79  
80   network_stat_t *get_network_stats(int *entries){
81 +
82 +        static int sizeof_network_stats=0;      
83 +        network_stat_t *network_stat_ptr;
84 +
85 + #ifdef SOLARIS
86          kstat_ctl_t *kc;
87          kstat_t *ksp;
88          kstat_named_t *knp;
89 + #endif
90  
91 <        static int sizeof_network_stats=0;      
92 <        
93 <        network_stat_t *network_stat_ptr;
91 > #ifdef LINUX
92 >        FILE *f;
93 >        /* Horrible big enough, but it should be quite easily */
94 >        char line[8096];
95 >        regex_t regex;
96 >        regmatch_t line_match[4];
97 > #endif
98  
99 + #ifdef SOLARIS
100          if ((kc = kstat_open()) == NULL) {
101                  return NULL;
102          }
103  
104 <        interfaces=0;  
104 >        interfaces=0;
105  
106          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
107                  if (!strcmp(ksp->ks_class, "net")) {
108                          kstat_read(kc, ksp, NULL);
109  
110 <                        if((knp=kstat_data_lookup(ksp, "rbytes64"))==NULL){
110 > #ifdef SOL7
111 > #define RLOOKUP "rbytes"
112 > #define WLOOKUP "obytes"
113 > #define VALTYPE value.ui32
114 > #else
115 > #define RLOOKUP "rbytes64"
116 > #define WLOOKUP "obytes64"
117 > #define VALTYPE value.ui64
118 > #endif
119 >
120 >                        if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){
121                                  /* Not a network interface, so skip to the next entry */
122                                  continue;
123                          }
# Line 100 | Line 127 | network_stat_t *get_network_stats(int *entries){
127                                  return NULL;
128                          }
129                          network_stat_ptr=network_stats+interfaces;
130 <                        network_stat_ptr->rx=knp->value.ui64;
130 >                        network_stat_ptr->rx=knp->VALTYPE;
131  
132 <                        if((knp=kstat_data_lookup(ksp, "obytes64"))==NULL){
132 >                        if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){
133                                  /* Not a network interface, so skip to the next entry */
134                                  continue;
135                          }
136 <                        network_stat_ptr->tx=knp->value.ui64;
136 >                        network_stat_ptr->tx=knp->VALTYPE;
137                          if(network_stat_ptr->interface_name!=NULL){
138                                  free(network_stat_ptr->interface_name);
139                          }
# Line 118 | Line 145 | network_stat_t *get_network_stats(int *entries){
145          }
146                  
147          kstat_close(kc);        
148 + #endif
149 + #ifdef LINUX
150 +        f=fopen("/proc/net/dev", "r");
151 +        if(f==NULL){
152 +                return NULL;
153 +        }
154 +        /* read the 2 lines.. Its the title, so we dont care :) */
155 +        fgets(line, sizeof(line), f);
156 +        fgets(line, sizeof(line), f);
157  
158 +
159 +        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){
160 +                return NULL;
161 +        }
162 +
163 +        interfaces=0;
164 +
165 +        while((fgets(line, sizeof(line), f)) != NULL){
166 +                if((regexec(&regex, line, 4, line_match, 0))!=0){
167 +                        continue;
168 +                }
169 +                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
170 +                if(network_stats==NULL){
171 +                        return NULL;
172 +                }
173 +                network_stat_ptr=network_stats+interfaces;
174 +
175 +                if(network_stat_ptr->interface_name!=NULL){
176 +                        free(network_stat_ptr->interface_name);
177 +                }
178 +
179 +                network_stat_ptr->interface_name=get_string_match(line, &line_match[1]);
180 +                network_stat_ptr->rx=get_ll_match(line, &line_match[2]);
181 +                network_stat_ptr->tx=get_ll_match(line, &line_match[3]);
182 +                network_stat_ptr->systime=time(NULL);
183 +
184 +                interfaces++;
185 +        }
186 +
187 + #endif
188          *entries=interfaces;
189  
190          return network_stats;  
191   }
192  
193 + long long transfer_diff(long long new, long long old){
194 + #ifdef SOL7
195 + #define MAXVAL 4294967296
196 + #else
197 + #define MAXVAL 18446744073709551616
198 + #endif
199 +        long long result;
200 +        if(new>=old){
201 +                result = (new-old);
202 +        }else{
203 +                result = (MAXVAL+(new-old));
204 +        }
205 +
206 +        return result;
207 +
208 + }
209 +
210   network_stat_t *get_network_stats_diff(int *entries){
211          static network_stat_t *network_stats_diff=NULL;
212          static int sizeof_net_stats_diff=0;
# Line 162 | Line 245 | network_stat_t *get_network_stats_diff(int *entries){
245          for(x=0;x<sizeof_net_stats_diff;x++){
246  
247                  if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){
248 <                        network_stats_diff_ptr->tx = network_stats_ptr->tx - network_stats_diff_ptr->tx;
249 <                        network_stats_diff_ptr->rx = network_stats_ptr->rx - network_stats_diff_ptr->rx;        
248 >                        network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx);
249 >                        network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx);
250                          network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime;
251                  }else{
252                          
253                          network_stats_ptr=network_stats;
254                          for(y=0;y<ifaces;y++){
255                                  if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){
256 <                                        network_stats_diff_ptr->tx = network_stats_ptr->tx - network_stats_diff_ptr->tx;
257 <                                        network_stats_diff_ptr->rx = network_stats_ptr->rx - network_stats_diff_ptr->rx;        
256 >                                        network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx);
257 >                                        network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx);  
258                                          network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime;
259                                          break;
260                                  }
# Line 184 | Line 267 | network_stat_t *get_network_stats_diff(int *entries){
267                  network_stats_diff_ptr++;
268          }
269  
270 +        *entries=sizeof_net_stats_diff;
271          return network_stats_diff;
272   }      
189
190
273  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines