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.5 by pajs, Sat Mar 1 02:42:27 2003 UTC vs.
Revision 1.6 by pajs, Fri Mar 7 11:56:44 2003 UTC

# Line 22 | Line 22
22   #include "config.h"
23   #endif
24  
25 #include <stdio.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")) {
# Line 118 | Line 135 | network_stat_t *get_network_stats(int *entries){
135          }
136                  
137          kstat_close(kc);        
138 + #endif
139 + #ifdef LINUX
140 +        f=fopen("/proc/net/dev", "r");
141 +        if(f==NULL){
142 +                return NULL;
143 +        }
144 +        /* read the 2 lines.. Its the title, so we dont care :) */
145 +        fgets(line, sizeof(line), f);
146 +        fgets(line, sizeof(line), f);
147  
148 +
149 +        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){
150 +                return NULL;
151 +        }
152 +
153 +        interfaces=0;
154 +
155 +        while((fgets(line, sizeof(line), f)) != NULL){
156 +                if((regexec(&regex, line, 4, line_match, 0))!=0){
157 +                        continue;
158 +                }
159 +                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
160 +                if(network_stats==NULL){
161 +                        return NULL;
162 +                }
163 +                network_stat_ptr=network_stats+interfaces;
164 +
165 +                if(network_stat_ptr->interface_name!=NULL){
166 +                        free(network_stat_ptr->interface_name);
167 +                }
168 +
169 +                network_stat_ptr->interface_name=get_string_match(line, &line_match[1]);
170 +                network_stat_ptr->rx=get_ll_match(line, &line_match[2]);
171 +                network_stat_ptr->tx=get_ll_match(line, &line_match[3]);
172 +                network_stat_ptr->systime=time(NULL);
173 +
174 +                interfaces++;
175 +        }
176 +
177 + #endif
178          *entries=interfaces;
179  
180          return network_stats;  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines