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.8 by pajs, Sat Mar 1 02:41:54 2003 UTC vs.
Revision 1.10 by pajs, Tue Mar 4 18:06:16 2003 UTC

# Line 23 | Line 23
23   #endif
24  
25   #include <stdlib.h>
26 #include <stdio.h>
26   #include <string.h>
27   #include "statgrab.h"
28  
29   #ifdef SOLARIS
30 + #include <stdio.h>
31   #include <sys/mnttab.h>
32   #include <sys/statvfs.h>
33   #include <kstat.h>
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
46  
47   char *copy_string(char *orig_ptr, const char *newtext){
# Line 67 | Line 75 | disk_stat_t *get_disk_stats(int *entries){
75  
76          char *fs_types[] = VALID_FS_TYPES;
77          int x, valid_type;
70
78          int num_disks=0;
72        struct mnttab mp;
73        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 83 | 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 92 | Line 124 | disk_stat_t *get_disk_stats(int *entries){
124                  if ((statvfs(mp.mnt_mountp, &fs)) !=0){
125                          continue;
126                  }
95
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 100 | 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 114 | 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 138 | 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 151 | Line 206 | disk_stat_t *get_disk_stats(int *entries){
206          return disk_stats;
207  
208   }
154
209   void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
210  
211          for(diskio_stats+=start; start<end; start++){
# Line 195 | 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  
264 +        num_diskio=0;
265 +
266 + #ifdef SOLARIS
267          if ((kc = kstat_open()) == NULL) {
268                  return NULL;
269          }
270  
206        num_diskio=0;
207
271          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
272                  if (!strcmp(ksp->ks_class, "disk")) {
273  
# Line 232 | Line 295 | diskio_stat_t *get_diskio_stats(int *entries){
295          }
296  
297          kstat_close(kc);
298 + #endif
299  
300 + #ifdef LINUX
301 +        f=fopen("/proc/stat", "r");
302 +        if(f==NULL){
303 +                return NULL;
304 +        }
305 +        if((line_ptr=f_read_line(f, "disk_io:"))==NULL){
306 +                return NULL;
307 +        }
308 +        while((line_ptr=strchr(line_ptr, ' '))!=NULL){
309 +                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
310 +                        fclose(f);
311 +                        return NULL;
312 +                }
313 +                diskio_stats_ptr=diskio_stats+num_diskio;
314 +
315 +
316 +                sscanf(line_ptr, "(%d,%d):(%*d, %lld, %*d, %lld, %*d)", \
317 +                        &major, \
318 +                        &minor, \
319 +                        &diskio_stats_ptr->read_bytes, \
320 +                        &diskio_stats_ptr->write_bytes);
321 +
322 +                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
323 +
324 +                switch(major){
325 +                        case 3:
326 +                                if(minor==0){
327 +                                        diskio_stats_ptr->disk_name=strdup("hda");
328 +                                }else{
329 +                                        diskio_stats_ptr->disk_name=strdup("hdb");
330 +                                }
331 +                                break;
332 +
333 +                        case 22:
334 +                                if(minor==0){
335 +                                        diskio_stats_ptr->disk_name=strdup("hdc");
336 +                                }else{
337 +                                        diskio_stats_ptr->disk_name=strdup("hdd");
338 +                                }
339 +                        case 8:
340 +                                dev_letter='a'+(minor/16);
341 +                                diskio_stats_ptr->disk_name=malloc(4);
342 +                                snprintf(diskio_stats_ptr->disk_name, 4, "sd%c", dev_letter);
343 +                        default:
344 +                                /* I have no idea what it is then :) */
345 +                                diskio_stats_ptr->disk_name=malloc(16);
346 +                                snprintf(diskio_stats_ptr->disk_name, 16, "%d %d", major, minor);
347 +                }
348 +
349 +                num_diskio++;
350 +        }
351 +
352 + #endif
353          *entries=num_diskio;
354  
355          return diskio_stats;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines