ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/disk_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/disk_stats.c (file contents):
Revision 1.6 by pajs, Sun Feb 23 23:28:19 2003 UTC vs.
Revision 1.12 by pajs, Wed Mar 5 17:11:22 2003 UTC

# Line 22 | Line 22
22   #include "config.h"
23   #endif
24  
25 #include "statgrab.h"
25   #include <stdlib.h>
27 #include <stdio.h>
28 #include "ukcprog.h"
26   #include <string.h>
27 + #include "statgrab.h"
28  
29   #ifdef SOLARIS
30 + #include <stdio.h>
31   #include <sys/mnttab.h>
33 #include <sys/types.h>
32   #include <sys/statvfs.h>
33   #include <kstat.h>
36
34   #define VALID_FS_TYPES {"ufs", "tmpfs"}
35 + #endif
36  
37 + #ifdef LINUX
38 + #include <stdio.h>
39 + #include <sys/vfs.h>
40 + #include <mntent.h>
41 + #include "tools.h"
42 + #define VALID_FS_TYPES {"ext2", "ext3", "xfs", "reiserfs", "vfat", "tmpfs"}
43   #endif
44  
45   #define START_VAL 1
# Line 71 | Line 75 | disk_stat_t *get_disk_stats(int *entries){
75  
76          char *fs_types[] = VALID_FS_TYPES;
77          int x, valid_type;
74
78          int num_disks=0;
76        struct mnttab mp;
77        struct statvfs fs;
79          FILE *f;
80  
81          disk_stat_t *disk_ptr;
82  
83 + #ifdef SOLARIS
84 +        struct mnttab *mp;
85 +        struct statvfs fs;
86 + #endif
87 + #ifdef LINUX
88 +        struct mntent *mp;
89 +        struct statfs fs;
90 + #endif
91 +
92          if(watermark==-1){
93                  disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
94                  if(disk_stats==NULL){
# Line 87 | Line 97 | disk_stat_t *get_disk_stats(int *entries){
97                  watermark=START_VAL;
98                  init_disk_stat(0, watermark-1, disk_stats);
99          }
100 + #ifdef LINUX
101 +        if ((f=setmntent("/etc/mtab", "r" ))==NULL){
102 +                return NULL;
103 +        }
104  
105 +        while((mp=getmntent(f))){
106 +                if((statfs(mp->mnt_dir, &fs)) !=0){
107 +                        continue;
108 +                }      
109  
110 +                valid_type=0;
111 +                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
112 +                        if(strcmp(mp->mnt_type, fs_types[x]) ==0){
113 +                                valid_type=1;
114 +                                break;
115 +                        }
116 +                }
117 + #endif
118 +
119 + #ifdef SOLARIS
120          if ((f=fopen("/etc/mnttab", "r" ))==NULL){
121                  return NULL;
122          }
# Line 96 | Line 124 | disk_stat_t *get_disk_stats(int *entries){
124                  if ((statvfs(mp.mnt_mountp, &fs)) !=0){
125                          continue;
126                  }
99
127                  valid_type=0;
128                  for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
129                          if(strcmp(mp.mnt_fstype, fs_types[x]) ==0){
# Line 104 | Line 131 | disk_stat_t *get_disk_stats(int *entries){
131                                  break;
132                          }
133                  }
134 + #endif
135  
136                  if(valid_type){
137                          if(num_disks>watermark-1){
# Line 118 | Line 146 | disk_stat_t *get_disk_stats(int *entries){
146                          }
147  
148                          disk_ptr=disk_stats+num_disks;
149 <        
149 > #ifdef LINUX
150 >                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
151 >                                return NULL;
152 >                        }
153 >                                
154 >                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){    
155 >                                return NULL;
156 >                        }
157 >
158 >                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){
159 >                                return NULL;
160 >                        }
161 >                        disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
162 >                        disk_ptr->avail = (long long)fs.f_bsize * (long long)fs.f_bavail;
163 >                        disk_ptr->used = (disk_ptr->size) - ((long long)fs.f_bsize * (long long)fs.f_bfree);
164 >
165 >                        disk_ptr->total_inodes=(long long)fs.f_files;
166 >                        disk_ptr->free_inodes=(long long)fs.f_ffree;
167 >                        /* Linux doesn't have a "available" inodes */
168 >                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
169 > #endif
170 >
171 > #ifdef SOLARIS
172                          /* Memory leak in event of realloc failing */
173                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
174 <                           Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE woul prob be upwards
175 <                           of a k each */
174 >                         * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
175 >                         * be upwards of a k each
176 >                         */
177                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){
178                                  return NULL;
179                          }
# Line 142 | Line 193 | disk_stat_t *get_disk_stats(int *entries){
193                          disk_ptr->total_inodes=(long long)fs.f_files;
194                          disk_ptr->used_inodes=disk_ptr->total_inodes - (long long)fs.f_ffree;
195                          disk_ptr->free_inodes=(long long)fs.f_favail;
196 <
196 > #endif
197                          num_disks++;
198                  }
199          }
# Line 155 | Line 206 | disk_stat_t *get_disk_stats(int *entries){
206          return disk_stats;
207  
208   }
158
209   void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
210  
211          for(diskio_stats+=start; start<end; start++){
# Line 199 | Line 249 | diskio_stat_t *get_diskio_stats(int *entries){
249          static int sizeof_diskio_stats=0;
250          diskio_stat_t *diskio_stats_ptr;
251  
252 + #ifdef SOLARIS
253          kstat_ctl_t *kc;
254          kstat_t *ksp;
255          kstat_io_t kios;
256 + #endif
257 + #ifdef LINUX
258 +        FILE *f;
259 +        char *line_ptr;
260 +        int major, minor;
261 +        char dev_letter;
262 + #endif
263 +        num_diskio=0;
264  
265 + #ifdef SOLARIS
266          if ((kc = kstat_open()) == NULL) {
267                  return NULL;
268          }
269  
210        num_diskio=0;
211
270          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
271                  if (!strcmp(ksp->ks_class, "disk")) {
272  
# Line 236 | Line 294 | diskio_stat_t *get_diskio_stats(int *entries){
294          }
295  
296          kstat_close(kc);
297 + #endif
298  
299 + #ifdef LINUX
300 +        f=fopen("/proc/stat", "r");
301 +        if(f==NULL){
302 +                *entries=0;
303 +                fclose(f);
304 +                return NULL;
305 +        }
306 +        if((line_ptr=f_read_line(f, "disk_io:"))==NULL){
307 +                *entries=0;
308 +                fclose(f);
309 +                return NULL;
310 +        }
311 +        while((line_ptr=strchr(line_ptr, ' '))!=NULL){
312 +                line_ptr++;
313 +                if(*line_ptr=='\0'){
314 +                        break;
315 +                }
316 +                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
317 +                        fclose(f);
318 +                        *entries=0;
319 +                        return NULL;
320 +                }
321 +                diskio_stats_ptr=diskio_stats+num_diskio;
322 +
323 +
324 +                if((sscanf(line_ptr, "(%d,%d):(%*d, %*d, %lld, %*d, %lld)", \
325 +                        &major, \
326 +                        &minor, \
327 +                        &diskio_stats_ptr->read_bytes, \
328 +                        &diskio_stats_ptr->write_bytes))!=4) {
329 +                                continue;
330 +                }
331 +
332 +                /* We read the number of blocks. Blocks are stored in 512 bytes */
333 +                diskio_stats_ptr->read_bytes=diskio_stats_ptr->read_bytes*512;
334 +                diskio_stats_ptr->write_bytes=diskio_stats_ptr->write_bytes*512;
335 +
336 +                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
337 +
338 +                switch(major){
339 +                        case 2:
340 +                                if(minor==0){
341 +                                        diskio_stats_ptr->disk_name=strdup("fd0");
342 +                                }
343 +                                break;
344 +
345 +                        case 3:
346 +                                if(minor==0){
347 +                                        diskio_stats_ptr->disk_name=strdup("hda");
348 +                                }else{
349 +                                        diskio_stats_ptr->disk_name=strdup("hdb");
350 +                                }
351 +                                break;
352 +
353 +                        case 22:
354 +                                if(minor==0){
355 +                                        diskio_stats_ptr->disk_name=strdup("hdc");
356 +                                }else{
357 +                                        diskio_stats_ptr->disk_name=strdup("hdd");
358 +                                }
359 +                                break;
360 +                        case 8:
361 +                                dev_letter='a'+(minor/16);
362 +                                diskio_stats_ptr->disk_name=malloc(4);
363 +                                snprintf(diskio_stats_ptr->disk_name, 4, "sd%c", dev_letter);
364 +                                break;
365 +                        default:
366 +                                /* I have no idea what it is then :) */
367 +                                diskio_stats_ptr->disk_name=malloc(16);
368 +                                snprintf(diskio_stats_ptr->disk_name, 16, "%d %d", major, minor);
369 +                                break;
370 +                }
371 +
372 +                num_diskio++;
373 +        }
374 +
375 + #endif
376          *entries=num_diskio;
377  
378          return diskio_stats;
# Line 303 | Line 439 | diskio_stat_t *get_diskio_stats_diff(int *entries){
439                  diskio_stats_diff_ptr++;        
440  
441          }
442 <
442 >        
443 >        *entries=sizeof_diskio_stats_diff;
444          return diskio_stats_diff;
445   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines