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.16 by pajs, Fri Apr 4 14:25:26 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 + #ifdef FREEBSD
46 + #include <sys/param.h>
47 + #include <sys/ucred.h>
48 + #include <sys/mount.h>
49 + #define VALID_FS_TYPES {"ufs", "mfs"}
50 + #endif
51   #define START_VAL 1
52  
53   char *copy_string(char *orig_ptr, const char *newtext){
# Line 67 | Line 81 | disk_stat_t *get_disk_stats(int *entries){
81  
82          char *fs_types[] = VALID_FS_TYPES;
83          int x, valid_type;
70
84          int num_disks=0;
85 <        struct mnttab mp;
73 <        struct statvfs fs;
85 > #if defined(LINUX) || defined (SOLARIS)
86          FILE *f;
87 + #endif
88  
89          disk_stat_t *disk_ptr;
90  
91 + #ifdef SOLARIS
92 +        struct mnttab mp;
93 +        struct statvfs fs;
94 + #endif
95 + #ifdef LINUX
96 +        struct mntent *mp;
97 +        struct statfs fs;
98 + #endif
99 + #ifdef FREEBSD
100 +        int nummnt;
101 +        struct statfs *mp;
102 + #endif
103 +
104          if(watermark==-1){
105                  disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
106                  if(disk_stats==NULL){
# Line 83 | Line 109 | disk_stat_t *get_disk_stats(int *entries){
109                  watermark=START_VAL;
110                  init_disk_stat(0, watermark-1, disk_stats);
111          }
112 + #ifdef FREEBSD
113 +        nummnt=getmntinfo(&mp , MNT_LOCAL);
114 +        if (nummnt<=0){
115 +                return NULL;
116 +        }
117 +        for(;nummnt--; mp++){
118 +                valid_type=0;
119 +                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
120 +                        if(strcmp(mp->f_fstypename, fs_types[x]) ==0){
121 +                                valid_type=1;
122 +                                break;
123 +                        }
124 +                }
125 + #endif
126  
127 + #ifdef LINUX
128 +        if ((f=setmntent("/etc/mtab", "r" ))==NULL){
129 +                return NULL;
130 +        }
131  
132 +        while((mp=getmntent(f))){
133 +                if((statfs(mp->mnt_dir, &fs)) !=0){
134 +                        continue;
135 +                }      
136 +
137 +                valid_type=0;
138 +                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
139 +                        if(strcmp(mp->mnt_type, fs_types[x]) ==0){
140 +                                valid_type=1;
141 +                                break;
142 +                        }
143 +                }
144 + #endif
145 +
146 + #ifdef SOLARIS
147          if ((f=fopen("/etc/mnttab", "r" ))==NULL){
148                  return NULL;
149          }
# Line 92 | Line 151 | disk_stat_t *get_disk_stats(int *entries){
151                  if ((statvfs(mp.mnt_mountp, &fs)) !=0){
152                          continue;
153                  }
95
154                  valid_type=0;
155                  for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
156                          if(strcmp(mp.mnt_fstype, fs_types[x]) ==0){
# Line 100 | Line 158 | disk_stat_t *get_disk_stats(int *entries){
158                                  break;
159                          }
160                  }
161 + #endif
162  
163                  if(valid_type){
164                          if(num_disks>watermark-1){
# Line 114 | Line 173 | disk_stat_t *get_disk_stats(int *entries){
173                          }
174  
175                          disk_ptr=disk_stats+num_disks;
176 <        
176 > #ifdef FREEBSD
177 >                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
178 >                                return NULL;
179 >                        }
180 >
181 >                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){
182 >                                return NULL;
183 >                        }
184 >
185 >                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){
186 >                                return NULL;
187 >                        }
188 >
189 >                        disk_ptr->size = (long long)mp->f_bsize * (long long) mp->f_blocks;
190 >                        disk_ptr->avail = (long long)mp->f_bsize * (long long) mp->f_bavail;
191 >                        disk_ptr->used = (disk_ptr->size) - ((long long)mp->f_bsize * (long long)mp->f_bfree);
192 >
193 >                        disk_ptr->total_inodes=(long long)mp->f_files;
194 >                        disk_ptr->free_inodes=(long long)mp->f_ffree;
195 >                        /* Freebsd doesn't have a "available" inodes */
196 >                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
197 > #endif
198 > #ifdef LINUX
199 >                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
200 >                                return NULL;
201 >                        }
202 >                                
203 >                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){    
204 >                                return NULL;
205 >                        }
206 >
207 >                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){
208 >                                return NULL;
209 >                        }
210 >                        disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
211 >                        disk_ptr->avail = (long long)fs.f_bsize * (long long)fs.f_bavail;
212 >                        disk_ptr->used = (disk_ptr->size) - ((long long)fs.f_bsize * (long long)fs.f_bfree);
213 >
214 >                        disk_ptr->total_inodes=(long long)fs.f_files;
215 >                        disk_ptr->free_inodes=(long long)fs.f_ffree;
216 >                        /* Linux doesn't have a "available" inodes */
217 >                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
218 > #endif
219 >
220 > #ifdef SOLARIS
221                          /* Memory leak in event of realloc failing */
222                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
223 <                           Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE woul prob be upwards
224 <                           of a k each */
223 >                         * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
224 >                         * be upwards of a k each
225 >                         */
226                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){
227                                  return NULL;
228                          }
# Line 138 | Line 242 | disk_stat_t *get_disk_stats(int *entries){
242                          disk_ptr->total_inodes=(long long)fs.f_files;
243                          disk_ptr->used_inodes=disk_ptr->total_inodes - (long long)fs.f_ffree;
244                          disk_ptr->free_inodes=(long long)fs.f_favail;
245 <
245 > #endif
246                          num_disks++;
247                  }
248          }
# Line 146 | Line 250 | disk_stat_t *get_disk_stats(int *entries){
250          *entries=num_disks;    
251  
252          /* If this fails, there is very little i can do about it, so i'll ignore it :) */
253 + #if defined(LINUX) || defined(SOLARIS)
254          fclose(f);
255 + #endif
256  
257          return disk_stats;
258  
259   }
154
260   void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
261  
262          for(diskio_stats+=start; start<end; start++){
# Line 195 | Line 300 | diskio_stat_t *get_diskio_stats(int *entries){
300          static int sizeof_diskio_stats=0;
301          diskio_stat_t *diskio_stats_ptr;
302  
303 + #ifdef SOLARIS
304          kstat_ctl_t *kc;
305          kstat_t *ksp;
306          kstat_io_t kios;
307 + #endif
308 + #ifdef LINUX
309 +        FILE *f;
310 +        char *line_ptr;
311 +        int major, minor;
312 +        char dev_letter;
313 + #endif
314 +        num_diskio=0;
315  
316 + #ifdef SOLARIS
317          if ((kc = kstat_open()) == NULL) {
318                  return NULL;
319          }
320  
206        num_diskio=0;
207
321          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
322                  if (!strcmp(ksp->ks_class, "disk")) {
323  
# Line 227 | Line 340 | diskio_stat_t *get_diskio_stats(int *entries){
340                          if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
341  
342                          diskio_stats_ptr->disk_name=strdup(ksp->ks_name);
343 +                        diskio_stats_ptr->systime=time(NULL);
344                          num_diskio++;
345                  }
346          }
347  
348          kstat_close(kc);
349 + #endif
350  
351 + #ifdef LINUX
352 +        f=fopen("/proc/stat", "r");
353 +        if(f==NULL){
354 +                *entries=0;
355 +                fclose(f);
356 +                return NULL;
357 +        }
358 +        if((line_ptr=f_read_line(f, "disk_io:"))==NULL){
359 +                *entries=0;
360 +                fclose(f);
361 +                return NULL;
362 +        }
363 +        while((line_ptr=strchr(line_ptr, ' '))!=NULL){
364 +                line_ptr++;
365 +                if(*line_ptr=='\0'){
366 +                        break;
367 +                }
368 +                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
369 +                        fclose(f);
370 +                        *entries=0;
371 +                        return NULL;
372 +                }
373 +                diskio_stats_ptr=diskio_stats+num_diskio;
374 +
375 +
376 +                if((sscanf(line_ptr, "(%d,%d):(%*d, %*d, %lld, %*d, %lld)", \
377 +                        &major, \
378 +                        &minor, \
379 +                        &diskio_stats_ptr->read_bytes, \
380 +                        &diskio_stats_ptr->write_bytes))!=4) {
381 +                                continue;
382 +                }
383 +
384 +                /* We read the number of blocks. Blocks are stored in 512 bytes */
385 +                diskio_stats_ptr->read_bytes=diskio_stats_ptr->read_bytes*512;
386 +                diskio_stats_ptr->write_bytes=diskio_stats_ptr->write_bytes*512;
387 +
388 +                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
389 +
390 +                switch(major){
391 +                        case 2:
392 +                                if(minor==0){
393 +                                        diskio_stats_ptr->disk_name=strdup("fd0");
394 +                                }
395 +                                break;
396 +
397 +                        case 3:
398 +                                if(minor==0){
399 +                                        diskio_stats_ptr->disk_name=strdup("hda");
400 +                                }else{
401 +                                        diskio_stats_ptr->disk_name=strdup("hdb");
402 +                                }
403 +                                break;
404 +
405 +                        case 22:
406 +                                if(minor==0){
407 +                                        diskio_stats_ptr->disk_name=strdup("hdc");
408 +                                }else{
409 +                                        diskio_stats_ptr->disk_name=strdup("hdd");
410 +                                }
411 +                                break;
412 +                        case 8:
413 +                                dev_letter='a'+(minor/16);
414 +                                diskio_stats_ptr->disk_name=malloc(4);
415 +                                snprintf(diskio_stats_ptr->disk_name, 4, "sd%c", dev_letter);
416 +                                break;
417 +                        default:
418 +                                /* I have no idea what it is then :) */
419 +                                diskio_stats_ptr->disk_name=malloc(16);
420 +                                snprintf(diskio_stats_ptr->disk_name, 16, "%d %d", major, minor);
421 +                                break;
422 +                }
423 +
424 +                diskio_stats_ptr->systime=time(NULL);
425 +                num_diskio++;
426 +        }
427 +
428 +        fclose(f);
429 +
430 + #endif
431          *entries=num_diskio;
432  
433          return diskio_stats;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines